You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
3.3 KiB
177 lines
3.3 KiB
|
|
#include <string>
|
|
#include <sys/time.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <netdb.h>
|
|
#include <errno.h>
|
|
#include <math.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/wait.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <syslog.h>
|
|
#include <pthread.h>
|
|
#include <sys/syscall.h>
|
|
|
|
|
|
#include "modelbahn.h"
|
|
#include "server.h"
|
|
|
|
int Server::Start() {
|
|
int err;
|
|
pthread_attr_t attr;
|
|
mtx = { 0 };
|
|
mtx = PTHREAD_MUTEX_INITIALIZER;
|
|
thread_running = 1;
|
|
pthread_attr_init (&attr);
|
|
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE);
|
|
err = pthread_create (&thread, &attr, ThreadEntry, this);
|
|
if (err != 0) {
|
|
debug (DEBUG_ERROR, (char*)"%s(%s:%d) pthread_create errror: %s", __FUNCTION__, __FILE__, __LINE__, strerror (errno));
|
|
running = 0;
|
|
return 0;
|
|
}
|
|
return 1;
|
|
};
|
|
|
|
|
|
Server::Server() {
|
|
thread = 0;
|
|
thread_running = 0;
|
|
railways.SetSize(200, 200);
|
|
status_text = "init server";
|
|
mode = SMODE_STARTUP;
|
|
data_reset.mr_idx = -1;
|
|
data_reset.mr_step = SMRESET_STEP_INIT;
|
|
gettimeofday(&data_reset.mr_timestamp, NULL);
|
|
|
|
Load ();
|
|
};
|
|
|
|
|
|
Server::~Server() {
|
|
if (IsChanged()) Save();
|
|
};
|
|
|
|
|
|
void Server::ThreadProcess() {
|
|
int i = 0;
|
|
while (running) {
|
|
interfaces.Loop();
|
|
turnouts.Loop();
|
|
|
|
debug (0, "%s:%d mode:%d data_reset.step:%d data_reset.idx:%d", __FILE__, __LINE__, mode, data_reset.mr_step, data_reset.mr_idx);
|
|
|
|
//
|
|
// startup process
|
|
if (mode == SMODE_STARTUP) {
|
|
SetModeReset();
|
|
}
|
|
|
|
//
|
|
// reset all internal data
|
|
else if (mode == SMODE_RESET) {
|
|
SetModeManual();
|
|
}
|
|
|
|
//
|
|
// mode manual
|
|
else if (mode == SMODE_MANUAL) {
|
|
}
|
|
|
|
//
|
|
// mode auto
|
|
else if (mode == SMODE_AUTO) {
|
|
}
|
|
|
|
usleep (25000);
|
|
}
|
|
debug (0, "Server::ThreadProcess Finished");
|
|
thread_running = 0;
|
|
};
|
|
|
|
|
|
void Server::LockThread() {
|
|
pthread_mutex_lock (&mtx);
|
|
};
|
|
|
|
|
|
void Server::UnLockThread() {
|
|
pthread_mutex_unlock (&mtx);
|
|
};
|
|
|
|
|
|
//
|
|
// return JSONObject with all data
|
|
void Server::GetJSONAll(JSONParse *json) {
|
|
debug (DEBUG_INFO, "* Track::GetJSONAll data");
|
|
if (json == NULL) return;
|
|
|
|
railways.GetJSONAll(json);
|
|
interfaces.GetJSONAll(json);
|
|
sensors.GetJSONAll(json);
|
|
locomotives.GetJSONAll(json);
|
|
turnouts.GetJSONAll(json);
|
|
}
|
|
|
|
|
|
bool Server::IsChanged() {
|
|
if (railways.IsChanged() ||
|
|
interfaces.IsChanged() ||
|
|
locomotives.IsChanged() ||
|
|
sensors.IsChanged() ||
|
|
turnouts.IsChanged()) return true;
|
|
else return false;
|
|
}
|
|
|
|
|
|
//
|
|
// Set Mode Auto
|
|
void Server::SetModeAuto() {
|
|
debug (0, "%s:%d * Mode Auto", __FILE__, __LINE__);
|
|
status_text = "Mode Auto";
|
|
}
|
|
|
|
//
|
|
// Set Mode Manual
|
|
void Server::SetModeManual() {
|
|
debug (0, "%s:%d * Mode Manual", __FILE__, __LINE__);
|
|
status_text = "Mode Manual";
|
|
}
|
|
|
|
//
|
|
// Set Mode Error
|
|
// FIXME: maybe adding error text as parameter?
|
|
void Server::SetModeError(string text) {
|
|
debug (0, "%s:%d * Mode Error :'%s'", __FILE__, __LINE__, text.c_str());
|
|
status_text = "Error:'" + text + "'";
|
|
}
|
|
|
|
//
|
|
// Set Mode Reset
|
|
void Server::SetModeReset() {
|
|
debug (0, "%s:%d * Reset Data", __FILE__, __LINE__);
|
|
status_text = "Mode Reset";
|
|
|
|
mode = SMODE_RESET;
|
|
data_reset.mr_step = SMRESET_STEP_INIT;
|
|
data_reset.mr_idx = -1;
|
|
gettimeofday(&data_reset.mr_timestamp, NULL);
|
|
};
|
|
|
|
|
|
string Server::GetStatus(void) {
|
|
string retval;
|
|
|
|
LockThread();
|
|
retval = status_text;
|
|
UnLockThread();
|
|
|
|
return retval;
|
|
};
|