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.
153 lines
2.2 KiB
153 lines
2.2 KiB
|
|
|
|
#include "modelbahn.h"
|
|
#include "block.h"
|
|
|
|
|
|
Blocks::Blocks () {
|
|
changed = 0;
|
|
blocks = (Block*) malloc(sizeof(Block)*SENSORS_MAX);
|
|
max = BLOCKS_MAX;
|
|
};
|
|
|
|
Blocks::~Blocks() {
|
|
free (blocks);
|
|
blocks = NULL;
|
|
max = 0;
|
|
};
|
|
|
|
|
|
|
|
|
|
int Blocks::Lock() {
|
|
if (pthread_mutex_lock(&mtx) == 0) return 1;
|
|
else return 0;
|
|
}
|
|
|
|
|
|
int Blocks::UnLock() {
|
|
if (pthread_mutex_unlock(&mtx) == 0) return 1;
|
|
else return 0;
|
|
}
|
|
|
|
|
|
JSONParse Blocks::_GetJSON(int idx) {
|
|
JSONParse json;
|
|
JSONElement je;
|
|
string s = "";
|
|
|
|
json.Clear();
|
|
|
|
s = blocks[idx].name; json.AddObject("name", s);
|
|
json.AddObject("flags", blocks[idx].flags);
|
|
|
|
return json;
|
|
};
|
|
|
|
|
|
JSONParse Blocks::GetJSON(string name) {
|
|
int i;
|
|
JSONParse jp;
|
|
|
|
jp.Clear();
|
|
|
|
Lock();
|
|
for (i = 0; i < max; i++) if (blocks[i].name[0] != 0) {
|
|
if (name.compare(blocks[i].name) == 0) {
|
|
jp = _GetJSON(i);
|
|
}
|
|
}
|
|
|
|
UnLock();
|
|
|
|
return jp;
|
|
};
|
|
|
|
|
|
void Blocks::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 = "blocks";
|
|
for (cnt = 0, i = 0; i < max; i++)
|
|
if (blocks[i].name[0] != 0) {
|
|
if (cnt != 0) je.value += ","; // not first element
|
|
je.value += _GetJSON(i).ToString();
|
|
cnt++;
|
|
}
|
|
json->AddObject(je);
|
|
|
|
UnLock();
|
|
};
|
|
|
|
|
|
Block Blocks::GetBlockFromJSON(JSONParse *j) {
|
|
Block bl;
|
|
string s;
|
|
|
|
bl.name[0] = 0;
|
|
bl.flags = 0;
|
|
|
|
j->GetValue("name", &s);
|
|
strncpy (bl.name, s.c_str(), REFERENCENAME_LEN);
|
|
j->GetValueInt("flags", &bl.flags);
|
|
|
|
return bl;
|
|
};
|
|
|
|
|
|
int Blocks::Change(Block *bl) {
|
|
int i;
|
|
int ifree = -1;
|
|
|
|
Lock();
|
|
|
|
for (i = 0; i < max; i++) {
|
|
if (blocks[i].name[0] != 0) {
|
|
// found element
|
|
if (strncmp(blocks[i].name, bl->name, REFERENCENAME_LEN) == 0) {
|
|
ifree = i;
|
|
break;
|
|
}
|
|
}
|
|
else if (ifree == -1) ifree = i;
|
|
}
|
|
// element not found add new element
|
|
if (ifree != -1 && ifree < max) {
|
|
blocks[ifree] = *bl;
|
|
strncpy (blocks[ifree].name, bl->name, REFERENCENAME_LEN);
|
|
}
|
|
|
|
changed = 1;
|
|
UnLock();
|
|
|
|
return 1;
|
|
};
|
|
|
|
|
|
int Blocks::Delete(string name) {
|
|
int i;
|
|
|
|
Lock();
|
|
for (i = 0; i < max; i++) if (blocks[i].name[0] != 0) {
|
|
if (name.compare(blocks[i].name) == 0) {
|
|
blocks[i].name[0] = 0;
|
|
blocks[i].flags = 0;
|
|
changed = 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
UnLock();
|
|
|
|
return 1;
|
|
};
|
|
|
|
|