-
Notifications
You must be signed in to change notification settings - Fork 0
/
vbus-data.js
208 lines (172 loc) · 4.55 KB
/
vbus-data.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
var vbus = require('resol-vbus');
var moment = require('moment');
var fs = require('fs');
var _ = require('lodash');
var Q = require('q');
var config = require('./config');
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
const adapter = new FileSync('database/solar.json');
const db = low(adapter);
var spec = vbus.Specification.getDefaultSpecification();
var packetId = 1;
var Pusher = require('pusher');
var update = false;
var dataChange = false;
var pusher = new Pusher({
appId: '556448',
key: '8a6d04a328c3b05e85f2',
secret: '555de261aa12f36db936',
cluster: 'eu',
encrypted: true
});
var main = function() {
var ctx = {
headerSet: null,
hsc: null,
connection: null,
};
var dataChanged = function() {
var packetFields = spec.getPacketFieldsForHeaders(ctx.headerSet.getSortedHeaders());
var changed1 = false;
var changed2 = false;
_.forEach(packetFields, function(pf) {
if (pf.name === 'Temperature sensor 1') {
const temp1 = db.get('temps')
.find({id: packetId - 1})
.get('data')
.find({name: pf.name})
.value();
if (temp1.rawValue !== pf.rawValue) {
changed1 = true;
}
else {
changed1 = false;
}
}
if (pf.name === 'Temperature sensor 2') {
const temp2 = db.get('temps')
.find({id: packetId - 1})
.get('data')
.find({name: pf.name})
.value();
if (temp2.rawValue !== pf.rawValue) {
changed2 = true;
}
else {
changed2 = false;
}
}
});
if (changed1 || changed2) {
return update = true;
}
else {
return update = false;
}
}
var latestId = function() {
var packet = db.get('temps')
.latest()
.value();
return packet.id;
}
db._.mixin({
latest: function(array) {
var long = array.length;
return array[long - 1];
}
});
var pushTrigger = function() {
db.read();
const temp2 = db.get('temps')
.latest()
.get('data')
.find({name: 'Temperature sensor 2'})
.value()
const temp1 = db.get('temps')
.latest()
.get('data')
.find({name: 'Temperature sensor 1'})
.value()
const time = db.get('temps')
.latest()
.get('time')
.value()
const date = db.get('temps')
.latest()
.get('date')
.value()
const test = [temp1, temp2, time, date]
pusher.trigger('vbus-channel', 'data-update-event', test);
}
var generateJsonData = function() {
var packetFields = spec.getPacketFieldsForHeaders(ctx.headerSet.getSortedHeaders());
var packetLength = packetFields.length;
moment.locale(config.locale);
var timestamp = moment().format('LT');
var datestamp = moment().format('L');
db.defaults({ temps: []})
.write();
_.forEach(packetFields, function(pf) {
if (pf.name === 'Temperature sensor 1' || pf.name === 'Temperature sensor 2') {
if (update) {
if (latestId() !== packetId) {
db.get('temps')
.push({id: packetId, time: timestamp, date: datestamp, data:[]})
.write();
}
db.get('temps')
.find({id: packetId})
.assign({time: timestamp, date: datestamp})
.write();
db.get('temps')
.find({id: packetId})
.get('data')
.push({id: pf.id, name: pf.name, rawValue: pf.rawValue})
.write();
dataChange = true;
}
}
});
if (dataChange) {
packetId++;
pushTrigger();
dataChange = false;
}
};
return Q.fcall(function() {
console.log('Connect to VBus data source...');
ctx.headerSet = new vbus.HeaderSet();
ctx.hsc = new vbus.HeaderSetConsolidator({
interval: config.loggingInterval,
});
ctx.connection = new vbus.SerialConnection({
path: config.usbConnection,
});
ctx.connection.on('packet', function(packet) {
ctx.headerSet.addHeader(packet);
ctx.hsc.addHeader(packet);
});
ctx.hsc.on('headerSet', function(headerSet) {
Q.fcall(function() {
console.log('HeaderSet complete');
dataChanged();
generateJsonData();
}).done();
});
return ctx.connection.connect();
}).then(function() {
ctx.hsc.startTimer();
return new Promise(function(resolve, reject) {
// nop, just run forever
});
});
};
if (require.main === module) {
Q.fcall(function() {
return main(process.argv.slice(2));
}).done();
} else {
module.exports = main;
}