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

3
dist/main.js vendored
View File

@ -271,6 +271,9 @@ allLabeledItems.push(relayBox);
// ---------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------------
let geoFences = new GeoFences_1.GeoFences(); let geoFences = new GeoFences_1.GeoFences();
geoFences.exec(); geoFences.exec();
geoFences.on('change', (attendanceSheet) => {
logger.info(`geoFences change event: ${JSON.stringify(attendanceSheet)}`);
});
// ---------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------------
let testFourButton = new HomematicFourButtonThing_1.HomematicFourButtonThing('Gnd', 'Hallway', 'TestButton', 9, [ let testFourButton = new HomematicFourButtonThing_1.HomematicFourButtonThing('Gnd', 'Hallway', 'TestButton', 9, [
new HomematicFourButtonThing_1.HomematicFourButtonSingleItem('dispatcher_ng/items/Gnd/Hallway/Testlight/dimmerIn'), new HomematicFourButtonThing_1.HomematicFourButtonSingleItem('dispatcher_ng/items/Gnd/Hallway/Testlight/dimmerIn'),

View File

@ -4,19 +4,27 @@ import * as logger from './log'
import * as bodyParser from 'body-parser' import * as bodyParser from 'body-parser'
import * as config from './config' import * as config from './config'
import { mqttHandler } from './MqttDispatcher' 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 app: express.Express
private server: http.Server private server: http.Server
public attendanceSheet : AttendanceSheetType
static geoFencesTopicPre : string = "dispatcher_ng/geofences" static geoFencesTopicPre : string = "dispatcher_ng/geofences"
constructor() { constructor() {
super()
this.app = express() this.app = express()
this.app.use(bodyParser.urlencoded({ extended: false })); this.app.use(bodyParser.urlencoded({ extended: false }));
this.app.use(bodyParser.json()); this.app.use(bodyParser.json());
this.attendanceSheet = {}
} }
exec() : void { exec() : void {
@ -28,13 +36,19 @@ export class GeoFences {
if (deviceId in config.dict.occupants) { if (deviceId in config.dict.occupants) {
occupantName = config.dict.occupants[deviceId] 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(`${deviceId} (${occupantName}) ${direction} ${location}`)
logger.info(JSON.stringify(reqData)) logger.info(JSON.stringify(reqData))
res.send('OK') res.send('OK')
const state : string = (reqData.entry == '1') ? 'present' : 'absent' const state : string = presence ? 'present' : 'absent'
mqttHandler.send(`${GeoFences.geoFencesTopicPre}/${occupantName}`, state) 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) let port = parseInt(config.dict.geofencesPort)

View File

@ -22,7 +22,7 @@ import { Cron } from './Cron'
import { HueColorBulbItem } from './HueColorBulbItem' import { HueColorBulbItem } from './HueColorBulbItem'
import { TouchSwitchMultiButtonThing, TouchSwitchButtonSingleItem } from './TouchSwitchMultiButtonThing' import { TouchSwitchMultiButtonThing, TouchSwitchButtonSingleItem } from './TouchSwitchMultiButtonThing'
import { RelayBoxThing } from './RelayBox' import { RelayBoxThing } from './RelayBox'
import { GeoFences } from './GeoFences' import { GeoFences, AttendanceSheetType } from './GeoFences'
logger.info("Dispatcher starting") logger.info("Dispatcher starting")
@ -349,6 +349,9 @@ allLabeledItems.push(relayBox)
let geoFences = new GeoFences() let geoFences = new GeoFences()
geoFences.exec() geoFences.exec()
geoFences.on('change', (attendanceSheet: AttendanceSheetType) => {
logger.info(`geoFences change event: ${JSON.stringify(attendanceSheet)}`)
})
// ---------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------------
let testFourButton = new HomematicFourButtonThing('Gnd', 'Hallway', 'TestButton', 9, [ let testFourButton = new HomematicFourButtonThing('Gnd', 'Hallway', 'TestButton', 9, [
new HomematicFourButtonSingleItem('dispatcher_ng/items/Gnd/Hallway/Testlight/dimmerIn'), new HomematicFourButtonSingleItem('dispatcher_ng/items/Gnd/Hallway/Testlight/dimmerIn'),