Skip to content
This repository has been archived by the owner on Mar 3, 2022. It is now read-only.

Commit

Permalink
Feat: added function: triggerGetAllRunningOrders
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan Nyman authored and Johan Nyman committed May 8, 2018
1 parent eaddadc commit 6cc5f04
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
36 changes: 27 additions & 9 deletions src/coreHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,23 @@ export class CoreMosDeviceHandler {
.then(() => {
this._coreParentHandler.logger.info('Core: Setting up observers..')
let observer = this.core.observe('peripheralDeviceCommands')
let cmds = this.core.getCollection('peripheralDeviceCommands')
if (!cmds) throw Error('"peripheralDeviceCommands" collection not found!')
observer.added = (id) => {

let addedChanged = (type: string, id: string) => {
let cmds = this.core.getCollection('peripheralDeviceCommands')
if (!cmds) throw Error('"peripheralDeviceCommands" collection not found!')
let cmd = cmds.findOne(id) as PeripheralDeviceCommand
console.log('added', cmd)
if (!cmd) throw Error('PeripheralCommand "' + id + '" not found!')
console.log(type, id, cmd)
this.executeFunction(cmd)
}
observer.added = (id) => {
addedChanged('added', id)
}
observer.changed = (id) => {
let cmd = cmds.findOne(id) as PeripheralDeviceCommand
console.log('changed', cmd)
this.executeFunction(cmd)
addedChanged('changed', id)
}
let cmds = this.core.getCollection('peripheralDeviceCommands')
if (!cmds) throw Error('"peripheralDeviceCommands" collection not found!')
cmds.find({}).forEach((cmd: PeripheralDeviceCommand) => {
console.log('cmd', cmd)
this.executeFunction(cmd)
Expand Down Expand Up @@ -215,12 +220,13 @@ export class CoreMosDeviceHandler {
mosRoFullStory (story: IMOSROFullStory ): Promise<any> {
return this._coreMosManipulate(P.methods.mosRoFullStory, story)
}

executeFunction (cmd: PeripheralDeviceCommand) {
if (cmd) {
console.log('executeFunction', cmd)
this._executedFunctions[cmd._id] = true
let cb = (err: any, res?: any) => {
console.log('cb')
console.log('cb', err, res)
this.core.callMethod(P.methods.functionReply, [cmd._id, err, res])
.then(() => {
console.log('cb done')
Expand All @@ -234,7 +240,7 @@ export class CoreMosDeviceHandler {
try {
if (!fcn) throw Error('Function "' + cmd.functionName + '" not found!')

Promise.resolve(fcn.apply(null, cmd.args))
Promise.resolve(fcn.apply(this, cmd.args))
.then((result) => {
console.log('got result')
cb(null, result)
Expand All @@ -247,6 +253,18 @@ export class CoreMosDeviceHandler {
}
}
}
triggerGetAllRunningOrders (): Promise<any> {
console.log('triggerGetAllRunningOrders')
return this._mosDevice.getAllRunningOrders()
.then((results) => {
console.log('GOT REPLY', results)
return results
})
.catch((err) => {
console.log('GOT ERR', err)
throw err
})
}
test (a: string) {
return new Promise(resolve => {
setTimeout(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ logger.info('Starting MOS Gateway')
let c = new Connector(logger)

logger.info('Core: ' + config.core.host + ':' + config.core.port)
logger.info('Mos id: ' + config.mos.self.mosID)
logger.info('My Mos id: ' + config.mos.self.mosID)
config.mos.devices.forEach((device) => {
if (device.primary) logger.info('Mos Primary: ' + device.primary.host)
if (device.secondary) logger.info('Mos Secondary: ' + device.secondary.host)
Expand Down

0 comments on commit 6cc5f04

Please sign in to comment.