From e6fe4a5ee9405ee936a576ffccddc8ee9d3d8c0a Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Wed, 11 Apr 2018 17:06:05 +0200 Subject: [PATCH] event emitting on change in geofences --- dist/GeoFences.js | 13 ++++++++++--- dist/main.js | 3 +++ src/GeoFences.ts | 20 +++++++++++++++++--- src/main.ts | 5 ++++- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/dist/GeoFences.js b/dist/GeoFences.js index 6c5cc7b..8c074e1 100644 --- a/dist/GeoFences.js +++ b/dist/GeoFences.js @@ -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, '', () => { diff --git a/dist/main.js b/dist/main.js index 9cf6b99..e73d50d 100644 --- a/dist/main.js +++ b/dist/main.js @@ -271,6 +271,9 @@ allLabeledItems.push(relayBox); // ---------------------------------------------------------------------------------------------------------- let geoFences = new GeoFences_1.GeoFences(); geoFences.exec(); +geoFences.on('change', (attendanceSheet) => { + logger.info(`geoFences change event: ${JSON.stringify(attendanceSheet)}`); +}); // ---------------------------------------------------------------------------------------------------------- let testFourButton = new HomematicFourButtonThing_1.HomematicFourButtonThing('Gnd', 'Hallway', 'TestButton', 9, [ new HomematicFourButtonThing_1.HomematicFourButtonSingleItem('dispatcher_ng/items/Gnd/Hallway/Testlight/dimmerIn'), diff --git a/src/GeoFences.ts b/src/GeoFences.ts index a529b7d..4e2f20d 100644 --- a/src/GeoFences.ts +++ b/src/GeoFences.ts @@ -4,19 +4,27 @@ import * as logger from './log' import * as bodyParser from 'body-parser' import * as config from './config' import { mqttHandler } from './MqttDispatcher' +import { EventEmitter } from 'events'; -export class GeoFences { + + +export type AttendanceSheetType = { [index: string]: boolean } + +export class GeoFences extends EventEmitter { private app: express.Express private server: http.Server + public attendanceSheet : AttendanceSheetType static geoFencesTopicPre : string = "dispatcher_ng/geofences" constructor() { + super() this.app = express() this.app.use(bodyParser.urlencoded({ extended: false })); this.app.use(bodyParser.json()); + this.attendanceSheet = {} } exec() : void { @@ -28,13 +36,19 @@ export class GeoFences { if (deviceId in config.dict.occupants) { occupantName = config.dict.occupants[deviceId] } - const direction : string = (reqData.entry == '1') ? 'arrives at' : 'leaves from' + const presence : boolean = reqData.entry == '1' + const direction : string = presence ? 'arrives at' : 'leaves from' logger.info(`${deviceId} (${occupantName}) ${direction} ${location}`) logger.info(JSON.stringify(reqData)) res.send('OK') - const state : string = (reqData.entry == '1') ? 'present' : 'absent' + const state : string = presence ? 'present' : 'absent' 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) diff --git a/src/main.ts b/src/main.ts index 50fe17c..5f060ed 100644 --- a/src/main.ts +++ b/src/main.ts @@ -22,7 +22,7 @@ import { Cron } from './Cron' import { HueColorBulbItem } from './HueColorBulbItem' import { TouchSwitchMultiButtonThing, TouchSwitchButtonSingleItem } from './TouchSwitchMultiButtonThing' import { RelayBoxThing } from './RelayBox' -import { GeoFences } from './GeoFences' +import { GeoFences, AttendanceSheetType } from './GeoFences' logger.info("Dispatcher starting") @@ -349,6 +349,9 @@ allLabeledItems.push(relayBox) let geoFences = new GeoFences() geoFences.exec() +geoFences.on('change', (attendanceSheet: AttendanceSheetType) => { + logger.info(`geoFences change event: ${JSON.stringify(attendanceSheet)}`) +}) // ---------------------------------------------------------------------------------------------------------- let testFourButton = new HomematicFourButtonThing('Gnd', 'Hallway', 'TestButton', 9, [ new HomematicFourButtonSingleItem('dispatcher_ng/items/Gnd/Hallway/Testlight/dimmerIn'),