ENV SAMPLE GENERATOR

What purpose? I shouldn't know about this.

by Ukjin Yang

HTML

<fieldset>
  <legend>Generate Options</legend>
  <label>CODE: <input type="text" id="code" value="EXAMPLE"></label>
  <input type="date" id="sdate">
  ~
  <input type="date" id="edate">
  <select id="type">
    <option value="air">Air Condition</option>
    <option value="env">Temp / Hum</option>
    <option value="eva">Electric</option>
  </select>
  <button type="button" id="start">Generate</button>
</fieldset>
<fieldset>
  <legend>Generated Result</legend>
  <textarea id="out" cols="60" rows="10"></textarea>
  <button type="button" id="down" disabled>Download</button>
</fieldset>

JavaScript

function saveTextAsFile(textToWrite, fileNameToSaveAs)
  {
    var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
    var downloadLink = document.createElement("a");
    downloadLink.download = fileNameToSaveAs;
    downloadLink.innerHTML = "Download File";
    downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
    downloadLink.onclick = () => document.body.removeChild(downloadLink);
    downloadLink.style.display = "none";
    document.body.appendChild(downloadLink);
    downloadLink.click();
  }
  (() => {
    const sd = new Date();
    sd.setMonth(0);sd.setDate(1);
    sdate.value = sd.toLocaleDateString('sv');
    edate.value = new Date().toLocaleDateString('sv');
    const result = [];
    var blobURL = URL.createObjectURL( new Blob([ '(',
        function(){
          const typicalTemps = [
          	[-10, 5],[-5, 10],[0, 10],[5, 15],
            [15, 25],[20, 30],[24, 36],[25, 35],
            [22, 32],[15, 25],[5, 15],[-5, 15]
          ], typicalHmcs = [
          	[20, 50],[20, 50],[20, 50],[20, 50],
            [40, 70],[50, 90],[60, 90],[60, 90],
            [50, 80],[40, 70],[30, 60],[20, 50]
          ], numonly = /\D/g;
          const rand = (max, min) => Math.round(Math.random() * max) + (min || 0);
          self.onmessage = e => {
          	if(!e.data || !e.data.type) return;
          	const sdate = new Date(e.data.sdate + ' 00:00:00'),
                  edate = new Date(e.data.edate + ' 23:59:59'),
                  code = e.data.code || '';
          	switch(e.data.type) {
            	case 'air':
                self.postMessage(false);
                const pms = [20, 20];
                while(sdate < edate) {
                  const iso = sdate.toLocaleString('sv').replace(numonly, '');
                  self.postMessage([
                  	code,
                    iso.substring(0,8),
                    iso.substring(8),
                    (pms[0] = Math.abs(pms[0] + rand(-4, 2))),
                   ...