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.
270 lines
5.3 KiB
270 lines
5.3 KiB
|
|
|
|
#include "modelbahn.h"
|
|
#include "locomotive.h"
|
|
|
|
|
|
Locomotives::Locomotives () {
|
|
changed = 0;
|
|
locomotives = (Locomotive*) malloc(sizeof(Locomotive)*LOCOMOTIVES_MAX);
|
|
memset(locomotives, 0x0, sizeof(Locomotive)*LOCOMOTIVES_MAX);
|
|
max = SENSORS_MAX;
|
|
};
|
|
|
|
Locomotives::~Locomotives() {
|
|
free (locomotives);
|
|
locomotives = NULL;
|
|
max = 0;
|
|
};
|
|
|
|
|
|
int Locomotives::Lock() {
|
|
if (pthread_mutex_lock(&mtx) == 0) return 1;
|
|
else return 0;
|
|
}
|
|
|
|
|
|
int Locomotives::UnLock() {
|
|
if (pthread_mutex_unlock(&mtx) == 0) return 1;
|
|
else return 0;
|
|
}
|
|
|
|
|
|
JSONParse Locomotives::_GetJSON(int idx) {
|
|
JSONParse json;
|
|
JSONElement je;
|
|
string s = "";
|
|
|
|
json.Clear();
|
|
|
|
s = locomotives[idx].name; json.AddObject("name", s);
|
|
s = locomotives[idx].ifname; json.AddObject("ifname", s);
|
|
json.AddObject("addr", locomotives[idx].addr);
|
|
json.AddObject("steps", locomotives[idx].steps);
|
|
json.AddObject("speed", locomotives[idx].speed);
|
|
json.AddObject("func", locomotives[idx].func);
|
|
json.AddObject("flags", locomotives[idx].flags);
|
|
json.AddObject("vmin", locomotives[idx].vmin);
|
|
json.AddObject("vslow", locomotives[idx].vslow);
|
|
json.AddObject("vmid", locomotives[idx].vmid);
|
|
json.AddObject("vfast", locomotives[idx].vfast);
|
|
json.AddObject("vmax", locomotives[idx].vmax);
|
|
|
|
return json;
|
|
};
|
|
|
|
|
|
JSONParse Locomotives::GetJSON(string name) {
|
|
int i;
|
|
JSONParse jp;
|
|
|
|
jp.Clear();
|
|
|
|
Lock();
|
|
for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) {
|
|
if (name.compare(locomotives[i].name) == 0) {
|
|
jp = _GetJSON(i);
|
|
}
|
|
}
|
|
|
|
UnLock();
|
|
|
|
return jp;
|
|
};
|
|
|
|
|
|
void Locomotives::GetJSONAll(JSONParse *json) {
|
|
int i, cnt;
|
|
JSONElement je;
|
|
|
|
Lock();
|
|
|
|
//
|
|
// write all railway data
|
|
// create json object array manualy
|
|
je.type = JSON_T_ARRAY;
|
|
je.name = "locomotives";
|
|
for (cnt = 0, i = 0; i < max; i++)
|
|
if (locomotives[i].name[0] != 0) {
|
|
if (cnt != 0) je.value += ","; // not first element
|
|
je.value += _GetJSON(i).ToString();
|
|
cnt++;
|
|
}
|
|
json->AddObject(je);
|
|
|
|
UnLock();
|
|
};
|
|
|
|
|
|
Locomotive Locomotives::GetLocomotiveFromJSON(JSONParse *j) {
|
|
Locomotive l;
|
|
string s;
|
|
|
|
l.name[0] = 0;
|
|
l.ifname[0] = 0;
|
|
l.addr = 0;
|
|
l.steps = 0;
|
|
l.vmin = 0;
|
|
l.vslow = 0;
|
|
l.vmid = 0;
|
|
l.vfast = 0;
|
|
l.vmax = 0;
|
|
l.flags = 0;
|
|
l.speed = 0;
|
|
l.func = 0;
|
|
|
|
j->GetValue("name", &s);
|
|
strncpy (l.name, s.c_str(), REFERENCENAME_LEN);
|
|
j->GetValue("ifname", &s);
|
|
strncpy (l.ifname, s.c_str(), REFERENCENAME_LEN);
|
|
j->GetValueInt("addr", &l.addr);
|
|
j->GetValueInt("steps", &l.steps);
|
|
j->GetValueInt("speed", &l.speed);
|
|
j->GetValueInt64("func", &l.func);
|
|
j->GetValueInt("flags", &l.flags);
|
|
j->GetValueInt("vmin", &l.vmin);
|
|
j->GetValueInt("vslow", &l.vslow);
|
|
j->GetValueInt("vmid", &l.vmid);
|
|
j->GetValueInt("vfast", &l.vfast);
|
|
j->GetValueInt("vmax", &l.vmax);
|
|
|
|
return l;
|
|
};
|
|
|
|
|
|
int Locomotives::Change(Locomotive *loco) {
|
|
int i;
|
|
int ifree = -1;
|
|
|
|
Lock();
|
|
|
|
for (i = 0; i < max; i++) {
|
|
if (locomotives[i].name[0] != 0) {
|
|
// found element
|
|
if (strncmp(locomotives[i].name, loco->name, REFERENCENAME_LEN) == 0) {
|
|
ifree = i;
|
|
break;
|
|
}
|
|
}
|
|
else if (ifree == -1) ifree = i;
|
|
}
|
|
// element not found add new element
|
|
if (ifree != -1 && ifree < max) {
|
|
locomotives[ifree] = *loco;
|
|
strncpy (locomotives[ifree].name, loco->name, REFERENCENAME_LEN);
|
|
strncpy (locomotives[ifree].ifname, loco->ifname, REFERENCENAME_LEN);
|
|
}
|
|
|
|
changed = 1;
|
|
UnLock();
|
|
|
|
return 1;
|
|
};
|
|
|
|
|
|
int Locomotives::Delete(string name) {
|
|
int i;
|
|
|
|
Lock();
|
|
for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) {
|
|
if (name.compare(locomotives[i].name) == 0) {
|
|
locomotives[i].name[0] = 0;
|
|
locomotives[i].ifname[0] = 0;
|
|
locomotives[i].addr = 0;
|
|
locomotives[i].steps = 0;
|
|
locomotives[i].vmin = 0;
|
|
locomotives[i].vslow = 0;
|
|
locomotives[i].vmid = 0;
|
|
locomotives[i].vfast = 0;
|
|
locomotives[i].vmax = 0;
|
|
locomotives[i].flags = 0;
|
|
changed = 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
UnLock();
|
|
|
|
return 1;
|
|
};
|
|
|
|
|
|
int Locomotives::SetSpeed(string name, int speed) {
|
|
int i;
|
|
|
|
Lock();
|
|
for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) {
|
|
if (name.compare(locomotives[i].name) == 0) {
|
|
server->interfaces.SetLocoSpeed(&locomotives[i], speed);
|
|
break;
|
|
}
|
|
}
|
|
|
|
UnLock();
|
|
|
|
return 1;
|
|
};
|
|
|
|
|
|
int Locomotives::SetFunction(string name, int func, int value) {
|
|
int i;
|
|
|
|
Lock();
|
|
for (i = 0; i < max; i++) if (locomotives[i].name[0] != 0) {
|
|
if (name.compare(locomotives[i].name) == 0) {
|
|
server->interfaces.SetLocoFunction(&locomotives[i], func, value);
|
|
break;
|
|
}
|
|
}
|
|
|
|
UnLock();
|
|
|
|
return 1;
|
|
};
|
|
|
|
|
|
|
|
//
|
|
// set values from bus...
|
|
//
|
|
int Locomotives::SetSpeedFromBus(string name, int addr, int speed) {
|
|
int i;
|
|
JSONParse jp;
|
|
|
|
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;
|
|
|
|
jp.AddObject("locomotive",_GetJSON(i));
|
|
if(network) network->ChangeListPushToAll(jp.ToString());
|
|
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
};
|
|
|
|
|
|
int Locomotives::SetDirectionFromBus(string name, int addr, int reverse) {
|
|
int i;
|
|
JSONParse jp;
|
|
|
|
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;
|
|
else locomotives[i].flags &= ~LOCO_F_REVERSE;
|
|
|
|
jp.AddObject("locomotive",_GetJSON(i));
|
|
if(network) network->ChangeListPushToAll(jp.ToString());
|
|
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
};
|
|
|
|
|
|
|
|
|
|
|