event emitting on change in geofences

This commit is contained in:
2018-04-11 17:06:05 +02:00
parent fca448a1ea
commit e6fe4a5ee9
4 changed files with 34 additions and 7 deletions

13
dist/GeoFences.js vendored
View File

@ -5,11 +5,14 @@ const logger = require("./log");
const bodyParser = require("body-parser");
const config = require("./config");
const MqttDispatcher_1 = require("./MqttDispatcher");
class GeoFences {
const events_1 = require("events");
class GeoFences extends events_1.EventEmitter {
constructor() {
super();
this.app = express();
this.app.use(bodyParser.urlencoded({ extended: false }));
this.app.use(bodyParser.json());
this.attendanceSheet = {};
}
exec() {
this.app.post('/', (req, res) => {
@ -20,12 +23,16 @@ class GeoFences {
if (deviceId in config.dict.occupants) {
occupantName = config.dict.occupants[deviceId];
}
const direction = (reqData.entry == '1') ? 'arrives at' : 'leaves from';
const presence = reqData.entry == '1';
const direction = presence ? 'arrives at' : 'leaves from';
logger.info(`${deviceId} (${occupantName}) ${direction} ${location}`);
logger.info(JSON.stringify(reqData));
res.send('OK');
const state = (reqData.entry == '1') ? 'present' : 'absent';
const state = presence ? 'present' : 'absent';
MqttDispatcher_1.mqttHandler.send(`${GeoFences.geoFencesTopicPre}/${occupantName}`, state);
this.attendanceSheet[occupantName] = presence;
logger.info(`attendanceSheet is now ${JSON.stringify(this.attendanceSheet)}`);
this.emit('change', this.attendanceSheet);
});
let port = parseInt(config.dict.geofencesPort);
this.server = this.app.listen(port, '', () => {