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.
Modelbahn/server/server.h

204 lines
6.7 KiB

#ifndef _SERVER_H_
#define _SERVER_H_
#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 "json.h"
#include "modelbahn.h"
#include "turnout.h"
#include "railway.h"
#include "locomotive.h"
#include "sensor.h"
#include "interfaces.h"
#include "block.h"
enum SMODE {
SMODE_STARTUP,
SMODE_RESET,
SMODE_MANUAL,
SMODE_AUTO,
SMODE_MAX
};
enum SMRESET {
SMRESET_STEP_INIT = 0,
SMRESET_STEP_LOCKS,
SMRESET_STEP_LOCOMOTIVES,
SMRESET_STEP_TURNOUTS,
SMRESET_STEP_SENSORS,
SMRESET_STEP_INTERFACES,
SMRESET_STEP_MAX
};
struct s_ModeResetData{
int mr_step; // current step
struct timeval mr_timestamp; // timestamp ( needed for times )
int mr_idx; // mode index
string mr_lastelm; // last element
} typedef ModeResetData;
class Server {
private:
int mode; // holds the current mode
int mode_progress; // to return a value from 0-100
string status_text; // some text
ModeResetData data_reset;
pthread_mutex_t mtx;
pthread_t thread;
int thread_running;
Turnouts turnouts;
Sensors sensors;
Railways railways;
Locomotives locomotives;
Interfaces interfaces;
Blocks blocks;
void ThreadProcess();
void LoopCheckChanges();
bool IsChanged();
int Load(string fn);
int Save(string fn);
friend class Interfaces;
friend class InterfacesZ21;
friend class Locomotives;
friend class Sensors;
friend class Turnouts;
friend class Blocks;
friend class Railways;
public:
/////////////////////////////////////////
// functions here are required to be thread save
Server();
~Server();
int Start();
int isRunning() { return thread_running; }
void LockThread();
void UnLockThread();
//////////////////////////////////////////////
// General Commands, for controlling trains,
// turnouts and sensors
//
void PowerOnOff(int onoff) { interfaces.PowerOnOff(onoff); };
int GetHeight() { return railways.GetHeight(); };
int GetWidth() { return railways.GetWidth(); };
//
// Load Save Part
int Load();
int Save();
//
// Reset dynamic data
JSONParse GetJSONServerStatus();
void SetModeReset(); // mode Reset if finished will go to Manual
void SetModeManual(); // Manual Mode
void SetModeAuto(); // only allowed if Manual was set
void SetModeError(string text); // will cut power and keep this mode, until reset or Mode Manual
void CycleModeReset(); // one Cycle in mode reset
string GetStatus(); // return status
/////////////////////////////////////////
// Railway
int RailwayChange(Railway *rw) { return railways.Change(rw);};
JSONParse RailwayGetJSONTrack() { return railways.GetJSONTrack(); };
JSONParse RailwayGetJSONRailway (int x, int y) { return railways.GetJSONRailway(x, y); };
Railway GetRailwayFromJSON(JSONParse *j) { return railways.GetRailwayFromJSON(j); };
/////////////////////////////////////////
// Turnout
int TurnoutChange(Turnout *t) { return turnouts.Change(t); };
Turnout TurnoutFromJSON(JSONParse *j) { return turnouts.GetTurnoutFromJSON(j); };
JSONParse TurnoutGetJSON(string name) { return turnouts.GetJSON(name); };
int TurnoutDelete(string name) { return turnouts.Delete(name); };
int TurnoutSet(string name, int active);
/////////////////////////////////////////
// Interface
int InterfaceChange(Interface *i) { return interfaces.Change(i); };
Interface InterfaceFromJSON(JSONParse *j) { return interfaces.GetInterfaceFromJSON(j); };
JSONParse InterfaceGetJSON(string name) { return interfaces.GetJSON(name); };
int InterfaceDelete(string name) { return interfaces.Delete(name); };
/////////////////////////////////////////
// Locomotive
int LocomotiveChange(Locomotive *l) { return locomotives.Change(l); };
Locomotive LocomotiveFromJSON(JSONParse *j) { return locomotives.GetLocomotiveFromJSON(j); };
JSONParse LocomotiveGetJSON(string name) { return locomotives.GetJSON(name); };
int LocomotiveDelete(string name) { return locomotives.Delete(name); };
int LocomotiveSetSpeed(string name, int speed) { return locomotives.SetSpeed(name, speed); };
int LocomotiveSetReverse(string name, int reverse) { return locomotives.SetReverse(name, reverse); };
int LocomotiveSetFunction(string name, int func, int value) { return locomotives.SetFunction(name, func, value); };
int LocomotiveSetDest(string name, string block, int direction) { return locomotives.SetDestination(name, block, direction); };
int LocomotiveSetAssign(string name, string block, int direction) { return locomotives.SetAssign(name, block, direction); };
int LocomotiveReset(string name) { return locomotives.Reset(name); };
int LocomotiveSetMan(string name) { return locomotives.SetModeMan(name); };
int LocomotiveSetAuto(string name) { return locomotives.SetModeAuto(name); };
int LocomotiveSetAutoMan(string name) { return locomotives.SetModeAutoMan(name); };
int LocomotiveSetAutoRand(string name) { return locomotives.SetModeAutoRand(name); };
int LocomotiveSetAutoShed(string name) { return locomotives.SetModeAutoShed(name); };
/////////////////////////////////////////
// Sensor
int SensorChange(Sensor *s) { return sensors.Change(s); };
Sensor SensorFromJSON(JSONParse *j) { return sensors.GetSensorFromJSON(j); };
JSONParse SensorGetJSON(string name) { return sensors.GetJSON(name); };
int SensorDelete(string name) { return sensors.Delete(name); };
int SensorSetActive(string name, int value) { return sensors.SetActive(name, value); };
/////////////////////////////////////////
// Blocks
int BlockChange(Block *s) { return blocks.Change(s); };
Block BlockFromJSON(JSONParse *j) { return blocks.GetBlockFromJSON(j); };
JSONParse BlockGetJSON(string name) { return blocks.GetJSON(name); };
int BlockDelete(string name) { return blocks.Delete(name); };
int BlockSetOff(string name) { return blocks.SetOff(name); };
int BlockClear(string name) { return blocks.Clear(name); };
void GetJSONAll(JSONParse *json);
/////////////////////////////////////////
// reports from interfaces
int LocomotiveAddrSpeed(string name, int addr, int speed) { return locomotives.SetSpeedFromBus(name, addr, speed); };
int LocomotiveAddrFunction(string name, int addr, int func) { return locomotives.SetFunctionFromBus(name, addr, func); };
int TurnoutAddrMode(string name, int addr, int active) { turnouts.SetFromBus(name, addr, active); return 1; };
int SensorAddrChange(string name, int addr, int active) { sensors.SetFromBus(name, addr, active); return 1; };
protected:
static void *ThreadEntry (void *This) { ((Server*)This)->ThreadProcess(); return NULL;};
};
#endif // _SERVER_H_