activate / deactivate

This commit is contained in:
Gerrit Linnemann 2017-05-15 19:19:28 +02:00
parent 143782ce4f
commit 6fa9f1c7bc

View File

@ -11,6 +11,7 @@ var lastTagID = '';
var timeOutForHoldingLastTagID = null;
const timeOutForHoldingLastTagIDInMillis = Conf.rfid.timeout;
var currentActionState = 0;
var deviceList = null;
Helper.each(Conf.items, function(item) {
@ -201,13 +202,11 @@ function doActionOnRFIDCallback(key) {
switch(currentActionState) {
case 0:
// deactivate alarm system
Log.log( 'deactivate alarm system' );
deactivateAlarmSystem();
deactivateAlarmSystem(key);
break;
case 1:
// activate alarm system
Log.log( 'activate alarm system' );
activateAlarmSystem();
activateAlarmSystem(key);
break;
default:
// nothing to do
@ -215,15 +214,42 @@ function doActionOnRFIDCallback(key) {
}
/* private */
function activateAlarmSystem() {
msg = '{type:"set_alarm_system_state", data:{state:"F111"}}';
send(msg);
function isKnownCard(uuid, callbackIfSo) {
if(Helper.isDefinedAndNotNull(deviceList)) {
var filterResultDevices = deviceList.filter(function(entry) { return entry.details.uuid === uuid; });
Log.inspect('check for ' + uuid + ' in ' + filterResultDevices.length + ' devices', filterResultDevices);
if(Helper.isDefinedAndNotNull(filterResultDevices) && filterResultDevices.length > 0) {
callbackIfSo();
} else {
Log.debug('No device for ' + uuid)
}
} else {
Log.debug('No devices');
}
}
/* private */
function deactivateAlarmSystem() {
function activateAlarmSystem(key) {
isKnownCard(key, function() {
Log.log( 'activate alarm system' );
msg = '{type:"set_alarm_system_state", data:{state:"F111"}}';
send(msg);
});
}
/* private */
function deactivateAlarmSystem(key) {
isKnownCard(key, function() {
Log.log( 'deactivate alarm system' );
msg = '{type:"set_alarm_system_state", data:{state:"F222"}}';
send(msg);
});
}
function fetchDeviceList() {
msg = '{type:"get_device_list"}';
send(msg);
}
/* private */
@ -240,7 +266,7 @@ var openWebSocket = function() {
ws.on('open', function() {
Log.log('ws open');
ws.send(Date.now().toString(), {mask: true});
fetchDeviceList();
});
ws.on('close', function() {
@ -248,10 +274,22 @@ var openWebSocket = function() {
openWebSocket();
});
ws.on('message', function incoming(data, flags) {
ws.on('message', function incoming(rawData, flags) {
// flags.binary will be set if a binary data is received.
// flags.masked will be set if the data was masked.
Log.inspect('data', data);
const obj = JSON.parse(rawData);
const type = obj.data.type;
const data = obj.data.data;
switch(type) {
case 'get_device_list':
const devices = data.result.filter(function(entry) { return entry.category.name === 'rfid_card' });
deviceList = devices;
Log.inspect('Devices', deviceList);
break;
default:
// nothing to do
}
});
}
openWebSocket();