-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotion-blinds.coffee
133 lines (104 loc) · 4.27 KB
/
motion-blinds.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# #Motion blinds
{ MotionGateway } = require 'motionblinds'
# ##The plugin code
# Your plugin must export a single function, that takes one argument and returns a instance of
# your plugin class. The parameter is an envirement object containing all pimatic related functions
# and classes. See the [startup.coffee](http://sweetpi.de/pimatic/docs/startup.html) for details.
module.exports = (env) ->
# ###require modules included in pimatic
# To require modules that are included in pimatic use `env.require`. For available packages take
# a look at the dependencies section in pimatics package.json
# Require the bluebird promise library
Promise = env.require 'bluebird'
# Require the [cassert library](https://github.com/rhoot/cassert).
assert = env.require 'cassert'
# Include your own depencies with nodes global require function:
#
# someThing = require 'someThing'
#
# ###MyPlugin classMotionBlinds # Create a class that extends the Plugin class and implements the following functions:
class MotionBlindsPlugin extends env.plugins.Plugin
# ####init()
# The `init` function is called by the framework to ask your plugin to initialise.
#
# #####params:
# * `app` is the [express] instance the framework is using.
# * `framework` the framework itself
# * `config` the properties the user specified as config for your plugin in the `plugins`
# section of the config.json file
#
#
init: (app, @framework, @config) =>
env.logger.info("Hello World", @config.apiKey)
gw = new MotionGateway(@config.apiKey, 10)
gw.on 'error', (err) =>
env.logger.error(err)
if @config.debug
gw.on 'report', (report) =>
env.logger.debug(report)
gw.start()
gw.getDeviceList().then( (devices) =>
env.logger.info('devices', devices)
).catch( (err) -> env.logger.error err )
deviceConfigDef = require("./device-config-schema")
@framework.deviceManager.registerDeviceClass("MotionShutterController", {
configDef: deviceConfigDef.MotionShutterController,
createCallback: (config, lastState) =>
return new MotionShutterController(config, @framework, gw, lastState)
})
@framework.deviceManager.on('discover', (eventData) =>
gw.getDeviceList().then( (devices) =>
devices.data.forEach (d) =>
if d.deviceType == MotionGateway.Blind
displayName = "Motion Blind #{d.mac}"
config = {
class: 'MotionShutterController',
name: displayName,
mac: d.mac
}
@framework.deviceManager.discoveredDevice(
'pimatic-motion-blinds', displayName, config
)
).catch( (err) -> env.logger.error err )
)
@framework.on 'destroy', () => gw.stop()
class MotionShutterController extends env.devices.ShutterController
constructor: (@config, @framework, @gw, lastState) ->
@name = @config.name
@id = @config.id
super()
@_destroyed = false
@_position = lastState?.position?.value or null
@gw.on 'report', @_onReport
destroy: () ->
@_destroyed = true
@gw.removeListener 'report', @_onReport
super()
_onReport: (info) =>
env.logger.info 'report', info
if info.mac != @config.mac
return
switch info.data.currentPosition
when 0 then @_setPosition('up')
when 100 then @_setPosition('down')
else @_setPosition('stopped')
getPosition: () ->
return Promise.resolve @_position
moveToPosition: (position) ->
if @_destroyed
return Promise.resolve()
operation = (
switch position
when 'up' then MotionGateway.Operation.OpenUp
when 'down' then MotionGateway.Operation.CloseDown
when 'stopped' then MotionGateway.Operation.Stop
)
@gw.writeDevice(@config.mac, MotionGateway.Blind, {
operation: operation,
}).then () => @_setPosition position
stop: () -> @moveToPosition("stopped")
# ###Finally
# Create a instance of my plugin
motionBlindsPlugin = new MotionBlindsPlugin
# and return it to the framework.
return motionBlindsPlugin