common.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. #ifndef __COMMON_H_
  2. #define __COMMON_H_
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <stdbool.h>
  8. #include "sqlite3.h"
  9. #include "pthread.h"
  10. #include "list.h"
  11. #include "modbus_handle.h"
  12. #include "elog.h"
  13. #include "mongoose.h"
  14. #include "ntpclient.h"
  15. #include <net-snmp/net-snmp-config.h>
  16. #include <net-snmp/net-snmp-includes.h>
  17. #include <net-snmp/agent/net-snmp-agent-includes.h>
  18. #include "pduMIB_form.h"
  19. #include "pduMIB_scalar.h"
  20. #include "pduMIB_trap.h"
  21. #include "language_hashmap.h"
  22. #include "hlw8110_handle.h"
  23. #include "language_common.h"
  24. #include <sys/shm.h>
  25. #include "cfg.h"
  26. #define MAX_RELAY_CHN 100
  27. #define SURPPORT_OLD485 0
  28. //是否在OS环境下运行
  29. #define OS_HANDWARE 1
  30. #define WEB_ROOT "../web"
  31. #define PARAS_FILE "pduParas"
  32. #define DATABASE_NAME "pdu_dat.db"
  33. #define DATABASE_BAK_NAME "pdu_dat_bak.db"
  34. #define SNMP_MIB_NAME "pduMIB"
  35. #define INI_FILE_NAME "smartPDU.ini"
  36. //高配表头串口定义-----------------------------------------------
  37. //控制板通信串口
  38. #define INTERL_MODBUS_PORT "/dev/ttyAS5"
  39. #define INTERL_MODBUS_BAUD 115200
  40. //表头级联通讯串口
  41. // #define CASCADE_MODBUS_PORT "/dev/ttyAS4"
  42. // #define CASCADE_MODBUS_BAUD 115200
  43. //传感器通信串口
  44. // #define INTERL_SENSOR_MODBUS_PORT "/dev/ttyAS2"
  45. // #define INTERL_SENSOR_MODBUS_BAUD 9600
  46. //表头级联通讯串口
  47. #define CASCADE_MODBUS_PORT "/dev/ttyS5"
  48. #define CASCADE_MODBUS_BAUD 115200
  49. //传感器通信串口
  50. #define INTERL_SENSOR_MODBUS_PORT "/dev/ttyS1"
  51. #define INTERL_SENSOR_MODBUS_BAUD 9600
  52. #define IMAGE_PATH "image"
  53. #define LOGO_FILE "logo.png"
  54. #define QRCODE_FILE "qrcode.png"
  55. #define AC_SINGLE_S_CUR_CHN_NUM 8
  56. #define AC_SINGLE_B_CUR_CHN_NUM 4
  57. #define AC_SINGLE_S_MAX_CHN_NUM 10 //单个控制板最大数量
  58. #define AC_SINGLE_S_MAX_ADDRESS_NUM 16 //控制板最大数量
  59. #define AC_MAX_VOLTAGE_VAL 400
  60. #define AC_MAX_CURRENT_VAL 100
  61. #define AC_MAX_POWER_VAL 100
  62. enum
  63. {
  64. TOTAL_VOLTAGE_TYPE = 0,//统计电压类型
  65. TOTAL_CURRENT_TYPE = 1,//统计电流类型
  66. TOTAL_POWER_TYPE = 2,//统计功率类型
  67. TOTAL_POWER_CONS_TYPE = 3 //统计耗电量类型
  68. };
  69. enum
  70. {
  71. POWER_MERITORIOUS_TYPE = 0,//有功功率类型
  72. POWER_REACTIVE_TYPE = 1,//无功功率类型
  73. POWER_APPARENT_TYPE = 2 //视在功率类型
  74. };
  75. enum
  76. {
  77. SENSOR_TYPE_TEMPERATURE = 0 , //温度传感器
  78. SENSOR_TYPE_SMOKE = 1 , //烟雾传感器
  79. SENSOR_TYPE_WATER = 2 , //水浸传感器
  80. SENSOR_TYPE_DOOR = 3 , //门禁传感器
  81. SENSOR_TYPE_GAS = 4 , //气体传感器
  82. SENSOR_TYPE_AIR_PRESS = 5 , //气体、气压传感器
  83. SENSOR_TYPE_DoubleT = 7 //双头温度
  84. };
  85. enum
  86. {
  87. SENSOR_VAL1_UPPER = 0,
  88. SENSOR_VAL1_LOWER = 1,
  89. SENSOR_VAL2_UPPER = 2,
  90. SENSOR_VAL2_LOWER = 3,
  91. };
  92. enum
  93. {
  94. SENSOR_TYPE_VALUE = 0 , //传感器类型0、4、5
  95. SENSOR_TYPE_LIST =1 //传感器类型1、2、3
  96. };
  97. /// @brief 板子类型
  98. enum MODBUS_BOARD_TYPE
  99. {
  100. AC_SINGLE_S_TYPE=1,
  101. AC_SINGLE_B_TYPE,
  102. AC_MULTI_S_TYPE,
  103. AC_MULTI_B_TYPE,
  104. DC_OUT_TYPE,
  105. DC_IN_TYPE,
  106. DCPDU_TYPE,
  107. TREE_AC_TYPE, //三相进三项出
  108. };
  109. enum AppProductType
  110. {
  111. SmartPDU_AC=0,
  112. SmartPDU_DC=1,
  113. SmartPDU_Tree_AC_Tree=3,
  114. SmartPDU_Tree_AC_One=4,
  115. SmartPDU_Tree_AC_One_B=5,
  116. };
  117. /// @brief 三相网页数据
  118. static char* g_product_t_ac_type_str[3] = {
  119. "L1",
  120. "L2",
  121. "L3",
  122. };
  123. /// @brief 按位标记
  124. enum BIT_CTRL
  125. {
  126. BIT_00 = 1 ,
  127. BIT_01 = 2 ,
  128. BIT_02 = 4 ,
  129. BIT_03 = 8 ,
  130. BIT_04 = 16,
  131. BIT_05 = 32,
  132. BIT_06 = 64,
  133. BIT_07 = 128,
  134. BIT_08 = 256,
  135. BIT_09 = 512,
  136. BIT_10 = 1024,
  137. BIT_11 = 2048,
  138. BIT_12 = 4096,
  139. BIT_13 = 8192,
  140. BIT_14 = 16384,
  141. BIT_15 = 32768,
  142. };
  143. /// @brief 使能位
  144. enum ENABLE_BIT_CTRL
  145. {
  146. ENABLE_V_UP = 2 ,
  147. ENABLE_V_DOWN = 8 ,
  148. ENABLE_A_UP = 32,
  149. ENABLE_W_UP = 128,
  150. ENABLE_P_UP = 512,
  151. ENABLE_ALL_UP = BIT_11,
  152. ENABLE_ALL_DOWN = BIT_12,
  153. };
  154. /// @brief 使能位三项
  155. enum ENABLE_TreeAC_CTRL
  156. {
  157. ENABLE_TAC_V_UP = BIT_01,
  158. ENABLE_TAC_V_DOWN = BIT_02,
  159. ENABLE_TAC_A_UP = BIT_03,
  160. ENABLE_TAC_W_UP = BIT_04,
  161. ENABLE_TAC_P_UP = BIT_05,
  162. ENABLE_TAC_STIME = BIT_06,
  163. ENABLE_TAC_ETIME = BIT_07,
  164. };
  165. /// @brief 报警位
  166. enum WARNING_BIT_CTRL
  167. {
  168. WARNING_V_UP = 4 ,
  169. WARNING_V_DOWN = 16 ,
  170. WARNING_A_UP = 64,
  171. WARNING_W_UP = 256,
  172. WARNING_P_UP = 1024,
  173. };
  174. enum {
  175. ALARM_TYPE_POWER=0,
  176. ALARM_TYPE_SENSOR,
  177. ALARM_TYPE_NETWORK,
  178. ALARM_TYPE_OPRATION,
  179. ALARM_TYPE_RUNNING,
  180. };
  181. enum {
  182. WARNING_IP_V_UP = 1,
  183. WARNING_IP_V_DOWN = 2,
  184. WARNING_IP_I_UP = 4,
  185. WARNING_IP_P_UP = 8,
  186. WARNING_IP_C_UP = 16,
  187. };
  188. enum {
  189. NETWORK_MAC_CHANGED=0,
  190. NETWORK_IP_CHANGED,
  191. };
  192. /// @brief 报警动作
  193. enum ALARM_CTRL
  194. {
  195. ALARM_CTRL_CLOSE_CH = 1 ,
  196. ALARM_CTRL_ALARM_ONLY = 2 ,
  197. ALARM_CTRL_CLOSE_ALLCH = 3 ,
  198. };
  199. ///////////////////////////////////////////////////////////////////////////////////
  200. typedef struct {
  201. int id;
  202. int up;
  203. char ip[48];
  204. char mac[24];
  205. char desc[64];
  206. }port_info_t;
  207. typedef struct {
  208. #define SW_PORT_MAX 28
  209. port_info_t port[SW_PORT_MAX];
  210. }netswitch_info_t;
  211. ///////////////////////////////////////////////////////////////////////////////////
  212. //modbus信息管理
  213. typedef struct
  214. {
  215. bool product_modbus_type ; //modbus模式
  216. unsigned int product_modbus_baud ; //波特率
  217. unsigned char product_modbus_addr ;//从Modbus地址信息
  218. char product_relay_ctrl_port[32] ;//继电器modbus控制端口
  219. }ModbusInfo_t;
  220. typedef struct
  221. {
  222. int mode; //1: static 0: dhcp
  223. char ip_address[64] ; //ip地址
  224. char mask[64]; //子网掩码
  225. char gateway[64]; //网关
  226. char dns1[64]; //dns1
  227. char dns2[64]; //dns2
  228. }NetworkInfo_t;
  229. /// @brief 用户登录
  230. typedef struct
  231. {
  232. char user_name[64];
  233. char user_password[64];
  234. char user_uuid[64];
  235. char user_token[64];
  236. struct list_head list;
  237. }_User_Log;
  238. //设备总体管理表
  239. typedef struct
  240. {
  241. int product_id ;//产品ID
  242. char product_number[16] ;//产品编号
  243. char product_name[32] ;//产品名称
  244. char product_status[32] ;//产品状态
  245. unsigned char product_type ;//产品类型
  246. unsigned char product_pwr_type ;//电源类型
  247. char product_version[8] ;//软件版本
  248. unsigned char product_language ;//多国语言
  249. unsigned char product_count ;//级联设备数
  250. unsigned short product_samp_time ;//采集周期 单位ms
  251. unsigned short product_save_time ;//采集周期 单位ms
  252. ModbusInfo_t _modbus_info ;//继电器控制端口
  253. NetworkInfo_t _network_info ; ;//网络信息
  254. ModbusInfo_t _gmodbus_info ;//级联端口
  255. float consumer ;
  256. }GlobalDeviceInfo;
  257. ///////////////////////////////////////////////////////////////////////////////////
  258. ///////////////////////////////////////////////////////////////////////////////////
  259. //自定义共享内存类
  260. typedef struct
  261. {
  262. //电源数据
  263. int bPDUSet;
  264. int nSelectPowerCH;
  265. int nMaxCh;
  266. float fV;
  267. float fA;
  268. float fW;
  269. float fF;
  270. float fS;
  271. int nStatus;
  272. //传感器数据
  273. int nSelectSensor;
  274. float fT;//温度
  275. float fH;//湿度
  276. } smartPDU_shm_data;
  277. ///////////////////////////////////////////////////////////////////////////////////
  278. ///////////////////////////////////////////////////////////////////////////////////
  279. //超限管理表
  280. typedef struct
  281. {
  282. char product_vol_upper_enable ;//电压上限使能
  283. float product_vol_upper_threshold ;//电压上限值
  284. char product_vol_over_upper_threshold_ctrl ;//电压超限动作
  285. char product_vol_over_upper_threshold_ctrl_para ;//电压超限关联参数
  286. char product_vol_lower_enable ;//电压下限使能
  287. float product_vol_lower_threshold ;//电压下限值
  288. char product_vol_over_lower_threshold_ctrl ;//电压超限动作
  289. char product_vol_over_lower_threshold_ctrl_para ;//电压超限动作
  290. char product_cur_upper_enable ;//电流下限使能
  291. float product_cur_upper_threshold ;//电流下限值
  292. float product_cur_lower_threshold; //
  293. char product_cur_over_upper_threshold_ctrl ;//电流超限动作
  294. char product_cur_over_upper_threshold_ctrl_para ;//电流超限动作
  295. char product_pwr_upper_enable ;//功率上限使能
  296. float product_pwr_upper_threshold ;//功率上限值
  297. char product_pwr_over_upper_threshold_ctrl ;//功率超限动作
  298. char product_pwr_over_upper_threshold_ctrl_para ;//功率超限动作
  299. char product_pwrcon_upper_enable ;//耗电量上限使能
  300. float product_pwrcon_upper_threshold ;//耗电量上限值
  301. char product_pwrcon_over_upper_threshold_ctrl ;//耗电量超限动作
  302. char product_pwrcon_over_upper_threshold_ctrl_para ;//耗电量超限动作
  303. }GlobalOverManager;
  304. /// @brief 定时管理
  305. typedef struct
  306. {
  307. char set_date[16]; //设置日期
  308. char ds_date[16]; //设置时间
  309. char ds_time[16]; //设置时间
  310. int set_stats; //状态
  311. int ds_is_triggered; //是否已触发
  312. struct list_head list;
  313. }_PowerDSManage_t;
  314. typedef struct
  315. {
  316. int firingSortno; //启动序号
  317. int closeSortno; //关闭序号
  318. }_PowerSXManage_t;
  319. typedef struct
  320. {
  321. int product_id;
  322. int mode;
  323. char ntpName[64];
  324. char ntpaddress[64];
  325. int port;
  326. char time[64];
  327. int weekday;
  328. int tzone;
  329. }NTPManager;
  330. NTPManager _globalNTPManager;
  331. enum
  332. {
  333. SNMP_AUTH_NONE = 0,
  334. SNMP_AUTH_MD5,
  335. SNMP_AUTH_SHA,
  336. SNMP_AUTH_MAX
  337. };
  338. enum
  339. {
  340. SNMP_ENCYPT_NONE = 0,
  341. SNMP_ENCYPT_DES,
  342. SNMP_ENCYPT_AES,
  343. SNMP_ENCYPT_MAX
  344. };
  345. typedef struct
  346. {
  347. char alg[12];
  348. char passwd[64];
  349. } crypt_data_t;
  350. typedef struct
  351. {
  352. char user[32];
  353. crypt_data_t auth;
  354. crypt_data_t priv;
  355. } crypt_t;
  356. typedef struct
  357. {
  358. int product_id;
  359. int mode;
  360. int version;
  361. char public[64];
  362. int trapmode;
  363. crypt_t crypt;
  364. char nmsIP[64];
  365. }SNMPManager;
  366. SNMPManager _globalSNMPManager;
  367. //分组管理
  368. typedef struct
  369. {
  370. int product_id;
  371. int GroupID;
  372. char GroupName[64];
  373. char GroupChList[512];
  374. struct list_head list;
  375. }GroupInfo;
  376. //电源部分
  377. typedef struct
  378. {
  379. float voltage ;//电压
  380. float current ;//电流
  381. float power ;//功率
  382. float freq ;//频率
  383. float consumption ;//耗电量
  384. float factor ;//功率因素
  385. unsigned short status ;//开关状态
  386. unsigned short NF_status ;//零线状态
  387. float tempratue ;
  388. float humidity ;
  389. port_info_t port; //
  390. }PowerInfo;
  391. //电源部分报警信息
  392. typedef struct
  393. {
  394. bool w_voltage_up ;//电压上限报警
  395. bool w_voltage_down ;//电压下限报警
  396. bool w_current ;//电流上限报警
  397. bool w_power ;//功率
  398. bool w_consumption ;//耗电量
  399. bool w_phase_lossA; // 缺相报警
  400. bool w_phase_lossB; // 缺相报警
  401. bool w_phase_lossC; // 缺相报警
  402. }PowerWarningInfo;
  403. //三项数据
  404. typedef struct
  405. {
  406. int product_id;
  407. int product_ch_id;
  408. int product_ph_id;
  409. int product_ph_type;//0:A相 1:B相 2:C相
  410. int product_saddr;
  411. int product_ch_addr;
  412. int product_ph_outputType;//三相控制方式1,三项2单项
  413. int product_ph_outputStatus;//该相是否输出,1输出2不输出
  414. PowerInfo _PowerInfo; //存储最新的数据(兼容三项输入)
  415. GlobalOverManager* global_over_manager ;//超限管理
  416. PowerWarningInfo _PowerWarninginfo; //存储报警数据(兼容三项输入)
  417. time_t product_log_time; //存储最新时间
  418. struct list_head list_Tree_AC; //存储三项数据指针
  419. }GlobalTreeACManager;
  420. //电源通道管理总表(输出)
  421. typedef struct
  422. {
  423. int product_id ;//产品ID
  424. //20240225 add
  425. char product_saddr ;//modbus地址记录
  426. char product_ch_id ;//通道号
  427. char product_ch_addr ;//通道号实际地址
  428. char product_ch_name[32] ;//通道名称
  429. char product_ch_type ;//通道类型
  430. char product_ch_status ;//通道状态
  431. char product_ch_NF_status ;//通道零线状态(三相)
  432. unsigned short product_ch_start_delay ;//通道延时
  433. unsigned short product_ch_stop_delay ;//通道延时
  434. GlobalOverManager* global_over_manager ;//超限管理
  435. struct list_head list_DS; //定时设置
  436. _PowerSXManage_t* _PowerSXManage; //时序管理
  437. PowerInfo _PowerInfo; //存储最新的数据
  438. PowerWarningInfo _PowerWarninginfo ; //存储报警数据
  439. time_t product_log_time; //存储最新时间
  440. struct list_head list_Tree_AC; //存储三项数据指针
  441. struct list_head list;
  442. } GlobalPowerManger;
  443. //电源通道管理总表(输入)
  444. typedef struct
  445. {
  446. int product_id ;//产品ID
  447. //20240225 add
  448. char product_saddr ;//modbus地址记录
  449. char product_saddr_name[32] ;//输入名称
  450. char product_saddr_type ;//输入类型
  451. GlobalOverManager* global_over_manager ;//超限管理
  452. PowerInfo _PowerInfo[3]; //存储最新的数据(兼容三项输入)
  453. PowerWarningInfo _PowerWarninginfo[3]; //存储报警数据(兼容三项输入)
  454. time_t product_log_time; //存储最新时间
  455. int product_MissingPH[3]; //缺相报警
  456. }GlobalPowerInManger;
  457. //电源控制板计数
  458. typedef struct
  459. {
  460. char product_saddr ;//modbus地址记录
  461. int product_number; //数量
  462. int product_type; //类型
  463. PowerInfo _PowerInfo[AC_SINGLE_S_MAX_CHN_NUM]; //存储最新的数据
  464. PowerWarningInfo _PowerWarninginfo[AC_SINGLE_S_MAX_CHN_NUM] ;//存储报警数据
  465. struct list_head list; //存储具有的通道指针
  466. GlobalPowerInManger _globalPowerInManger; //总输入数据
  467. }GlobalPowerCtrlBoard;
  468. ///////////////////////////////////////////////////////////////////////////////////
  469. ///////////////////////////////////////////////////////////////////////////////////
  470. //电源信息统计
  471. typedef struct
  472. {
  473. float ydata[60]; //value
  474. char xAxis[60][32];//时间点
  475. }GetTotalInfo;
  476. //系统总数信息
  477. typedef struct
  478. {
  479. int channel_total ;//通道总数
  480. int warn_total ;//告警总数
  481. int sensor_total ;//传感器总数
  482. int log_total ;//运行日志总数
  483. }SystemTotalInfo;
  484. //功率趋势接口
  485. typedef struct
  486. {
  487. float ydata[3][60] ;//0:有功功率;1:无功功率;2:视在功率;
  488. char xAxis[60][32] ;//时间点
  489. }PowerTrendInfo;
  490. ///////////////////////////////////////////////////////////////////////////////////
  491. ///////////////////////////////////////////////////////////////////////////////////
  492. //电源监控
  493. typedef struct
  494. {
  495. int product_id ;//产品ID
  496. char product_ch_id ;//电源通道
  497. unsigned short product_status ;//当前状态
  498. PowerInfo _power_info ;//采集值
  499. char samp_time[64] ;//采集时间
  500. GetTotalInfo _total_voltage ;//统计电压
  501. GetTotalInfo _total_current ;//统计电流
  502. GetTotalInfo _total_power ;//统计功率
  503. GetTotalInfo _total_consumption ;//统计耗电量
  504. SystemTotalInfo _system_total_info ;//系统总数信息
  505. PowerTrendInfo _power_trend_info ;//功率趋势图
  506. struct list_head list;
  507. }GlobalPowerInfo;
  508. ///////////////////////////////////////////////////////////////////////////////////
  509. ///////////////////////////////////////////////////////////////////////////////////
  510. ///////////////////////////////////////////////////////////////////////////////////
  511. ///////////////////////////////////////////////////////////////////////////////////
  512. //传感器值
  513. typedef struct
  514. {
  515. char samp_time[64] ;//采集时间
  516. int product_id ;//产品ID
  517. char sensor_id ;//传感器ID
  518. float val1 ;//值1
  519. float val2 ;//值2
  520. time_t product_log_time; //存储最新时间
  521. struct list_head list;
  522. }GlobalSensorInfo;
  523. typedef struct
  524. {
  525. bool sensor_val1_upper_enabled ;//
  526. float sensor_val1_upper_threshold;
  527. char sensor_val1_upper_threshold_ctrl;
  528. char sensor_val1_upper_threshold_ctrl_para[512];
  529. bool sensor_val1_lower_enabled ;//
  530. float sensor_val1_lower_threshold;
  531. char sensor_val1_lower_threshold_ctrl;
  532. char sensor_val1_lower_threshold_ctrl_para[512];
  533. bool sensor_val2_upper_enabled ;//
  534. float sensor_val2_upper_threshold;
  535. char sensor_val2_upper_threshold_ctrl;
  536. char sensor_val2_upper_threshold_ctrl_para[512];
  537. bool sensor_val2_lower_enabled ;//
  538. float sensor_val2_lower_threshold;
  539. char sensor_val2_lower_threshold_ctrl;
  540. char sensor_val2_lower_threshold_ctrl_para[512];
  541. }GlobalSensorOverManage;
  542. //传感器报警状态表
  543. typedef struct
  544. {
  545. bool sensor_val1_upper;
  546. bool sensor_val1_lower;
  547. bool sensor_val2_upper;
  548. bool sensor_val2_lower;
  549. }GlobalSensorOverInfo;
  550. //传感器管理总表
  551. typedef struct
  552. {
  553. int product_id ;//产品ID
  554. char sensor_id ;//传感器ID
  555. char sensor_type ;//传感器类型
  556. char sensor_interface_type; //接口类型
  557. char sensor_name[64] ;//传感器名称
  558. char sensor_port_name[32] ;//传感器端口号
  559. int sensor_baud ;//波特率
  560. char sensor_addr ;//modbus地址
  561. int sensor_status ;//传感器状态
  562. char sensor_val_count ;//传感器值个数
  563. int sensor_node_number ;//节点号
  564. GlobalSensorInfo Cur_sensor_info ;
  565. GlobalSensorOverManage* over_manager ;//超限管理
  566. GlobalSensorOverInfo warning_status;//报警状态
  567. struct list_head list;
  568. }GlobalSensorManger;
  569. ///////////////////////////////////////////////////////////////////////////////////
  570. ///////////////////////////////////////////////////////////////////////////////////
  571. ///////////////////////////////////////////////////////////////////////////////////
  572. ///////////////////////////////////////////////////////////////////////////////////
  573. ///温湿度数据信息管理
  574. typedef struct
  575. {
  576. float temperature;
  577. float humidity;
  578. }SenorTempVal;
  579. //采集KB值
  580. typedef struct
  581. {
  582. float voltage_k;//电压K
  583. float voltage_b;//电压B
  584. float current_k;//电流K
  585. float current_b;//电流B
  586. }SwitchKbVal;
  587. ///////////////////////////////////////////////////////////////////////////////////
  588. ///////////////////////////////////////////////////////////////////////////////////
  589. //SMTP管理
  590. typedef struct
  591. {
  592. int idx;
  593. int use;
  594. char account[64];
  595. }smtp_recv_t;
  596. typedef struct
  597. {
  598. int id;
  599. int mode; //0: open 1: close
  600. char account[64]; //用户账号
  601. char password[24]; //用户密码
  602. char server[64]; //服务器
  603. char port[10]; //端口
  604. char auth[16]; //认证方式
  605. }smtp_send_t;
  606. typedef struct
  607. {
  608. smtp_send_t send;
  609. #define SMTP_RECV_MAX 10
  610. smtp_recv_t recv[SMTP_RECV_MAX]; //接收账号列表
  611. }smtp_info_t;
  612. typedef struct {
  613. int id; //id
  614. int mode; //0: open 1: close
  615. char server[512]; //server
  616. char ip[48]; //server ip
  617. char port[10]; //server port
  618. char cid[32]; //client ID
  619. char user[32]; //认证方式
  620. char password[32];
  621. }mqtt_server_t;
  622. typedef struct {
  623. #define MQTT_SERV_MAX 10
  624. mqtt_server_t serv[MQTT_SERV_MAX];
  625. }mqtt_info_t;
  626. typedef struct
  627. {
  628. int product_id;
  629. int userID; //用户ID
  630. char* userName; //用户名-登录
  631. char* userPW; //用户密码
  632. char* nickName; //用户名称
  633. int roleID; //角色ID
  634. char* createTime; //用户创建时间
  635. struct list_head list;
  636. }UserInfo;
  637. typedef struct
  638. {
  639. int product_id;
  640. int roleID; //角色ID
  641. char* roleName; //角色名称
  642. int roleLevel; //角色等级
  643. char* roleAuthority; //角色权限
  644. int roleSort; //显示顺序
  645. char* createTime; //创建时间
  646. struct list_head list;
  647. }RoleInfo;
  648. typedef struct MenuAuthority{
  649. bool PowerEnabled;
  650. bool SensorEnabled;
  651. bool AlarmEnabled;
  652. bool LogEnabled;
  653. bool UserInfoEnabled;
  654. bool CommEnabled;
  655. bool ServiceEnabled;
  656. bool MaintenanceEnabled;
  657. //1、电源监控菜单
  658. bool MonitorallEnabled;
  659. bool ChannelstatusEnabled;
  660. bool SwitchtatusEnabled;
  661. bool MonitorhistoryEnabled;
  662. //2、传感器管理菜单
  663. bool SensorStatusEnabled;
  664. bool SensorHistorylEnabled;
  665. //3、告警管理菜单
  666. bool XtjgEnabled;
  667. bool YzszEnabled;
  668. //4、日志信息菜单
  669. bool CzrzEnabled;
  670. bool YxrzEnabled;
  671. //5、用户管理菜单
  672. bool UserEnabled;
  673. bool RoleEnabled;
  674. bool MenuEnabled;
  675. //6、通信管理菜单
  676. bool WkszEnabled;
  677. bool RsszEnabled;
  678. //7、服务管理菜单
  679. bool SmtpszEnabled;
  680. bool SnmpszEnabled;
  681. bool MqszEnabled;
  682. bool NtpszEnabled;
  683. bool DsszEnabled;
  684. bool SxszEnabled;
  685. bool FzszEnabled;
  686. //8、设备维护菜单
  687. bool SbcqEnabled;
  688. bool GjsjEnabled;
  689. bool HhccszEnabled;
  690. bool PzdrdcEnabled;
  691. bool ShfwEnabled;
  692. bool GysbEnabled;
  693. }MenuAuthority;
  694. typedef struct CascadeDevices{
  695. int product_id; //级联设备ID modbus地址
  696. int type; //
  697. char device_name[20]; //接连设备名;
  698. float valtage; //485
  699. float current;
  700. float power;
  701. float factor;
  702. float freq;
  703. float consumption;
  704. }CascadeDevices;
  705. struct modbus_slave_read_reg{
  706. uint16_t deveice_type; //设备类型
  707. uint16_t slave_addr; //设备地址
  708. uint32_t voltage; //当前电压
  709. uint32_t current; //当前电流
  710. uint32_t power; //当前功率
  711. uint32_t consumption_cur; //耗电量
  712. uint32_t freq; //频率
  713. uint32_t factor; //功率因素
  714. uint32_t temprature; //温度值
  715. uint32_t humidity; //湿度值
  716. uint32_t warning; //告警
  717. uint16_t power_supplay_status; //供电状态
  718. uint16_t sencor_status; //传感器状态
  719. uint32_t temprature2; //温度值2
  720. // uint32_t Warinfo;
  721. // uint16_t sercor_type;
  722. };
  723. typedef struct cascade_pw_info{
  724. uint32_t voltage ;//电压
  725. uint32_t current ;//电流
  726. uint32_t power ;//功率
  727. uint32_t freq ;//频率
  728. uint32_t consumption ;//耗电量
  729. uint32_t factor ;//功率因素
  730. }cascade_single_chn_info; //siggle hell
  731. typedef struct temp_hum{
  732. uint16_t temperature;
  733. uint16_t humidity;
  734. }temp_hum_t;
  735. #pragma pack (1)
  736. typedef struct cascade_single_mmap{
  737. uint16_t dev_chn;
  738. cascade_single_chn_info all_ch[128];
  739. uint32_t total_valtage;
  740. uint32_t total_current;
  741. uint32_t total_power;
  742. uint32_t total_consumer;
  743. temp_hum_t total_temp;
  744. uint16_t chn_status[64];
  745. uint16_t open_delay[64];
  746. uint16_t close_delay[64];
  747. uint16_t reservers[29];
  748. uint16_t set_chn_open_delay[64];
  749. uint16_t set_chn_close_delay[64];
  750. uint16_t set_chn_switch_status[64];
  751. uint16_t set_chn_all_switch_status;
  752. }cascade_single_mmap_t;
  753. #pragma pack ()
  754. #pragma pack (1)
  755. #define MAX_CHN 64
  756. typedef struct cascade_triphasic_pw_out{
  757. uint32_t o_voltage;
  758. uint32_t o_current;
  759. uint32_t o_power;
  760. uint32_t o_consumption;
  761. uint32_t o_status;
  762. }cascade_triphasic_pw_out_t;
  763. typedef struct cascade_triphasic_pw_o_chn{
  764. cascade_triphasic_pw_out_t chn[3]; // A B C
  765. }cascade_triphasic_pw_o_chn_t;
  766. typedef struct cascade_triphasic_pw_in{
  767. uint32_t i_total_voltage;
  768. uint32_t i_total_current;
  769. uint32_t i_total_power;
  770. uint32_t i_total_consumption;
  771. }cascade_triphasic_pw_in_t;
  772. typedef struct cascade_triphasic_pw_i_total{
  773. cascade_triphasic_pw_in_t pw_i_total[3]; // A B C
  774. }cascade_triphasic_pw_i_total_t;
  775. typedef struct cascade_triphasic_chn_info{
  776. uint32_t chn_power;
  777. uint32_t chn_freq;
  778. uint32_t chn_consumption;
  779. uint32_t chn_factor;
  780. uint32_t chn_type;
  781. }cascade_triphasic_chn_info_t;
  782. typedef struct cascade_triphasic_mmap{
  783. uint16_t dev_chn;
  784. cascade_triphasic_pw_i_total_t i_total;
  785. cascade_triphasic_chn_info_t chn_info[MAX_CHN];
  786. cascade_triphasic_pw_o_chn_t pw_o_chn[MAX_CHN];
  787. uint16_t temperature;
  788. uint16_t humidity;
  789. uint16_t chn_status[MAX_CHN];
  790. uint16_t open_delay[MAX_CHN];
  791. uint16_t close_delay[MAX_CHN];
  792. uint16_t set_chn_open_delay[MAX_CHN];
  793. uint16_t set_chn_close_delay[MAX_CHN];
  794. uint16_t set_chn_switch_status[MAX_CHN];
  795. uint16_t set_chn_all_switch_status;
  796. }cascade_triphasic_mmap_t;
  797. typedef struct case_ip_485_data{
  798. uint16_t saddr;
  799. uint16_t status;
  800. uint16_t temprature;
  801. uint16_t humidity;
  802. uint16_t temprature_status;
  803. uint32_t voltage;
  804. uint32_t current;
  805. uint32_t power;
  806. uint32_t consumer;
  807. uint32_t factor;
  808. uint32_t freq;
  809. //uint32_t angle;
  810. }case_ip_485_data_t;
  811. typedef struct
  812. {
  813. modbus_t *ctx;
  814. char ip[64];
  815. uint16_t port;
  816. uint8_t slave_id;
  817. uint8_t power_type;
  818. modbus_mapping_t *map;
  819. int connection;
  820. pthread_mutex_t mutex;
  821. }Modbus_Manger_Tcp;
  822. typedef struct {
  823. uint16_t waning;
  824. }case_ip_wanning_t;
  825. typedef struct cascade_ip_mmap{
  826. uint16_t dev_chn;
  827. uint16_t mac[6];
  828. case_ip_485_data_t data[32];
  829. case_ip_wanning_t waning[32];
  830. }cascade_ip_mmap_t;
  831. #pragma pack ()
  832. struct modbus_slave_write_reg{
  833. uint16_t voltage_high_enable:1; //电压高阈值检测使能
  834. uint16_t voltage_low_enable:1; //电压低阈值检测使能
  835. uint16_t current_enable:1; //电流阈值使能
  836. uint16_t power_enable:1; //功率阈值使能
  837. uint16_t consumption_cur_enable:1; //功耗阈值使能
  838. uint32_t voltage_upper_threshold; //
  839. uint32_t voltage_lower_threshold;
  840. uint32_t current_upper_threshold;
  841. uint32_t power_upper_threshold;
  842. uint32_t consumption_threshold;
  843. };
  844. enum
  845. {
  846. DI,
  847. RS485
  848. };
  849. enum{
  850. SWITCH_CONNETCTED,
  851. SWITCH_DISCONNETCTED,
  852. SWITCH_OFFLINE
  853. };
  854. #define SWITCH_CHN_1_SET (1<<0)
  855. #define SWITCH_CHN_2_SET (1<<1)
  856. #define SWITCH_CHN_3_SET (1<<2)
  857. #define SWITCH_CHN_4_SET (1<<3)
  858. #define SWITCH_CHN_5_SET (1<<4)
  859. #define SWITCH_CHN_6_SET (1<<5)
  860. #define SWITCH_CHN_DI_1_SET (1<<6)
  861. #define SWITCH_CHN_DI_2_SET (1<<7)
  862. typedef struct breaker_switch_sta{
  863. uint16_t status;
  864. uint16_t gather;
  865. char com[30];
  866. uint8_t product_name[256];
  867. }breaker_switch_sta_t;
  868. typedef struct GlobalBreakerManger{
  869. uint32_t all_count;
  870. uint32_t online_count;
  871. uint32_t mask;
  872. uint8_t saddr;
  873. void *manager;
  874. breaker_switch_sta_t sta[8]; // 6 + 2
  875. pthread_mutex_t b_mutex;
  876. }GlobalBreakerManger;
  877. typedef struct GlobalBreakerManager{
  878. uint32_t product_id;
  879. uint32_t breaker_id;
  880. uint32_t breaker_chn;
  881. uint32_t breaker_status;
  882. uint32_t breaker_gather_addr;
  883. char breaker_switch_name[128];
  884. char breaker_com[64];
  885. char breaker_gather_type[64];
  886. char breaker_chns_name[128];
  887. struct list_head list;
  888. }GlobalBreakerManager;
  889. //设备总体管理
  890. typedef struct
  891. {
  892. sqlite3* db; //数据库接口
  893. pthread_mutex_t dbThreadlock; //写入线程锁
  894. pthread_t appweb_thread; //appweb线程
  895. pthread_t dev_samp_thread;//电源采集与控制线程
  896. pthread_t dev_update_thread;//电源采集与控制线程
  897. pthread_t io_thread; //IO采集线程
  898. pthread_t sensor_thread; //传感器采集现成
  899. bool dev_samp_flag ; //电源采集线程标志
  900. GlobalDeviceInfo _globalDevInfo; //设备管理信息
  901. GlobalPowerManger _globalPowerManger; //电源管理表
  902. GlobalSensorManger _globalSensorManger; //传感器管理表
  903. Modbus_Manger sensor_modbus_manager; //通信接口管理
  904. unsigned int useSlaveCount ; //有效地址
  905. Modbus_Manger _globalRelaySampManger; //内部继电器使用
  906. unsigned char uart_init_flag[6] ; //
  907. bool ws_isok; //ws准备好
  908. struct mg_connection *ws_client; //websocket管理信息
  909. HashTable* alarmTab; //全局的报警映射
  910. int nTreeACLshow; //三项总数据显示L1=0,L2=1,L3=2
  911. pthread_t snmp_agent_thread;//SNMP代理线程
  912. bool _ReloadChm_flag; //重新加载端口数据
  913. GlobalPowerCtrlBoard _all_ctrl_board[MAX_CHN_COUNT]; //所有控制板
  914. LowConf* _RS485LowConfData;//485Data
  915. //传感器
  916. struct modbus_slave_read_reg g_modebus_read;
  917. struct modbus_slave_write_reg g_modebus_write;
  918. smartPDU_shm_data* g_shm_Data;
  919. pthread_mutex_t _power_ds; //定时器锁
  920. pthread_mutex_t _power_update; //数据刷新锁
  921. pthread_mutex_t _sensor_update; //传感器刷新锁
  922. //pthread_mutex_t _breaker_mutex; // breaker info
  923. //cascade_single_mmap_t *single_mmap;
  924. //cascade_triphasic_mmap_t *triphasic_mmap;
  925. //GlobalBreakerManager g_new_global_breaker;
  926. cascade_ip_mmap_t * mmp;
  927. smtp_info_t smtpInfo;
  928. mqtt_info_t mqttInfo;
  929. Modbus_Manger_Tcp *md_tcp;
  930. }GlobalDeviceManager;
  931. extern GlobalDeviceManager __globalDeviceManage;
  932. extern GlobalDeviceManager __globalDeviceManage2;
  933. extern int ntp_sync_flag;
  934. extern unsigned long __globalSxIdx;
  935. extern _User_Log _GlobalUserLoginInfo;//全局用户登录信息
  936. extern UserInfo _GlobalUserInfoNew;////最近登录的信息
  937. extern char _GlobalUserToken[64];////最近登录的信息
  938. extern MenuAuthority _GlobalMenuAuthority;////角色菜单权限
  939. extern NTPManager _globalNTPManager;
  940. void initLogger(void);
  941. //打印设备信息结构
  942. void printf_global_device_info(GlobalDeviceInfo* _globalDeviceInfo);
  943. //打印设备超限电源管理
  944. void printf_global_power_manage_info(GlobalPowerManger* _globalPowerManger);
  945. //打印设备超限电源通道管理
  946. //void printf_global_power_chn_manage_info(GlobalPowerChnManger* _globalPowerChnManger);
  947. void printf_power_info(GlobalPowerInfo* _globalPowerInfo);
  948. int getTotalMemorySize(unsigned int* totalSize);
  949. char* getLocalIpAddress(char* dev);
  950. char* get_mac_address(const char *iface_name) ;
  951. void get_disk_occupy(char ** reused);
  952. int set_zone(int zone);
  953. int set_time(struct tm *t, int zone);
  954. int set_utctime(time_t t);
  955. int set_time_zone(struct tm *t, int zone);
  956. int set_utctime_zone(time_t t, int zone);
  957. time_t mktime0(void);
  958. time_t mktime2(struct tm *t, int zone);
  959. int get_time_str(char *s, int len, time_t t);
  960. int get_time_str2(char *s, int len, time_t t);
  961. int get_bldtime(char *buf, int len);
  962. void print_time(char *s, struct tm *tm);
  963. void print_utc(char *s, time_t t);
  964. char* getUserNamebyToken(char* Token);
  965. int is_process_running(const char *process_name);
  966. bool CheckLoginStatus(char* Token);
  967. // 辅助函数:判断是否是闰年
  968. int is_leap_year(int year);
  969. // 辅助函数:获取某月的天数
  970. int days_in_month(int year, int month);
  971. // 函数:增加一天到日期
  972. void add_day_to_date(char *date_str, int *year, int *month, int *day);
  973. void SetMenuAuthority(int RoleID);
  974. #endif