-
Notifications
You must be signed in to change notification settings - Fork 1
/
fpd.cpp
420 lines (339 loc) · 9.38 KB
/
fpd.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
/*
* 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 "fpd.h"
#include "CLog.h"
#include "CSettings.h"
// Output modules
#include "CMysqlDb.h"
#include "CXMLDb.h"
// Interface modules
#include "CSerial.h"
#include "CSocket.h"
#include "CBlueTooth.h"
// Parser modules
#include "CSolutronicProbe.h"
#include "CSmaProbe.h"
#include "CFroniusProbe.h"
#include "CSunergyProbe.h"
#include <signal.h>
CLog Log;
bool debugFlag = false;
bool SigTermFlag = false;
void* RestoreThread(void *ptr);
pthread_t RestoringThread;
pthread_t DbConnectThread;
bool RestoringFinished;
string stripPathFromArgv(char *argv);
void handler(int value);
struct RestoreThreadStruct
{
COutput *from;
COutput *to;
};
void handler(int value)
{
syslog(LOG_INFO, "caught SIGTERM, shutting down operations");
SigTermFlag = true;
}
COutput* StartOutput(OutputContainer *outp)
{
COutput *out = 0;
if(!outp->type.compare("mysql"))
{
CMysqlDb *Mysql = new CMysqlDb(outp->settings);
out = dynamic_cast<COutput *> (Mysql);
}
else if(!outp->type.compare("xml"))
{
CXMLDb *xml = new CXMLDb(outp->settings);
out = dynamic_cast<COutput *> (xml);
}
else
{
syslog(LOG_ERR, "Unsupported output requested\n");
}
return out;
}
CInterface* StartInterface(InputContainer *inp)
{
CInterface *iface = 0;
if(!inp->iface.compare("serial"))
{
CSerial *Serial = new CSerial(inp->ifaceSettings);
iface = dynamic_cast<CInterface *> (Serial);
}
else if(!inp->iface.compare("socket"))
{
CSocket *sock = new CSocket(inp->ifaceSettings);
iface = dynamic_cast<CInterface *> (sock);
}
else if(!inp->iface.compare("bluetooth"))
{
CBlueTooth *bt = new CBlueTooth(inp->ifaceSettings);
iface = dynamic_cast<CInterface *> (bt);
}
else
Log.error("Unsupported Interface requested\n");
return iface;
}
CProbe* StartProbe(InputContainer *inp, CInterface* iface)
{
CProbe *Probe = 0;
if(!inp->type.compare("fronius"))
{
CFroniusProbe *FProbe = new CFroniusProbe(iface, inp->uuid);
Probe = dynamic_cast<CProbe *> (FProbe);
}
else if(!inp->type.compare("sunergy"))
{
//CSunergyProbe *SProbe = new CSunergyProbe(iface, CurInput->uuid);
//Probe = dynamic_cast<CProbe *> (SProbe);
}
else if(!inp->type.compare("sma"))
{
CSmaProbe *SProbe = new CSmaProbe(iface, inp->uuid);
Probe = dynamic_cast<CProbe *> (SProbe);
}
else if(!inp->type.compare("solutronic"))
{
CSolutronicProbe *SProbe = new CSolutronicProbe(iface, inp->sensors, inp->uuid);
Probe = dynamic_cast<CProbe *> (SProbe);
}
else
Log.error("Unsupported Probe requested\n");
return Probe;
}
int main(int argc, char *argv[])
{
pid_t sid;
struct sigaction new_action;
int c;
opterr = 0;
while ((c = getopt (argc, argv, "d")) != -1)
{
if(c == 'd')
debugFlag = true;
}
Log.Init();
Log.log("SolarSpy Probe Daemon %d.%d Starting...", MAJOR_VERSION, MINOR_VERSION);
// Daemonize!
if(debugFlag == false)
{
// fork the process into the background
int rv = fork();
if(rv < 0)
return -1;
else if(rv > 0)
return 0;
umask(0);
// Get a new group ID
sid = setsid();
if (sid < 0)
{
Log.error("Cannot set SID");
return -1;
}
// Close all contact with the world
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
// Register signal handlers for proper termination of the daemon - sigterm & ctrl-C
new_action.sa_handler = handler;
sigemptyset (&new_action.sa_mask);
new_action.sa_flags = 0;
if( sigaction (SIGINT, &new_action, NULL) == -1 || sigaction (SIGTERM, &new_action, NULL) == -1)
{
Log.error("Failed to register Signal Handlers, Exiting");
return(EXIT_FAILURE);
}
// Now start the actual application
CSettings Settings;
if(!Settings.LoadFile("settings.xml"))
{
Log.error("Error reading settings\n");
return(EXIT_FAILURE);
}
Log.log("Loaded Settings file\n");
list<CProbe*> Inputs;
map<string, COutput*> Outputs;
// Parse and start the outputs
for(list<OutputContainer>::iterator CurOutput=Settings.OutputsDB.begin();CurOutput!=Settings.OutputsDB.end(); ++CurOutput)
{
COutput *out = StartOutput(&(*CurOutput));
if(!out)
{
Log.error("Failed to start output [%s]\n", CurOutput->name.c_str());
continue;
}
if(!out->Connect())
{
Log.error("Failed to connect to output [%s]\n", CurOutput->name.c_str());
delete out;
continue;
}
Outputs[CurOutput->name] = out;
}
// Parse and start the inputs
for(list<InputContainer>::iterator CurInput=Settings.InputsDB.begin();CurInput!=Settings.InputsDB.end(); ++CurInput)
{
// Start The interface first
CInterface *iface = StartInterface(&(*CurInput));
if(!iface)
{
Log.error("Failed to initialize interface [%s]\n", CurInput->uuid.c_str());
continue;
}
if(!iface->Connect())
{
Log.error("Failed to connect to interface [%s]\n", CurInput->uuid.c_str());
// Don't stop the interface. Let it try to reconnect byitself later
//delete iface;
//continue;
}
// And pass it to the probe
CProbe *Probe = StartProbe(&(*CurInput),iface);
if(!Probe || !Probe->Start())
{
Log.error("Failed to start probe [%s]\n", CurInput->uuid.c_str());
if(Probe)
delete Probe;
delete iface;
continue;
}
Probe->output = (*CurInput).output;
Probe->cache = (*CurInput).cache;
Log.log("main %s - second %s\n", (*CurInput).output.c_str(), (*CurInput).cache.c_str());
Inputs.push_back(Probe);
}
if(Inputs.empty())
{
Log.error("No running inputs. Exiting\n");
for (map<string, COutput*>::iterator obj = Outputs.begin(); obj != Outputs.end(); obj++)
delete obj->second;
return -1;
}
if(Outputs.empty())
{
Log.error("No running outputs. Exiting\n");
for (list<CProbe*>::iterator obj = Inputs.begin(); obj != Inputs.end(); obj++)
delete *(obj);
return -1;
}
// By now, we have:
// opened the interfaces
// inited and started the parsers
// inited the output modules.
// Now we have to handle the message flow from input to output in standard intervals as defined in the settings
Log.log("Solarspy Daemon initialized. Initializing operations...\n");
RestoringFinished = true;
while(true)
{
struct tm *ptr;
time_t TimeStamp;
TimeStamp = time(NULL);
ptr = localtime((const time_t *) &TimeStamp);
if(ptr->tm_sec == 0)
{
for(list<CProbe*>::iterator CurProbe = Inputs.begin(); CurProbe != Inputs.end(); ++CurProbe)
{
list<int> Sensors = (*CurProbe)->GetConnectedInverters();
for(list<int>::iterator CurSensor = Sensors.begin();CurSensor != Sensors.end(); ++CurSensor)
{
DataContainer Results;
Results["TimeStamp"] = int2string(TimeStamp);
Results["inverter"] = int2string(*CurSensor);
if((*CurProbe)->GetAverage(Results) == false)
{
Log.warn("Non responsive Inverter %s\n", Results["inverter"].c_str());
Results["status"] = "5";
}
// Start running through the list of outputs until the insert suceeds
string CurrOutput = (*CurProbe)->output;
if(Outputs[CurrOutput]->Insert(Results) != true)
{
Log.warn("Unable to output data to %s\n", CurrOutput.c_str());
CurrOutput = (*CurProbe)->cache;
if(Outputs[CurrOutput]->Insert(Results) != true)
Log.warn("Unable to output data to cache %s\n", CurrOutput.c_str());
}
else if(RestoringFinished == true)
{
if(RestoringThread)
{
pthread_join(RestoringThread, NULL);
RestoringThread = 0;
}
RestoreThreadStruct ThreadStruct;
CurrOutput = (*CurProbe)->cache;
ThreadStruct.from = Outputs[CurrOutput];
CurrOutput = (*CurProbe)->output;
ThreadStruct.to = Outputs[CurrOutput];
pthread_create( &RestoringThread, NULL, &RestoreThread, &ThreadStruct );
}
}
}
// Wait for the second to finish so that we process only one time
do
{
TimeStamp = time(NULL);
ptr = localtime((const time_t *) &TimeStamp);
if(ptr->tm_sec == 0)
usleep(100000);
} while(ptr->tm_sec == 0);
}
usleep(30000);
if(SigTermFlag == true)
break;
}
// We are done. Delete all the objects in order to run their destructors
for (list<CProbe*>::iterator obj = Inputs.begin(); obj != Inputs.end(); obj++)
delete *(obj);
for (map<string, COutput*>::iterator obj = Outputs.begin(); obj != Outputs.end(); obj++)
delete obj->second;
return 0;
}
void* RestoreThread(void *ptr)
{
RestoringFinished = false;
RestoreThreadStruct *ThreadStruct = (RestoreThreadStruct *) ptr;
while(true)
{
DataContainerList Data2Restore;
if(ThreadStruct->from->Restore(Data2Restore, 50) == false)
{
Log.error("Error Restoring from output\n");
break;
}
if(Data2Restore.size() == 0)
break;
if(ThreadStruct->to->Insert(Data2Restore) == false)
{
Log.error("Error Inserting data to database\n");
break;
}
}
RestoringFinished = true;
pthread_exit(NULL);
return NULL;
}
string stripPathFromArgv(char *argv)
{
char *lastSlash, *Slash;
char path[256];
Slash = argv;
do
{
lastSlash = Slash;
Slash = strstr(lastSlash+1, "/");
} while(Slash);
strncpy(path, argv, lastSlash-argv);
path[lastSlash-argv+1] = 0x0;
return string(path);
}