forked from skumlos/ihcserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIHCServer.cpp
More file actions
310 lines (275 loc) · 9.74 KB
/
IHCServer.cpp
File metadata and controls
310 lines (275 loc) · 9.74 KB
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
#include "IHCServer.h"
#include "IHCInterface.h"
#include "utils/TCPSocketServer.h"
#include "IHCEvent.h"
#include "Configuration.h"
#include "Userlevel.h"
#include "IHCHTTPServer.h"
#include "IHCServerRequestWorker.h"
#include "IHCServerEventWorker.h"
#include <unistd.h>
#include <cstdlib>
#include "utils/printutil.h"
const std::string IHCServer::version = "0.3.1";
IHCServer* IHCServer::m_instance = NULL;
pthread_mutex_t IHCServer::m_instanceMutex = PTHREAD_MUTEX_INITIALIZER;
IHCServer* IHCServer::getInstance() {
pthread_mutex_lock(&m_instanceMutex);
if(m_instance == NULL) {
m_instance = new IHCServer();
}
pthread_mutex_unlock(&m_instanceMutex);
return m_instance;
};
IHCServer::IHCServer() :
m_alarmState(false)
{
printf("\nIHCServer v%s\nby Martin Hejnfelt\n\n",version.c_str());
pthread_cond_init(&m_eventCond,NULL);
pthread_mutex_init(&m_eventMutex,NULL);
// Initialize and load the configuration
m_configuration = Configuration::getInstance();
try {
printf("IHCServer: Loading configuration.\n");
m_configuration->load();
} catch (bool& ex) {
printf("IHCServer: Error in configuration, exitting... Edit config file manually.\n");
exit(1);
}
// Initialize userlevel subsystem
Userlevel::init();
// Create the interface and tcp socket servers
try {
m_ihcinterface = new IHCInterface(m_configuration->getSerialDevice());
} catch(bool& ex) {
printf("IHCServer: Could not instantiate IHC interface, check configuration...\n");
exit(1);
}
m_requestServer = new TCPSocketServer(m_requestServerPort,this);
m_eventServer = new TCPSocketServer(m_eventServerPort,this);
m_httpServer = IHCHTTPServer::getInstance();
// Attach to all IHC I/Os
std::list<IHCInput*> inputs = m_ihcinterface->getAllInputs();
std::list<IHCInput*>::iterator input = inputs.begin();
for(; input != inputs.end(); ++input) {
(*input)->attach(this);
}
std::list<IHCOutput*> outputs = m_ihcinterface->getAllOutputs();
std::list<IHCOutput*>::iterator output = outputs.begin();
for(; output != outputs.end(); ++output) {
(*output)->attach(this);
}
// Lets get running
start();
}
IHCServer::~IHCServer() {
stop();
std::list<IHCInput*> inputs = m_ihcinterface->getAllInputs();
std::list<IHCInput*>::iterator input = inputs.begin();
for(; input != inputs.end(); ++input) {
(*input)->detach(this);
}
std::list<IHCOutput*> outputs = m_ihcinterface->getAllOutputs();
std::list<IHCOutput*>::iterator output = outputs.begin();
for(; output != outputs.end(); ++output) {
(*output)->detach(this);
}
m_eventServer->stop();
m_requestServer->stop();
delete m_eventServer;
delete m_requestServer;
m_ihcinterface->stop();
delete m_ihcinterface;
pthread_mutex_destroy(&m_eventMutex);
pthread_cond_destroy(&m_eventCond);
}
IHCInterface* IHCServer::getIHCInterface() {
return m_ihcinterface;
}
bool IHCServer::getInputState(int moduleNumber, int inputNumber) {
bool ret = false;
IHCInput* inp = m_ihcinterface->getInput(moduleNumber,inputNumber);
if(inp != NULL) {
ret = inp->getState();
}
return ret;
}
bool IHCServer::getOutputState(int moduleNumber, int outputNumber) {
bool ret = false;
printf("getOutputState %d %d\n",moduleNumber,outputNumber);
IHCOutput* out = m_ihcinterface->getOutput(moduleNumber,outputNumber);
if(out != NULL) {
ret = out->getState();
}
printf("result: %d\n",ret);
return ret;
}
void IHCServer::setOutputState(int moduleNumber, int outputNumber, bool state) {
IHCOutput* output = m_ihcinterface->getOutput(moduleNumber,outputNumber);
if(output != NULL) {
m_ihcinterface->changeOutput(output,state);
}
return;
}
void IHCServer::activateInput(int moduleNumber, int inputNumber, bool shouldActivate) {
IHCInput* input = m_ihcinterface->getInput(moduleNumber,inputNumber);
if(input != NULL) {
m_ihcinterface->changeInput(input,shouldActivate);
}
return;
}
void IHCServer::thread() {
m_requestServer->start();
m_eventServer->start();
while(m_ihcinterface->isRunning()) {
pthread_mutex_lock(&m_eventMutex);
while(m_eventList.empty()) {
pthread_cond_wait(&m_eventCond,&m_eventMutex);
}
IHCEvent* event = m_eventList.front();
m_eventList.pop_front();
pthread_mutex_unlock(&m_eventMutex);
notify((void*)event);
delete event;
};
}
void IHCServer::update(Subject* sub, void* obj) {
bool changeAlarm = false;
bool newAlarmState = false;
bool isEntry = false;
pthread_mutex_lock(&m_eventMutex);
if(dynamic_cast<IHCOutput*>(sub) != 0) {
IHCEvent* event = new IHCEvent();
event->m_event = IHCServerDefs::OUTPUT_CHANGED;
event->m_io = (IHCOutput*)sub;
printTimeStamp();
printf("OUTPUT %d.%d changed to %s\n",event->m_io->getModuleNumber(),event->m_io->getIONumber(),event->m_io->getState()?"ON":"OFF");
if(m_configuration->getIOAlarm(IHCServerDefs::OUTPUTMODULE,event->m_io->getModuleNumber(),event->m_io->getIONumber())) {
changeAlarm = true;
newAlarmState = event->m_io->getState();
}
m_eventList.push_back(event);
if(m_configuration->getIOEntry(IHCServerDefs::OUTPUTMODULE,event->m_io->getModuleNumber(),event->m_io->getIONumber())) {
IHCEvent* entryEvent = new IHCEvent();
entryEvent->m_event = IHCServerDefs::ENTRY;
entryEvent->m_io = (IHCOutput*)sub;
m_eventList.push_back(entryEvent);
}
pthread_cond_signal(&m_eventCond);
} else if(dynamic_cast<IHCInput*>(sub) != 0) {
IHCEvent* event = new IHCEvent();
event->m_event = IHCServerDefs::INPUT_CHANGED;
event->m_io = (IHCInput*)sub;
printTimeStamp();
printf("INPUT %d.%d changed to %s\n",event->m_io->getModuleNumber(),event->m_io->getIONumber(),event->m_io->getState()?"ON":"OFF");
if(m_configuration->getIOAlarm(IHCServerDefs::INPUTMODULE,event->m_io->getModuleNumber(),event->m_io->getIONumber())) {
changeAlarm = true;
newAlarmState = event->m_io->getState();
}
m_eventList.push_back(event);
if(m_configuration->getIOEntry(IHCServerDefs::INPUTMODULE,event->m_io->getModuleNumber(),event->m_io->getIONumber())) {
IHCEvent* entryEvent = new IHCEvent();
entryEvent->m_event = IHCServerDefs::ENTRY;
entryEvent->m_io = (IHCOutput*)sub;
m_eventList.push_back(entryEvent);
}
pthread_cond_signal(&m_eventCond);
}
pthread_mutex_unlock(&m_eventMutex);
if(changeAlarm) setAlarmState(newAlarmState);
}
void IHCServer::socketConnected(TCPSocket* newSocket, TCPSocketServer* caller) {
if(caller == m_eventServer) {
new IHCServerEventWorker(newSocket);
} else if (caller == m_requestServer) {
new IHCServerRequestWorker(newSocket);
}
}
void IHCServer::toggleModuleState(enum IHCServerDefs::Type type, int moduleNumber) {
bool currentState = m_configuration->getModuleState(type,moduleNumber);
m_configuration->setModuleState(type,moduleNumber,!currentState);
return;
}
bool IHCServer::getModuleState(enum IHCServerDefs::Type type, int moduleNumber) {
return m_configuration->getModuleState(type,moduleNumber);
}
void IHCServer::setIODescription(enum IHCServerDefs::Type type, int moduleNumber, int ioNumber, std::string description) {
m_configuration->setIODescription(type,moduleNumber,ioNumber,description);
return;
}
std::string IHCServer::getIODescription(enum IHCServerDefs::Type type, int moduleNumber, int ioNumber) {
return m_configuration->getIODescription(type,moduleNumber,ioNumber);
}
void IHCServer::setIOProtected(enum IHCServerDefs::Type type, int moduleNumber, int ioNumber, bool isProtected) {
m_configuration->setIOProtected(type,moduleNumber,ioNumber,isProtected);
}
bool IHCServer::getIOProtected(enum IHCServerDefs::Type type, int moduleNumber, int ioNumber) {
return m_configuration->getIOProtected(type,moduleNumber,ioNumber);
}
void IHCServer::setIOAlarm(enum IHCServerDefs::Type type, int moduleNumber, int ioNumber, bool isAlarm) {
m_configuration->setIOAlarm(type,moduleNumber,ioNumber,isAlarm);
}
bool IHCServer::getIOAlarm(enum IHCServerDefs::Type type, int moduleNumber, int ioNumber) {
return m_configuration->getIOAlarm(type,moduleNumber,ioNumber);
}
void IHCServer::setIOEntry(enum IHCServerDefs::Type type, int moduleNumber, int ioNumber, bool isEntry) {
m_configuration->setIOEntry(type,moduleNumber,ioNumber,isEntry);
}
bool IHCServer::getIOEntry(enum IHCServerDefs::Type type, int moduleNumber, int ioNumber) {
return m_configuration->getIOEntry(type,moduleNumber,ioNumber);
}
bool IHCServer::getAlarmState() {
bool ret = false;
for(unsigned int j = 1; j<=8; j++) {
for(unsigned int k = 1; k<=16; k++) {
if(m_configuration->getIOAlarm(IHCServerDefs::INPUTMODULE,j,k) &&
m_ihcinterface->getInput(j,k)->getState()) {
ret = true;
break;
}
}
}
if(!ret) {
for(unsigned int j = 1; j<=16; j++) {
for(unsigned int k=1; k<=8; k++) {
if(m_configuration->getIOAlarm(IHCServerDefs::OUTPUTMODULE,j,k) &&
m_ihcinterface->getOutput(j,k)->getState()) {
ret = true;
break;
}
}
}
}
return ret;
}
void IHCServer::setAlarmState(bool alarmState) {
bool oldState = m_alarmState;
m_alarmState = alarmState;
if(m_alarmState != oldState) {
printf("IHCServer: New AlarmState %s, old alarmstate %s\n",(m_alarmState ? "TRUE" : "FALSE"), (oldState ? "TRUE" : "FALSE"));
IHCEvent* event = new IHCEvent();
event->m_event = m_alarmState ? IHCServerDefs::ALARM_ARMED : IHCServerDefs::ALARM_DISARMED;
/* for(unsigned int j = 1; j<=8; j++) {
for(unsigned int k = 1; k<=16; k++) {
if(m_configuration->getIOAlarm(IHCServerDefs::INPUTMODULE,j,k)) {
IHCInput* input = m_ihcinterface->getInput(j,k);
}
}
}*/
for(unsigned int j = 1; j<=16; j++) {
for(unsigned int k = 1; k<=8; k++) {
if(m_configuration->getIOAlarm(IHCServerDefs::OUTPUTMODULE,j,k)) {
IHCOutput* output = m_ihcinterface->getOutput(j,k);
m_ihcinterface->changeOutput(output,m_alarmState);
}
}
}
pthread_mutex_lock(&m_eventMutex);
m_eventList.push_back(event);
pthread_cond_signal(&m_eventCond);
pthread_mutex_unlock(&m_eventMutex);
}
}
void IHCServer::saveConfiguration() {
m_configuration->save();
}