-
Notifications
You must be signed in to change notification settings - Fork 1
/
CFroniusProbe.cpp
623 lines (520 loc) · 13.5 KB
/
CFroniusProbe.cpp
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
* Copyright (c) 2012 Alexandros Nikolopoulos <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*/
#include "CFroniusProbe.h"
#include "errno.h"
CFroniusProbe::CFroniusProbe(CInterface *SerialLine, string uuid)
{
m_Interface = SerialLine;
m_SendingThread = m_ReceivingThread = 0;
m_Uuid = uuid;
pthread_mutex_init(&m_queueMutex, NULL);
}
CFroniusProbe::CFroniusProbe(CInterface *SerialLine, list<int> sensors, string uuid)
{
m_Interface = SerialLine;
m_SendingThread = m_ReceivingThread = 0;
m_Uuid = uuid;
m_connected = sensors;
pthread_mutex_init(&m_queueMutex, NULL);
}
CFroniusProbe::~CFroniusProbe()
{
Stop();
delete(m_Interface);
}
bool
CFroniusProbe::probeInverters(void)
{
// Try to send probing message in order to find all the active inverters in the network
for(int l=0; l < 5; l++)
{
if(!SendMessage(m_Interface, CMD_ACTIVE_INVERTERS, 0, TYPE_GENERAL, NULL, 0))
return false; // Will never normally happen
pthread_mutex_lock(&m_queueMutex);
std::list<struct MsgStruct>::iterator i = m_MsgQueue.begin();
while (i != m_MsgQueue.end())
{
if(i->Data[3] == 0x04 && i->DataLen>5)
{
int j = 0;
for(j=0; j<i->DataLen - 5; j++)
m_connected.push_back(i->Data[j+4]);
i = m_MsgQueue.erase(i);
pthread_mutex_unlock(&m_queueMutex);
syslog(LOG_INFO, "Found %d inverters\n", j);
return true;
}
else
i++;
}
pthread_mutex_unlock(&m_queueMutex);
}
return false;
}
bool
CFroniusProbe::probeStaticValues(void)
{
return true;
}
int
CFroniusProbe::Start(void)
{
struct ThreadStruct RecArgs;
RecArgs.interface = m_Interface;
RecArgs.queueMutex = &m_queueMutex;
RecArgs.MsgQueue = &m_MsgQueue;
if(!m_ReceivingThread)
pthread_create( &m_ReceivingThread, NULL, &CFroniusProbe::ReceivingFunction, &RecArgs);
// Check for inverters. If none is found, stop the process
if(m_connected.empty() == true)
{
if(probeInverters() == false)
{
Stop();
return false;
}
}
// All is well.
// Start the main sending thread
struct ThreadStruct args;
args.interface = m_Interface;
args.connected = &m_connected;
if(!m_SendingThread)
pthread_create( &m_SendingThread, NULL, &CFroniusProbe::SendingFunction, &args);
sleep(1);
return true;
}
int
CFroniusProbe::Stop(void)
{
if(m_SendingThread)
{
pthread_cancel(m_SendingThread);
m_SendingThread = 0;
}
if(m_ReceivingThread)
{
pthread_cancel(m_ReceivingThread);
m_ReceivingThread = 0;
}
return true;
}
list<int>
CFroniusProbe::GetConnectedInverters(void)
{
// Save the old list. If the probe fails, restore the old one
list<int> ConnectedProbes = m_connected;
if(probeInverters() == false)
m_connected = ConnectedProbes;
return m_connected;
}
int
CFroniusProbe::GetAverage(DataContainer &AverageData)
{
int inverterId = atoi(AverageData["inverter"].c_str());
bool rv = true;
if(m_MsgQueue.size() == 0)
{
syslog(LOG_ERR, "No messages in Queue\n");
return false;
}
pthread_mutex_lock(&m_queueMutex);
std::list<struct MsgStruct>::iterator i = m_MsgQueue.begin();
int answers = 0;
while(i != m_MsgQueue.end())
{
if(i->Data[2] == inverterId && i->Data[3] == CMD_INVERTER_STATUS)
{
if(i->Data[0] > 0)
AverageData["status"] = int2string(i->Data[4]);
else
{
pthread_mutex_unlock(&m_queueMutex);
if(debugFlag == true)
syslog(LOG_INFO, "Inverter is offline (%ld secs ago)", time(NULL) - i->TimeStamp);
return false;
}
i = m_MsgQueue.erase(i);
answers++;
}
else
i++;
}
pthread_mutex_unlock(&m_queueMutex);
if(answers == 0)
{
syslog(LOG_WARNING, "No Status results");
return false;
}
// If the inverter is not in operating state, don't probe for any other values as
// the fronius inverters seem to send out faulty data on other states
if(atoi(AverageData["status"].c_str()) != 2)
return false;
uint32_t u32;
uint64_t u64;
float flt;
if(RetreiveFromStack(inverterId, CMD_POWER_NOW, &u32) == false)
rv = false;
else
AverageData["power"] = int2string(u32);
if(RetreiveFromStack(inverterId, CMD_AC_CURRENT_NOW, &flt) == false)
rv = false;
else
AverageData["currentAC"] = float2string(flt);
if(RetreiveFromStack(inverterId, CMD_DC_CURRENT_NOW, &flt) == false)
rv = false;
else
AverageData["currentDC"] = float2string(flt);
if(RetreiveFromStack(inverterId, CMD_PHASE_CURRENT1, &flt) == false)
rv = false;
else
AverageData["Phase1Current"] = float2string(flt);
if(RetreiveFromStack(inverterId, CMD_PHASE_CURRENT2, &flt) == false)
rv = false;
else
AverageData["Phase2Current"] = float2string(flt);
if(RetreiveFromStack(inverterId, CMD_PHASE_CURRENT3, &flt) == false)
rv = false;
else
AverageData["Phase3Current"] = float2string(flt);
if(RetreiveFromStack(inverterId, CMD_AC_FREQUENCY_NOW, &flt) == false)
rv = false;
else
AverageData["frequencyAC"] = float2string(flt);
if(RetreiveFromStack(inverterId, CMD_AC_VOLTAGE_NOW, &u32) == false)
rv = false;
else
AverageData["voltageAC"] = int2string(u32);
if(RetreiveFromStack(inverterId, CMD_DC_VOLTAGE_NOW, &u32) == false)
rv = false;
else
AverageData["voltageDC"] = int2string(u32);
if(RetreiveFromStack(inverterId, CMD_PHASE_VOLTAGE1, &u32) == false)
rv = false;
else
AverageData["Phase1Voltage"] = int2string(u32);
if(RetreiveFromStack(inverterId, CMD_PHASE_VOLTAGE2, &u32) == false)
rv = false;
else
AverageData["Phase2Voltage"] = int2string(u32);
if(RetreiveFromStack(inverterId, CMD_PHASE_VOLTAGE3, &u32) == false)
rv = false;
else
AverageData["Phase3Voltage"] = int2string(u32);
if(RetreiveFromStack(inverterId, CMD_ENERGY_DAY, &u32) == false)
rv = false;
else
AverageData["DailyEnergy"] = int2string(u32);
if(RetreiveFromStack(inverterId, CMD_ENERGY_TOTAL_EX, &u64) == false)
rv = false;
else
AverageData["TotalEnergy"] = int2string(u64);
AverageData["clientID"] = "\"" + m_Uuid + "\"";
return rv;
}
// RetreiveFromStack with 64bit value is used only for TotalPower. For this, we don't want
// An averaging...
// TODO: Make a better way to differantiate between averaged/non averaged values
int
CFroniusProbe::RetreiveFromStack(int DeviceNumber, int Command, uint64_t *result)
{
pthread_mutex_lock(&m_queueMutex);
std::list<struct MsgStruct>::iterator i = m_MsgQueue.begin();
uint64_t sum = 0;
uint16_t answers = 0;
while (i != m_MsgQueue.end())
{
if(i->Data[2] == DeviceNumber && i->Data[3] == Command)
{
if(Command == CMD_ENERGY_TOTAL_EX)
sum = ProcessExponent64(&(i->Data[4]), i->Data[0]);
else
sum += ProcessExponent64(&(i->Data[4]), i->Data[0]);
answers++;
i = m_MsgQueue.erase(i);
}
else
i++;
}
pthread_mutex_unlock(&m_queueMutex);
if(answers > 0)
{
if(Command == CMD_ENERGY_TOTAL_EX)
*result = sum;
else
*result = sum/answers;
return true;
}
else
{
*result = 0;
return false;
}
}
int
CFroniusProbe::RetreiveFromStack(int DeviceNumber, int Command, uint32_t *result)
{
pthread_mutex_lock(&m_queueMutex);
std::list<struct MsgStruct>::iterator i = m_MsgQueue.begin();
uint64_t sum = 0;
uint16_t answers = 0;
while (i != m_MsgQueue.end())
{
if(i->Data[2] == DeviceNumber && i->Data[3] == Command)
{
if(Command == CMD_ENERGY_DAY)
sum = ProcessExponent32(&(i->Data[4]), i->Data[0]);
else
sum += ProcessExponent32(&(i->Data[4]), i->Data[0]);
answers++;
i = m_MsgQueue.erase(i);
}
else
i++;
}
pthread_mutex_unlock(&m_queueMutex);
if(answers > 0)
{
if(Command == CMD_ENERGY_DAY)
*result = sum;
else
*result = sum/answers;
return true;
}
else
{
*result = 0;
return false;
}
}
int
CFroniusProbe::RetreiveFromStack(int DeviceNumber, int Command, float *result)
{
pthread_mutex_lock(&m_queueMutex);
std::list<struct MsgStruct>::iterator i = m_MsgQueue.begin();
float sum = 0;
uint16_t answers = 0;
while (i != m_MsgQueue.end())
{
if(i->Data[2] == DeviceNumber && i->Data[3] == Command)
{
sum += ProcessExponent32(&(i->Data[4]), i->Data[0]);
answers++;
i = m_MsgQueue.erase(i);
}
else
i++;
}
pthread_mutex_unlock(&m_queueMutex);
if(answers > 0)
{
*result = sum/answers;
return true;
}
else
{
*result = 0;
return false;
}
}
int
CFroniusProbe::ResetStack(void)
{
std::list<struct MsgStruct>::iterator i = m_MsgQueue.begin();
pthread_mutex_lock(&m_queueMutex);
while (i != m_MsgQueue.end())
{
i = m_MsgQueue.erase(i);
i++;
}
pthread_mutex_unlock(&m_queueMutex);
return true;
}
float
CFroniusProbe::ProcessExponent32(uint8_t *exp, uint8_t len)
{
if(len > 5)
return 0;
uint8_t mainLen = len-1;
uint32_t number = 0;
for(int i=0; i<mainLen; i++)
number |= ((uint32_t) *(exp+i)) << ((56-(64-mainLen*8))- (8 * i));
return number * pow(10, (int8_t) *(exp+len-1));
}
uint64_t
CFroniusProbe::ProcessExponent64(uint8_t *exp, uint8_t len)
{
if(len > 11)
return 0;
uint8_t mainLen = len-1;
uint32_t number = 0;
for(int i=0; i<mainLen; i++)
number |= ((uint32_t) *(exp+i)) << ((56-(64-mainLen*8))- (8 * i));
return number * pow(10, (int8_t) *(exp+len-1));
}
void *
CFroniusProbe::SendingFunction(void *ptr)
{
struct ThreadStruct args;
memcpy(&args, ptr, sizeof(struct ThreadStruct));
while(true)
{
for(list<int>::iterator curInv=args.connected->begin();curInv!=args.connected->end(); ++curInv)
{
SendMessage(args.interface, CMD_INVERTER_STATUS, *curInv, TYPE_INVERTER, NULL, 0);
SendMessage(args.interface, CMD_POWER_NOW, *curInv, TYPE_INVERTER, NULL, 0);
SendMessage(args.interface, CMD_AC_CURRENT_NOW, *curInv, TYPE_INVERTER, NULL, 0);
SendMessage(args.interface, CMD_DC_CURRENT_NOW, *curInv, TYPE_INVERTER, NULL, 0);
SendMessage(args.interface, CMD_PHASE_CURRENT1, *curInv, TYPE_INVERTER, NULL, 0);
SendMessage(args.interface, CMD_PHASE_CURRENT2, *curInv, TYPE_INVERTER, NULL, 0);
SendMessage(args.interface, CMD_PHASE_CURRENT3, *curInv, TYPE_INVERTER, NULL, 0);
SendMessage(args.interface, CMD_AC_VOLTAGE_NOW, *curInv, TYPE_INVERTER, NULL, 0);
SendMessage(args.interface, CMD_DC_VOLTAGE_NOW, *curInv, TYPE_INVERTER, NULL, 0);
SendMessage(args.interface, CMD_PHASE_VOLTAGE1, *curInv, TYPE_INVERTER, NULL, 0);
SendMessage(args.interface, CMD_PHASE_VOLTAGE2, *curInv, TYPE_INVERTER, NULL, 0);
SendMessage(args.interface, CMD_PHASE_VOLTAGE3, *curInv, TYPE_INVERTER, NULL, 0);
SendMessage(args.interface, CMD_AC_FREQUENCY_NOW, *curInv, TYPE_INVERTER, NULL, 0);
SendMessage(args.interface, CMD_ENERGY_DAY, *curInv, TYPE_INVERTER, NULL, 0);
SendMessage(args.interface, CMD_ENERGY_TOTAL_EX, *curInv, TYPE_INVERTER, (uint8_t *) "\x01", 1);
}
}
return NULL;
}
void *
CFroniusProbe::ReceivingFunction(void *ptr)
{
struct ThreadStruct args;
memcpy(&args, ptr, sizeof(struct ThreadStruct));
CInterface *Interface = args.interface;
uint8_t CurrentMessage[512];
uint16_t MsgLen = 0;
uint8_t in = 0;
uint16_t MsgPtr = 0;
uint8_t SeqDetect = 0;
bool SeqDetected = false;
while(true)
{
// Read from serial
if(!Interface->Receive(&in, 1, 1))
{
syslog(LOG_ERR, "Error Reading from Interface\n");
sleep(1);
continue;
}
// 0x808080 Detector
// Don't continue to the parsing if we don't detect the start sequence
// Also, reset the parsing if we detect a start sequence
if(in == 0x80)
SeqDetect++;
else
{
if(SeqDetect >= 3)
{
SeqDetected = true;
MsgPtr = 0;
}
SeqDetect = 0;
}
// Don't parse if there was no sequence detected
if(SeqDetected == false)
continue;
CurrentMessage[MsgPtr] = in;
// If this is the first byte, grab the length
if(MsgPtr == 0)
{
// If the length is too big, then it's probably an error. Reset
if(in > 0x85)
{
SeqDetected = false;
continue;
}
MsgLen = in;
}
// If the current pointer matches the length, go for processing
else if(MsgPtr == MsgLen + 5)
{
int i = 0;
uint8_t sum = 0;
while(i < MsgPtr-1)
{
sum += CurrentMessage[i];
i++;
}
if(sum == CurrentMessage[i])
{
if(debugFlag == true)
{
//syslog(LOG_INFO, "Message Received:\n");
//Log.hexDump(LOG_DEBUG, CurrentMessage, MsgPtr);
}
// Push the message back to the message queue
struct MsgStruct msg;
msg.DataLen = MsgPtr;
memcpy((void *)msg.Data, CurrentMessage, MsgPtr);
msg.TimeStamp = time(NULL);
msg.Iface = Interface;
pthread_mutex_lock(args.queueMutex);
args.MsgQueue->push_back(msg);
pthread_mutex_unlock(args.queueMutex);
}
// Reset and wait for new message
SeqDetected = false;
MsgPtr = 0;
continue;
}
// Continue with the next byte of the message
MsgPtr++;
}
return NULL;
}
int
CFroniusProbe::SendMessage(CInterface *interface, uint8_t Command, uint8_t networkNumber, uint8_t DeviceType, uint8_t *data, uint8_t datalen)
{
uint8_t message[256];
uint16_t ptr = 0;
// Message Sync. 0x808080
message[0] = message[1] = message[2] = 0x80;
ptr += 3;
// Length
message[ptr++] = datalen;
// Device/option - Hardcoded to Inverter
message[ptr++] = DeviceType;
// Network Number
message[ptr++] = networkNumber;
// Command
message[ptr++] = Command;
// data
if(data && datalen>0)
{
memcpy(&message[ptr], (void*) data, datalen);
ptr+= datalen;
}
// Checksum
message[ptr] = 0x0;
for(int i=3; i<ptr; i++)
{
message[ptr] += message[i];
}
ptr++;
if(debugFlag == true)
{
//syslog(LOG_INFO, "Sending msg:\n");
//Log.hexDump(LOG_DEBUG, message, ptr);
}
if(!interface->Send(message, ptr))
{
usleep(250000);
return false;
}
else
{
usleep(50000);
return true;
}
}