-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjointspace.coffee
189 lines (154 loc) · 6.5 KB
/
jointspace.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# #Plugin template
# This is an plugin template and mini tutorial for creating pimatic plugins. It will explain the
# basics of how the plugin system works and what a plugin should look like.
# ##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'
request = require 'request'
# Include your own depencies with nodes global require function:
#
# someThing = require 'someThing'
#
# ###MyPlugin class
# Create a class that extends the Plugin class and implements the following functions:
class Jointspace 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 @config.tvip
@tvip = @config.tvip
deviceConfigDef = require("./device-config-schema")
@framework.deviceManager.registerDeviceClass("JointspaceDirectInput", {
configDef: deviceConfigDef.JointspaceDirectInput,
createCallback: (config) => new JointspaceDirectInput(config, @)
})
@framework.deviceManager.registerDeviceClass("JointspaceSourceSelection", {
configDef: deviceConfigDef.JointspaceSourceSelection,
createCallback: (config) => new JointspaceSourceSelection(config, @)
})
@framework.deviceManager.on 'discover', () =>
env.logger.debug("Starting discovery - debug")
env.logger.info("Starting discovery - info")
@framework.deviceManager.discoverMessage(
'pimatic-jointspace', "Searching for devices"
)
#als erstes dann ein JointspacePowerButton device anbieten??
devManager = @framework.deviceManager
# Sources durchegehen
request.get {uri:"http://#{@tvip}:1925/1/sources", json : true}, (error, response, body) ->
if error isnt null
env.logger.error("#{error.syscall} #{error.errno}")
else
deviceConfig =
class: "JointspaceSourceSelection"
name: "Jointspace Source Selection"
id: "jointspace-source-selection"
env.logger.debug(body)
buttonsArray = []
for key, value of body
env.logger.debug(value)
button_command = key
button_text = ""
for k, v of value
if k is "name"
button_text = v
button_id = key
button_config =
id: button_id
text: button_text
command: button_command
buttonsArray.push(button_config)
deviceConfig.buttons = buttonsArray
#notify about the discovered device
#@framework.deviceManager.discoveredDevice(
devManager.discoveredDevice(
'pimatic-jointspace', "#{deviceConfig.name}", deviceConfig
)
class JointspaceDirectInput extends env.devices.ButtonsDevice
constructor: (@config, @plugin) ->
@name = @config.name
@tvIP = @plugin.tvip
@buttons = @config.buttons
env.logger.info("Button Device init")
env.logger.info("IP of #{@name} is #{@tvIP}")
for b in @config.buttons
if b.text is ""
b.text = b.command
if b.id is ""
b.id = "jointspace_#{@tvIP.replace(/\./g, "-")}_#{b.command.toLowerCase()}"
super(@config)
destroy: () ->
@requestPromise.cancel() if @requestPromise?
super()
buttonPressed: (buttonId) ->
btn_id_found = false
for b in @config.buttons
if b.id is buttonId
btn_id_found = true
env.logger.info("pressing button #{b.command}")
json_params = {key: b.command}
request.post {uri:"http://#{@tvIP}:1925/1/input/key", json : json_params}, (error, response, body) ->
#env.logger.debug(error)
#env.logger.debug(response)
#env.logger.debug(response.statusCode)
if error isnt null
env.logger.error(error)
else if response.statusCode isnt 200
throw new Error("returned #{response.statusCode}")
else
env.logger.debug "emitting"
@emit 'button', b.id
env.logger.debug "returning"
return Promise.resolve()
if not btn_id_found
throw new Error("No button with the id #{buttonId} found")
class JointspaceSourceSelection extends env.devices.ButtonsDevice
constructor: (@config, @plugin) ->
@name = @config.name
@tvIP = @plugin.tvip
@buttons = @config.buttons
super(@config)
destroy: () ->
@requestPromise.cancel() if @requestPromise?
super()
buttonPressed: (buttonId) ->
btn_id_found = false
for b in @config.buttons
if b.id is buttonId
btn_id_found = true
env.logger.info("Selecting Input #{b.text}")
json_params = {id: b.command}
request.post {uri:"http://#{@tvIP}:1925/1/sources/current", json : json_params}, (error, response, body) ->
if error isnt null
env.logger.error(error)
else if response.statusCode isnt 200
throw new Error("returned #{response.statusCode}")
else
env.logger.debug "emitting"
@emit 'button', b.id
env.logger.debug "returning"
return Promise.resolve()
if not btn_id_found
throw new Error("No button with the id #{buttonId} found")
# ###Finally
# Create a instance of my plugin
myPlugin = new Jointspace
# and return it to the framework.
return myPlugin