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.
136 lines
4.3 KiB
136 lines
4.3 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 "interface.h"
|
|
|
|
class Server {
|
|
private:
|
|
pthread_mutex_t mtx;
|
|
pthread_t thread;
|
|
int thread_running;
|
|
Turnouts turnouts;
|
|
Sensors sensors;
|
|
Railways railways;
|
|
Locomotives locomotives;
|
|
Interfaces interfaces;
|
|
|
|
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;
|
|
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(); };
|
|
|
|
/////////////////////////////////////////
|
|
// 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) { return turnouts.Set(name, 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 LocomotiveSetFunction(string name, int func, int value) { return locomotives.SetFunction(name, func, value); };
|
|
|
|
/////////////////////////////////////////
|
|
// 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); };
|
|
|
|
void GetJSONAll(JSONParse *json);
|
|
|
|
/////////////////////////////////////////
|
|
// reports from interfaces
|
|
int LocomotiveAddrSpeed(string name, int addr, int speed) { return locomotives.SetSpeedFromBus(name, addr, speed); };
|
|
int LocomotiveAddrDirection(string name, int addr, int reverse) { return locomotives.SetDirectionFromBus(name, addr, reverse); };
|
|
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; };
|
|
|
|
|
|
//
|
|
// Load Save Part
|
|
int Load();
|
|
int Save();
|
|
|
|
protected:
|
|
static void *ThreadEntry (void *This) { ((Server*)This)->ThreadProcess(); return NULL;};
|
|
};
|
|
|
|
#endif // _SERVER_H_
|