diff --git a/server/interface-z21.cc b/server/interface-z21.cc index 5e81594..c32c0b8 100644 --- a/server/interface-z21.cc +++ b/server/interface-z21.cc @@ -213,7 +213,7 @@ int InterfaceZ21::Loop(string interfacename) { loconet_map[inloc].addr = loconet_last.addr; } - debug (0, "%s:%d Loc:%d Speed:%d\n", __FILE__, __LINE__, loconet_map[inloc].addr, inspeed); + debug (0, "%s:%d Loc:%d Speed:%d", __FILE__, __LINE__, loconet_map[inloc].addr, inspeed); if (loconet_map[inloc].addr > 0) { int speed = inspeed; server->LocomotiveAddrSpeed(interfacename, loconet_map[inloc].addr, speed); @@ -230,8 +230,14 @@ int InterfaceZ21::Loop(string interfacename) { if (inloc >= 0 && inloc <= INTF_Z21_LOCONET_MAXADDR) { if (loconet_last.addr) loconet_map[inloc] = loconet_last; - debug (0, "%s:%d Loc:%d Function:%d\n", __FILE__, __LINE__, loconet_map[inloc].addr, infunc); + debug (0, "%s:%d Loc:%d Function:%d", __FILE__, __LINE__, loconet_map[inloc].addr, infunc); } + + debug (0, "%s:%d Loc:%d Function:%d", __FILE__, __LINE__, loconet_map[inloc].addr, infunc); + if (loconet_map[inloc].addr > 0) { + server->LocomotiveAddrFunction(interfacename, loconet_map[inloc].addr, infunc); + } + loconet_last.addr = 0; } } @@ -341,8 +347,8 @@ void InterfaceZ21::SetLocoSpeed(Locomotive *l, int step) { else if (step > 0) { l->flags &= ~LOCO_F_REVERSE; } - if (l->flags & LOCO_F_REVERSE) buffer[Z21_IDX_SETLOCO_DRIVE_STEP] = 0x80; - else buffer[Z21_IDX_SETLOCO_DRIVE_STEP] = 0x00; + if (l->flags & LOCO_F_REVERSE) buffer[Z21_IDX_SETLOCO_DRIVE_STEP] = 0x00; + else buffer[Z21_IDX_SETLOCO_DRIVE_STEP] = 0x80; // // setup loc addr diff --git a/server/interface.cc b/server/interface.cc index b7f82b1..b2cdf7b 100644 --- a/server/interface.cc +++ b/server/interface.cc @@ -341,9 +341,12 @@ void Interfaces::SetLocoSpeed(Locomotive *l, int speed) { int i; int step = 0; + // TEST + // (l->steps-1) ????? + if (abs(speed) < l->vmin) step = 0; - if (abs(speed) >= l->vmin) step = (speed * (l->steps-1)) / l->vmax; - if (abs(speed) > l->vmax) step = l->steps-1; + if (abs(speed) >= l->vmin) step = (speed * (l->steps)) / l->vmax; + if (abs(speed) > l->vmax) step = l->steps; l->speed = speed; diff --git a/server/locomotive.cc b/server/locomotive.cc index 93be245..4d7a11d 100644 --- a/server/locomotive.cc +++ b/server/locomotive.cc @@ -206,6 +206,28 @@ int Locomotives::SetSpeed(string name, int speed) { }; +int Locomotives::SetReverse(string name, int reverse) { + int i; + + Lock(); + for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) { + if (name.compare(locomotives[i].name) == 0) { + if (reverse) + locomotives[i].flags |= LOCO_F_REVERSE; + else + locomotives[i].flags &= ~LOCO_F_REVERSE; + + server->interfaces.SetLocoSpeed(&locomotives[i], locomotives[i].speed); + break; + } + } + + UnLock(); + + return 1; +}; + + int Locomotives::SetFunction(string name, int func, int value) { int i; @@ -227,13 +249,21 @@ int Locomotives::SetFunction(string name, int func, int value) { // // set values from bus... // -int Locomotives::SetSpeedFromBus(string name, int addr, int speed) { +int Locomotives::SetSpeedFromBus(string ifname, int addr, int speed) { int i; JSONParse jp; + debug (0, "%s:%d SetSpeedFromBus IfName:%s Addr:%d Speed:%d", __FILE__, __LINE__, ifname.c_str(), addr, speed); + for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) { - if (name.compare(locomotives[i].name) == 0 && locomotives[i].addr == addr) { - locomotives[i].speed = speed; + if (ifname.compare(locomotives[i].ifname) == 0 && locomotives[i].addr == addr) { + if (speed == 0) locomotives[i].speed = 0; + if (speed >= (127-((127/locomotives[i].steps)+1))) locomotives[i].speed = locomotives[i].vmax; + else { + printf ("\n\n Speed:%d test:%d", speed, (127-((127/locomotives[i].steps)+1))); + locomotives[i].speed = (1+speed) * locomotives[i].vmax / 127; + if (locomotives[i].speed > locomotives[i].vmax) locomotives[i].speed = locomotives[i].vmax; + } jp.AddObject("locomotive",_GetJSON(i)); if(network) network->ChangeListPushToAll(jp.ToString()); @@ -245,13 +275,14 @@ int Locomotives::SetSpeedFromBus(string name, int addr, int speed) { }; -int Locomotives::SetDirectionFromBus(string name, int addr, int reverse) { +int Locomotives::SetFunctionFromBus(string ifname, int addr, int func) { int i; JSONParse jp; + debug (0, "%s:%d SetDirectionFromBus IfName:%s Addr:%d function:%d", __FILE__, __LINE__, ifname.c_str(), addr, func); for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) { - if (name.compare(locomotives[i].name) == 0 && locomotives[i].addr == addr) { - if (reverse) locomotives[i].flags |= LOCO_F_REVERSE; + if (ifname.compare(locomotives[i].ifname) == 0 && locomotives[i].addr == addr) { + if (func & 32) locomotives[i].flags |= LOCO_F_REVERSE; else locomotives[i].flags &= ~LOCO_F_REVERSE; jp.AddObject("locomotive",_GetJSON(i)); diff --git a/server/locomotive.h b/server/locomotive.h index aa9d436..c8c926e 100644 --- a/server/locomotive.h +++ b/server/locomotive.h @@ -45,10 +45,11 @@ class Locomotives { int Change(Locomotive *loco); int Delete(string name); int SetSpeed(string name, int speed); + int SetReverse(string name, int reverse); int SetFunction(string name, int func, int value); - int SetSpeedFromBus (string name, int addr, int speed); - int SetDirectionFromBus (string name, int addr, int speed); + int SetSpeedFromBus (string ifname, int addr, int speed); + int SetFunctionFromBus (string ifname, int addr, int func); JSONParse GetJSON(string name); void GetJSONAll(JSONParse *json); diff --git a/server/modelbahn.h b/server/modelbahn.h index 2fdbd48..8c1f8e7 100644 --- a/server/modelbahn.h +++ b/server/modelbahn.h @@ -6,7 +6,7 @@ #define BUFFERSIZE 64000 #define MAXWAITTIME 500 #define UNIX_SOCKET_FILE "/tmp/modelbahn.socket" -#define DEFAULT_DATADIR "/home/steffen/Dokumente/Programmierung/Modelbahn/" +#define DEFAULT_DATADIR "/home/steffen/Dokumente/Programmierung/modelbahn/" #define SESSIONS_MAX 8 #define REFERENCENAME_LEN 128 diff --git a/server/server.cc b/server/server.cc index 2447f7e..0b5d258 100644 --- a/server/server.cc +++ b/server/server.cc @@ -22,7 +22,6 @@ #include "modelbahn.h" #include "server.h" - int Server::Start() { int err; pthread_attr_t attr; diff --git a/server/server.h b/server/server.h index 1301d54..2b829a6 100644 --- a/server/server.h +++ b/server/server.h @@ -29,8 +29,40 @@ #include "sensor.h" #include "interface.h" +enum { + SSTATUS_STARTUP, + SSTATUS_RESET, + SSTATUS_MANUAL, + SSTATUS_AUTO, + + SSTATUS_MAX +}; + +enum { + MRESET_STEP_INTERFACES, + MRESET_STEP_LOCOMOTIVES, + MRESET_STEP_TURNOUTS, + MRESET_STEP_SENSORS, + + MRESET_STEP_MAX +}; + +struct { + int mr_step; // current step + int mr_timestamp; // timestamp ( needed for times ) + int mr_idx; // mode index +} typedef ModeResetData; + + + class Server { private: + int status; // holds the current mode + int status_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; @@ -74,6 +106,24 @@ public: int GetHeight() { return railways.GetHeight(); }; int GetWidth() { return railways.GetWidth(); }; + // + // Load Save Part + int Load(); + int Save(); + + // + // Reset dynamic data + // FIXME: + // FIXME: + // FIXME: finish me + // FIXME: + // FIXME: + JSONParse GetJSONServerStatus(); + void ModeReset(); // mode Reset if finished will go to Manual + void ModeManual(); // Manual Mode + void ModeAuto(); // only allowed if Manual was set + void ModeError(); // will cut power and keep this mode, until reset or Mode Manual + ///////////////////////////////////////// // Railway int RailwayChange(Railway *rw) { return railways.Change(rw);}; @@ -104,6 +154,7 @@ public: 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); }; ///////////////////////////////////////// @@ -118,16 +169,10 @@ public: ///////////////////////////////////////// // 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 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; }; - - // - // Load Save Part - int Load(); - int Save(); - protected: static void *ThreadEntry (void *This) { ((Server*)This)->ThreadProcess(); return NULL;}; }; diff --git a/server/session.cc b/server/session.cc index 0256a89..def98e5 100644 --- a/server/session.cc +++ b/server/session.cc @@ -107,11 +107,15 @@ int Session::ProcessData(JSONParse *jin, JSONParse *jout) { debug (0, "* Session Poweroff"); server->PowerOnOff(0); } - else if (command.compare("save") == 0) { debug (0, "* Save All"); server->Save(); } + else if (command.compare("resetdata") == 0) { + debug (0, "* Reset All Data"); +#warning FIXME: continue here +// server->ResetData(); + } else if (command.compare("getall") == 0) { json.Clear(); server->GetJSONAll(&json); @@ -377,6 +381,12 @@ void Session::SetJSONLocomotive(JSONParse *jp) { debug (0, "%s:%d SetJSONLocomotive Element %s Speed:%d", __FILE__, __LINE__, loconame.c_str(), speed); server->LocomotiveSetSpeed(loconame, speed); } + if (jloco.GetValueInt("reverse", &value) == 1) { + // + // reverse + debug (0, "%s:%d SetJSONLocomotive Element %s Reverse:%d", __FILE__, __LINE__, loconame.c_str(), value); + server->LocomotiveSetReverse(loconame, value); + } if (jloco.GetValueInt("function", &func) == 1 && jloco.GetValueInt("value", &value) == 1) { // // set function