-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
399 lines (306 loc) · 11.3 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/**
*
* raspilc adapter
* supports MCP3204-module with 4 analogue inputs 12bit resolution and the
* 1AI/1AO-module based on a MCP3201 and MCP4921 (used simultaneous)
*/
/* jshint -W097 */ // jshint strict :false
/*jslint node : true */
'use strict';
// you have to require the utils module and call adapter function
const utils = require(__dirname + '/lib/utils'); // Get common adapter utils
// 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.raspilc.0
const adapter = new utils.Adapter('raspilc');
/*Variable declaration, since ES6 there are let to declare variables. Let has a more clearer definition where
it is available then var.The variable is available inside a block and it's childs, but not outside.
You can define the same variable name inside a child without produce a conflict with the variable of the parent block.*/
let variable = 1234;
const i2c = require('i2c-bus');
var Statelist = [];
var StateToSave = 0;
var framFound;
// is called when adapter shuts down - callback has to be called under any circumstances!
adapter.on('unload', function(callback) {
try {
adapter.log.info('cleaned everything up...');
callback();
} catch (e) {
callback();
}
});
// is called if a subscribed object changes
//adapter.on('objectChange', function(id, obj) {
// Warning, obj can be null if it was deleted
// adapter.log.info('objectChange ' + id + ' ' + JSON.stringify(obj));
//});
//is called if a subscribed state changes
adapter.on('stateChange', function(id, state) {
adapter.log.debug('changed ID: ' + id + ' Value: ' + state.val);
if (adapter.config.FRAM == true) {
FRAMsave(Statelist.indexOf(id), state.val);
} else return;
});
// you can use the ack flag to detect if it is status (true) or command (false)
// if (state && !state.ack) {
// adapter.log.info('ack is not set!');
// Some message was sent to adapter instance over message box. Used by email, pushover, text2speech, ...
adapter.on('message', function(obj) {
if (typeof obj === 'object' && obj.message) {
if (obj.command === 'send') {
// e.g. send email or pushover or whatever
console.log('send command');
// Send response in callback if required
if (obj.callback) adapter.sendTo(obj.from, obj.command, 'Message received', obj.callback);
}
}
});
function FRAMStartup() {
//Check if FRAM is available:
var i2cdet;
if (adapter.config.FRAM == true) {
const i2c1 = require('i2c-bus');
const i2copen = i2c1.openSync(Number(adapter.config.i2cBusNo));
i2cdet = i2copen.scanSync();
framFound = i2cdet.indexOf(80);
adapter.log.debug('Detected devices: ' + i2cdet);
};
if ((adapter.config.FRAM == true) && (framFound >= 0)) {
setTimeout(function() {
Statelist = adapter.config.dp;
Statelist = Statelist.split(",");
for (var i = 0; i < Statelist.length; i++) {
adapter.subscribeForeignStates(Statelist[i]);
adapter.log.debug(Statelist[i]);
}
if (framFound >= 0) {
FRAMwrite();
}
}, Number(adapter.config.FRAM_load_delay));
} else {
adapter.log.warn('WARNING! No FRAM found!!!');
return;
};
}
function FRAMwrite() {
const FRAM_ADDR = 0x50;
const i2c1 = i2c.openSync(Number(adapter.config.i2cBusNo));
var DataBuf = new Buffer([0x00, 0x00]);
var sendBytes = 0;
var firstStart = false;
var i = 0;
var tempReceive = 0;
for (i = 0; i < (Statelist.length); i++) {
DataBuf[0] = i >>> 8; //Puffer in LOW- und HIGHbyte zerlegen
DataBuf[1] = i % 255;
i2c1.i2cWriteSync(0x50, 2, DataBuf); //Zeiger im FRAM auf Adresse setzen
tempReceive = i2c1.receiveByteSync(0x50);
if (tempReceive == 254) {
adapter.setForeignState(Statelist[i], false);
// adapter.log.info("Objekt Name: " + Statelist[i] + " bool false written");
} else if (tempReceive == 255) {
adapter.setForeignState(Statelist[i], true);
// adapter.log.info("Objekt Name: " + Statelist[i] + " bool true written");
} else if (tempReceive <= 253) {
adapter.setForeignState(Statelist[i], tempReceive);
// adapter.log.info("Objekt Name: " + Statelist[i] + " number: " + tempReceive + " written");
} else {
adapter.log.warn('Variable kann nicht geschrieben werden');
}
adapter.log.debug('Objekt Name: ' + Statelist[i] + ' restored');
}
i2c1.closeSync();
}
function FRAMsave(StateToSave, valToWrite) {
const i2c = require('i2c-bus');
const FRAM_ADDR = 0x50;
const i2c1 = i2c.openSync(Number(adapter.config.i2cBusNo));
var temp;
var temp2 = 0;
var data = new Buffer([0x00, 0x00, 0x00]);
var DataBuf = new Buffer([0x00, 0x00]);
var sendBytes = 0;
// adapter.getState((Statelist[StateToSave]), function(err, state) {
// if (!err) {
// valToWrite = state.val;
// adapter.log.info('Variablentyp in Funktion: ' + typeof(valToWrite));
// }
// });
if (valToWrite === true) { //Wenn BOOL > TRUE dann 255
temp2 = 0xff;
} else if (valToWrite === false) { //Wenn BOOL > FALSE dann 254
temp2 = 0xfe;
} else {
if (valToWrite <= 253) {
temp2 = Math.round(valToWrite); //Runden auf Ganzzahl!
} else {
adapter.log.warn('Wert zu gross zum Speichern oder falscher Typ, ignoriert!')
};
};
//Adresse in LOW-und HIGH-Byte zerlegen / Puffer aufbereiten
data[0] = StateToSave >>> 8;
data[1] = StateToSave % 255;
data[2] = temp2;
sendBytes = i2c1.i2cWriteSync(FRAM_ADDR, 3, data);
//console.log("Transmitted bytes: " + sendBytes);
i2c1.closeSync();
}
// is called when databases are connected and adapter received configuration.
// start here!
adapter.on('ready', function() {
main();
})
function main() {
FRAMStartup();
// The adapters config (in the instance object everything under the attribute "native") is accessible via
// adapter.config :
adapter.log.debug('config FRAM : ' + adapter.config.FRAM);
adapter.log.debug('config Module_inserted : ' + adapter.config.Module_inserted);
adapter.log.debug('config mySelect : ' + adapter.config.mySelect);
adapter.log.debug('config busNumber : ' + adapter.config.busNum);
adapter.log.debug('config deviceNumber : ' + adapter.config.devNum);
adapter.log.debug('config Startup-Delay : ' + adapter.config.FRAM_load_delay);
adapter.log.debug('config i2c-bus : ' + adapter.config.i2cBusNo);
/**
*
* For every state in the system there has to be also an object of type state
*
* Here a simple raspilc for a boolean variable named "testVariable"
*
* Because every adapter instance uses its own unique namespace variable names can't collide with other adapters variables
*
*/
if (adapter.config.Module_inserted == true && (adapter.config.mySelect == '4-AI-Module')) {
adapter.setObject('Analog.Channel0', {
type: 'state',
common: {
name: 'Channel0',
type: 'number',
role: 'indicator'
},
native: {}
});
adapter.setObject('Analog.Channel1', {
type: 'state',
common: {
name: 'Channel1',
type: 'number',
role: 'indicator'
},
native: {}
});
adapter.setObject('Analog.Channel2', {
type: 'state',
common: {
name: 'Channel2',
type: 'number',
role: 'indicator'
},
native: {}
});
adapter.setObject('Analog.Channel3', {
type: 'state',
common: {
name: 'Channel3',
type: 'number',
role: 'indicator'
},
native: {}
});
read4AI();
adapter.log.debug('Analogports lesen gestartet');
} else if (adapter.config.Module_inserted == true && (adapter.config.mySelect == '1AI-1AO-Module')) {
adapter.setObject('Analog.AnalogIn', {
type: 'state',
common: {
name: 'AnalogIn',
type: 'number',
role: 'value'
},
native: {}
});
adapter.setObject('Analog.AnalogOut', {
type: 'state',
common: {
name: 'AnalogOut',
type: 'number',
role: 'value'
},
native: {}
});
} else return;
readwrite1AI1AO();
}
const spi = require('spi-device');
function read4AI() {
var Interval = Number(adapter.config.Interval);
var busNum = Number(adapter.config.busNum);
var devNum = Number(adapter.config.devNum);
// const mcpadc = require('spi-device');
adapter.setState('Analog.Channel0', 0);
adapter.setState('Analog.Channel1', 0);
adapter.setState('Analog.Channel2', 0);
adapter.setState('Analog.Channel3', 0, read1);
}
function read1() {
adapter.log.debug('Read 4AI-Funktion gestartet');
var Interval = Number(adapter.config.Interval);
var busNum = Number(adapter.config.busNum);
var devNum = Number(adapter.config.devNum);
const mcpadc = require('spi-device');
const mcp3204 = spi.open(busNum, devNum, (err) => {
var i;
for (i = 0; i < 4; i++) {
(function(i) {
setInterval(() => {
const message = [{
sendBuffer: Buffer.from([0x04, ((0x00 + i) << 6), 0x00]), // Sent to read
receiveBuffer: Buffer.alloc(3), // received raw data
byteLength: 3,
speedHz: 20000
}];
if (err) throw err;
mcp3204.transfer(message, (err, message) => {
if (err) throw err;
adapter.setState(('Analog.Channel' + i), (((message[0].receiveBuffer[1] & 0x0f) << 8) + message[0].receiveBuffer[2]), true);
});
}, Interval);
})(i);
}
});
};
//*******ab hier 1AI-1AO-Modul!!!******
function readwrite1AI1AO() {
adapter.setState('Analog.AnalogIn', 0);
adapter.setState('Analog.AnalogOut', 0, readwrite);
function readwrite() {
var analogsend = 0;
var Interval = adapter.config.Interval;
var busNum = Number(adapter.config.busNum);
var devNum = Number(adapter.config.devNum);
const spi = require('spi-device');
const mcp3201 = spi.open(busNum, devNum, (err) => {
setInterval(() => {
adapter.getState('Analog.AnalogOut', function(err, state) {
analogsend = state.val;
});
if (analogsend > 4095) { //zu große Eingabe begrenzen
analogsend = 4095;
}
const message = [{
sendBuffer: Buffer.from([0x30 + (analogsend >> 8), analogsend & 0xff]), // Sent to read
receiveBuffer: Buffer.alloc(2), // Raw data read
byteLength: 2,
speedHz: 20000
}];
if (err) throw err;
mcp3201.transfer(message, (err, message) => {
if (err) throw err;
var analogIN = (((message[0].receiveBuffer[0] & 0x0F) << 8) + message[0].receiveBuffer[1]);
adapter.setState('Analog.AnalogIn', analogIN, true);
});
}, Interval);
});
}
}