-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·325 lines (291 loc) · 10.6 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* jshint -W097 */// jshint strict:false
/* jslint node: true */
"use strict";
const utils = require('@iobroker/adapter-core');
// you have to call the adapter function and pass a options object
// name has to be set and has to be equal to adapters folder name and main file
// name excluding extension
// adapter will be restarted automatically every time as the configuration
// changed, e.g system.adapter.mobile-alerts.0
let adapter;
let RESETMODE={
0: 'SetToMin',
1: 'SetToMax'
};
let BOUNCEMODE={
0: 'Stay',
1: 'JumpToOtherEnd',
2: 'InvertIncrement'
};
function startAdapter(options) {
options = options || {};
Object.assign(options, {
name: 'sawtooth',
ready: main,
unload: callback => {
try {
adapter.log.debug('cleaned everything up...');
callback();
} catch (e) {
callback();
}
}
});
adapter = new utils.Adapter(options);
return adapter;
}
async function main() {
adapter.log.info(" **** sawtooth adapter started ****");
// processing each individial sawtooth:
var list = adapter.config.list;
for (var i=0; i<list.length;i++) {
processSawtooth(list[i]);
}
adapter.log.info(" **** sawtooth adapter finished ****");
adapter.terminate ? adapter.terminate() :process.exit(0);
}
async function processSawtooth(item) {
adapter.log.info(" Processing "+item.techname);
var states=[
{name:'currentValue'},
{name:'minValue'},
{name:'maxValue'},
{name:'increment'},
{name:'reset'},
{name:'resetMode'},
{name:'lastValue'},
{name:'readOnly'},
{name:'bounceMode'}
];
await readNextState(item,states,0);
}
async function createSawtooth(item,el) {
adapter.log.info(" ** createSawtooth");
await adapter.setObjectNotExistsAsync(item.techname, {
type: 'device',
common: {
name: item.description
},
native: {}
});
if (el=="currentValue") {
await adapter.setObjectNotExistsAsync(item.techname+'.'+el, {
type: 'state',
common: {
name: 'sawtooth.'+item.techname+'.'+el,
type: 'number',
role: 'value',
read: true,
write: true,
desc: 'Current sawtooth Value'
},
native: {}
});
adapter.setState(item.techname+'.'+el, {val : 0,ack : true});
}
if (el=="minValue") {
await adapter.setObjectNotExistsAsync(item.techname+'.'+el, {
type: 'state',
common: {
name: 'sawtooth.'+item.techname+'.'+el,
type: 'number',
role: 'value',
read: true,
write: true,
desc: 'Minimum allowed Value'
},
native: {val : 0,ack : true}
});
adapter.setState(item.techname+'.'+el, {val : 0,ack : true});
}
if (el=="maxValue") {
await adapter.setObjectNotExistsAsync(item.techname+'.'+el, {
type: 'state',
common: {
name: 'sawtooth.'+item.techname+'.'+el,
type: 'number',
role: 'value',
read: true,
write: true,
desc: 'Maximum allowed Value'
},
native: {val : 100,ack : true}
});
adapter.setState(item.techname+'.'+el, {val : 100,ack : true});
}
if (el=="increment") {
await adapter.setObjectNotExistsAsync(item.techname+'.'+el, {
type: 'state',
common: {
name: 'sawtooth.'+item.techname+'.'+el,
type: 'number',
role: 'value',
read: true,
write: true,
desc: 'The increment per Cycle, positive and negative values possible'
},
native: {}
});
adapter.setState(item.techname+'.'+el, {val : 1,ack : true});
}
if (el=="reset") {
await adapter.setObjectNotExistsAsync(item.techname+'.'+el, {
type: 'state',
common: {
name: 'sawtooth.'+item.techname+'.'+el,
type: 'boolean',
role: 'button.reset',
read: true,
write: true,
desc: 'Reset sawtooth value to default'
},
native: {}
});
adapter.setState(item.techname+'.'+el, {val : false,ack : true});
}
if (el=="resetMode") {
await adapter.setObjectNotExistsAsync(item.techname+'.'+el, {
type: 'state',
common: {
name: 'sawtooth.'+item.techname+'.'+el,
type: 'number',
role: 'indicator.status',
read: true,
write: true,
desc: 'How to set value if resetted',
states: RESETMODE
},
native: {}
});
adapter.setState(item.techname+'.'+el, {val : 0,ack : true});
}
if (el=="bounceMode") {
await adapter.setObjectNotExistsAsync(item.techname+'.'+el, {
type: 'state',
common: {
name: 'sawtooth.'+item.techname+'.'+el,
type: 'number',
role: 'indicator.status',
read: true,
write: true,
desc: 'Behavier when reaching boundaries',
states: BOUNCEMODE
},
native: {}
});
adapter.setState(item.techname+'.'+el, {val : 0,ack : true});
}
if (el=="lastValue") {
await adapter.setObjectNotExistsAsync(item.techname+'.'+el, {
type: 'state',
common: {
name: 'sawtooth.'+item.techname+'.'+el,
type: 'number',
role: 'value',
read: true,
write: false,
desc: 'Last saved value for internal processing'
},
native: {}
});
adapter.setState(item.techname+'.'+el, {val : 0,ack : true});
}
if (el=="readOnly") {
await adapter.setObjectNotExistsAsync(item.techname+'.'+el, {
type: 'state',
common: {
name: 'sawtooth.'+item.techname+'.'+el,
type: 'boolean',
role: 'switch',
read: true,
write: true,
desc: 'The currentValue could only be changed by adapter itself'
},
native: {}
});
adapter.setState(item.techname+'.'+el, {val : false,ack : true});
}
}
async function readNextState(item,states,idx) {
if (idx>=states.length) {
// all values read, lets process
adapter.log.info(" all states read of "+item.techname);
var params={};
for (var i = 0;i < states.length; i++) {
params[states[i].name] = states[i].val;
adapter.log.info(" > "+states[i].name+" = "+states[i].val);
}
// check increment (0 means "disabled")
if (params.increment == 0) {
adapter.log.info(" increment = 0 ==> sawtooth disabled for "+item.techname);
// just set lastValue and exit
adapter.setState(item.techname+'.lastValue', { val : params.currentValue, ack:true});
return;
}
// check readonly
if (params.readOnly) params.currentValue=params.lastValue;
// check null
if (params.currentValue==null) params.currentValue = 0;
// check reset
if (params.reset) {
adapter.log.info(" >> reset");
// read reset-mode
if (RESETMODE[params.resetMode] == 'SetToMin') params.currentValue=params.minValue; else params.currentValue=params.maxValue;
// temporary set some parameters, so that reset-value wont stay for that cycle
params.lastValue=params.currentValue;
params.increment=0;
adapter.setState(item.techname+'.reset', { val : false, ack:true});
}
// check if curValue was changed
if (params.currentValue!=params.lastValue) {
adapter.log.info(" >> value changed!");
if (params.currentValue<params.lastValue && params.increment<0) params.currentValue=params.minValue; else params.currentValue=params.maxValue;
// temporary set some parameters, so that the value wont be adjusted
params.lastValue=params.currentValue;
params.increment=0;
}
// do the increment
params.currentValue+=params.increment;
// check boundaries
if (params.currentValue>params.maxValue) {
if (BOUNCEMODE[params.bounceMode] == 'Stay') params.currentValue=params.maxValue;
if (BOUNCEMODE[params.bounceMode] == 'JumpToOtherEnd') params.currentValue=params.minValue;
if (BOUNCEMODE[params.bounceMode] == 'InvertIncrement') {
params.currentValue=params.maxValue;
adapter.setState(item.techname+'.increment', { val : -params.increment, ack:true});
}
}
if (params.currentValue<params.minValue) {
if (BOUNCEMODE[params.bounceMode] == 'Stay') params.currentValue=params.minValue;
if (BOUNCEMODE[params.bounceMode] == 'JumpToOtherEnd') params.currentValue=params.maxValue;
if (BOUNCEMODE[params.bounceMode] == 'InvertIncrement') {
params.currentValue=params.minValue;
adapter.setState(item.techname+'.increment', { val : -params.increment, ack:true});
}
}
// store currentValue also as lastValue
adapter.setState(item.techname+'.currentValue', { val : params.currentValue, ack:true});
adapter.setState(item.techname+'.lastValue', { val : params.currentValue, ack:true});
adapter.log.info(" >> new value: "+params.currentValue);
return;
}
var stateName=states[idx].name;
var path=item.techname+"."+stateName;
adapter.getState(path, function (err, state) {
if (state!=null) {
states[idx].val = state.val;
readNextState(item,states,idx+1);
} else {
adapter.log.info(" >! "+path);
createSawtooth(item,states[idx].name);
readNextState(item,states,idx);
}
});
}
// If started as allInOne/compact mode => return function to create instance
if (module && module.parent) {
module.exports = startAdapter;
} else {
// or start the instance directly
startAdapter();
}