-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
175 lines (129 loc) · 4.97 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
'use strict';
var utils = require(__dirname + '/lib/utils'); // Get common adapter utils
var request = require('request');
var adapter = new utils.Adapter('poolcontroller');
adapter.on('ready', function () {
main();
});
function main() {
var host = adapter.config.host
var port = adapter.config.port
var result, json;
//#################################################################
request(
{
url: "http://" + host + ":" + port + "/GetState.csv"
},
function(error, responce, content) {
adapter.log.debug('Request done');
if (!error){
content =content.replace(/ /g, '_'); //alle Leerzeichen durch Unterstrich ersetzten
var data=CSVToArray(content); //CSV in ein Array einlesen
// User Variablen anlegen Achtung keine Punkte im Namen verwenden.
var i=0;
for (i=0; i<=41;i++){
adapter.setObjectNotExists(data[1][i], {
type: 'state',
common: {
name: data[1][i],
type: 'number',
unit: data[2][i],
role: 'value',
write: false,
read: true
},
});
}
//SYSINFO Variablen mit aktuellen Werten beschreiben
adapter.setState('sysinfo.VERSION', data[0][1]);
adapter.setState('sysinfo.CPU_TIME', parseFloat(Number(data[0][2]).toFixed(2)));
adapter.setState('sysinfo.RESET_ROOT_CAUSE', parseFloat(Number(data[0][3]).toFixed(2)));
adapter.setState('sysinfo.NTP_FAULT_STATE', parseFloat(Number(data[0][4]).toFixed(2)));
adapter.setState('sysinfo.CONFIG_OTHER_ENABLE', parseFloat(Number(data[0][5]).toFixed(2)));
adapter.setState('sysinfo.DOSAGE_CNTRL', parseFloat(Number(data[0][6]).toFixed(2)));
adapter.setState('sysinfo.pH+_DOSAGE_RELAIS_ID', parseFloat(Number(data[0][7]).toFixed(2)));
adapter.setState('sysinfo.pH-_DOSAGE_RELAIS_ID', parseFloat(Number(data[0][8]).toFixed(2)));
adapter.setState('sysinfo.Chlor_DOSAGE_RELAIS_ID', parseFloat(Number(data[0][9]).toFixed(2)));
// User Variablen mit aktuellen Werten beschreiben
i=0;
for (i=0; i<=41;i++){
var wert = offset + ( gain * value);
var offset = parseFloat(data[3][i]);
var gain = parseFloat(data[4][i]);
var value = parseFloat(data[5][i]);
var wert = offset + ( gain * value);
adapter.setState(data[1][i], parseFloat(Number(wert).toFixed(2)));
}
adapter.log.info("Variablen updated");
}
else {
adapter.log.error(error);
}
}
);
setTimeout(function () {
adapter.stop();
}, 10000);
//###########################################################################################
function CSVToArray( strData, strDelimiter ){
// Check to see if the delimiter is defined. If not,
// then default to comma.
strDelimiter = (strDelimiter || ",");
// Create a regular expression to parse the CSV values.
var objPattern = new RegExp(
(
// Delimiters.
"(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
// Quoted fields.
"(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
// Standard fields.
"([^\"\\" + strDelimiter + "\\r\\n]*))"
),
"gi"
);
// Create an array to hold our data. Give the array
// a default empty first row.
var arrData = [[]];
// Create an array to hold our individual pattern
// matching groups.
var arrMatches = null;
// Keep looping over the regular expression matches
// until we can no longer find a match.
while (arrMatches = objPattern.exec( strData )){
// Get the delimiter that was found.
var strMatchedDelimiter = arrMatches[ 1 ];
// Check to see if the given delimiter has a length
// (is not the start of string) and if it matches
// field delimiter. If id does not, then we know
// that this delimiter is a row delimiter.
if (
strMatchedDelimiter.length &&
strMatchedDelimiter !== strDelimiter
){
// Since we have reached a new row of data,
// add an empty row to our data array.
arrData.push( [] );
}
var strMatchedValue;
// Now that we have our delimiter out of the way,
// let's check to see which kind of value we
// captured (quoted or unquoted).
if (arrMatches[ 2 ]){
// We found a quoted value. When we capture
// this value, unescape any double quotes.
strMatchedValue = arrMatches[ 2 ].replace(
new RegExp( "\"\"", "g" ),
"\""
);
} else {
// We found a non-quoted value.
strMatchedValue = arrMatches[ 3 ];
}
// Now that we have our value string, let's add
// it to the data array.
arrData[ arrData.length - 1 ].push( strMatchedValue );
}
// Return the parsed data.
return( arrData );
}
}