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SAAS,EAAS,KAErB,OAAf,EAAS,GACa,QAArB,EAAU,aAGP,gBAAgB,EAAU,IAAK,EAAU,MAAM,GAGjD,EAAa,SAChB,IAAI,EAAU,KAAO,6BACf,gBAAgB,EAAU,IAAK,EAAU,MAAM,IAI9B,OAArB,EAAU,aAGP,gBAAgB,EAAU,IAAK,EAAU,MAAM,GAGjD,EAAa,SAChB,IAAI,EAAU,KAAO,6BACf,gBAAgB,EAAU,IAAK,EAAU,MAAM,GAO1D,KAA0B,OAAf,EAAS,KACf,SAAS,OAAO,MAAM,MAAM,EAAS,IAAI,WAAa,SAAS,EAAS,IAEnD,OAApB,EAAU,cACP,eAAe,EAAU,IAAK,EAAU,MAAM,GAI7B,MAApB,EAAU,cACP,gBAAgB,EAAU,IAAK,EAAU,MAAM,GASzD,CAGD,EAgEA,OAAO,WAAa,eAAe,GAG/B,GAAG,cAAe,EAAc,KAAK,UAAU,OAAO,MAAO,KAAM,IAAI,SAAS,GAAM,GAAG,EAAK,MAAM,CAAK,GAC7G,EAOA,OAAO,wBAA0B,uBACvB,UAAU,wBAAwB,OAAO,QACnD,EACA,OAAO,oBAAsB,WACzB,UAAU,oBAAoB,OAAO,QAAS,OAAO,MACzD,EAIA,OAAO,QAAU","file":"/npm/domoticz-heaters@1.5.8/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/domoticz-heaters@1.5.8/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","\"use strict\";\n\n/**\n    Main module.\n\n */\n\n// dependencies\nconst fs        = require(\"fs\");\nconst MyDate    = require(\"./class/date\");\nconst domoAPI   = require(\"./class/domoticzAPI\");\nconst Room      = require(\"./class/room\");\nconst gsheetAPI = require(\"./class/googleSheetAPI\");\n\nconst domoJS = {};\ndomoJS.configs = {};\ndomoJS.state   = {};\n\n\n/**\n * Some stupid console.log wrapper with prepended time\n * @param msg\n */\nfunction say( msg ) {\n    console.log(new MyDate().stringTime5() + \" \" + msg);\n}\n\n\n/**\n * We load the configs from configuration file (URL of Domoticz, user/pwd, ...)\n * @param path\n */\ndomoJS.loadConfigs = function( path ) {\n    domoJS.configs = JSON.parse( fs.readFileSync( path ) );\n\n    // give domoticz API access to configs\n    domoAPI.setUrl( domoJS.configs.domoticz );\n};\n\n\n/**\n * Get the state of the house from json state file : last update, rooms, temperatures, heaters, ...\n * @param path\n */\ndomoJS.loadState = function( path ) {\n    // load state from file\n    domoJS.state = JSON.parse( fs.readFileSync( path ) );\n\n    // make it easier to find which file we read the state from\n    domoJS.state.file = path;\n\n    // make all rooms inherit from Room prototypal object\n    // (I love prototypal inheritance !!!)\n    for (let room in domoJS.state.rooms)\n        domoJS.state.rooms[room].__proto__ = Room;\n};\n\n\n/**\n * Reads the temperatures in Google Calc \"proxy\" file, and writes them in state.rooms.xxx.wantedTemps\n */\ndomoJS.updateWantedTemps = async function() {\n    if (! fs.existsSync(domoJS.configs.root + \"wantedTemps.json\")) {\n        console.log(\"could not load \" + domoJS.configs.root + \"wantedTemps.json from local disk\");\n        return\n    }\n\n    let wantedTemps = JSON.parse(fs.readFileSync(domoJS.configs.root + \"wantedTemps.json\"));\n    let now5 = new MyDate().stringTime5();\n\n    // from the sheet of wanted temps, we get which temperature we now want\n    for (let room in wantedTemps) {\n\n        let wantedTemp = 10;\n\n        for (let keyHours in wantedTemps[room])\n            if (now5 > keyHours)\n                wantedTemp = wantedTemps[room][keyHours];\n\n        domoJS.state.rooms[room].wantedTemp = Number(wantedTemp);\n\n        // console.log(domoJS.state.rooms[room]);\n    }\n    // say(\" Wanted Temperatures:\"); console.log(wantedTemps);\n}\n\n\n\ndomoJS.loadTemperaturesFromDomoticz = async function() {\n\n\tconst temps = await domoAPI.getTemperatures();\n\t// console.log(temps);\n\n\tfor (const sensor of temps) {\n\t\t// console.log(sensor);\n\n\t\t// we get the room from the name of the device: tempBed -> Bed\n\t\tlet room_name = sensor.Name.substring(4);\n\n\t\t// update state\n\t\tif (! domoJS.state.rooms[room_name]) {\n\t\t\t// say(\"Room \" + room_name + \" does not exist in domoJS.state\");\n\t\t\tcontinue;\n\t\t}\n\t\tdomoJS.state.rooms[room_name].setTemperature(sensor.Temp);\n\t\tdomoJS.state.rooms[room_name].setHumidity(sensor.Humidity);\n\t\tdomoJS.state.rooms[room_name].setLastSensorTime(sensor.LastUpdate);\n\t}\n\n\t// console.log(this.state);\n}\n\n/**\n *  Get the wanted temperature\n *  Get the heater switches status\n *  send Domoticz API call\n *  get result as list of switch data\n *  process each switch (heater) in a callback\n *  then count consumption\n */\ndomoJS.updateSwitches = async function() {\n\n\t// reset all power of rooms to zero\n\tfor (const name in domoJS.state.rooms) {\n\t\tlet room = domoJS.state.rooms[name];\n\t\troom.power = 0;\n\t}\n\t// console.log(domoJS.state);\n\n\t// console.log(domoJS.state);\n\n\tlet switches = await domoAPI.getSwitchesInfo();\n\n\tfor (const oneSwitch of switches) {\n\t\tlet namebits = oneSwitch.Name.split('-');\n\t\t// oneSwitch.idx\n\t\t// Heater-Kitchen-Small-dir-36-1000\n\t\t\n\t\t// deal with the heaters\n\t\tif (namebits[0] == 'Heater') {\n\t\t\tlet current_room = domoJS.state.rooms[namebits[1]];\n\t\n\t\t\t// now we check if heater and room state are coherent:\n\t\t\t// room should impose the value of heater. If not coherent, we switch heater\n\t\t\tif (current_room.state === \"On\") {\n\t\t\t\tif (namebits[3] == 'inv') {\n\t\t\t\t\tif (oneSwitch.Status === \"On\") {\n\t\t\t\t\t\t// something is wrong: heater should be aligned with room\n\t\t\t\t\t\t// say(oneSwitch.Name + \" (inverted) is On and should be Off !!!\");\n\t\t\t\t\t\tawait switchHeaterOn(oneSwitch.idx, oneSwitch.Name, true);\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// resend ?\n\t\t\t\t\t\tif (current_room.resend) {\n\t\t\t\t\t\t\tsay(oneSwitch.Name + \" resend On command\");\n\t\t\t\t\t\t\tawait switchHeaterOn(oneSwitch.idx, oneSwitch.Name, true);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tif (oneSwitch.Status === \"Off\") {\n\t\t\t\t\t\t// something is wrong: heater should be aligned with room\n\t\t\t\t\t\t// say(oneSwitch.Name + \" is Off and should be On !!!\");\n\t\t\t\t\t\tawait switchHeaterOn(oneSwitch.idx, oneSwitch.Name, false);\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// resend ?\n\t\t\t\t\t\tif (current_room.resend) {\n\t\t\t\t\t\t\tsay(oneSwitch.Name + \" resend On command\");\n\t\t\t\t\t\t\tawait switchHeaterOn(oneSwitch.idx, oneSwitch.Name, false);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tcurrent_room.power += parseInt(namebits[4]);\n\t\t\t} else {\n\t\t\t\tif (namebits[3] == 'inv') {\n\t\t\t\t\tif (oneSwitch.Status === \"Off\") {\n\t\t\t\t\t\t// something is wrong: heater should be aligned with room\n\t\t\t\t\t\t// say(oneSwitch.Name + \" (inverted) is Off and should be On !!!\");\n\t\t\t\t\t\tawait switchHeaterOff(oneSwitch.idx, oneSwitch.Name, true);\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// resend ?\n\t\t\t\t\t\tif (current_room.resend) {\n\t\t\t\t\t\t\tsay(oneSwitch.Name + \" resend Off command\");\n\t\t\t\t\t\t\tawait switchHeaterOff(oneSwitch.idx, oneSwitch.Name, true);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tif (oneSwitch.Status === \"On\") {\n\t\t\t\t\t\t// something is wrong: heater should be aligned with room\n\t\t\t\t\t\t// say(oneSwitch.Name + \" is On and should be Off !!!\");\n\t\t\t\t\t\tawait switchHeaterOff(oneSwitch.idx, oneSwitch.Name, false);\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// resend ?\n\t\t\t\t\t\tif (current_room.resend) {\n\t\t\t\t\t\t\tsay(oneSwitch.Name + \" resend Off command\");\n\t\t\t\t\t\t\tawait switchHeaterOff(oneSwitch.idx, oneSwitch.Name, false);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\n\t\t// deal with the VMC\n\t\t} else if (namebits[0] == 'VMC') {\n\t\t\tif (parseInt(domoJS.state.rooms[namebits[1]].humidity) >= parseInt(namebits[2])) {\n\t\t\t\t// too wet\n\t\t\t\tif (oneSwitch.Status == 'Off') {\n\t\t\t\t\tawait switchHeaterOn(oneSwitch.idx, oneSwitch.Name, false);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// dry enough\n\t\t\t\tif (oneSwitch.Status == 'On') {\n\t\t\t\t\tawait switchHeaterOff(oneSwitch.idx, oneSwitch.Name, false);\n\t\t\t\t}\n\t\t\t}\n\t\n\t\t// Other\n\t\t} else {\n\t\t\t// console.log(oneSwitch.Name);\n\t\t}\n\t\n\t}\n\n\t// console.log(switches);\n}\n\n\n// switching on a heater\nasync function switchHeaterOn (devIdx, name, isInverted) {\n    say( \"sending switching ON  command to \" + name );\n\n    // this sends the request\n    if (isInverted) {\n        await domoAPI.switchDevice( devIdx, \"Off\");\n    } else {\n        await domoAPI.switchDevice( devIdx, \"On\");\n    }\n};\n\n\n// switching off a heater\nasync function switchHeaterOff (devIdx, name, isInverted) {\n    say( \"sending switching OFF command to \" + name );\n\n    // this sends the request\n    if (isInverted) {\n        await domoAPI.switchDevice( devIdx, \"On\");\n    } else {\n        await domoAPI.switchDevice( devIdx, \"Off\");\n    }\n};\n\n\n\n/**\n * Count each minute each heater has been ON.\n * Scatter those minutes between High price hours and low price hours.\n */\nfunction countConsumption() {\n\n    const now = new MyDate();\n\n    // console.log(state);\n    const lastStateUpdate = new MyDate(domoJS.state.lastUpdate);\n    const minSinceLastRun = Math.round( (now - lastStateUpdate) / 1000 / 60 );\n    const HCorHP = lastStateUpdate.getHCHP();\n\n    // we add the number of minutes each heater has been on (in state)\n    for (let room_name in domoJS.state.rooms) {\n        let room = domoJS.state.rooms[room_name];\n        if (room.state === \"On\") {\n            room[HCorHP] += minSinceLastRun;\n            // console.log( room.name + \" is \" + room.state);\n        }\n    }\n\n    // update last update in the state and save it to disk\n    domoJS.state.lastUpdate = now.toISOString();\n\n    // we save state in 5 seconds: we want to be sure that everything has been done\n    setTimeout( writeState, 5000);\n\n}\n\n\n/**\n * Write down state in json file\n */\ndomoJS.writeState = async function(filepathname) {\n//\tconsole.log(filepathname);\n//\tconsole.log(JSON.stringify(domoJS.state, null, 4));\n    fs.writeFileSync( filepathname, JSON.stringify(domoJS.state, null, 4), function(err){ if(err) throw err; } );\n}\n\n\n/**\n * Wrappers around gsheetAPI just so that I don't need to call\n * gsheetAPI (and have to require it) from the outside world.\n */\ndomoJS.getTempsFromGoogleSheet = async function() {\n    await gsheetAPI.getTempsFromGoogleSheet(domoJS.configs);\n};\ndomoJS.uploadToGoogleSheet = function() {\n    gsheetAPI.uploadToGoogleSheet(domoJS.configs, domoJS.state);\n};\n\n\n\nmodule.exports = domoJS;\n"]}