-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetio.js
205 lines (181 loc) · 5.34 KB
/
netio.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
"use strict";
/*
* Created with @iobroker/create-adapter v1.15.1
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require("@iobroker/adapter-core");
const request = require("request");
class Netio extends utils.Adapter {
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: "netio",
});
this.on("ready", this.onReady.bind(this));
this.on("objectChange", this.onObjectChange.bind(this));
this.on("stateChange", this.onStateChange.bind(this));
this.on("unload", this.onUnload.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Reset the connection indicator during startup
this.setState("info.connection", false, true);
await this.setObjectAsync("info.name", {
type: "state",
common: {
name: "name",
type: "string",
role: "info.name",
read: true,
write: true,
},
native: {},
});
for (let p=1; p<=this.config.numPorts; p++) {
await this.setObjectAsync("port" + p, {
type: "state",
common: {
name: "port" + p,
type: "boolean",
role: "switch.power",
read: true,
write: true,
},
native: {},
});
}
this.state = Array(this.config.numPorts).fill();
this.connected = false;
this.setState("info.name", this.config.deviceName, true);
// in this template all states changes inside the adapters namespace are subscribed
this.subscribeStates("*");
const that = this;
const requestState = function() {
// request port states from device
const url = `http://${that.config.netIoAddress}:${that.config.netIoPort}/tgi/control.tgi?login=p:${that.config.username}:${that.config.password}&port=list&quit=quit`;
request(url, function (error, response, body) {
// that.log.info("E " + error + JSON.stringify(error))
// that.log.info("R " + JSON.stringify(response))
// that.log.info("B " + body);
if (that.state && body && body.includes("BYE")) {
const state = body.substr(6, 2*that.config.numPorts-1)
.split(" ")
.map(x => x==1);
for (let i=0; i<state.length; i++) {
if (state[i] !== that.state[i]) {
that.setState("port"+(i+1), state[i], true);
that.state[i] = state[i];
}
}
if (!that.connected) {
that.connected = true;
that.setState("info.connection", true, true);
}
} else {
that.log.error(JSON.stringify(error));
if (that.connected) {
that.connected = false;
that.setState("info.connection", false, true);
}
}
});
};
requestState();
this.timer = null;
if (this.config.polling)
this.timer = setInterval(requestState, this.config.pollingInterval * 1000);
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
// cancel timer
// @ts-ignore
clearInterval(this.timer);
this.log.info("cleaned everything up ...");
callback();
} catch (e) {
callback();
}
}
/**
* Is called if a subscribed object changes
* @param {string} id
* @param {ioBroker.Object | null | undefined} obj
*/
onObjectChange(id, obj) {
// this.log.info("YYY " + id + " " + JSON.stringify(obj))
if (obj) {
// The object was changed
this.log.info(`object ${id} changed: ${JSON.stringify(obj)}`);
} else {
// The object was deleted
this.log.info(`object ${id} deleted`);
}
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
onStateChange(id, state) {
// this.log.info("XXX " + id + " " + JSON.stringify(state))
if (state) {
// The state was changed
this.log.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
if (id.includes("port")) {
const s = Array(this.config.numPorts).fill("u");
const index = parseInt(id.substr(-1)) - 1;
s[index] = state.val ? "1" : "0";
const list = s.join("");
const that = this;
const url = `http://${this.config.netIoAddress}:${this.config.netIoPort}/tgi/control.tgi?login=p:${this.config.username}:${this.config.password}&port=${list}&port=list&quit=quit`;
request(url, function (error, response, body) {
if (that.state && body && body.includes("OK")) {
const state = body.substr(13, 2*that.config.numPorts-1)
.split(" ")
.map(x => x==1);
for (let i=0; i<state.length; i++) {
if (state[i] !== that.state[i]) {
that.setState("port"+(i+1), state[i], true);
that.state[i] = state[i];
}
}
if (!that.connected) {
that.connected = true;
that.setState("info.connection", true, true);
}
} else {
that.log.error(JSON.stringify(error));
if (that.connected) {
that.connected = false;
that.setState("info.connection", false, true);
}
}
});
}
} else {
// The state was deleted
this.log.info(`state ${id} deleted`);
}
}
}
// @ts-ignore parent is a valid property on module
if (module.parent) {
// Export the constructor in compact mode
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Netio(options);
} else {
// otherwise start the instance directly
new Netio();
}