-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
156 lines (133 loc) · 3.55 KB
/
main.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
#include <unistd.h>
#include <signal.h>
#include "VantagePro.h"
#include "testWriter.h"
#include "testReader.h"
#include "wunderground.h"
#include "weatherbug.h"
#include "pwsweather.h"
#include "WeatherSink.h"
#include "WeatherSource.h"
#include "CWOP.h"
#include "Config.h"
#include <vector>
#include "Debug.h"
#include "httpserver.h"
#include "socket.h"
#include "Alarms.h"
#include <string.h>
using namespace std;
int running = true;
void SigHandler( int sigNum )
{
if ( sigNum == SIGTERM || sigNum == SIGINT )
{
running = false;
}
}
int main( int argc, char **argv )
{
char *configFile = NULL;
Debug dbg("main");
dbg.printf(EMERG, "WXPro DR1\n");
dbg.printf(EMERG, "Copyright (C) 2008 Travis E. Brown. All right reserved.\n");
dbg.printf(EMERG, "Initializing....\n");
dbg.printf(EMERG, "\n\n");
dbg.printf(EMERG, "---------------------------------------\n");
dbg.printf(EMERG, "WxPro DR1 Initilizing\n");
int c;
extern char *optarg;
int pid;
if (fork()) return 0;
pid = fork();
if ( pid )
{
dbg.printf(EMERG, "Backgrounding process. PID = %d\n", pid );
return 0;
}
while (( c= getopt( argc, argv, "c:")) != -1 )
{
switch (c)
{
case 'c':
configFile = optarg;
break;
}
}
if ( configFile == NULL )
{
configFile = strdup("wxpro.cfg");
}
dbg.printf(EMERG, "Reading config file: %s\n", configFile );
signal( SIGINT, SigHandler );
signal( SIGUSR1, SigHandler );
signal( SIGUSR2, SigHandler );
signal( SIGTERM, SigHandler );
signal( SIGPIPE, SigHandler );
Config cfg(configFile);
if ( cfg.getInteger("test_only") == 1 )
{
printf( "Test Only. No data will be sent to wunderground/CWOP\n");
dbg.printf(EMERG, "Test Only. No data will be sent to wunderground/CWOP\n");
}
WeatherData wd( cfg );
vector< WeatherSource * > sources;
vector< WeatherSink * > sinks;
if ( cfg.getInteger("test_only") == 1 )
{
sources.push_back( new TestReader(cfg ));
}
else
{
sources.push_back( new VantagePro(cfg ));
}
sinks.push_back( new Wunderground(cfg, &wd));
sinks.push_back( new WeatherBug(cfg, &wd));
sinks.push_back( new PWSWeather(cfg, &wd));
//sinks.push_back( new TestWriter(cfg, &wd));
sinks.push_back( new CWOP(cfg, &wd));
sinks.push_back( new HTTPServer(cfg, &wd));
sinks.push_back( new Socket(cfg, &wd));
sinks.push_back( new Alarms(cfg, &wd));
bool successfulRead = false;
while ( running )
{
successfulRead = false;
wd.now->updateTime();
for ( vector< WeatherSource *>::iterator iter = sources.begin();
iter!=sources.end();
++iter )
{
successfulRead |= (*iter)->read( &wd );
}
if ( successfulRead )
{
for ( vector< WeatherSink *>::iterator iter = sinks.begin();
iter!=sinks.end();
++iter )
{
(*iter)->newData( );
}
}
else
{
dbg.printf(CRIT, "No successful read. Skipping writes\n");
}
sleep(1);
}
// Delete all our sources and sinks
for ( vector< WeatherSource *>::iterator iter = sources.begin();
iter!=sources.end(); ++iter )
{
delete ( *iter );
}
sources.erase( sources.begin(), sources.end() );
for ( vector< WeatherSink *>::iterator iter = sinks.begin();
iter!=sinks.end(); ++iter )
{
delete ( *iter );
}
sinks.erase( sinks.begin(), sinks.end() );
//printf("Cleaning up...\n\r\n");
return 0;
}