adding sensor context menu for simulation purpose.

master
Steffen Pohle 4 years ago
parent 4ae0036c4f
commit 8d44d32e6b

@ -1,3 +1,6 @@
2021-12-19:
- adding sensor context menu for sensor simulation
2021-12-10: 2021-12-10:
- adding support for scheduling ways - adding support for scheduling ways
- fixed: webinterface, update on loco will now change its detail display - fixed: webinterface, update on loco will now change its detail display

@ -389,6 +389,7 @@ int Locomotives::Reset(string name) {
for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0)
if (name.compare(locomotives[i].name) == 0) { if (name.compare(locomotives[i].name) == 0) {
debug (0, "Locomotives::Reset (Name:%s)", name.c_str()); 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].blockassign[0] = 0;
locomotives[i].blockdest[0] = 0; locomotives[i].blockdest[0] = 0;
locomotives[i].blockprev[0] = 0; locomotives[i].blockprev[0] = 0;
@ -398,21 +399,25 @@ int Locomotives::Reset(string name) {
locomotives[i].auto_onroute = 0; locomotives[i].auto_onroute = 0;
locomotives[i].auto_data = 0; locomotives[i].auto_data = 0;
locomotives[i].auto_timenext = { 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; break;
} }
server->railways.ClearLockedby(name); server->railways.ClearLockedby(name);
server->blocks.ClearLockedby(name); server->blocks.ClearLockedby(name);
jp.AddObject("locomotive",_GetJSON(i));
if(network) network->ChangeListPushToAll(jp.ToString());
UnLock(); UnLock();
return 1; return 1;
}; };
int Locomotives::SetMan(string name) { int Locomotives::SetModeMan(string name) {
int i; int i;
JSONParse jp;
Lock(); Lock();
for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) 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_onroute = 0;
locomotives[i].auto_way[0] = 0; locomotives[i].auto_way[0] = 0;
locomotives[i].auto_wayold[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; break;
} }
UnLock(); UnLock();
@ -431,8 +441,9 @@ int Locomotives::SetMan(string name) {
return 1; return 1;
}; };
int Locomotives::SetAutoMan(string name) { int Locomotives::SetModeAutoMan(string name) {
int i; int i;
JSONParse jp;
Lock(); Lock();
for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) 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()); debug (0, "Locomotives::SetAutoMan (Name:%s)\n", name.c_str());
locomotives[i].flags |= LOCO_F_AUTOSTOP; locomotives[i].flags |= LOCO_F_AUTOSTOP;
locomotives[i].flags &= ~LOCO_F_RANDOM; locomotives[i].flags &= ~LOCO_F_RANDOM;
jp.Clear();
jp.AddObject("locomotive",_GetJSON(i));
if(network) network->ChangeListPushToAll(jp.ToString());
break; break;
} }
UnLock(); UnLock();
@ -447,8 +463,9 @@ int Locomotives::SetAutoMan(string name) {
return 1; return 1;
}; };
int Locomotives::SetAuto(string name) { int Locomotives::SetModeAuto(string name) {
int i; int i;
JSONParse jp;
Lock(); Lock();
for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) 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()); debug (0, "Locomotives::SetAuto (Name:%s)\n", name.c_str());
locomotives[i].flags |= LOCO_F_AUTO; locomotives[i].flags |= LOCO_F_AUTO;
locomotives[i].flags &= ~LOCO_F_RANDOM; locomotives[i].flags &= ~LOCO_F_RANDOM;
jp.Clear();
jp.AddObject("locomotive",_GetJSON(i));
if(network) network->ChangeListPushToAll(jp.ToString());
break; break;
} }
UnLock(); UnLock();
@ -463,8 +485,9 @@ int Locomotives::SetAuto(string name) {
return 1; return 1;
}; };
int Locomotives::SetAutoRand(string name) { int Locomotives::SetModeAutoRand(string name) {
int i; int i;
JSONParse jp;
Lock(); Lock();
for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) 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()); debug (0, "Locomotives::SetRandom (Name:%s)\n", name.c_str());
locomotives[i].flags |= LOCO_F_RANDOM; locomotives[i].flags |= LOCO_F_RANDOM;
locomotives[i].flags &= ~LOCO_F_AUTOSTOP; locomotives[i].flags &= ~LOCO_F_AUTOSTOP;
jp.Clear();
jp.AddObject("locomotive",_GetJSON(i));
if(network) network->ChangeListPushToAll(jp.ToString());
break; break;
} }
UnLock(); UnLock();
@ -699,7 +727,6 @@ void::Locomotives::SendUpdate (Locomotive *loc) {
break; break;
} }
} }
} }
@ -722,25 +749,26 @@ int Locomotives::Loop() {
// //
// only in automate do anything alone // 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) { 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 // check if the loco mode is set to autostop
if (loco->flags & LOCO_F_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);
} }
else if (loco->auto_onroute == LOCO_OR_SEARCH) {
// //
// try to find and prepare(lock) a new way. // try to find and prepare(lock) a new way.
// nothing found check if we can reverse direction, this will be done only // nothing found check if we can reverse direction, this will be done only

@ -97,10 +97,10 @@ class Locomotives {
int SetReverse(string name, int reverse); int SetReverse(string name, int reverse);
int SetFunction(string name, int func, int value); int SetFunction(string name, int func, int value);
int Reset(string name); int Reset(string name);
int SetMan(string name); int SetModeMan(string name);
int SetAutoMan(string name); int SetModeAutoMan(string name);
int SetAuto(string name); int SetModeAuto(string name);
int SetAutoRand(string name); int SetModeAutoRand(string name);
int SetSpeedFromBus (string ifname, int addr, int speed); int SetSpeedFromBus (string ifname, int addr, int speed);
int SetFunctionFromBus (string ifname, int addr, int func); int SetFunctionFromBus (string ifname, int addr, int func);
int SetDestination (string name, string block, int direction); int SetDestination (string name, string block, int direction);

@ -55,6 +55,7 @@ private:
void AddJSONSensor(JSONParse *jp); void AddJSONSensor(JSONParse *jp);
void DelJSONSensor(JSONParse *jp); void DelJSONSensor(JSONParse *jp);
void SetJSONSensorActive(JSONParse *jp);
void AddJSONInterface(JSONParse *jp); void AddJSONInterface(JSONParse *jp);
void DelJSONInterface(JSONParse *jp); void DelJSONInterface(JSONParse *jp);

@ -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() { int Sensors::Lock() {
if (pthread_mutex_lock(&mtx) == 0) return 1; if (pthread_mutex_lock(&mtx) == 0) return 1;
else return 0; else return 0;

@ -37,6 +37,7 @@ class Sensors {
int Change(Sensor *se); int Change(Sensor *se);
int Get(int idx, Sensor *dest); int Get(int idx, Sensor *dest);
int GetActive(string name); int GetActive(string name);
int SetActive(string name, int value);
int Get(string name, Sensor *dest); int Get(string name, Sensor *dest);
int Delete(string name); int Delete(string name);

@ -164,10 +164,10 @@ public:
int LocomotiveSetDest(string name, string block, int direction) { return locomotives.SetDestination(name, block, direction); }; 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 LocomotiveSetAssign(string name, string block, int direction) { return locomotives.SetAssign(name, block, direction); };
int LocomotiveReset(string name) { return locomotives.Reset(name); }; int LocomotiveReset(string name) { return locomotives.Reset(name); };
int LocomotiveSetMan(string name) { return locomotives.SetMan(name); }; int LocomotiveSetMan(string name) { return locomotives.SetModeMan(name); };
int LocomotiveSetAuto(string name) { return locomotives.SetAuto(name); }; int LocomotiveSetAuto(string name) { return locomotives.SetModeAuto(name); };
int LocomotiveSetAutoMan(string name) { return locomotives.SetAutoMan(name); }; int LocomotiveSetAutoMan(string name) { return locomotives.SetModeAutoMan(name); };
int LocomotiveSetAutoRand(string name) { return locomotives.SetAutoRand(name); }; int LocomotiveSetAutoRand(string name) { return locomotives.SetModeAutoRand(name); };
int LocomotiveTest(string name) { return locomotives.Test(name); }; int LocomotiveTest(string name) { return locomotives.Test(name); };
///////////////////////////////////////// /////////////////////////////////////////
@ -176,6 +176,7 @@ public:
Sensor SensorFromJSON(JSONParse *j) { return sensors.GetSensorFromJSON(j); }; Sensor SensorFromJSON(JSONParse *j) { return sensors.GetSensorFromJSON(j); };
JSONParse SensorGetJSON(string name) { return sensors.GetJSON(name); }; JSONParse SensorGetJSON(string name) { return sensors.GetJSON(name); };
int SensorDelete(string name) { return sensors.Delete(name); }; int SensorDelete(string name) { return sensors.Delete(name); };
int SensorSetActive(string name, int value) { return sensors.SetActive(name, value); };
///////////////////////////////////////// /////////////////////////////////////////
// Blocks // Blocks

@ -64,6 +64,10 @@ int Session::ProcessData(JSONParse *jin, JSONParse *jout) {
debug (0, "* Session Del Interface"); debug (0, "* Session Del Interface");
DelJSONInterface(jin); DelJSONInterface(jin);
} }
//
// turnouts
//
else if (command.compare("addsensor") == 0) { else if (command.compare("addsensor") == 0) {
debug (0, "* Session Add Sensor"); debug (0, "* Session Add Sensor");
AddJSONSensor(jin); AddJSONSensor(jin);
@ -72,6 +76,11 @@ int Session::ProcessData(JSONParse *jin, JSONParse *jout) {
debug (0, "* Session del Sensor"); debug (0, "* Session del Sensor");
DelJSONSensor(jin); 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 // turnouts
@ -688,7 +697,6 @@ void Session::SetJSONTurnout(JSONParse *jp) {
}; };
// //
// add new block // 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); res = json.GetValue("command", &value);

@ -44,7 +44,7 @@ function locomotive_Update(data) {
if (data.schedway) locomotives[i].schedway = data.schedway; if (data.schedway) locomotives[i].schedway = data.schedway;
else locomotives[i].schedway = ""; else locomotives[i].schedway = "";
if (data.auto_way) locomotives[i].auto_way = data.auto_way; 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); lococtrl_setData(data);
return; return;

@ -234,6 +234,7 @@ function rw_Click(x,y) {
// Sensor // Sensor
else if (track.elements[idx].type == RAILWAY_SENSOR) { else if (track.elements[idx].type == RAILWAY_SENSOR) {
if (track.elements[idx].name != "") { if (track.elements[idx].name != "") {
sensor_contextmenu(track.elements[idx].name);
} }
} }

@ -320,3 +320,48 @@ function sensorlist_cb_close () {
// ***********************************************************************************************
// ***********************************************************************************************
// sensor contextmenu: show a context menu for the sensor
//
// ***********************************************************************************************
// ***********************************************************************************************
function sensor_contextmenu(name) {
let innerhtml = "";
innerhtml = "<center>";
innerhtml += "<button id=\"contextbox_On\" type=\"button\">Set On</button><br>";
innerhtml += "<button id=\"contextbox_Off\" type=\"button\">Set Off</button><br>";
innerhtml += "<button id=\"contextbox_Close\" type=\"button\">Close</button></center>";
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);
};

Loading…
Cancel
Save