Skip to content

Instantly share code, notes, and snippets.

@mkremeyer

mkremeyer/kobold.coffee Secret

Created Jul 16, 2017
Embed
What would you like to do?
Promise not resolving
module.exports = (env) ->
_ = env.require 'lodash'
Promise = env.require 'bluebird'
libKobold = require 'node-kobold'
class KoboldPlugin extends env.plugins.Plugin
init: (app, @framework, @config) =>
deviceConfigDef = require("./device-config-schema")
@framework.deviceManager.registerDeviceClass("KoboldRobot", {
configDef: deviceConfigDef.KoboldRobot,
createCallback: (config) -> new KoboldRobot(config)
})
class KoboldRobot extends env.devices.Device
attributes:
battery:
description: "Battery in %"
type: "number"
unit: '°C'
getBattery: -> Promise.resolve(10)
actions:
startCleaning:
description: "Start cleaning the house"
params:
eco:
type: Boolean
#stopCleaning:
# description: "Stop cleaning & return to base"
#pauseCleaning:
# description: "Pause cleaning"
#resumeCleaning:
# description: "Resume cleaning"
startCleaning: (eco) ->
env.logger.info("start cleaning")
updateInfo: () ->
connect: () =>
return new Promise( (resolve, reject) =>
@client.authorize(@config.email, @config.password, false, (error) ->
if error
env.logger.warn error
reject
else
env.logger.info("successfully connected to robot")
resolve
)
)
getRob: () =>
env.logger.info("getrob 0")
return new Promise( (resolve, reject) =>
env.logger.info("getrob 1")
@client.getRobots( (error, robots) =>
env.logger.info("getrob 2")
if error
env.logger.warn error
reject
else
env.logger.info("getrob 3")
if robots.length
@robot = robots[@config.robotno]
env.logger.info("robot selected")
resolve
else
env.logger.warn "no robot found"
reject
)
)
constructor: (@config) ->
@name = @config.name
@id = @config.id
@client = new libKobold.Client()
@connect().then( () =>
env.logger.warn "connected"
)
#setInterval( ( => @updateInfo() ), @config.interval)
super()
destroy: () ->
super()
Kobold = new KoboldPlugin
return Kobold
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment