Skip to content

Commit 2c0adb5

Browse files
Merge pull request #11 from MK-2001/master
Added send Option to FS20
2 parents c72f3b5 + 9a51d92 commit 2c0adb5

File tree

6 files changed

+103
-43
lines changed

6 files changed

+103
-43
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
2-
node_modules
2+
node_modules
3+
.DS_Store

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ ioBroker adapter to control FS20, Max!, HMS and other devices via [CUL](http://b
1818
- *MORITZ* - MAX!
1919
- *WS* - KS300TH, S300TH, WS2000/WS7000
2020

21+
## HowTo
22+
23+
### Send a command to a FS20 Device in e.g. JavaScript
24+
```sendTo("cul.0", "send", {"protocol":"FS20", "housecode":"A1B2", "address":"01", "command":"00"});```
25+
26+
This command uses the CUL Library of this adapter to send the command to a FS20 Device.
2127

2228
## Changelog
2329
### 1.1.0 (2020-01-04)

admin/index.html

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
"de": "CUL Adapter Einstellungen",
2222
"ru": "Настройки CUL Драйвера"
2323
},
24-
"Serial port:": {"en": "Serial port:", "de": "Serialport:", "ru": "Порт:"},
25-
"Baud rate": {"en": "Baud rate:", "de": "Baud rate:", "ru": "Скорость:"},
26-
"Mode": {"en": "Mode", "de": "Modus:", "ru": "Режим:"},
27-
"Type:": {"en": "Type:", "de": "Typ:", "ru": "Тип:"},
28-
"Select port": {"en": "Select port", "de": "Port auswählen", "ru": "Select port"},
29-
"IP/Hostname:": {"en": "IP/Hostname:", "de": "IP/Hostname:", "ru": "IP/Hostname:"},
30-
"Port": {"en": "Port:", "de": "Port:", "ru": "Port:"}
24+
"Serial port:": {"en": "Serial port:", "de": "Serialport:", "ru": "Порт:"},
25+
"Baud rate": {"en": "Baud rate:", "de": "Baud rate:", "ru": "Скорость:"},
26+
"Mode": {"en": "Mode", "de": "Modus:", "ru": "Режим:"},
27+
"Type:": {"en": "Type:", "de": "Typ:", "ru": "Тип:"},
28+
"Select port": {"en": "Select port", "de": "Port auswählen", "ru": "Select port"},
29+
"IP/Hostname:": {"en": "IP/Hostname:", "de": "IP/Hostname:", "ru": "IP/Hostname:"},
30+
"Port": {"en": "Port:", "de": "Port:", "ru": "Port:"},
31+
"Experimental:": {"en": "Experimental:", "de": "Experimentiermodus:","ru": "экспериментальный:"},
3132
};
3233

3334
var timeout;
@@ -179,13 +180,16 @@
179180

180181
<tr>
181182
<td><label for="ip" class="translate">IP/Hostname:</label></td>
182-
<td><input id="ip" class="value" value="127.0.0.1"></input></td>
183+
<td><input id="ip" class="value" value="127.0.0.1"></input></td>
183184
</tr>
184-
<tr>
185+
<tr>
185186
<td><label for="port" class="translate">Port:</label></td>
186-
<td><input id="port" class="value" value="23"></input></td>
187+
<td><input id="port" class="value" value="23"></input></td>
188+
</tr>
189+
<tr>
190+
<td><label for="experimental" class="translate">Experimental:</label></td>
191+
<td><input id="experimental" class="value" type="checkbox" value="true" /></td>
187192
</tr>
188-
189193
</table>
190194

191195
</div>

cul.js

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
/* jshint -W097 */// jshint strict:false
2-
/*jslint node: true */
1+
/* jshint -W097 */
2+
/* jshint strict: false */
3+
/* jslint node: true */
34

45
'use strict';
5-
var Cul = process.env.DEBUG ? require(__dirname + '/lib/debugCul.js') : require('cul');
6+
var Cul = process.env.DEBUG ? require('./lib/debugCul.js') : require('cul');
67

78
// you have to require the utils module and call adapter function
89
var utils = require('@iobroker/adapter-core'); // Get common adapter utils
@@ -27,7 +28,27 @@ try {
2728
}
2829

2930
adapter.on('stateChange', function (id, state) {
30-
//if (cul) cul.cmd();
31+
if (!state.ack) {
32+
adapter.log.debug('State Change ' + JSON.stringify(id) + ', State: ' + JSON.stringify(state));
33+
// State Change "cul.0.FS20.123401.cmd" State: {"val":2,"ack":false,"ts":1581365531968,"q":0,"from":"system.adapter.admin.0","user":"system.user.admin","lc":1581365531968}
34+
var oAddr = id.split('.');
35+
// 0: cul; 1:0; 2:FS20; 3:123401; 4:cmd;
36+
var sHousecode = oAddr[3].substring(0, 4);
37+
var sAddress = oAddr[3].substring(4, 6);
38+
if (oAddr[2] === 'FS20' || adapter.config.experimental === true || adapter.config.experimental === 'true') {
39+
switch (oAddr[4]) {
40+
case 'cmdRaw':
41+
sendCommand({protocol: oAddr[2], housecode: sHousecode, address: sAddress, command: state.val});
42+
break;
43+
44+
deafult:
45+
adapter.log.error('Write of State ' + oAddr[4] + ' currently not implemented');
46+
break;
47+
}
48+
} else {
49+
dapter.log.error('Only FS20 Devices are tested. Please contribute here: https://github.com/ioBroker/ioBroker.cul');
50+
}
51+
}
3152
});
3253

3354
adapter.on('unload', function (callback) {
@@ -42,7 +63,8 @@ adapter.on('unload', function (callback) {
4263
});
4364

4465
adapter.on('ready', function () {
45-
adapter.setState('info.connection', false, false);
66+
adapter.setState('info.connection', false, true);
67+
4668
checkPort(function (err) {
4769
if (!err || process.env.DEBUG) {
4870
main();
@@ -68,25 +90,44 @@ adapter.on('message', function (obj) {
6890
adapter.sendTo(obj.from, obj.command, [{comName: 'Not available'}], obj.callback);
6991
}
7092
}
71-
93+
break;
94+
95+
case 'send':
96+
sendCommand({protocol: obj.message.protocol, housecode: obj.message.housecode, address: obj.message.address, command: obj.message.command});
97+
break;
98+
99+
default:
100+
adapter.log.error('No such command: ' + obj.command);
72101
break;
73102
}
74103
}
75104
});
76105

106+
/***
107+
* Send a command to the cul module
108+
* @param {obj.message.protocol, obj.message.housecode, obj.message.address, obj.message.command}
109+
*/
110+
function sendCommand(o) {
111+
adapter.log.info('Send command received. Housecode: ' + o.housecode + '; address: ' + o.address + '; command: ' + o.command);
112+
cul.cmd(o.protocol, o.housecode, o.address, o.command);
113+
}
114+
77115
function checkConnection(host, port, timeout, callback) {
78116
timeout = timeout || 10000; //default 10 seconds
117+
79118
var timer = setTimeout(function() {
80119
socket.end();
81-
callback("Timeout");
120+
callback('Timeout');
82121
callback = null;
83122
}, timeout);
123+
84124
var socket = Net.createConnection(port, host, function() {
85125
clearTimeout(timer);
86126
socket.end();
87127
callback(null);
88128
callback = null;
89129
});
130+
90131
socket.on('error', function(err) {
91132
clearTimeout(timer);
92133
socket.end();
@@ -98,12 +139,12 @@ function checkConnection(host, port, timeout, callback) {
98139
function checkPort(callback) {
99140
if (adapter.config.type === 'cuno') {
100141
checkConnection(adapter.config.ip, adapter.config.port, 10000, function(err) {
101-
if (callback) callback(err);
142+
callback && callback(err);
102143
callback = null;
103144
});
104145
} else {
105146
if (!adapter.config.serialport) {
106-
if (callback) callback('Port is not selected');
147+
callback && callback('Port is not selected');
107148
return;
108149
}
109150
var sPort;
@@ -114,24 +155,23 @@ function checkPort(callback) {
114155
});
115156
sPort.on('error', function (err) {
116157
if (sPort.isOpen) sPort.close();
117-
if (callback) callback(err);
158+
callback && callback(err);
118159
callback = null;
119160
});
120161

121162
sPort.open(function (err) {
122-
if (sPort.isOpen) sPort.close();
123-
124-
if (callback) callback(err);
163+
sPort.isOpen && sPort.close();
164+
callback && callback(err);
125165
callback = null;
126166
});
127167
} catch (e) {
128168
adapter.log.error('Cannot open port: ' + e);
129169
try {
130-
if (sPort.isOpen) sPort.close();
170+
sPort.isOpen && sPort.close();
131171
} catch (ee) {
132172

133173
}
134-
if (callback) callback(e);
174+
callback && callback(e);
135175
}
136176
}
137177
}
@@ -141,6 +181,7 @@ var tasks = [];
141181
function processTasks() {
142182
if (tasks.length) {
143183
var task = tasks.shift();
184+
144185
if (task.type === 'state') {
145186
adapter.setForeignState(task.id, task.val, true, function () {
146187
setTimeout(processTasks, 0);
@@ -178,7 +219,9 @@ function setStates(obj) {
178219
var isStart = !tasks.length;
179220

180221
for (var state in obj.data) {
181-
if (!obj.data.hasOwnProperty(state)) continue;
222+
if (!obj.data.hasOwnProperty(state)) {
223+
continue;
224+
}
182225
var oid = adapter.namespace + '.' + id + '.' + state;
183226
var meta = objects[oid];
184227
var val = obj.data[state];
@@ -193,7 +236,7 @@ function setStates(obj) {
193236
}
194237
tasks.push({type: 'state', id: oid, val: val});
195238
}
196-
if (isStart) processTasks();
239+
isStart && processTasks();
197240
}
198241

199242
function connect() {
@@ -207,7 +250,7 @@ function connect() {
207250
host: adapter.config.ip,
208251
port: adapter.config.port,
209252
debug: true,
210-
logger: adapter.log.info
253+
logger: adapter.log.debug
211254
};
212255

213256
cul = new Cul(options);
@@ -276,22 +319,16 @@ function connect() {
276319
}
277320

278321
setStates(obj);
279-
if (isStart) processTasks();
322+
isStart && processTasks();
280323
});
281324

282325
}
283326

284-
function insertObjects(objs, cb) {
285-
if (objs && objs.length) {
286-
var newObject = objs.pop();
287-
288-
} else if (cb) {
289-
cb();
290-
}
291-
}
292-
293327
function main() {
294-
328+
329+
// in this template all states changes inside the adapters namespace are subscribed
330+
adapter.subscribeStates('*');
331+
295332
adapter.getForeignObject('cul.meta.roles', function (err, res) {
296333
metaRoles = res.native;
297334
adapter.getObjectView('system', 'device', {startkey: adapter.namespace + '.', endkey: adapter.namespace + '.\u9999'}, function (err, res) {

io-package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
{
22
"common": {
33
"name": "cul",
4-
"version": "1.1.0",
4+
"version": "1.1.1",
55
"news": {
6+
"1.1.1": {
7+
"en": "Sending of FS20 cmdRAW possible or via sendTo as described in the readme.",
8+
"de": "Senden von FS20 cmdRAW möglich oder über sendTo wie in der Readme beschrieben.",
9+
"ru": "Отправка FS20 cmdRAW возможна или через sendTo, как описано в файле readme.",
10+
"pt": "É possível enviar FS20 cmdRAW ou via sendTo, conforme descrito no leia-me.",
11+
"nl": "Verzenden van FS20 cmdRAW mogelijk of via sendTo zoals beschreven in de readme.",
12+
"fr": "Envoi de FS20 cmdRAW possible ou via sendTo comme décrit dans le readme.",
13+
"it": "Invio di cmdRAW FS20 possibile o tramite sendTo come descritto nel file Leggimi.",
14+
"es": "Se puede enviar FS20 cmdRAW o mediante sendTo como se describe en el archivo Léame.",
15+
"pl": "Wysyłanie FS20 cmdRAW jest możliwe lub poprzez sendTo zgodnie z opisem w pliku Readme.",
16+
"zh-cn": "可以发送FS20 cmdRAW,也可以按照自述文件中的说明发送sendTo。"
17+
},
618
"1.1.0": {
719
"en": "removed usage of adapter.objects",
820
"de": "Verwendung von adapter.objects entfernt",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "iobroker.cul",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "Javascript/Node.js based Busware CUL USB / culfw adapter",
55
"author": "hobbyquaker <hq@ccu.io>",
66
"contributors": [

0 commit comments

Comments
 (0)