|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const DaikinCloud = require("daikin-controller-cloud"); |
| 4 | +const fs = require("fs"); |
| 5 | + |
| 6 | +module.exports = function(config, ircbot, utils) { |
| 7 | + const moduleConfig = { |
| 8 | + keys: ['ac'], |
| 9 | + description: 'shows AC status', |
| 10 | + }; |
| 11 | + |
| 12 | + /** |
| 13 | + * Options to initialize the DaikinCloud instance with |
| 14 | + */ |
| 15 | + const options = { |
| 16 | + logger: console.log, // optional, logger function used to log details depending on loglevel |
| 17 | + logLevel: 'info', // optional, Loglevel of Library, default 'warn' (logs nothing by default) |
| 18 | + communicationTimeout: 10000, // Amount of ms to wait for request and responses before timeout |
| 19 | + communicationRetries: 3 // Amount of retries when connection timed out |
| 20 | + }; |
| 21 | + |
| 22 | + const tokenSet = utils.file.readJson(config.tokenPath); |
| 23 | + |
| 24 | + if (!tokenSet) { |
| 25 | + console.error('No token file or invalid tokens: ', config.tokenPath); |
| 26 | + |
| 27 | + return { |
| 28 | + ...moduleConfig, |
| 29 | + execute: () => {}, |
| 30 | + }; |
| 31 | + } |
| 32 | + |
| 33 | + const daikinCloud = new DaikinCloud(tokenSet, options); |
| 34 | + |
| 35 | + // Event that will be triggered on new or updated tokens, save into file |
| 36 | + daikinCloud.on('token_update', tokenSet => { |
| 37 | + console.log(`UPDATED tokens, use for future and wrote to ${config.tokenPath}`); |
| 38 | + fs.writeFileSync(config.tokenPath, JSON.stringify(tokenSet)); |
| 39 | + }); |
| 40 | + |
| 41 | + async function execute(replyTo) { |
| 42 | + const devices = await daikinCloud.getCloudDevices(); |
| 43 | + |
| 44 | + if (devices && devices.length) { |
| 45 | + for (let dev of devices) { |
| 46 | + const mode = dev.getData('climateControl', 'operationMode'); |
| 47 | + const setPointMode = ['heating', 'cooling', 'auto'].indexOf(mode) > -1 ? mode : 'auto'; |
| 48 | + const tempControlPath = '/operationModes/' + setPointMode + '/setpoints/roomTemperature'; |
| 49 | + |
| 50 | + ircbot.say(replyTo, |
| 51 | + dev.getData('climateControl', 'name').value + ': ' + |
| 52 | + (dev.getData('climateControl', 'onOffMode').value ? 'ON' : 'OFF') + ', ' + |
| 53 | + 'Mode: ' + dev.getData('climateControl', 'operationMode').value + ', ' + |
| 54 | + 'Target: ' + dev.getData('climateControl', 'temperatureControl', tempControlPath).value + '°C, ' + |
| 55 | + 'Room temp: ' + dev.getData('climateControl', 'sensoryData', '/roomTemperature').value + '°C, ' + |
| 56 | + 'Room humidity: ' + dev.getData('climateControl', 'sensoryData', '/roomHumidity').value + '%, ' + |
| 57 | + 'Out temp: ' + dev.getData('climateControl', 'sensoryData', '/outdoorTemperature').value + '°C, ' + |
| 58 | + 'Econo: ' + dev.getData('climateControl', 'econoMode').value + ', ' + |
| 59 | + 'Powerful: ' + dev.getData('climateControl', 'powerfulMode').value + '-' + |
| 60 | + (dev.getData('climateControl', 'isPowerfulModeActive').value ? 'active' : 'inactive') |
| 61 | + ); |
| 62 | + } |
| 63 | + } else { |
| 64 | + ircbot.say(replyTo, 'No devices returned'); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return { |
| 69 | + ...moduleConfig, |
| 70 | + execute, |
| 71 | + }; |
| 72 | +}; |
0 commit comments