homebridge-syntex-webhooks
Advanced tools
Comparing version 1.0.5-b3 to 1.0.5-b30
358
index.js
@@ -5,2 +5,4 @@ var request = require('request'); | ||
var store = require('json-fs-store'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var Service, Characteristic; | ||
@@ -20,2 +22,3 @@ | ||
var config; | ||
var storage; | ||
@@ -33,3 +36,3 @@ function SynTexWebHookPlatform(slog, sconfig, api) | ||
this.storage = store(this.cacheDirectory); | ||
storage = store(this.cacheDirectory); | ||
} | ||
@@ -45,3 +48,3 @@ | ||
{ | ||
var Sensor = new SynTexWebHookSensorAccessory(this.sensors[i], this.storage); | ||
var Sensor = new SynTexWebHookSensorAccessory(this.sensors[i]); | ||
accessories.push(Sensor); | ||
@@ -52,3 +55,3 @@ } | ||
{ | ||
var Switch = new SynTexWebHookSwitchAccessory(this.switches[i], this.storage); | ||
var Switch = new SynTexWebHookSwitchAccessory(this.switches[i]); | ||
accessories.push(Switch); | ||
@@ -88,79 +91,19 @@ } | ||
{ | ||
this.storage.load('storage', (err, obj) => { | ||
if(obj) | ||
{ | ||
log(obj.devices); | ||
var found = false; | ||
for(var i = 0; i < obj.devices.length; i++) | ||
{ | ||
if(obj.devices[i].mac === urlParams.mac) | ||
{ | ||
if(urlParams.type && obj.devices[i].type) | ||
{ | ||
if(urlParams.type == obj.devices[i].type) | ||
{ | ||
obj.devices[i].value = urlParams.value; | ||
found = true; | ||
} | ||
} | ||
else | ||
{ | ||
obj.devices[i].value = urlParams.value; | ||
found = true; | ||
} | ||
} | ||
} | ||
if(found == false) | ||
{ | ||
if(urlParams.type) | ||
{ | ||
obj.devices[obj.devices.length] = {mac: urlParams.mac, value: urlParams.value, type: urlParams.type}; | ||
} | ||
else | ||
{ | ||
obj.devices[obj.devices.length] = {mac: urlParams.mac, value: urlParams.value}; | ||
} | ||
} | ||
this.storage.add(obj, (err) => { | ||
if(err) | ||
{ | ||
log('\x1b[31m%s\x1b[0m', "[ERROR]", "Storage.json konnte nicht aktualisiert werden!"); | ||
} | ||
}); | ||
} | ||
else if(!err) | ||
{ | ||
log('\x1b[33m%s\x1b[0m', "[INFO]", "Storage.json wurde ohne Inhalt geladen!"); | ||
if(urlParams.type) | ||
{ | ||
var device = { | ||
id: "storage", | ||
devices: [{mac: urlParams.mac, value: urlParams.value, type: urlParams.type}] | ||
}; | ||
} | ||
else | ||
{ | ||
var device = { | ||
id: "storage", | ||
devices: [{mac: urlParams.mac, value: urlParams.value}] | ||
}; | ||
} | ||
this.storage.add(device, (err) => { | ||
if(err) | ||
{ | ||
log('\x1b[31m%s\x1b[0m', "[ERROR]", "Storage.json konnte nicht aktualisiert werden!"); | ||
} | ||
}); | ||
} | ||
if(urlParams.type) | ||
{ | ||
var device = { | ||
mac: urlParams.mac, | ||
value: urlParams.value, | ||
type: urlParams.type | ||
}; | ||
} | ||
else | ||
{ | ||
var device = { | ||
mac: urlParams.mac, | ||
value: urlParams.value | ||
}; | ||
} | ||
updateDevice(device).then(function(res) { | ||
@@ -200,7 +143,2 @@ for(var i = 0; i < accessories.length; i++) | ||
} | ||
if(err) | ||
{ | ||
log('\x1b[31m%s\x1b[0m', "[ERROR 2]", "Storage.json konnte nicht geladen werden!"); | ||
} | ||
}); | ||
@@ -213,44 +151,25 @@ | ||
{ | ||
this.storage.load('storage', (err, obj) => { | ||
if(obj) | ||
{ | ||
var found = false; | ||
for(var i = 0; i < obj.devices.length; i++) | ||
{ | ||
if(obj.devices[i].mac === urlParams.mac) | ||
{ | ||
if(urlParams.type && obj.devices[i].type) | ||
{ | ||
if(urlParams.type == obj.devices[i].type) | ||
{ | ||
response.write(obj.devices[i].value.toString()); | ||
response.end(); | ||
found = true; | ||
} | ||
} | ||
else | ||
{ | ||
response.write(obj.devices[i].value.toString()); | ||
response.end(); | ||
found = true; | ||
} | ||
} | ||
} | ||
if(!found) | ||
{ | ||
response.write("Es wurde kein passendes Gerät gefunden!"); | ||
response.end(); | ||
log('\x1b[31m%s\x1b[0m', "[ERROR]", "Es wurde kein passendes Gerät gefunden! (" + urlParams.mac + ")"); | ||
} | ||
var id = urlParams.mac; | ||
if(urlParams.type) | ||
{ | ||
id += '-' + urlParams.type[0]; | ||
} | ||
storage.load(id, (err, obj) => { | ||
if(obj && !err) | ||
{ | ||
response.write(obj.value); | ||
response.end(); | ||
log('\x1b[36m%s\x1b[0m', "[READ]", "HomeKit Status für '" + urlParams.mac + "' ist '" + obj.value + "'"); | ||
} | ||
if(err || !obj) | ||
{ | ||
log('\x1b[31m%s\x1b[0m', "[ERROR 3]", "Storage.json konnte nicht geladen werden!"); | ||
response.write("Es wurde kein passendes Gerät gefunden!"); | ||
response.end(); | ||
log('\x1b[31m%s\x1b[0m', "[ERROR]", "Es wurde kein passendes Gerät gefunden! (" + urlParams.mac + ")"); | ||
} | ||
@@ -276,3 +195,3 @@ }); | ||
function SynTexWebHookSensorAccessory(sensorConfig, storage) | ||
function SynTexWebHookSensorAccessory(sensorConfig) | ||
{ | ||
@@ -283,3 +202,2 @@ this.mac = sensorConfig["mac"]; | ||
this.type = sensorConfig["type"]; | ||
this.storage = storage; | ||
@@ -359,3 +277,2 @@ if(this.type === "contact") | ||
} | ||
/* | ||
@@ -365,11 +282,8 @@ else if(this.type === "occupancy") | ||
this.service = new Service.OccupancySensor(this.name); | ||
this.changeHandler = (function(newState) { | ||
this.changeHandler = (function(newState) | ||
{ | ||
this.service.getCharacteristic(Characteristic.OccupancyDetected).updateValue(newState ? Characteristic.OccupancyDetected.OCCUPANCY_DETECTED : Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED, undefined, CONTEXT_FROM_WEBHOOK); | ||
if (this.autoRelease) { | ||
setTimeout(function() { | ||
this.storage.setItemSync("http-webhook-" + this.id, false); | ||
this.service.getCharacteristic(Characteristic.OccupancyDetected).updateValue(Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED, undefined, CONTEXT_FROM_TIMEOUTCALL); | ||
}.bind(this), this.autoReleaseTime); | ||
} | ||
}).bind(this); | ||
this.service.getCharacteristic(Characteristic.OccupancyDetected).on('get', this.getState.bind(this)); | ||
@@ -380,6 +294,9 @@ } | ||
this.service = new Service.SmokeSensor(this.name); | ||
this.changeHandler = (function(newState) { | ||
this.changeHandler = (function(newState) | ||
{ | ||
log("Change HomeKit state for smoke sensor to '%s'.", newState); | ||
this.service.getCharacteristic(Characteristic.SmokeDetected).updateValue(newState ? Characteristic.SmokeDetected.SMOKE_DETECTED : Characteristic.SmokeDetected.SMOKE_NOT_DETECTED, undefined, CONTEXT_FROM_WEBHOOK); | ||
}).bind(this); | ||
this.service.getCharacteristic(Characteristic.SmokeDetected).on('get', this.getState.bind(this)); | ||
@@ -390,6 +307,9 @@ } | ||
this.service = new Service.AirQualitySensor(this.name); | ||
this.changeHandler = (function(newState) { | ||
this.changeHandler = (function(newState) | ||
{ | ||
log("Change HomeKit value for air quality sensor to '%s'.", newState); | ||
this.service.getCharacteristic(Characteristic.AirQuality).updateValue(newState, undefined, CONTEXT_FROM_WEBHOOK); | ||
}).bind(this); | ||
this.service.getCharacteristic(Characteristic.AirQuality).on('get', this.getState.bind(this)); | ||
@@ -404,33 +324,16 @@ } | ||
this.storage.load('storage', (err, obj) => { | ||
var id = this.mac; | ||
if(this.type == 'rain' || this.type == 'light' || this.type == 'temperature' || this.type == 'humidity') | ||
{ | ||
id += '-' + this.type[0]; | ||
} | ||
storage.load(id, (err, obj) => { | ||
if(obj) | ||
if(obj && !err) | ||
{ | ||
for(var i = 0; i < obj.devices.length; i++) | ||
{ | ||
if(obj.devices[i].mac === this.mac) | ||
{ | ||
if(obj.devices[i].type) | ||
{ | ||
if(obj.devices[i].type == this.type) | ||
{ | ||
state = obj.devices[i].value; | ||
if(state == 'true' || state == 'false') | ||
{ | ||
state = (state === 'true'); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
state = obj.devices[i].value; | ||
if(state == 'true' || state == 'false') | ||
{ | ||
state = (state === 'true'); | ||
} | ||
} | ||
} | ||
} | ||
state = obj.value; | ||
log('\x1b[36m%s\x1b[0m', "[READ]", "HomeKit Status für '" + this.name + "' ist '" + state + "' ( " + this.mac + " )"); | ||
} | ||
@@ -440,3 +343,3 @@ | ||
{ | ||
log('\x1b[31m%s\x1b[0m', "[ERROR 4]", "Storage.json konnte nicht geladen werden!"); | ||
log('\x1b[31m%s\x1b[0m', "[ERROR]", this.mac + ".json konnte nicht geladen werden!"); | ||
} | ||
@@ -457,2 +360,12 @@ | ||
} | ||
/* | ||
else if(this.type === "smoke") | ||
{ | ||
callback(null, state ? Characteristic.SmokeDetected.SMOKE_DETECTED : Characteristic.SmokeDetected.SMOKE_NOT_DETECTED); | ||
} | ||
else if(this.type === "occupancy") | ||
{ | ||
callback(null, state ? Characteristic.OccupancyDetected.OCCUPANCY_DETECTED : Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED); | ||
} | ||
*/ | ||
else if(this.type === "light") | ||
@@ -465,17 +378,4 @@ { | ||
callback(null, state); | ||
} | ||
log('\x1b[36m%s\x1b[0m', "[READ]", "HomeKit Status für '" + this.name + "' ist '" + state + "' ( " + this.mac + " )"); | ||
} | ||
}); | ||
/* | ||
else if(this.type === "smoke") | ||
{ | ||
callback(null, state ? Characteristic.SmokeDetected.SMOKE_DETECTED : Characteristic.SmokeDetected.SMOKE_NOT_DETECTED); | ||
} | ||
else if(this.type === "occupancy") | ||
{ | ||
callback(null, state ? Characteristic.OccupancyDetected.OCCUPANCY_DETECTED : Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED); | ||
} | ||
*/ | ||
}; | ||
@@ -489,3 +389,3 @@ | ||
function SynTexWebHookSwitchAccessory(switchConfig, storage) | ||
function SynTexWebHookSwitchAccessory(switchConfig) | ||
{ | ||
@@ -506,3 +406,2 @@ this.mac = switchConfig["mac"]; | ||
this.offHeaders = switchConfig["off_headers"] || "{}"; | ||
this.storage = storage; | ||
@@ -524,7 +423,6 @@ this.service = new Service.Switch(this.name); | ||
this.storage.load('storage', (err, obj) => { | ||
storage.load('storage', (err, obj) => { | ||
if(obj) | ||
if(obj && !err) | ||
{ | ||
for(var i = 0; i < obj.devices.length; i++) | ||
@@ -546,3 +444,3 @@ { | ||
{ | ||
log('\x1b[31m%s\x1b[0m', "[ERROR 5]", "Storage.json konnte nicht geladen werden!"); | ||
log('\x1b[31m%s\x1b[0m', "[ERROR]", "Storage.json konnte nicht geladen werden!"); | ||
} | ||
@@ -569,5 +467,14 @@ | ||
this.storage.load('storage', (err, obj) => { | ||
if(!powerOn) | ||
{ | ||
urlToCall = this.offURL; | ||
urlMethod = this.offMethod; | ||
urlBody = this.offBody; | ||
urlForm = this.offForm; | ||
urlHeaders = this.offHeaders; | ||
} | ||
storage.load('storage', (err, obj) => { | ||
if(obj) | ||
if(obj && !err) | ||
{ | ||
@@ -591,3 +498,3 @@ var found = false; | ||
this.storage.add(obj, (err) => { | ||
storage.add(obj, (err) => { | ||
@@ -607,3 +514,3 @@ if(err) | ||
this.storage.add(device, (err) => { | ||
storage.add(device, (err) => { | ||
@@ -618,11 +525,2 @@ if(err) | ||
if(!powerOn) | ||
{ | ||
urlToCall = this.offURL; | ||
urlMethod = this.offMethod; | ||
urlBody = this.offBody; | ||
urlForm = this.offForm; | ||
urlHeaders = this.offHeaders; | ||
} | ||
if(urlToCall != "") | ||
@@ -675,2 +573,62 @@ { | ||
return [this.service]; | ||
}; | ||
}; | ||
async function updateDevice(obj) | ||
{ | ||
return new Promise(resolve => { | ||
if(obj.type) | ||
{ | ||
var device = { | ||
id: obj.mac + '-' + obj.type[0], | ||
value: obj.value, | ||
type: obj.type | ||
}; | ||
} | ||
else | ||
{ | ||
var device = { | ||
id: obj.mac, | ||
value: obj.value | ||
}; | ||
} | ||
storage.add(device, (err) => { | ||
if(err) | ||
{ | ||
log('\x1b[31m%s\x1b[0m', "[ERROR]", obj.mac + ".json konnte nicht aktualisiert werden!"); | ||
resolve(false); | ||
} | ||
else | ||
{ | ||
resolve(true); | ||
} | ||
}); | ||
}); | ||
} | ||
async function readDevice(obj) | ||
{ | ||
return new Promise(resolve => { | ||
storage.load(obj.mac, (err, obj) => { | ||
if(obj && !err) | ||
{ | ||
response.write(obj.value); | ||
response.end(); | ||
log('\x1b[36m%s\x1b[0m', "[READ]", "HomeKit Status für '" + this.name + "' ist '" + state + "' ( " + this.mac + " )"); | ||
} | ||
if(err || !obj) | ||
{ | ||
response.write("Es wurde kein passendes Gerät gefunden!"); | ||
response.end(); | ||
log('\x1b[31m%s\x1b[0m', "[ERROR]", "Es wurde kein passendes Gerät gefunden! (" + urlParams.mac + ")"); | ||
} | ||
}); | ||
}); | ||
} |
{ | ||
"name": "homebridge-syntex-webhooks", | ||
"version": "1.0.5-b3", | ||
"version": "1.0.5-b30", | ||
"description": "A webhook plugin for HTTP devices", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
58334
4
1146
2
3