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.
191 lines
3.5 KiB
191 lines
3.5 KiB
|
|
|
|
#include "modelbahn.h"
|
|
#include "sensor.h"
|
|
|
|
|
|
Sensors::Sensors () {
|
|
changed = 0;
|
|
sensors = (Sensor*) malloc(sizeof(Sensor)*SENSORS_MAX);
|
|
max = SENSORS_MAX;
|
|
};
|
|
|
|
Sensors::~Sensors() {
|
|
free (sensors);
|
|
sensors = NULL;
|
|
max = 0;
|
|
};
|
|
|
|
|
|
|
|
|
|
int Sensors::Lock() {
|
|
if (pthread_mutex_lock(&mtx) == 0) return 1;
|
|
else return 0;
|
|
}
|
|
|
|
|
|
int Sensors::UnLock() {
|
|
if (pthread_mutex_unlock(&mtx) == 0) return 1;
|
|
else return 0;
|
|
}
|
|
|
|
|
|
JSONParse Sensors::_GetJSON(int idx) {
|
|
JSONParse json;
|
|
JSONElement je;
|
|
string s = "";
|
|
|
|
json.Clear();
|
|
|
|
s = sensors[idx].name; json.AddObject("name", s);
|
|
s = sensors[idx].ifname; json.AddObject("ifname", s);
|
|
json.AddObject("addr", sensors[idx].addr);
|
|
json.AddObject("flags", sensors[idx].flags);
|
|
|
|
return json;
|
|
};
|
|
|
|
|
|
JSONParse Sensors::GetJSON(string name) {
|
|
int i;
|
|
JSONParse jp;
|
|
|
|
jp.Clear();
|
|
|
|
Lock();
|
|
for (i = 0; i < max; i++) if (sensors[i].name[0] != 0) {
|
|
if (name.compare(sensors[i].name) == 0) {
|
|
jp = _GetJSON(i);
|
|
}
|
|
}
|
|
|
|
UnLock();
|
|
|
|
return jp;
|
|
};
|
|
|
|
|
|
void Sensors::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 = "sensors";
|
|
for (cnt = 0, i = 0; i < max; i++)
|
|
if (sensors[i].name[0] != 0) {
|
|
if (cnt != 0) je.value += ","; // not first element
|
|
je.value += _GetJSON(i).ToString();
|
|
cnt++;
|
|
}
|
|
json->AddObject(je);
|
|
|
|
UnLock();
|
|
};
|
|
|
|
|
|
Sensor Sensors::GetSensorFromJSON(JSONParse *j) {
|
|
Sensor se;
|
|
string s;
|
|
|
|
se.name[0] = 0;
|
|
se.ifname[0] = 0;
|
|
se.addr = 0;
|
|
se.flags = 0;
|
|
|
|
j->GetValue("name", &s);
|
|
strncpy (se.name, s.c_str(), REFERENCENAME_LEN);
|
|
j->GetValue("ifname", &s);
|
|
strncpy (se.ifname, s.c_str(), REFERENCENAME_LEN);
|
|
j->GetValueInt("addr", &se.addr);
|
|
j->GetValueInt("flags", &se.flags);
|
|
|
|
return se;
|
|
};
|
|
|
|
|
|
int Sensors::Change(Sensor *se) {
|
|
int i;
|
|
int ifree = -1;
|
|
|
|
Lock();
|
|
|
|
for (i = 0; i < max; i++) {
|
|
if (sensors[i].name[0] != 0) {
|
|
// found element
|
|
if (strncmp(sensors[i].name, se->name, REFERENCENAME_LEN) == 0) {
|
|
ifree = i;
|
|
break;
|
|
}
|
|
}
|
|
else if (ifree == -1) ifree = i;
|
|
}
|
|
// element not found add new element
|
|
if (ifree != -1 && ifree < max) {
|
|
sensors[ifree] = *se;
|
|
strncpy (sensors[ifree].name, se->name, REFERENCENAME_LEN);
|
|
strncpy (sensors[ifree].ifname, se->ifname, REFERENCENAME_LEN);
|
|
}
|
|
|
|
changed = 1;
|
|
UnLock();
|
|
|
|
return 1;
|
|
};
|
|
|
|
|
|
int Sensors::Delete(string name) {
|
|
int i;
|
|
|
|
Lock();
|
|
for (i = 0; i < max; i++) if (sensors[i].name[0] != 0) {
|
|
if (name.compare(sensors[i].name) == 0) {
|
|
sensors[i].name[0] = 0;
|
|
sensors[i].ifname[0] = 0;
|
|
sensors[i].addr = 0;
|
|
sensors[i].flags = 0;
|
|
changed = 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
UnLock();
|
|
|
|
return 1;
|
|
};
|
|
|
|
|
|
//
|
|
// got some information from an interface..
|
|
void Sensors::SetFromBus(string name, int addr, int active) {
|
|
int i;
|
|
JSONParse jp;
|
|
|
|
for (i = 0; i < max; i++) if (sensors[i].name[0] != 0) {
|
|
if (strncmp (sensors[i].ifname, name.c_str(), REFERENCENAME_LEN) == 0 && sensors[i].addr == addr) {
|
|
debug (0, "* Sensor %s changed:%d", sensors[i].name, active);
|
|
if (sensors[i].flags & SENSOR_F_INVERSE) {
|
|
if (active) sensors[i].flags &= ~SENSOR_F_ACTIVE;
|
|
else sensors[i].flags |= SENSOR_F_ACTIVE;
|
|
}
|
|
else {
|
|
if (active) sensors[i].flags |= SENSOR_F_ACTIVE;
|
|
else sensors[i].flags &= ~SENSOR_F_ACTIVE;
|
|
}
|
|
|
|
jp.AddObject("sensor", _GetJSON(i));
|
|
if(network) network->ChangeListPushToAll(jp.ToString());
|
|
}
|
|
}
|
|
|
|
jp.AddObject("infoline", "Sensor:"+name+"["+ to_string(addr) +"]");
|
|
if(network) network->ChangeListPushToAll(jp.ToString());
|
|
|
|
};
|
|
|