var Tinkerforge = require('../Tinkerforge') , Conf = require('./config.json') , Helper = require('../Adawim/helper') , Log = require('../Adawim/logging') , http = require('http') , WebSocket = require('ws'); var ws = null; var lastTagID = ''; var timeOutForHoldingLastTagID = null; const timeOutForHoldingLastTagIDInMillis = Conf.rfid.timeout; var currentActionState = 0; Helper.each(Conf.items, function(item) { connect(item); }); /* private */ function reconnect(item) { Log.debug('Reconnect ' + item.host); setTimeout(function() { connect(item) }, 10000); } /* private */ function connect(item) { var HOST = item.host; var PORT = item.port; Log.log('Init @' + HOST); var ipcon = new Tinkerforge.IPConnection(); // Create IP connection var counter = 0; var readyForNextNotification = true; ipcon.connect(HOST, PORT, function (error) { /* IPConnection.ERROR_ALREADY_CONNECTED = 11 IPConnection.ERROR_NOT_CONNECTED = 12 IPConnection.ERROR_CONNECT_FAILED = 13 IPConnection.ERROR_INVALID_FUNCTION_ID = 21 IPConnection.ERROR_TIMEOUT = 31 IPConnection.ERROR_INVALID_PARAMETER = 41 IPConnection.ERROR_FUNCTION_NOT_SUPPORTED = 42 IPConnection.ERROR_UNKNOWN_ERROR = 43 */ Log.error('Error @'+HOST+': ' + error); if(error === 13) { reconnect(item); } } ); // Connect to brickd // Don't use device before ipcon is connected ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED, function (connectReason) { switch(connectReason) { case Tinkerforge.IPConnection.CONNECT_REASON_REQUEST: Log.log('Connect by request @'+HOST); break; case Tinkerforge.IPConnection.CONNECT_REASON_AUTO_RECONNECT: Log.log('Connect by auto-reconnect @'+HOST); break; } connectRFIDReader(item, ipcon); connectMultitouch(item, ipcon); } ); process.on( 'SIGINT', function() { Log.log( "Gracefully disconnect " + HOST ); ipcon.disconnect(); process.exit( ); }); } /* private */ function connectRFIDReader(item, ipcon) { const UID = item.rfid.uid; var nr = new Tinkerforge.BrickletNFCRFID(UID, ipcon); // Create device object var tagType = 0; // start scan loop nr.requestTagID(Tinkerforge.BrickletNFCRFID.TAG_TYPE_MIFARE_CLASSIC); // Register state changed callback nr.on(Tinkerforge.BrickletNFCRFID.CALLBACK_STATE_CHANGED, // Callback function for state changed callback /* STATE: BrickletNFCRFID.STATE_INITIALIZATION = 0 BrickletNFCRFID.STATE_IDLE = 128 BrickletNFCRFID.STATE_ERROR = 192 BrickletNFCRFID.STATE_REQUEST_TAG_ID = 2 BrickletNFCRFID.STATE_REQUEST_TAG_ID_READY = 130 BrickletNFCRFID.STATE_REQUEST_TAG_ID_ERROR = 194 BrickletNFCRFID.STATE_AUTHENTICATING_MIFARE_CLASSIC_PAGE = 3 BrickletNFCRFID.STATE_AUTHENTICATING_MIFARE_CLASSIC_PAGE_READY = 131 BrickletNFCRFID.STATE_AUTHENTICATING_MIFARE_CLASSIC_PAGE_ERROR = 195 BrickletNFCRFID.STATE_WRITE_PAGE = 4 BrickletNFCRFID.STATE_WRITE_PAGE_READY = 132 BrickletNFCRFID.STATE_WRITE_PAGE_ERROR = 196 BrickletNFCRFID.STATE_REQUEST_PAGE = 5 BrickletNFCRFID.STATE_REQUEST_PAGE_READY = 133 BrickletNFCRFID.STATE_REQUEST_PAGE_ERROR = 197 */ function (state, idle) { if(idle) { tagType = (tagType + 1) % 3; nr.requestTagID(tagType); } if(state == Tinkerforge.BrickletNFCRFID.STATE_REQUEST_TAG_ID_READY) { nr.getTagID( function (tagType, tidLength, tid) { var s = 'Found tag of type ' + tagType + ' with ID [' + tid[0].toString(16); var tagID = tid[0].toString(16); for(var i = 1; i < tidLength; i++) { s += ' ' + tid[i].toString(16); tagID += ' ' + tid[i].toString(16); } s += ']'; if(lastTagID !== tagID) { Log.log(s); lastTagID = tagID; clearTimeoutForHoldingLastTagID(); doActionOnRFIDCallback(tagID); } }, function (error) { Log.error('Error: ' + error); } ); } } ); } /* private */ function clearTimeoutForHoldingLastTagID() { clearTimeout(timeOutForHoldingLastTagID); timeOutForHoldingLastTagID = setTimeout(function() { lastTagID = ''; }, timeOutForHoldingLastTagIDInMillis); } /* private */ function connectMultitouch(item, ipcon) { const UID = item.mtouch.uid; var mt = new Tinkerforge.BrickletMultiTouch(UID, ipcon); // Create device object // Register touch state callback mt.on(Tinkerforge.BrickletMultiTouch.CALLBACK_TOUCH_STATE, // Callback function for touch state callback function (state) { var s = ''; if(state & (1 << 12)) { s += 'In proximity, '; } if((state & 0xfff) === 0) { s += 'No electrodes touched'; } else { s += 'Electrodes '; for(var i = 0; i < 12; ++i) { if(state & (1 << i)) { s += i + ' '; activate(i); } } s += 'touched'; } Log.log(s); } ); } /* private */ function activate(node) { lastTagID = ''; currentActionState = node; } /* private */ function doActionOnRFIDCallback(key) { switch(currentActionState) { case 0: // deactivate alarm system Log.log( 'deactivate alarm system' ); deactivateAlarmSystem(); break; case 1: // activate alarm system Log.log( 'activate alarm system' ); activateAlarmSystem(); break; default: // nothing to do } } /* private */ function activateAlarmSystem() { msg = '{type:"set_alarm_system_state", data:{state:"F111"}}'; send(msg); } /* private */ function deactivateAlarmSystem() { msg = '{type:"set_alarm_system_state", data:{state:"F222"}}'; send(msg); } /* private */ function send(msg) { if(Helper.isDefinedAndNotNull(ws) && Helper.isDefinedAndNotNull(msg)) { ws.send(msg); } else { Log.error('ws is null'); } } var openWebSocket = function() { ws = new WebSocket(Conf.hobu.url); ws.on('open', function() { Log.log('ws open'); ws.send(Date.now().toString(), {mask: true}); }); ws.on('close', function() { Log.log('ws disconnect'); openWebSocket(); }); ws.on('message', function incoming(data, 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); }); } openWebSocket();