#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); json.AddObject("lockedby", blocks[idx].lockedby); json.AddObject("sensor_pos_1", blocks[idx].s_pos_1); json.AddObject("sensor_center", blocks[idx].s_center); json.AddObject("sensor_neg_1", blocks[idx].s_neg_1); 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); // j->GetValue("lockedby", &s); // strncpy (bl.lockedby, s.c_str(), REFERENCENAME_LEN); s = ""; j->GetValue("sensor_pos_1", &s); strncpy (bl.s_pos_1, s.c_str(), REFERENCENAME_LEN); s = ""; j->GetValue("sensor_center", &s); strncpy (bl.s_center, s.c_str(), REFERENCENAME_LEN); s = ""; j->GetValue("sensor_neg_1", &s); strncpy (bl.s_neg_1, s.c_str(), REFERENCENAME_LEN); printf ("%s:%d Sensor (%s ---> %s ---> %s\n", __FILE__, __LINE__, bl.s_pos_1, bl.s_center, bl.s_neg_1); 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; }; // // thread will not be locked.. int Blocks::SetOff(string name) { int i; for (i = 0; i < max; i++) if (blocks[i].name[0] != 0) { if (name.compare(blocks[i].name) == 0) { changed = 1; blocks[i].flags |= BLOCK_F_OFF; printf ("%s:%d set block %s off", __FILE__, __LINE__, blocks[i].name); break; } } return 1; }; int Blocks::Clear(string name) { int i; for (i = 0; i < max; i++) if (blocks[i].name[0] != 0) { if (name.compare(blocks[i].name) == 0) { changed = 1; blocks[i].flags &= ~BLOCK_F_OFF; printf ("%s:%d clear block %s off", __FILE__, __LINE__, blocks[i].name); break; } } return 1; };