geofences started

This commit is contained in:
2018-04-11 16:30:34 +02:00
parent 82cb8d56cb
commit fca448a1ea
9 changed files with 528 additions and 2 deletions

50
src/GeoFences.ts Normal file
View File

@ -0,0 +1,50 @@
import * as http from 'http'
import * as express from 'express'
import * as logger from './log'
import * as bodyParser from 'body-parser'
import * as config from './config'
import { mqttHandler } from './MqttDispatcher'
export class GeoFences {
private app: express.Express
private server: http.Server
static geoFencesTopicPre : string = "dispatcher_ng/geofences"
constructor() {
this.app = express()
this.app.use(bodyParser.urlencoded({ extended: false }));
this.app.use(bodyParser.json());
}
exec() : void {
this.app.post('/', (req: express.Request , res: express.Response) => {
const reqData = req.body
const deviceId = reqData.device
let occupantName : string = 'unknown'
const location = reqData.name
if (deviceId in config.dict.occupants) {
occupantName = config.dict.occupants[deviceId]
}
const direction : string = (reqData.entry == '1') ? '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'
mqttHandler.send(`${GeoFences.geoFencesTopicPre}/${occupantName}`, state)
})
let port = parseInt(config.dict.geofencesPort)
this.server = this.app.listen(port, '', () => {
logger.info(`geofences server listening on ${port}`)
})
}
}

View File

@ -22,6 +22,7 @@ import { Cron } from './Cron'
import { HueColorBulbItem } from './HueColorBulbItem'
import { TouchSwitchMultiButtonThing, TouchSwitchButtonSingleItem } from './TouchSwitchMultiButtonThing'
import { RelayBoxThing } from './RelayBox'
import { GeoFences } from './GeoFences'
logger.info("Dispatcher starting")
@ -344,7 +345,9 @@ let relayBox = new RelayBoxThing('base', 'labor', 'relaybox', 'IoT/Command/Relay
relayBox.start()
allLabeledItems.push(relayBox)
// ----------------------------------------------------------------------------------------------------------
let geoFences = new GeoFences()
geoFences.exec()
// ----------------------------------------------------------------------------------------------------------
let testFourButton = new HomematicFourButtonThing('Gnd', 'Hallway', 'TestButton', 9, [