#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #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"; Load (); }; Server::~Server() { if (IsChanged()) Save(); }; void Server::ThreadProcess() { int i = 0; while (running) { interfaces.Loop(); turnouts.Loop(); 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::ModeAuto() { debug (0, "%s:%d * Mode Auto", __FILE__, __LINE__); status_text = "Mode Auto"; } // // Set Mode Manual void Server::ModeManual() { debug (0, "%s:%d * Mode Manual", __FILE__, __LINE__); status_text = "Mode Manual"; } // // Set Mode Error // FIXME: maybe adding error text as parameter? void Server::ModeError(string text) { debug (0, "%s:%d * Mode Error :'%s'", __FILE__, __LINE__, text.c_str()); status_text = "Error:'" + text + "'"; } // // Set Mode Reset void Server::ModeReset() { debug (0, "%s:%d * Reset Data", __FILE__, __LINE__); status_text = "Mode Reset"; }; string Server::GetStatus(void) { string retval; LockThread(); retval = status_text; UnLockThread(); return retval; };