traffic_info.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #ifndef __DEF__H__
  2. #define __DEF__H__
  3. #include "point.h"
  4. #include <time.h>
  5. #include "line.h"
  6. #include <ctime>
  7. #include "classdef.h"
  8. NAMESPACE_POINT_BEGIN(NAMESPACE_POINT)
  9. enum Priority
  10. {
  11. PRI_INIT = 0,
  12. PRI_CROSSING= 1,
  13. PRI_AVOIDANCE=2,
  14. PRI_MANUALCONTROL=3
  15. };
  16. //和web对应
  17. enum LIGHT_COLOUR
  18. {
  19. INIT=0x01,
  20. RED,
  21. GREEN,
  22. SPARK
  23. };
  24. //红绿灯
  25. class TrafficLight :public point
  26. {
  27. public:
  28. TrafficLight(int32_t id,double x,double y,double z, int32_t readerid,std::string ip,int32_t specialFlag,int32_t port,uint32_t phy_id,uint8_t phy_dir)
  29. :point(x,y)
  30. ,m_id(id),z_(z)
  31. ,readerId_(readerid)
  32. ,m_ip(ip)
  33. ,m_specialFlag(specialFlag)
  34. ,m_directDist(0)
  35. ,m_state(GREEN)
  36. ,m_port(port)
  37. ,m_physics_light_id(phy_id)
  38. ,m_physics_direction(phy_dir)
  39. ,m_rec_time(time(NULL))
  40. ,light_state(0)
  41. {
  42. }
  43. void setState(LIGHT_COLOUR lc=GREEN)
  44. {
  45. m_state = lc;
  46. }
  47. void set(double xx,double yy,std::string ip,int flag,int readerid,int32_t port,uint32_t phy_id,uint8_t phy_dir)
  48. {
  49. x = xx;
  50. y = yy;
  51. m_ip = ip;
  52. m_specialFlag =flag;
  53. readerId_ = readerid;
  54. m_port = port;
  55. m_physics_light_id = phy_id;
  56. m_physics_direction = phy_dir;
  57. }
  58. inline int32_t getid(){return m_id;}
  59. inline LIGHT_COLOUR getState(){return m_state;}
  60. inline std::string getip(){return m_ip;}
  61. int32_t m_id;
  62. double z_;
  63. int32_t readerId_;
  64. std::string m_ip;
  65. int32_t m_specialFlag;
  66. double m_directDist;
  67. LIGHT_COLOUR m_state;
  68. int32_t m_port;
  69. op::line m_line;
  70. //add by zhuyf 2018/11/06
  71. uint32_t m_physics_light_id; // 物理灯号:红绿灯正反面共享此灯号
  72. uint8_t m_physics_direction; // 物理朝向:正面,反面
  73. uint64_t m_rec_time; // 红绿灯接收时间
  74. int light_state; // 红绿灯状态
  75. INT64 m_event_list[88]; // 保存事件Id
  76. };
  77. //红绿灯组
  78. class TrafficLightGroup : public point
  79. {
  80. public:
  81. TrafficLightGroup(double x, double y, double z,int32_t gId,int32_t mapid,int32_t areaid,double scope,int32_t ctime)
  82. :point(x,y)
  83. ,m_gId(gId),m_mapId(mapid),m_areaId(areaid)
  84. ,m_priority(PRI_INIT)
  85. //,m_ownerFlag(ATOMIC_FLAG_INIT)
  86. ,m_scope(scope)
  87. ,m_special(false)
  88. ,m_controlTime(ctime)
  89. ,m_timeStamp(0)
  90. ,m_used(false)
  91. ,m_nLightId(-1)
  92. {
  93. m_ownerFlag.clear();
  94. }
  95. TrafficLightGroup(double x, double y, double z,int32_t gId,int32_t mapid,int32_t areaid,double scope,int32_t ctime,uint16_t ait)
  96. :point(x,y)
  97. ,m_gId(gId),m_mapId(mapid),m_areaId(areaid)
  98. ,m_priority(PRI_INIT)
  99. //,m_ownerFlag(ATOMIC_FLAG_INIT)
  100. ,m_scope(scope)
  101. ,m_special(false)
  102. ,m_controlTime(ctime)
  103. ,m_timeStamp(0)
  104. ,m_auto_control_interval(ait)
  105. ,m_used(false)
  106. {
  107. m_ownerFlag.clear();
  108. }
  109. std::vector<std::tuple<int,int,int,std::string,int,std::string,int>> getTrafficLightInfo()
  110. {
  111. std::vector<std::tuple<int,int,int,std::string,int,std::string,int>> vt;
  112. for (const auto x:m_pTrafficLight)
  113. vt.push_back(std::make_tuple(m_gId,x->getid(),x->getState(),m_ControlName,m_ControlName.empty()?0:1,x->getip(),x->m_port));
  114. return std::move(vt);
  115. }
  116. void set(double xx,double yy,int32_t mapid,int32_t areaid,int32_t ct)
  117. {
  118. x = xx;
  119. y = yy;
  120. m_mapId = mapid;
  121. m_areaId = areaid;
  122. m_controlTime = ct;
  123. }
  124. void set(double xx,double yy,int32_t mapid,int32_t areaid,int32_t ct,uint16_t lat)
  125. {
  126. x = xx;
  127. y = yy;
  128. m_mapId = mapid;
  129. m_areaId = areaid;
  130. m_controlTime = ct;
  131. m_auto_control_interval = lat;
  132. }
  133. std::shared_ptr<TrafficLight> get(int32_t lightId)
  134. {
  135. for(const auto x:m_pTrafficLight)
  136. if (x->m_id == lightId)
  137. return x;
  138. return nullptr;
  139. }
  140. void put(std::shared_ptr<TrafficLight> t)
  141. {
  142. t->m_directDist = this->dist_direct(*t);
  143. std::cout << "Lemon: x:"<<x<<",y:"<<y<<"light x:"<<t->x<<",y:"<<t->y<<",dist:"<<t->m_directDist<<std::endl;
  144. line l(*this,point(t->x,t->y));
  145. t->m_line = l;
  146. m_pTrafficLight.push_back(t);
  147. if(t->m_specialFlag == 1)
  148. m_special = true;
  149. }
  150. bool getflag(){return m_special;}
  151. double scope(){return m_scope;}
  152. std::string showinfo()
  153. {
  154. std::stringstream ss;
  155. ss<<"GID:"<<m_gId<<",gx:"<<x<<",gy:"<<y<<",priority:"<<load()<<",scope="<<m_scope<<" special=" <<(m_special?"true":"false")<<" lights:";
  156. for(const auto px:m_pTrafficLight)
  157. ss<<"["<<px->m_id<<","<<px->x<<","<<px->y<<","<<px->m_state<<"]";
  158. return std::move(ss.str());
  159. }
  160. void store(int x)
  161. {
  162. m_priority.store(x);
  163. }
  164. int load()
  165. {
  166. return m_priority.load();
  167. }
  168. inline void set_status(const bool status)
  169. {
  170. m_used.store(status);
  171. }
  172. bool get_status()
  173. {
  174. return m_used.load();
  175. }
  176. bool timeout()
  177. {
  178. std::time_t t = time(NULL);
  179. if(t-m_timeStamp >= m_controlTime)
  180. return true;
  181. return false;
  182. }
  183. /*
  184. 设置灯组内红绿灯颜色,满足条件的设置为指定灯形状,不满足条件3个面的设为其他相同的灯形状
  185. */
  186. void setLight(int ld,LIGHT_COLOUR lc,LIGHT_COLOUR lc_rever)
  187. {
  188. for(auto & px : m_pTrafficLight)
  189. {
  190. if(px->m_id == ld)
  191. px->setState(lc);
  192. else
  193. px->setState(lc_rever);
  194. }
  195. }
  196. //手动设置
  197. void setCrossing(const std::string & cardid,Priority p)
  198. {
  199. m_card = cardid;
  200. store(p);
  201. }
  202. void setAvoidance(int ld,LIGHT_COLOUR lc,LIGHT_COLOUR lc_rever)
  203. {
  204. m_card.clear();
  205. m_timeStamp =0;
  206. store(PRI_AVOIDANCE);
  207. setLight(ld,lc,lc_rever);
  208. }
  209. void setManualControl(const std::string &name,int ld,LIGHT_COLOUR lc)
  210. {
  211. getTurn();
  212. m_ControlName = name;
  213. m_timeStamp = time(NULL);
  214. store(PRI_MANUALCONTROL);
  215. m_card.clear();
  216. for(auto & px:m_pTrafficLight)
  217. if(px->m_id == ld)
  218. px->setState(lc);
  219. releaseTurn();
  220. }
  221. bool Diff(int ld,LIGHT_COLOUR lc,LIGHT_COLOUR lc_rever)
  222. {
  223. bool b = false;
  224. getTurn();
  225. if (load()<=2)
  226. {
  227. for(auto & px : m_pTrafficLight)
  228. {
  229. if(px->m_id == ld)
  230. {
  231. if (px->getState() != lc)
  232. {
  233. b=true;
  234. break;
  235. }
  236. }
  237. else
  238. {
  239. if (px->getState() != lc_rever)
  240. {
  241. b=true;
  242. break;
  243. }
  244. }
  245. }
  246. }
  247. if (b)
  248. //setAvoidance(ld,lc,lc);
  249. setAvoidance(ld,lc,lc_rever);
  250. releaseTurn();
  251. return b;
  252. }
  253. void resetGroup()
  254. {
  255. store(PRI_INIT);
  256. m_timeStamp = 0;
  257. m_ControlName.clear();
  258. m_card.clear();
  259. m_sCardId = "";
  260. set_status(false);
  261. for(auto &px:m_pTrafficLight)
  262. px->setState();
  263. }
  264. void getTurn()
  265. {
  266. while(m_ownerFlag.test_and_set(std::memory_order_acquire));
  267. //std::cout << "getTurn.."<<m_gId<<std::endl;
  268. }
  269. void releaseTurn()
  270. {
  271. m_ownerFlag.clear(std::memory_order_release);
  272. //std::cout <<"releaseTurn..."<<m_gId<<std::endl;
  273. }
  274. std::string getId()const{return m_card;}
  275. bool noCar(){return m_card.empty();}
  276. void setId(std::string & id){m_card = id;}
  277. std::vector<std::shared_ptr<TrafficLight>> m_pTrafficLight; //include ligits
  278. int32_t m_gId,m_mapId,m_areaId; // group id;
  279. std::atomic<int> m_priority;//优先级 0 初始值 1 路口控制 2 避让控制 3手动控制
  280. //int m_priority;
  281. std::atomic_flag m_ownerFlag;
  282. double m_scope;
  283. bool m_special; //
  284. uint32_t m_controlTime;
  285. std::time_t m_timeStamp;
  286. std::string m_ControlName;
  287. std::string m_card; //如果是优先级1表示小车,2表示大车id
  288. uint32_t m_auto_control_interval; //灯组内红绿灯断网情况下自动控制的时间间隔
  289. std::atomic<bool> m_used;
  290. int m_nLightId; // 灯组被控制后变绿灯的灯ID
  291. std::string m_sCardId; // 绑定此灯组的卡号
  292. ~TrafficLightGroup()
  293. {}
  294. };
  295. NAMESPACE_POINT_END(NAMESPACE_POINT)
  296. #endif