-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
205 lines (183 loc) · 7.95 KB
/
main.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.20.0
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require("@iobroker/adapter-core");
let pollingInterval;
// Load your modules here, e.g.:
var PalazzettiRequest = require('./lib/palazzettiRequest').PalazzettiRequest;
var setObjectStates = require('./lib/palazzettiObjectStateMap');
const { rejects } = require("assert");
class Palazzetti extends utils.Adapter {
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: "palazzetti",
});
this.on("ready", this.onReady.bind(this));
this.on("stateChange", this.onStateChange.bind(this));
// this.on("message", this.onMessage.bind(this));
this.on("unload", this.onUnload.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Initialize your adapter here
// The adapters config (in the instance object everything under the attribute "native") is accessible via
// this.config:
this.log.info("config ip-address: " + this.config.ipAddress);
this.log.info("config port: " + this.config.port);
this.log.info("config poll time: " + this.config.polltime);
this.palazzettiRequest = new PalazzettiRequest({
ipaddress: this.config.ipAddress,
port: this.config.port
});
this.subscribeStates("*");
this.updateState();
pollingInterval = setInterval(function() {
this.updateState();
}.bind(this), this.config.polltime * 1000 || 60000);
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
this.log.info("cleaned everything up...");
clearInterval(pollingInterval);
callback();
} catch (e) {
callback();
}
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
onStateChange(id, state) {
try {
if (state) {
let instanceName = this.name + "." + this.instance;
// The state was changed
//this.log.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
switch (id) {
case instanceName + ".control.f3l":
if(!state.ack && state.val >= 0 && state.val <= 5) {
this.palazzettiRequest.setCommand("FN3L+" + String(state.val)).then(function(result) {
this.log.info("set left fan level: " + JSON.stringify(result));
this.updateState(true);
}.bind(this)).catch(function(err) {
this.log.error(err);
}.bind(this));
}
break;
case instanceName + ".control.f4l":
if(!state.ack && state.val >= 0 && state.val <= 5) {
this.palazzettiRequest.setCommand("FN4L+" + String(state.val)).then(function(result) {
this.log.info("set right fan level: " + JSON.stringify(result));
this.updateState(true);
}.bind(this)).catch(function(err) {
this.log.error(err);
}.bind(this));
}
break;
case instanceName + ".control.f2l":
if (!state.ack && state.val >= 0 && state.val <= 7) {
this.palazzettiRequest.setCommand("RFAN+" + String(state.val)).then(function(result) {
this.log.info("set fan level: " + JSON.stringify(result));
this.updateState(true);
}.bind(this)).catch(function(err) {
this.log.error(err);
}.bind(this));
}
break;
case instanceName + ".control.setp":
if (!state.ack && state.val >= 0 && state.val <= 40) {
this.palazzettiRequest.setCommand("SETP+" + String(state.val)).then(function(result) {
this.log.info("set point: " + JSON.stringify(result));
this.updateState(true);
}.bind(this)).catch(function(err) {
this.log.error(err);
}.bind(this));
}
break;
case instanceName + ".control.pwr":
if (!state.ack && state.val >= 0 && state.val <= 5) {
this.palazzettiRequest.setCommand("POWR+" + String(state.val)).then(function(result) {
this.log.info("set power: " + JSON.stringify(result));
this.updateState(true);
}.bind(this)).catch(function(err) {
this.log.error(err);
}.bind(this));
}
break;
case instanceName + ".control.onoff":
if (!state.ack && state.val !== null) {
this.palazzettiRequest.powerCommand(state.val === false ? 'OFF' : 'ON').then(function(result) {
this.log.info("set on/off: " + JSON.stringify(result));
this.updateState(true);
}.bind(this)).catch(function(err) {
this.log.error(err);
}.bind(this));
}
break;
default:
break;
}
} else {
// The state was deleted
this.log.info(`state ${id} deleted`);
}
} catch (e) {
this.log.error(e);
}
}
}
Palazzetti.prototype.updateState = function(onlyValues) {
let aRequests = [];
aRequests.push(this.palazzettiRequest.getAlls());
if (!onlyValues) {
aRequests.push(this.palazzettiRequest.getTimer());
aRequests.push(this.palazzettiRequest.getLabel());
}
Promise.all(aRequests).then(
function(result) {
try {
var setRequests = [];
if (!!result[0]) {
setRequests.concat(setObjectStates.StateInfo(this, result[0]));
setRequests.concat(setObjectStates.StateGet(this, result[0]));
setRequests.concat(setObjectStates.StateControl(this, result[0]));
}
if (!!result[1]) setRequests.concat(setObjectStates.StateTimer(this, result[1]));
if (!!result[2]) setRequests.concat(setObjectStates.StateLabel(this, result[2]));
Promise.all(setRequests).catch(function(err) {
this.log.error(err);
}.bind(this));
} catch (err) {
this.log.error(err);
}
}.bind(this)).catch(
function(error) {
this.log.error(error);
}.bind(this));
}
// @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 Palazzetti(options);
} else {
// otherwise start the instance directly
new Palazzetti();
};