/
CleanOMat980Platform.js
253 lines (210 loc) · 7.87 KB
/
CleanOMat980Platform.js
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
'use strict'
const path = require('path')
const fs = require('fs')
var appRoot = path.dirname(require.main.filename)
if (appRoot.endsWith('bin')) { appRoot = appRoot + '/../lib' }
if (appRoot.endsWith('node_modules/daemonize2/lib')) {
appRoot = path.join(appRoot,'..','..','..','lib')
if (!fs.existsSync(path.join(appRoot,'HomematicVirtualPlatform.js'))) {
appRoot = path.join(path.dirname(require.main.filename),'..','..','..','node_modules','homematic-virtual-interface','lib')
}
}
appRoot = path.normalize(appRoot);
var HomematicVirtualPlatform = require(appRoot + '/HomematicVirtualPlatform.js')
var util = require('util')
var HomematicDevice
var url = require('url')
const dorita980 = require('dorita980')
function CleanOMat980Platform (plugin, name, server, log, instance) {
CleanOMat980Platform.super_.apply(this, arguments)
HomematicDevice = server.homematicDevice
}
util.inherits(CleanOMat980Platform, HomematicVirtualPlatform)
CleanOMat980Platform.prototype.init = function () {
var that = this
this.configuration = this.server.configuration;
var devfile = path.join(__dirname,'HM-RC-Key4_Clean.json');
var buffer = fs.readFileSync(devfile);
var devdata = JSON.parse(buffer.toString());
this.server.transferHMDevice('HM-RC-Key4-2Clean',devdata);
var serial = 'Cleo980'
this.hmDevice = new HomematicDevice(this.getName())
var data = this.bridge.deviceDataWithSerial(serial)
if (data!=undefined) {
this.hmDevice.initWithStoredData(data)
}
if (this.hmDevice.initialized === false) {
this.hmDevice.initWithType('HM-RC-Key4-2Clean',serial)
this.hmDevice.serialNumber = serial
this.bridge.addDevice(this.hmDevice,true)
} else {
this.bridge.addDevice(this.hmDevice,false)
}
this.hmDevice.on('device_channel_value_change', function (parameter) {
var newValue = parameter.newValue
var channel = that.hmDevice.getChannel(parameter.channel)
if (parameter.name === 'PRESS_SHORT') {
let cred = that.credentials()
if (cred != undefined) {
switch (channel.index) {
case '1':
// Start
that.log.info('Sending Start')
that.command('start')
break;
case '2':
// Stop
that.log.info('Sending Stop')
that.command('stop')
break;
case '3':
// Dock
that.log.info('Sending Dock')
that.command('dock')
break;
}
} else {
that.log.error('there was no robot configurated')
}
}
})
// if you want to periodical check the state of your physical device you may use something like that
this.plugin.initialized = true
this.log.info('initialization completed %s', this.plugin.initialized)
this.command('update')
}
CleanOMat980Platform.prototype.command = function (command) {
// send command to the robot
var that = this
let cred = this.credentials()
if (cred != undefined) {
this.myRobotViaLocal = new dorita980.Local(cred.blid, cred.pwd, cred.ip); // robot IP address
this.myRobotViaLocal.on('connect', function () {
that.log.debug('connected to robot')
switch (command) {
case 'start':
setTimeout(function(){
that.log.debug('start command')
that.myRobotViaLocal.start().then(() => {
that.log.debug('end connection')
that.myRobotViaLocal.end();
}).catch((err) => {
that.log.error(err);
});
}, 500);
break;
case 'stop' :
setTimeout(function(){
that.log.debug('stop command')
that.myRobotViaLocal.stop().then(() => {
that.log.debug('end connection')
that.myRobotViaLocal.end();
}).catch((err) => {
that.log.error(err);
});
}, 500);
break;
case 'dock' :
setTimeout(function(){
that.log.debug('dock command')
that.myRobotViaLocal.dock().then(() => {
that.log.debug('end connection')
that.myRobotViaLocal.end();
}).catch((err) => {
that.log.error(err);
});
}, 500);
break;
case 'update' :
setTimeout(function(){
that.log.debug('update command')
that.myRobotViaLocal.getRobotState(['batPct', 'bin','cleanMissionStatus']).then((actualState) => {
that.log.debug('end connection')
that.myRobotViaLocal.end()
if ((actualState.batPct) && (actualState.bin) && (actualState.cleanMissionStatus)) {
var di_channel = that.hmDevice.getChannelWithTypeAndIndex('KEY','4')
di_channel.updateValue('CUR_STATE',actualState.cleanMissionStatus.phase,true,true);
di_channel.updateValue('BATTERIE',actualState.batPct,true,true);
di_channel.updateValue('BIN',actualState.bin.full,true,true);
}
clearTimeout(that.timer)
that.timer = setTimeout(function() {
that.command('update')
}, (that.refreshTime * 60000))
}).catch(function(_data){
that.log.error('getRobotState Rejection %s',_data)
});
}, 500);
break;
default:
that.myRobotViaLocal.end()
that.log.debug('end connection thru default')
}
});
this.myRobotViaLocal.on('offline', function () {
that.log.debug('Robot went offline next update in %s min',that.refreshTime)
clearTimeout(that.timer)
that.timer = setTimeout(function() {
that.command('update')
}, (that.refreshTime * 60000))
})
} else {
this.log.error('Please setup your robot credentials')
}
}
CleanOMat980Platform.prototype.credentials = function() {
let blid = this.configuration.getValueForPluginWithDefault(this.name,'blid',undefined);
let password = this.configuration.getValueForPluginWithDefault(this.name,'password',undefined);
let robotip = this.configuration.getValueForPluginWithDefault(this.name,'robotip',undefined);
this.refreshTime = this.configuration.getValueForPluginWithDefault(this.name,'refresh',2);
if ((blid != undefined) && (password != undefined) && (robotip != undefined)) {
return {'blid':blid,'pwd':password,'ip':robotip}
} else {
return undefined;
}
}
CleanOMat980Platform.prototype.showSettings = function(dispatched_request) {
let blid = this.configuration.getValueForPluginWithDefault(this.name,'blid','')
let password = this.configuration.getValueForPluginWithDefault(this.name,'password','')
let robotip = this.configuration.getValueForPluginWithDefault(this.name,'robotip','')
let refresh = this.configuration.getValueForPluginWithDefault(this.name,'refresh',2)
var desc = 'you may get the blib by executing the following tool:<br /> node ' + __dirname + '/node_modules/dorita980/bin/getpassword.js IPOfYourRobot';
var result = []
result.push({'control':'text','name':'blid','label':'Blid','value':blid, 'description': desc})
result.push({'control':'text','name':'password','label':'Password','value':password})
result.push({'control':'text','name':'robotip','label':'Robot IP','value':robotip})
result.push({'control':'text','name':'refresh','label':'Refresh','value':refresh})
return result;
}
CleanOMat980Platform.prototype.saveSettings = function(settings) {
let that = this
let blid = settings.blid;
let password = settings.password;
let robotip = settings.robotip;
let refresh = settings.refresh;
if ((password) && (password) && (robotip)) {
this.configuration.setValueForPlugin(this.name,'blid',blid)
this.configuration.setValueForPlugin(this.name,'password',password)
this.configuration.setValueForPlugin(this.name,'robotip',robotip)
if (refresh < 1) {refresh = 1}
this.configuration.setValueForPlugin(this.name,'refresh',1)
this.command('update')
}
}
CleanOMat980Platform.prototype.handleConfigurationRequest = function (dispatchedRequest) {
var template = 'index.html'
var requesturl = dispatchedRequest.request.url
var queryObject = url.parse(requesturl, true).query
var deviceList = ''
if (queryObject['do'] !== undefined) {
switch (queryObject['do']) {
case 'app.js':
{
template = 'app.js'
}
break
}
}
dispatchedRequest.dispatchFile(this.plugin.pluginPath, template, {'listDevices': deviceList})
}
module.exports = CleanOMat980Platform