common.h 36 KB

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