syntex-automation
Advanced tools
Comparing version 1.2.0-b3 to 1.2.0-b30
{ | ||
"env": { | ||
"browser": true, | ||
"es2021": true, | ||
"node": true | ||
}, | ||
"extends": "eslint:recommended", | ||
"parserOptions": { | ||
"ecmaVersion": 13, | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
} | ||
"env": { | ||
"browser": true, | ||
"es2021": true, | ||
"node": true | ||
}, | ||
"extends": "eslint:recommended", | ||
"parserOptions": { | ||
"ecmaVersion": 13, | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
} | ||
} |
28
main.js
let LogikEngine = require('./src/automation'), RouteManager = require('./src/route-manager'); | ||
const EventEmitter = require('events'); | ||
module.exports = class AutomationSystem extends EventEmitter | ||
module.exports = class AutomationSystem | ||
{ | ||
constructor(platform) | ||
{ | ||
super(); | ||
super.setMaxListeners(512); | ||
this.pluginName = platform.pluginName; | ||
@@ -18,23 +12,3 @@ this.logger = platform.logger; | ||
this.LogikEngine = new LogikEngine(platform, this); | ||
} | ||
setInputStream(stream, sender, callback) | ||
{ | ||
super.on(stream, (destination, state) => { | ||
if(sender.id == destination.id && sender.letters == destination.letters) | ||
{ | ||
callback(state); | ||
this.logger.debug('<<< ' + stream + ' [' + JSON.stringify(destination) + '] ' + JSON.stringify(state)); | ||
} | ||
}); | ||
} | ||
setOutputStream(stream, destination, state) | ||
{ | ||
super.emit(stream, destination, state); | ||
this.logger.debug('>>> ' + stream + ' [' + JSON.stringify(destination) + '] ' + JSON.stringify(state)); | ||
} | ||
} |
{ | ||
"name": "syntex-automation", | ||
"version": "1.2.0-b3", | ||
"version": "1.2.0-b30", | ||
"description": "An automation system for your smart home", | ||
@@ -13,4 +13,4 @@ "main": "main.js", | ||
"axios": "^0.21.1", | ||
"syntex-filesystem": "1.0.2-b6", | ||
"syntex-logger": "1.0.8-b11" | ||
"syntex-filesystem": "1.0.2-b15", | ||
"syntex-logger": "1.0.8-b24" | ||
}, | ||
@@ -17,0 +17,0 @@ "keywords": [ |
@@ -8,2 +8,4 @@ const axios = require('axios'); | ||
this.ready = false; | ||
this.automation = []; | ||
@@ -19,2 +21,3 @@ this.timeLock = {}; | ||
this.ContextManager = platform.ContextManager; | ||
this.EventManager = platform.EventManager; | ||
this.TypeManager = platform.TypeManager; | ||
@@ -24,11 +27,45 @@ | ||
this.files.readFile('automation/automation-lock.json').then((data) => { | ||
this.loadAutomation().then((automationSuccess) => { | ||
if(automationSuccess) | ||
{ | ||
this.loadLock().then((lockSuccess) => { | ||
if(data != null) | ||
{ | ||
this.timeLock = data.timeLock || {}; | ||
this.stateLock = data.stateLock || {}; | ||
if(lockSuccess) | ||
{ | ||
this.initNetwork(); | ||
this.timeInterval = setInterval(() => { | ||
var changed = false; | ||
for(const automation of this.automation) | ||
{ | ||
if(automation.active && this._includesTime(automation)) | ||
{ | ||
if(this._checkLock(automation)) | ||
{ | ||
changed = true; | ||
} | ||
if(!this._isLocked(automation)) | ||
{ | ||
this.checkTrigger(automation, { name : ('0' + new Date().getHours()).slice(-2) + ':' + ('0' + new Date().getMinutes()).slice(-2) }, {}); | ||
} | ||
} | ||
} | ||
if(changed) | ||
{ | ||
this.files.writeFile('automation/automation-lock.json', { timeLock : this.timeLock, stateLock : this.stateLock }); | ||
} | ||
}, 60000); | ||
this.logger.log('success', 'automation', 'Automation', '%automation_load_success%!'); | ||
this.ready = true; | ||
} | ||
}); | ||
} | ||
this.loadAutomation(); | ||
}); | ||
@@ -46,19 +83,34 @@ } | ||
this.automation = data; | ||
} | ||
resolve(true); | ||
resolve(data != null); | ||
this.parseAutomation(); | ||
}).catch(() => { | ||
this.logger.log('warn', 'automation', 'Automation', '%automation_load_error%!'); | ||
this.logger.log('success', 'automation', 'Automation', '%automation_load_success%!'); | ||
} | ||
else | ||
{ | ||
this.automation = []; | ||
resolve(false); | ||
}); | ||
}); | ||
} | ||
resolve(false); | ||
loadLock() | ||
{ | ||
return new Promise((resolve) => { | ||
this.logger.log('warn', 'automation', 'Automation', '%automation_load_error%!'); | ||
this.files.readFile('automation/automation-lock.json').then((data) => { | ||
if(data != null) | ||
{ | ||
this.timeLock = data.timeLock || {}; | ||
this.stateLock = data.stateLock || {}; | ||
} | ||
this.ready = true; | ||
resolve(true); | ||
}).catch(() => { | ||
this.logger.log('warn', 'automation', 'Automation', '%automation_load_error%!'); | ||
resolve(false); | ||
}); | ||
@@ -68,77 +120,42 @@ }); | ||
parseAutomation() | ||
initNetwork() | ||
{ | ||
try | ||
if(this.EventManager != null) | ||
{ | ||
for(const i in this.automation) | ||
{ | ||
if(this.automation[i].trigger != null && this.automation[i].trigger.groups != null) | ||
this.EventManager.setInputStream('updateLock', { source : this, external : true }, (message) => { | ||
if(message.id != null && message.lock != null) | ||
{ | ||
for(const j in this.automation[i].trigger.groups) | ||
if(this.stateLock[message.id] == null) | ||
{ | ||
if(this.automation[i].trigger.groups[j].blocks != null) | ||
this.stateLock[message.id] = {}; | ||
} | ||
if(message.blockID != null) | ||
{ | ||
if(this.stateLock[message.id].trigger == null) | ||
{ | ||
for(const k in this.automation[i].trigger.groups[j].blocks) | ||
{ | ||
if(this.automation[i].trigger.groups[j].blocks[k].state != null) | ||
{ | ||
if(this.automation[i].trigger.groups[j].blocks[k].state.value != null) | ||
{ | ||
this.automation[i].trigger.groups[j].blocks[k].state.value = JSON.parse(this.automation[i].trigger.groups[j].blocks[k].state.value); | ||
} | ||
if(this.automation[i].trigger.groups[j].blocks[k].state.hue != null) | ||
{ | ||
this.automation[i].trigger.groups[j].blocks[k].state.hue = JSON.parse(this.automation[i].trigger.groups[j].blocks[k].state.hue); | ||
} | ||
if(this.automation[i].trigger.groups[j].blocks[k].state.saturation != null) | ||
{ | ||
this.automation[i].trigger.groups[j].blocks[k].state.saturation = JSON.parse(this.automation[i].trigger.groups[j].blocks[k].state.saturation); | ||
} | ||
if(this.automation[i].trigger.groups[j].blocks[k].state.brightness != null) | ||
{ | ||
this.automation[i].trigger.groups[j].blocks[k].state.brightness = JSON.parse(this.automation[i].trigger.groups[j].blocks[k].state.brightness); | ||
} | ||
} | ||
} | ||
this.stateLock[message.id].trigger = {}; | ||
} | ||
this.stateLock[message.id].trigger[message.blockID] = message.lock; | ||
} | ||
else | ||
{ | ||
this.stateLock[message.id].result = message.lock; | ||
} | ||
} | ||
}); | ||
if(this.automation[i].result != null) | ||
{ | ||
for(const j in this.automation[i].result) | ||
{ | ||
if(this.automation[i].result[j].state != null) | ||
{ | ||
if(this.automation[i].result[j].state.value != null) | ||
{ | ||
this.automation[i].result[j].state.value = JSON.parse(this.automation[i].result[j].state.value); | ||
} | ||
this.EventManager.setInputStream('updateAutomation', { source : this, external : true }, () => { | ||
if(this.automation[i].result[j].state.hue != null) | ||
{ | ||
this.automation[i].result[j].state.hue = JSON.parse(this.automation[i].result[j].state.hue); | ||
} | ||
this.loadAutomation().then((success) => { | ||
if(this.automation[i].result[j].state.saturation != null) | ||
{ | ||
this.automation[i].result[j].state.saturation = JSON.parse(this.automation[i].result[j].state.saturation); | ||
} | ||
if(this.automation[i].result[j].state.brightness != null) | ||
{ | ||
this.automation[i].result[j].state.brightness = JSON.parse(this.automation[i].result[j].state.brightness); | ||
} | ||
} | ||
if(success) | ||
{ | ||
this.logger.log('success', 'automation', 'Automation', '%automation_load_success%!'); | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
} | ||
catch(e) | ||
{ | ||
this.logger.log('error', 'automation', 'Automation', 'Automation %json_parse_error%!', e); | ||
} | ||
} | ||
@@ -150,24 +167,2 @@ | ||
const INCLUDES = (automation, id, letters) => { | ||
if(automation.trigger != null) | ||
{ | ||
for(const i in automation.trigger.groups) | ||
{ | ||
if(automation.trigger.groups[i].blocks != null) | ||
{ | ||
for(const j in automation.trigger.groups[i].blocks) | ||
{ | ||
if(automation.trigger.groups[i].blocks[j].id == id && automation.trigger.groups[i].blocks[j].letters == letters) | ||
{ | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
}; | ||
if(this.ready) | ||
@@ -177,64 +172,15 @@ { | ||
for(const i in this.automation) | ||
for(const automation of this.automation) | ||
{ | ||
if(this.automation[i].active && INCLUDES(this.automation[i], service.id, service.letters)) | ||
if(automation.active && this._includesBlock(automation, service.id, service.letters)) | ||
{ | ||
if(this.stateLock[this.automation[i].id] != null && this.stateLock[this.automation[i].id].trigger != null && this.stateLock[this.automation[i].id].trigger[service.id + '#' + service.letters] != null) | ||
if(this._checkLock(automation, service, state)) | ||
{ | ||
for(const j in this.automation[i].trigger.groups) | ||
{ | ||
for(const k in this.automation[i].trigger.groups[j].blocks) | ||
{ | ||
if(this.automation[i].trigger.groups[j].blocks[k].id == service.id && this.automation[i].trigger.groups[j].blocks[k].letters == service.letters) | ||
{ | ||
if(this.automation[i].trigger.groups[j].blocks[k].operation == '>') | ||
{ | ||
if(state.value != null && this.automation[i].trigger.groups[j].blocks[k].state.value != null && state.value < this.automation[i].trigger.groups[j].blocks[k].state.value | ||
|| state.hue != null && this.automation[i].trigger.groups[j].blocks[k].state.hue != null && state.hue < this.automation[i].trigger.groups[j].blocks[k].state.hue | ||
|| state.saturation != null && this.automation[i].trigger.groups[j].blocks[k].state.saturation != null && state.saturation < this.automation[i].trigger.groups[j].blocks[k].state.saturation | ||
|| state.brightness != null && this.automation[i].trigger.groups[j].blocks[k].state.brightness != null && state.brightness < this.automation[i].trigger.groups[j].blocks[k].state.brightness) | ||
{ | ||
this.stateLock[this.automation[i].id].trigger[service.id + '#' + service.letters] = false; | ||
changed = true; | ||
} | ||
this.logger.debug('Automation [' + this.automation[i].name + '] %automation_lower% ' + this.automation[i].id + ' ' + service.id + '#' + service.letters); | ||
changed = true; | ||
} | ||
} | ||
if(this.automation[i].trigger.groups[j].blocks[k].operation == '<') | ||
{ | ||
if(state.value != null && this.automation[i].trigger.groups[j].blocks[k].state.value != null && state.value > this.automation[i].trigger.groups[j].blocks[k].state.value | ||
|| state.hue != null && this.automation[i].trigger.groups[j].blocks[k].state.hue != null && state.hue > this.automation[i].trigger.groups[j].blocks[k].state.hue | ||
|| state.saturation != null && this.automation[i].trigger.groups[j].blocks[k].state.saturation != null && state.saturation > this.automation[i].trigger.groups[j].blocks[k].state.saturation | ||
|| state.brightness != null && this.automation[i].trigger.groups[j].blocks[k].state.brightness != null && state.brightness > this.automation[i].trigger.groups[j].blocks[k].state.brightness) | ||
{ | ||
this.stateLock[this.automation[i].id].trigger[service.id + '#' + service.letters] = false; | ||
this.logger.debug('Automation [' + this.automation[i].name + '] %automation_greater% ' + this.automation[i].id + ' ' + service.id + '#' + service.letters); | ||
changed = true; | ||
} | ||
} | ||
if(this.automation[i].trigger.groups[j].blocks[k].operation == '=') | ||
{ | ||
if(state.value != null && this.automation[i].trigger.groups[j].blocks[k].state.value != null && state.value != this.automation[i].trigger.groups[j].blocks[k].state.value | ||
|| state.hue != null && this.automation[i].trigger.groups[j].blocks[k].state.hue != null && state.hue != this.automation[i].trigger.groups[j].blocks[k].state.hue | ||
|| state.saturation != null && this.automation[i].trigger.groups[j].blocks[k].state.saturation != null && state.saturation != this.automation[i].trigger.groups[j].blocks[k].state.saturation | ||
|| state.brightness != null && this.automation[i].trigger.groups[j].blocks[k].state.brightness != null && state.brightness != this.automation[i].trigger.groups[j].blocks[k].state.brightness) | ||
{ | ||
this.stateLock[this.automation[i].id].trigger[service.id + '#' + service.letters] = false; | ||
this.logger.debug('Automation [' + this.automation[i].name + '] %automation_different% ' + this.automation[i].id + ' ' + service.id + '#' + service.letters); | ||
changed = true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if(!this._isLocked(automation)) | ||
{ | ||
this.checkTrigger(automation, service, state); | ||
} | ||
this.checkTrigger(this.automation[i], service, state); | ||
} | ||
@@ -253,3 +199,3 @@ } | ||
async checkTrigger(automation, service) | ||
async checkTrigger(automation, service, state) | ||
{ | ||
@@ -262,7 +208,7 @@ const TRIGGER = (blocks, logic) => { | ||
for(const i in blocks) | ||
for(const block of blocks) | ||
{ | ||
promiseArray.push(new Promise((callback) => this._getState(automation, blocks[i]).then((state) => callback({ block : blocks[i], state : state || {} })))); | ||
promiseArray.push(new Promise((callback) => (block.id != service.id || block.letters != service.letters ? this._getState(automation, block).then((state) => callback({ block : block, state : state || {} })) : callback({ block : block, state })))); | ||
} | ||
Promise.all(promiseArray).then((result) => { | ||
@@ -286,42 +232,2 @@ | ||
const LOGIC = (block, state) => { | ||
var success = false; | ||
if(block.operation == '>') | ||
{ | ||
if(state.value != null && block.state.value != null && state.value > block.state.value | ||
|| state.hue != null && block.state.hue != null && state.hue > block.state.hue | ||
|| state.saturation != null && block.state.saturation != null && state.saturation > block.state.saturation | ||
|| state.brightness != null && block.state.brightness != null && state.brightness > block.state.brightness) | ||
{ | ||
success = true; | ||
} | ||
} | ||
if(block.operation == '<') | ||
{ | ||
if(state.value != null && block.state.value != null && state.value < block.state.value | ||
|| state.hue != null && block.state.hue != null && state.hue < block.state.hue | ||
|| state.saturation != null && block.state.saturation != null && state.saturation < block.state.saturation | ||
|| state.brightness != null && block.state.brightness != null && state.brightness < block.state.brightness) | ||
{ | ||
success = true; | ||
} | ||
} | ||
if(block.operation == '=') | ||
{ | ||
if(state.value == null || block.state.value == null || state.value == block.state.value | ||
&& (state.hue == null || block.state.hue == null || state.hue == block.state.hue) | ||
&& (state.saturation == null || block.state.saturation == null || state.saturation == block.state.saturation) | ||
&& (state.brightness == null || block.state.brightness == null || state.brightness == block.state.brightness)) | ||
{ | ||
success = true; | ||
} | ||
} | ||
return success; | ||
}; | ||
const AND = (blocks) => { | ||
@@ -331,5 +237,5 @@ | ||
for(const i in blocks) | ||
for(const block of blocks) | ||
{ | ||
if(!LOGIC(blocks[i].block, blocks[i].state)) | ||
if(!this._getOutput(block.block, block.state)) | ||
{ | ||
@@ -347,5 +253,5 @@ success = false; | ||
for(const i in blocks) | ||
for(const block of blocks) | ||
{ | ||
if(LOGIC(blocks[i].block, blocks[i].state)) | ||
if(this._getOutput(block.block, block.state)) | ||
{ | ||
@@ -359,28 +265,2 @@ success = true; | ||
const HAS_LOCK = () => { | ||
if(automation.trigger != null && automation.trigger.groups != null) | ||
{ | ||
for(const i in automation.trigger.groups) | ||
{ | ||
if(automation.trigger.groups[i].blocks != null) | ||
{ | ||
for(const j in automation.trigger.groups[i].blocks) | ||
{ | ||
if(automation.trigger.groups[i].blocks[j].options != null | ||
&& automation.trigger.groups[i].blocks[j].options.stateLock == true) | ||
{ | ||
if(this.stateLock[automation.id] != null && this.stateLock[automation.id].trigger != null && this.stateLock[automation.id].trigger[service.id + '#' + service.letters] == true) | ||
{ | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
}; | ||
var promiseArray = []; | ||
@@ -390,7 +270,7 @@ | ||
{ | ||
for(const i in automation.trigger.groups) | ||
for(const group of automation.trigger.groups) | ||
{ | ||
if(automation.trigger.groups[i].blocks != null && automation.trigger.groups[i].logic != null) | ||
if(group.blocks != null && group.logic != null) | ||
{ | ||
promiseArray.push(TRIGGER(automation.trigger.groups[i].blocks, automation.trigger.groups[i].logic, service.id, service.letters)); | ||
promiseArray.push(TRIGGER(group.blocks, group.logic, service.id, service.letters)); | ||
} | ||
@@ -404,13 +284,10 @@ } | ||
{ | ||
if(this.timeLock[automation.id] == null || new Date().getTime() >= this.timeLock[automation.id]) | ||
if(automation.options == null | ||
|| automation.options.timeLock == null | ||
|| this.timeLock[automation.id] == null | ||
|| new Date().getTime() >= this.timeLock[automation.id]) | ||
{ | ||
if((automation.options != null && automation.options.stateLock == false) || this.stateLock[automation.id] == null || this.stateLock[automation.id].result != true) | ||
{ | ||
if(!HAS_LOCK()) | ||
{ | ||
this.logger.debug('Automation [' + automation.name + '] %trigger_activated%'); | ||
this.logger.debug('Automation [' + automation.name + '] %trigger_activated%'); | ||
this.executeResult(automation, service); | ||
} | ||
} | ||
this.executeResult(automation, service); | ||
} | ||
@@ -422,2 +299,4 @@ } | ||
this._updateSockets(false, automation.id); | ||
this.logger.debug('Automation [' + automation.name + '] %automation_different% ' + automation.id); | ||
@@ -502,58 +381,98 @@ } | ||
executeResult(automation, trigger) | ||
async executeResult(automation, trigger) | ||
{ | ||
var success = false; | ||
var promiseArray = [], firstSuccess = false, | ||
locked = this.stateLock[automation.id] != null && this.stateLock[automation.id].result == true; | ||
for(const i in automation.result) | ||
for(const block of automation.result) | ||
{ | ||
var result = automation.result[i]; | ||
if(result.url != null) | ||
if(block.delay != null) | ||
{ | ||
let theRequest = { | ||
url : result.url, | ||
timeout : 10000 | ||
}; | ||
this.fetchRequest(theRequest, automation.name, result); | ||
success = true; | ||
await new Promise((resolve) => setTimeout(() => resolve(), block.delay)); | ||
} | ||
if(result.id != null && result.letters != null && result.state != null && result.name != null) | ||
if((block.options != null && block.options.stateLock == false) || !locked) | ||
{ | ||
var state = { ...result.state }; | ||
if((state = this.TypeManager.validateUpdate(result.id, result.letters, state)) != null) | ||
if(block.url != null) | ||
{ | ||
if(this.TypeManager.letterToType(result.letters[0]) == 'statelessswitch') | ||
{ | ||
state.event = state.value; | ||
state.value = 0; | ||
} | ||
promiseArray.push(new Promise((resolve) => { | ||
if(result.plugin != null && this.manager.pluginName != result.plugin && this.manager.RouteManager.getPort(result.plugin) != null) | ||
{ | ||
let theRequest = { | ||
url : 'http://' + (result.bridge || '127.0.0.1') + ':' + this.manager.RouteManager.getPort(result.plugin) + '/devices?id=' + result.id + '&type=' + this.TypeManager.letterToType(result.letters[0]) + '&counter=' + result.letters[1], | ||
var theRequest = { | ||
url : block.url, | ||
timeout : 10000 | ||
}; | ||
this.fetchRequest(theRequest, automation.name, block).then((data) => { | ||
for(const x in state) | ||
{ | ||
theRequest.url += '&' + x + '=' + state[x]; | ||
} | ||
if(!firstSuccess) | ||
{ | ||
firstSuccess = true; | ||
this.fetchRequest(theRequest, automation.name, result); | ||
} | ||
else | ||
{ | ||
this.manager.setOutputStream('SynTexAutomation', { id : result.id, letters : result.letters }, state); | ||
} | ||
this._automationLock(automation, block); | ||
} | ||
success = true; | ||
resolve(data != null); | ||
}); | ||
})); | ||
} | ||
else | ||
if(block.id != null && block.letters != null && block.state != null && block.name != null) | ||
{ | ||
this.logger.log('error', result.id, result.letters, '[' + result.name + '] %update_error%! ( ' + result.id + ' )'); | ||
promiseArray.push(new Promise((resolve) => { | ||
var state = { ...block.state }; | ||
if((state = this.TypeManager.validateUpdate(block.id, block.letters, state)) != null) | ||
{ | ||
if(this.TypeManager.letterToType(block.letters[0]) == 'statelessswitch') | ||
{ | ||
state.event = state.value; | ||
state.value = 0; | ||
} | ||
if(block.bridge != null && block.port != null) | ||
{ | ||
var theRequest = { | ||
url : 'http://' + block.bridge + ':' + block.port + '/devices?id=' + block.id + '&type=' + this.TypeManager.letterToType(block.letters[0]) + '&counter=' + block.letters[1], | ||
timeout : 10000 | ||
}; | ||
for(const x in state) | ||
{ | ||
theRequest.url += '&' + x + '=' + state[x]; | ||
} | ||
this.fetchRequest(theRequest, automation.name, block).then((data) => { | ||
if(!firstSuccess) | ||
{ | ||
firstSuccess = true; | ||
this._automationLock(automation, block); | ||
} | ||
resolve(data != null); | ||
}); | ||
} | ||
else | ||
{ | ||
this.EventManager.setOutputStream('changeHandler', { receiver : { id : block.id, letters : block.letters } }, state); | ||
if(!firstSuccess) | ||
{ | ||
firstSuccess = true; | ||
this._automationLock(automation, block); | ||
} | ||
resolve(true); | ||
} | ||
} | ||
else | ||
{ | ||
this.logger.log('error', block.id, block.letters, '[' + block.name + '] %update_error%! ( ' + block.id + ' )'); | ||
resolve(false); | ||
} | ||
})); | ||
} | ||
@@ -563,6 +482,13 @@ } | ||
if(success) | ||
{ | ||
this._automationSuccess(automation, trigger); | ||
} | ||
Promise.all(promiseArray).then((success) => { | ||
if(success.includes(true)) | ||
{ | ||
this._automationLock(automation); | ||
this.ContextManager.updateAutomation(trigger.id, trigger.letters, automation); | ||
this.logger.log('success', trigger.id, trigger.letters, '[' + trigger.name + '] %automation_executed[0]% [' + automation.name + '] %automation_executed[1]%!'); | ||
} | ||
}); | ||
} | ||
@@ -583,85 +509,318 @@ | ||
_automationSuccess(automation, trigger) | ||
_automationLock(automation, block) | ||
{ | ||
var changed = false; | ||
this.ContextManager.updateAutomation(trigger.id, trigger.letters, automation); | ||
if(automation.options != null && automation.options.timeLock != null) | ||
if(block != null) | ||
{ | ||
this.timeLock[automation.id] = new Date().getTime() + automation.options.timeLock; | ||
if(block.options == null || block.options.stateLock != false) | ||
{ | ||
if(this.stateLock[automation.id] == null) | ||
{ | ||
this.stateLock[automation.id] = {}; | ||
} | ||
changed = true; | ||
if(this.stateLock[automation.id].result != true) | ||
{ | ||
this.stateLock[automation.id].result = true; | ||
this._updateSockets(true, automation.id); | ||
changed = true; | ||
} | ||
} | ||
} | ||
for(const i in automation.trigger.groups) | ||
else | ||
{ | ||
for(const j in automation.trigger.groups[i].blocks) | ||
if(automation.options != null && automation.options.timeLock != null) | ||
{ | ||
if(automation.trigger.groups[i].blocks[j].options != null && automation.trigger.groups[i].blocks[j].options.stateLock == true) | ||
this.timeLock[automation.id] = new Date().getTime() + automation.options.timeLock; | ||
changed = true; | ||
} | ||
for(const i in automation.trigger.groups) | ||
{ | ||
for(const j in automation.trigger.groups[i].blocks) | ||
{ | ||
if(this.stateLock[automation.id] == null) | ||
if(automation.trigger.groups[i].blocks[j].options != null && automation.trigger.groups[i].blocks[j].options.stateLock == true) | ||
{ | ||
this.stateLock[automation.id] = {}; | ||
} | ||
if(this.stateLock[automation.id] == null) | ||
{ | ||
this.stateLock[automation.id] = {}; | ||
} | ||
if(this.stateLock[automation.id].trigger == null) | ||
{ | ||
this.stateLock[automation.id].trigger = {}; | ||
if(this.stateLock[automation.id].trigger == null) | ||
{ | ||
this.stateLock[automation.id].trigger = {}; | ||
} | ||
this.stateLock[automation.id].trigger[i + '' + j] = true; | ||
this._updateSockets(true, automation.id, i + '' + j); | ||
changed = true; | ||
} | ||
} | ||
} | ||
} | ||
this.stateLock[automation.id].trigger[automation.trigger.groups[i].blocks[j].id + '#' + automation.trigger.groups[i].blocks[j].letters] = true; | ||
if(changed) | ||
{ | ||
this.files.writeFile('automation/automation-lock.json', { timeLock : this.timeLock, stateLock : this.stateLock }); | ||
} | ||
} | ||
changed = true; | ||
async _getState(automation, block) | ||
{ | ||
var state = null; | ||
if(block.id != null && block.letters != null) | ||
{ | ||
if((block.bridge != null && block.port != null) || (block.plugin != null && this.manager.pluginName != block.plugin && this.manager.RouteManager.getPort(block.plugin) != null)) | ||
{ | ||
var theRequest = { | ||
url : 'http://' + (block.bridge || '127.0.0.1') + ':' + (block.port || this.manager.RouteManager.getPort(block.plugin)) + '/devices?id=' + block.id + '&type=' + this.TypeManager.letterToType(block.letters[0]) + '&counter=' + block.letters[1], | ||
timeout : 10000 | ||
}; | ||
try | ||
{ | ||
state = await this.fetchRequest(theRequest, automation.name, block); | ||
} | ||
catch(e) | ||
{ | ||
this.logger.log('error', 'automation', 'Automation', 'Request %json_parse_error%!', e); | ||
} | ||
} | ||
else | ||
{ | ||
state = this.platform.readAccessoryService(block.id, block.letters, true); | ||
} | ||
} | ||
if(automation.options == null || automation.options.stateLock == true) | ||
return state; | ||
} | ||
_getOutput(block, state = {}) | ||
{ | ||
var now = new Date(); | ||
if(block.time != null && block.time.includes(':') && block.operation != null) | ||
{ | ||
if(this.stateLock[automation.id] == null) | ||
var begin = new Date(), end = new Date(); | ||
begin.setMinutes(0); | ||
begin.setSeconds(0); | ||
begin.setMilliseconds(0); | ||
end.setMinutes(0); | ||
end.setSeconds(0); | ||
end.setMilliseconds(0); | ||
if(block.operation == '>') | ||
{ | ||
this.stateLock[automation.id] = {}; | ||
begin.setHours(block.time.split(':')[0]); | ||
begin.setMinutes(block.time.split(':')[1]); | ||
end.setHours(24); | ||
} | ||
this.stateLock[automation.id].result = true; | ||
if(block.operation == '<') | ||
{ | ||
begin.setHours(0); | ||
changed = true; | ||
end.setHours(block.time.split(':')[0]); | ||
end.setMinutes(block.time.split(':')[1]); | ||
} | ||
if(block.operation == '=') | ||
{ | ||
begin.setHours(block.time.split(':')[0]); | ||
begin.setMinutes(block.time.split(':')[1]); | ||
end.setHours(block.time.split(':')[0]); | ||
end.setMinutes(parseInt(block.time.split(':')[1]) + 1); | ||
} | ||
if(now.getTime() > begin.getTime() | ||
&& now.getTime() < end.getTime()) | ||
{ | ||
return true; | ||
} | ||
} | ||
if(changed) | ||
if(block.days != null && Array.isArray(block.days) && block.days.includes(now.getDay())) | ||
{ | ||
this.files.writeFile('automation/automation-lock.json', { timeLock : this.timeLock, stateLock : this.stateLock }); | ||
return true; | ||
} | ||
this.logger.log('success', trigger.id, trigger.letters, '[' + trigger.name + '] %automation_executed[0]% [' + automation.name + '] %automation_executed[1]%!'); | ||
if(block.id != null && block.letters != null && block.state != null && block.state instanceof Object && block.operation != null) | ||
{ | ||
if(block.operation == '>') | ||
{ | ||
if((state.value == null || block.state.value == null || state.value > block.state.value) | ||
&& (state.hue == null || block.state.hue == null || state.hue > block.state.hue) | ||
&& (state.saturation == null || block.state.saturation == null || state.saturation > block.state.saturation) | ||
&& (state.brightness == null || block.state.brightness == null || state.brightness > block.state.brightness)) | ||
{ | ||
return true; | ||
} | ||
} | ||
if(block.operation == '<') | ||
{ | ||
if((state.value == null || block.state.value == null || state.value < block.state.value) | ||
&& (state.hue == null || block.state.hue == null || state.hue < block.state.hue) | ||
&& (state.saturation == null || block.state.saturation == null || state.saturation < block.state.saturation) | ||
&& (state.brightness == null || block.state.brightness == null || state.brightness < block.state.brightness)) | ||
{ | ||
return true; | ||
} | ||
} | ||
if(block.operation == '=') | ||
{ | ||
if((state.value == null || block.state.value == null || state.value == block.state.value) | ||
&& (state.hue == null || block.state.hue == null || state.hue == block.state.hue) | ||
&& (state.saturation == null || block.state.saturation == null || state.saturation == block.state.saturation) | ||
&& (state.brightness == null || block.state.brightness == null || state.brightness == block.state.brightness)) | ||
{ | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
async _getState(automation, block) | ||
_getBlocks(id) | ||
{ | ||
var state = null; | ||
var blocks = []; | ||
if(this.manager.pluginName != block.plugin && block.plugin != null && this.manager.RouteManager.getPort(block.plugin) != null) | ||
for(const automation of this.automation) | ||
{ | ||
var theRequest = { | ||
url : 'http://' + (block.bridge || '127.0.0.1') + ':' + this.manager.RouteManager.getPort(block.plugin) + '/devices?id=' + block.id + '&type=' + this.TypeManager.letterToType(block.letters[0]) + '&counter=' + block.letters[1], | ||
timeout : 10000 | ||
}; | ||
if((id == null || automation.id == id) && automation.trigger != null && automation.trigger.groups != null) | ||
{ | ||
for(const i in automation.trigger.groups) | ||
{ | ||
if(automation.trigger.groups[i].blocks != null) | ||
{ | ||
for(const j in automation.trigger.groups[i].blocks) | ||
{ | ||
blocks.push({ blockID : i + '' + j, ...automation.trigger.groups[i].blocks[j] }); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
try | ||
return blocks; | ||
} | ||
_isLocked(automation) | ||
{ | ||
var blocks = this._getBlocks(automation.id); | ||
for(const block of blocks) | ||
{ | ||
if(block.options != null | ||
&& block.options.stateLock == true) | ||
{ | ||
state = await this.fetchRequest(theRequest, automation.name, block); | ||
if(this.stateLock[automation.id] != null | ||
&& this.stateLock[automation.id].trigger != null | ||
&& this.stateLock[automation.id].trigger[block.blockID] == true) | ||
{ | ||
return true; | ||
} | ||
} | ||
catch(e) | ||
} | ||
return false; | ||
} | ||
_updateSockets(lock, id, blockID) | ||
{ | ||
if(this.EventManager != null) | ||
{ | ||
var message = { id, lock }; | ||
if(blockID != null) | ||
{ | ||
this.logger.log('error', 'automation', 'Automation', 'Request %json_parse_error%!', e); | ||
message.blockID = blockID; | ||
} | ||
this.EventManager.setOutputStream('updateLock', { sender : this }, message); | ||
} | ||
else | ||
} | ||
_includesBlock(automation, id, letters) | ||
{ | ||
var blocks = this._getBlocks(automation.id); | ||
for(const block of blocks) | ||
{ | ||
state = this.platform.readAccessoryService(block.id, block.letters, true); | ||
if(block.id == id && block.letters == letters) | ||
{ | ||
return true; | ||
} | ||
} | ||
return state; | ||
return false; | ||
} | ||
_includesTime(automation) | ||
{ | ||
var blocks = this._getBlocks(automation.id); | ||
for(const block of blocks) | ||
{ | ||
if(block.days != null || block.time != null) | ||
{ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
_checkLock(automation, service = {}, state = {}) | ||
{ | ||
var blocks = this._getBlocks(automation.id), changed = false; | ||
for(const block of blocks) | ||
{ | ||
if(this.stateLock[automation.id] != null | ||
&& this.stateLock[automation.id].trigger != null | ||
&& this.stateLock[automation.id].trigger[block.blockID] == true) | ||
{ | ||
if((block.id == service.id && block.letters == service.letters) || block.days != null || block.time != null) | ||
{ | ||
if(!this._getOutput(block, state)) | ||
{ | ||
this.stateLock[automation.id].trigger[block.blockID] = false; | ||
this._updateSockets(false, automation.id, block.blockID); | ||
if(block.operation == '<') | ||
{ | ||
this.logger.debug('Automation [' + automation.name + '] %automation_greater% ' + automation.id + ' ' + block.blockID); | ||
} | ||
else if(block.operation == '>') | ||
{ | ||
this.logger.debug('Automation [' + automation.name + '] %automation_lower% ' + automation.id + ' ' + block.blockID); | ||
} | ||
else | ||
{ | ||
this.logger.debug('Automation [' + automation.name + '] %automation_different% ' + automation.id + ' ' + block.blockID); | ||
} | ||
changed = true; | ||
} | ||
} | ||
} | ||
} | ||
return changed; | ||
} | ||
} |
@@ -16,11 +16,12 @@ module.exports = class RouteManager | ||
{ | ||
for(const i in data.platforms) | ||
for(const platform of data.platforms) | ||
{ | ||
if(data.platforms[i].baseDirectory != null && (data.platforms[i].port != null || (data.platforms[i].options != null && data.platforms[i].options.port != null))) | ||
if(platform.platform != 'SynTex' && platform.baseDirectory != null && (platform.port != null || (platform.options != null && platform.options.port != null))) | ||
{ | ||
this.plugins.push({ name : data.platforms[i].platform, port : (data.platforms[i].port || data.platforms[i].options.port) }); | ||
this.plugins.push({ name : platform.platform, port : (platform.port || platform.options.port) }); | ||
} | ||
} | ||
} | ||
}); | ||
}).catch(() => {}); | ||
} | ||
@@ -33,7 +34,7 @@ } | ||
{ | ||
for(const i in this.plugins) | ||
for(const plugin of this.plugins) | ||
{ | ||
if(this.plugins[i].name == pluginName) | ||
if(plugin.name == pluginName) | ||
{ | ||
return this.plugins[i].port; | ||
return plugin.port; | ||
} | ||
@@ -40,0 +41,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
710
25440
+ Addedsyntex-filesystem@1.0.2-b15(transitive)
+ Addedsyntex-logger@1.0.8-b24(transitive)
- Removedsyntex-filesystem@1.0.2-b6(transitive)
- Removedsyntex-logger@1.0.8-b11(transitive)
Updatedsyntex-filesystem@1.0.2-b15
Updatedsyntex-logger@1.0.8-b24