From 8d44d32e6b9c139c4b6c4fb556ab0f82c633e032 Mon Sep 17 00:00:00 2001 From: Steffen Pohle Date: Sun, 19 Dec 2021 22:38:04 +0100 Subject: [PATCH] adding sensor context menu for simulation purpose. --- ChangeLog | 3 ++ server/locomotive.cc | 66 +++++++++++++++++++++++++++----------- server/locomotive.h | 8 ++--- server/network.h | 1 + server/sensor.cc | 23 +++++++++++++ server/sensor.h | 1 + server/server.h | 9 +++--- server/session.cc | 31 +++++++++++++++++- webinterface/locomotive.js | 2 +- webinterface/railways.js | 1 + webinterface/sensor.js | 45 ++++++++++++++++++++++++++ 11 files changed, 161 insertions(+), 29 deletions(-) diff --git a/ChangeLog b/ChangeLog index 280b6a5..3ad01e5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +2021-12-19: +- adding sensor context menu for sensor simulation + 2021-12-10: - adding support for scheduling ways - fixed: webinterface, update on loco will now change its detail display diff --git a/server/locomotive.cc b/server/locomotive.cc index b9fcb57..8cf8ec5 100644 --- a/server/locomotive.cc +++ b/server/locomotive.cc @@ -389,6 +389,7 @@ int Locomotives::Reset(string name) { for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) if (name.compare(locomotives[i].name) == 0) { debug (0, "Locomotives::Reset (Name:%s)", name.c_str()); + locomotives[i].flags &= ~(LOCO_F_AUTO | LOCO_F_AUTOSTOP | LOCO_F_RANDOM); locomotives[i].blockassign[0] = 0; locomotives[i].blockdest[0] = 0; locomotives[i].blockprev[0] = 0; @@ -398,21 +399,25 @@ int Locomotives::Reset(string name) { locomotives[i].auto_onroute = 0; locomotives[i].auto_data = 0; locomotives[i].auto_timenext = { 0 }; - locomotives[i].sched_step = 0; + locomotives[i].sched_step = -1; + + jp.Clear(); + jp.AddObject("locomotive",_GetJSON(i)); + if(network) network->ChangeListPushToAll(jp.ToString()); + break; } server->railways.ClearLockedby(name); server->blocks.ClearLockedby(name); - jp.AddObject("locomotive",_GetJSON(i)); - if(network) network->ChangeListPushToAll(jp.ToString()); UnLock(); return 1; }; -int Locomotives::SetMan(string name) { +int Locomotives::SetModeMan(string name) { int i; + JSONParse jp; Lock(); for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) @@ -422,7 +427,12 @@ int Locomotives::SetMan(string name) { locomotives[i].auto_onroute = 0; locomotives[i].auto_way[0] = 0; locomotives[i].auto_wayold[0] = 0; - locomotives[i].sched_step = 0; + locomotives[i].sched_step = -1; + + jp.Clear(); + jp.AddObject("locomotive",_GetJSON(i)); + if(network) network->ChangeListPushToAll(jp.ToString()); + break; } UnLock(); @@ -431,8 +441,9 @@ int Locomotives::SetMan(string name) { return 1; }; -int Locomotives::SetAutoMan(string name) { +int Locomotives::SetModeAutoMan(string name) { int i; + JSONParse jp; Lock(); for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) @@ -440,6 +451,11 @@ int Locomotives::SetAutoMan(string name) { debug (0, "Locomotives::SetAutoMan (Name:%s)\n", name.c_str()); locomotives[i].flags |= LOCO_F_AUTOSTOP; locomotives[i].flags &= ~LOCO_F_RANDOM; + + jp.Clear(); + jp.AddObject("locomotive",_GetJSON(i)); + if(network) network->ChangeListPushToAll(jp.ToString()); + break; } UnLock(); @@ -447,8 +463,9 @@ int Locomotives::SetAutoMan(string name) { return 1; }; -int Locomotives::SetAuto(string name) { +int Locomotives::SetModeAuto(string name) { int i; + JSONParse jp; Lock(); for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) @@ -456,6 +473,11 @@ int Locomotives::SetAuto(string name) { debug (0, "Locomotives::SetAuto (Name:%s)\n", name.c_str()); locomotives[i].flags |= LOCO_F_AUTO; locomotives[i].flags &= ~LOCO_F_RANDOM; + + jp.Clear(); + jp.AddObject("locomotive",_GetJSON(i)); + if(network) network->ChangeListPushToAll(jp.ToString()); + break; } UnLock(); @@ -463,8 +485,9 @@ int Locomotives::SetAuto(string name) { return 1; }; -int Locomotives::SetAutoRand(string name) { +int Locomotives::SetModeAutoRand(string name) { int i; + JSONParse jp; Lock(); for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) @@ -472,6 +495,11 @@ int Locomotives::SetAutoRand(string name) { debug (0, "Locomotives::SetRandom (Name:%s)\n", name.c_str()); locomotives[i].flags |= LOCO_F_RANDOM; locomotives[i].flags &= ~LOCO_F_AUTOSTOP; + + jp.Clear(); + jp.AddObject("locomotive",_GetJSON(i)); + if(network) network->ChangeListPushToAll(jp.ToString()); + break; } UnLock(); @@ -699,7 +727,6 @@ void::Locomotives::SendUpdate (Locomotive *loc) { break; } } - } @@ -722,25 +749,26 @@ int Locomotives::Loop() { // // only in automate do anything alone // - if (loco->blockassign[0] == 0) continue; + if (loco->blockassign[0] == 0) { + debug (0, "%s:%d Locomotive not assigned to any block. Set Mode to Man", __FILE__, __LINE__); + SetModeMan(loco->name); + continue; + } if (loco->auto_onroute == LOCO_OR_NOTHING) { - timer_start(&loco->auto_timenext); - loco->auto_onroute = LOCO_OR_SEARCH; // // check if the loco mode is set to autostop if (loco->flags & LOCO_F_AUTOSTOP) { - SetMan(loco->name); + SetModeMan(loco->name); + continue; + } + else { + timer_start(&loco->auto_timenext); + loco->auto_onroute = LOCO_OR_SEARCH; } } else if (loco->auto_onroute == LOCO_OR_SEARCH) { - // - // check if the loco mode is set to autostop - if (loco->flags & LOCO_F_AUTOSTOP) { - SetMan(loco->name); - } - // // try to find and prepare(lock) a new way. // nothing found check if we can reverse direction, this will be done only diff --git a/server/locomotive.h b/server/locomotive.h index 46d81ff..fd0bed9 100644 --- a/server/locomotive.h +++ b/server/locomotive.h @@ -97,10 +97,10 @@ class Locomotives { int SetReverse(string name, int reverse); int SetFunction(string name, int func, int value); int Reset(string name); - int SetMan(string name); - int SetAutoMan(string name); - int SetAuto(string name); - int SetAutoRand(string name); + int SetModeMan(string name); + int SetModeAutoMan(string name); + int SetModeAuto(string name); + int SetModeAutoRand(string name); int SetSpeedFromBus (string ifname, int addr, int speed); int SetFunctionFromBus (string ifname, int addr, int func); int SetDestination (string name, string block, int direction); diff --git a/server/network.h b/server/network.h index d538aff..51957e7 100644 --- a/server/network.h +++ b/server/network.h @@ -55,6 +55,7 @@ private: void AddJSONSensor(JSONParse *jp); void DelJSONSensor(JSONParse *jp); + void SetJSONSensorActive(JSONParse *jp); void AddJSONInterface(JSONParse *jp); void DelJSONInterface(JSONParse *jp); diff --git a/server/sensor.cc b/server/sensor.cc index b773da0..af8c39e 100644 --- a/server/sensor.cc +++ b/server/sensor.cc @@ -35,6 +35,29 @@ int Sensors::GetActive(string name) { }; +int Sensors::SetActive(string name, int value) { + int i; + JSONParse jp; + + Lock(); + for (i = 0; i < max; i++) if (sensors[i].name[0] != 0) { + if (name.compare(sensors[i].name) == 0) { + if (value) sensors[i].flags |= SENSOR_F_ACTIVE; + else sensors[i].flags &= ~SENSOR_F_ACTIVE; + + jp.Clear(); + jp.AddObject("sensor", _GetJSON(i)); + if(network) network->ChangeListPushToAll(jp.ToString()); + + break; + } + } + + UnLock(); + + return 1; +}; + int Sensors::Lock() { if (pthread_mutex_lock(&mtx) == 0) return 1; else return 0; diff --git a/server/sensor.h b/server/sensor.h index c0136ea..0fc5074 100644 --- a/server/sensor.h +++ b/server/sensor.h @@ -37,6 +37,7 @@ class Sensors { int Change(Sensor *se); int Get(int idx, Sensor *dest); int GetActive(string name); + int SetActive(string name, int value); int Get(string name, Sensor *dest); int Delete(string name); diff --git a/server/server.h b/server/server.h index d291ea7..207b686 100644 --- a/server/server.h +++ b/server/server.h @@ -164,10 +164,10 @@ public: 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.SetMan(name); }; - int LocomotiveSetAuto(string name) { return locomotives.SetAuto(name); }; - int LocomotiveSetAutoMan(string name) { return locomotives.SetAutoMan(name); }; - int LocomotiveSetAutoRand(string name) { return locomotives.SetAutoRand(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 LocomotiveTest(string name) { return locomotives.Test(name); }; ///////////////////////////////////////// @@ -176,6 +176,7 @@ public: 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 diff --git a/server/session.cc b/server/session.cc index 9cbe213..4a3d75c 100644 --- a/server/session.cc +++ b/server/session.cc @@ -64,6 +64,10 @@ int Session::ProcessData(JSONParse *jin, JSONParse *jout) { debug (0, "* Session Del Interface"); DelJSONInterface(jin); } + + // + // turnouts + // else if (command.compare("addsensor") == 0) { debug (0, "* Session Add Sensor"); AddJSONSensor(jin); @@ -72,6 +76,11 @@ int Session::ProcessData(JSONParse *jin, JSONParse *jout) { debug (0, "* Session del Sensor"); DelJSONSensor(jin); } + else if (command.compare("sensorSetActive") == 0) { + debug (0, "* Session Sensor Set Active"); + printf ("%s:%d jin:%s", __FILE__, __LINE__, jin->ToString().c_str()); + SetJSONSensorActive(jin); + } // // turnouts @@ -688,7 +697,6 @@ void Session::SetJSONTurnout(JSONParse *jp) { }; - // // add new block // @@ -769,6 +777,27 @@ void Session::BlockJSONClear(JSONParse *jp) { }; +// +// add new block +// +void Session::SetJSONSensorActive(JSONParse *jp) { + string name; + int enabled; + JSONParse jtmp; + JSONParse jout; + + server->LockThread(); + jp->GetValue("sensor", &name); + jp->GetValueInt("enabled", &enabled); + server->SensorSetActive(name, enabled); +// jout.Clear(); +// jout.AddObject("block", server->BlockGetJSON(bl.name)); +// if (network) network->ChangeListPushToAll(jout.ToString()); + server->UnLockThread(); +}; + + + /* res = json.GetValue("command", &value); diff --git a/webinterface/locomotive.js b/webinterface/locomotive.js index e4fd1d6..c676858 100644 --- a/webinterface/locomotive.js +++ b/webinterface/locomotive.js @@ -44,7 +44,7 @@ function locomotive_Update(data) { if (data.schedway) locomotives[i].schedway = data.schedway; else locomotives[i].schedway = ""; if (data.auto_way) locomotives[i].auto_way = data.auto_way; - if (loco_name.value == data.name) locodetail_setData(locomotives[i]); + if (loco_name == data.name) locodetail_setData(locomotives[i]); lococtrl_setData(data); return; diff --git a/webinterface/railways.js b/webinterface/railways.js index df99f20..e4dbe13 100644 --- a/webinterface/railways.js +++ b/webinterface/railways.js @@ -234,6 +234,7 @@ function rw_Click(x,y) { // Sensor else if (track.elements[idx].type == RAILWAY_SENSOR) { if (track.elements[idx].name != "") { + sensor_contextmenu(track.elements[idx].name); } } diff --git a/webinterface/sensor.js b/webinterface/sensor.js index 41789e5..1828b95 100644 --- a/webinterface/sensor.js +++ b/webinterface/sensor.js @@ -320,3 +320,48 @@ function sensorlist_cb_close () { +// *********************************************************************************************** +// *********************************************************************************************** +// sensor contextmenu: show a context menu for the sensor +// +// *********************************************************************************************** +// *********************************************************************************************** +function sensor_contextmenu(name) { + let innerhtml = ""; + + innerhtml = "
"; + innerhtml += "
"; + innerhtml += "
"; + innerhtml += "
"; + + gContextmenuCreate(name, innerhtml); + + gAddEventListener("contextbox_On", 'click', sensor_ctxmenu_On); + gAddEventListener("contextbox_Off", 'click', sensor_ctxmenu_Off); + gAddEventListener("contextbox_Close", 'click', gContextmenuClose); +}; + + + +function sensor_ctxmenu_On (element, value) { + let name = document.getElementById("ConextMenuHead").innerHTML; + sensor_server_setactive(name, "1"); + gContextmenuClose(); +} + +function sensor_ctxmenu_Off (element, value) { + let name = document.getElementById("ConextMenuHead").innerHTML; + sensor_server_setactive(name, "0"); + gContextmenuClose(); +} + + +// +// send sensor activate and deactivate to server +// +function sensor_server_setactive(name, value) { + var request = { command: "sensorSetActive", sensor: name, enabled: value }; + debug ("send sensor active:" + name); + serverinout (request, serverinout_defaultCallback); +}; +