common.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  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. ModbusInfo_t relay; //继电器控制端口
  255. NetworkInfo_t network; //网络信息
  256. ModbusInfo_t cascade; //级联端口
  257. ProductInfo_t product; //产品信息
  258. }GlobalDeviceInfo;
  259. ///////////////////////////////////////////////////////////////////////////////////
  260. ///////////////////////////////////////////////////////////////////////////////////
  261. //自定义共享内存类
  262. typedef struct
  263. {
  264. //电源数据
  265. int bPDUSet;
  266. int nSelectPowerCH;
  267. int nMaxCh;
  268. float fV;
  269. float fA;
  270. float fW;
  271. float fF;
  272. float fS;
  273. int nStatus;
  274. //传感器数据
  275. int nSelectSensor;
  276. float fT;//温度
  277. float fH;//湿度
  278. //三项数据
  279. bool bT_ACType;//三相类型
  280. int nSelectPh;//当前通道的三相ph参数:0:A相 1:B相 2:C相 -1通道总计
  281. } smartPDU_shm_data;
  282. ///////////////////////////////////////////////////////////////////////////////////
  283. ///////////////////////////////////////////////////////////////////////////////////
  284. //超限管理表
  285. typedef struct
  286. {
  287. char product_vol_upper_enable ;//电压上限使能
  288. float product_vol_upper_threshold ;//电压上限值
  289. char product_vol_over_upper_threshold_ctrl ;//电压超限动作
  290. char product_vol_over_upper_threshold_ctrl_para ;//电压超限关联参数
  291. char product_vol_lower_enable ;//电压下限使能
  292. float product_vol_lower_threshold ;//电压下限值
  293. char product_vol_over_lower_threshold_ctrl ;//电压超限动作
  294. char product_vol_over_lower_threshold_ctrl_para ;//电压超限动作
  295. char product_cur_upper_enable ;//电流下限使能
  296. float product_cur_upper_threshold ;//电流下限值
  297. float product_cur_lower_threshold; //
  298. char product_cur_over_upper_threshold_ctrl ;//电流超限动作
  299. char product_cur_over_upper_threshold_ctrl_para ;//电流超限动作
  300. char product_pwr_upper_enable ;//功率上限使能
  301. float product_pwr_upper_threshold ;//功率上限值
  302. char product_pwr_over_upper_threshold_ctrl ;//功率超限动作
  303. char product_pwr_over_upper_threshold_ctrl_para ;//功率超限动作
  304. char product_pwrcon_upper_enable ;//耗电量上限使能
  305. float product_pwrcon_upper_threshold ;//耗电量上限值
  306. char product_pwrcon_over_upper_threshold_ctrl ;//耗电量超限动作
  307. char product_pwrcon_over_upper_threshold_ctrl_para ;//耗电量超限动作
  308. }GlobalOverManager;
  309. /// @brief 定时管理
  310. typedef struct
  311. {
  312. char set_date[16]; //设置日期
  313. char ds_date[16]; //设置时间
  314. char ds_time[16]; //设置时间
  315. int set_stats; //状态
  316. int ds_is_triggered; //是否已触发
  317. struct list_head list;
  318. }_PowerDSManage_t;
  319. typedef struct
  320. {
  321. int firingSortno; //启动序号
  322. int closeSortno; //关闭序号
  323. }_PowerSXManage_t;
  324. typedef struct
  325. {
  326. int product_id;
  327. int mode;
  328. char ntpName[64];
  329. char ntpaddress[64];
  330. int port;
  331. char time[64];
  332. int weekday;
  333. int tzone;
  334. }NTPManager;
  335. enum {
  336. SNMP_AUTH_NONE=0,
  337. SNMP_AUTH_MD5,
  338. SNMP_AUTH_SHA,
  339. SNMP_AUTH_MAX
  340. };
  341. enum {
  342. SNMP_ENCYPT_NONE=0,
  343. SNMP_ENCYPT_DES,
  344. SNMP_ENCYPT_AES,
  345. SNMP_ENCYPT_MAX
  346. };
  347. typedef struct {
  348. char alg[12];
  349. char passwd[64];
  350. }crypt_data_t;
  351. typedef struct {
  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. port_info_t port; //
  388. }PowerInfo;
  389. //电源部分报警信息
  390. typedef struct
  391. {
  392. bool w_voltage_up ;//电压上限报警
  393. bool w_voltage_down ;//电压下限报警
  394. bool w_current ;//电流上限报警
  395. bool w_power ;//功率
  396. bool w_consumption ;//耗电量
  397. bool w_phase_lossA; // 缺相报警
  398. bool w_phase_lossB; // 缺相报警
  399. bool w_phase_lossC; // 缺相报警
  400. }PowerWarningInfo;
  401. //三项数据
  402. typedef struct
  403. {
  404. int product_id;
  405. int product_ch_id;
  406. int product_ph_id;
  407. int product_ph_type;//0:A相 1:B相 2:C相
  408. int product_saddr;
  409. int product_ch_addr;
  410. int product_ph_outputType;//三相控制方式1,三项2单项
  411. int product_ph_outputStatus;//该相是否输出,1输出2不输出
  412. char samp_time[64]; //采集时间
  413. PowerInfo _PowerInfo; //存储最新的数据(兼容三项输入)
  414. GlobalOverManager* global_over_manager; //超限管理
  415. PowerWarningInfo _PowerWarninginfo; //存储报警数据(兼容三项输入)
  416. time_t product_log_time; //存储最新时间
  417. struct list_head list_Tree_AC; //存储三项数据指针
  418. }GlobalTreeACManager;
  419. //电源通道管理总表(输出)
  420. typedef struct
  421. {
  422. int product_id ;//产品ID
  423. //20240225 add
  424. char product_saddr ;//modbus地址记录
  425. char product_ch_id ;//通道号
  426. char product_ch_addr ;//通道号实际地址
  427. char product_ch_name[32] ;//通道名称
  428. char product_ch_type ;//通道类型
  429. char product_ch_status ;//通道状态
  430. char product_ch_NF_status ;//通道零线状态(三相)
  431. unsigned short product_ch_start_delay ;//通道延时
  432. unsigned short product_ch_stop_delay ;//通道延时
  433. GlobalOverManager* global_over_manager ;//超限管理
  434. struct list_head list_DS; //定时设置
  435. _PowerSXManage_t* _PowerSXManage; //时序管理
  436. PowerInfo _PowerInfo; //存储最新的数据
  437. PowerWarningInfo _PowerWarninginfo ; //存储报警数据
  438. time_t product_log_time; //存储最新时间
  439. struct list_head list_Tree_AC; //存储三项数据指针
  440. struct list_head list;
  441. } GlobalPowerManger;
  442. //电源通道管理总表(输入)
  443. typedef struct
  444. {
  445. int product_id ;//产品ID
  446. //20240225 add
  447. char product_saddr ;//modbus地址记录
  448. char product_saddr_name[32] ;//输入名称
  449. char product_saddr_type ;//输入类型
  450. GlobalOverManager* global_over_manager ;//超限管理
  451. PowerInfo _PowerInfo[3]; //存储最新的数据(兼容三项输入)
  452. PowerWarningInfo _PowerWarninginfo[3]; //存储报警数据(兼容三项输入)
  453. time_t product_log_time; //存储最新时间
  454. int product_MissingPH[3]; //缺相报警
  455. }GlobalPowerInManger;
  456. //电源控制板计数
  457. typedef struct
  458. {
  459. char product_saddr ;//modbus地址记录
  460. int product_number; //数量
  461. int product_type; //类型
  462. PowerInfo _PowerInfo[AC_SINGLE_S_MAX_CHN_NUM]; //存储最新的数据
  463. PowerWarningInfo _PowerWarninginfo[AC_SINGLE_S_MAX_CHN_NUM] ;//存储报警数据
  464. struct list_head list; //存储具有的通道指针
  465. GlobalPowerInManger _globalPowerInManger; //总输入数据
  466. }GlobalPowerCtrlBoard;
  467. ///////////////////////////////////////////////////////////////////////////////////
  468. ///////////////////////////////////////////////////////////////////////////////////
  469. //电源信息统计
  470. typedef struct
  471. {
  472. float ydata[60]; //value
  473. char xAxis[60][32];//时间点
  474. }GetTotalInfo;
  475. //系统总数信息
  476. typedef struct
  477. {
  478. int channel_total ;//通道总数
  479. int warn_total ;//告警总数
  480. int sensor_total ;//传感器总数
  481. int log_total ;//运行日志总数
  482. }SystemTotalInfo;
  483. //功率趋势接口
  484. typedef struct
  485. {
  486. float ydata[3][60] ;//0:有功功率;1:无功功率;2:视在功率;
  487. char xAxis[60][32] ;//时间点
  488. }PowerTrendInfo;
  489. ///////////////////////////////////////////////////////////////////////////////////
  490. ///////////////////////////////////////////////////////////////////////////////////
  491. //电源监控
  492. typedef struct
  493. {
  494. int product_id ;//产品ID
  495. char product_ch_id ;//电源通道
  496. unsigned short product_status ;//当前状态
  497. PowerInfo _power_info ;//采集值
  498. char samp_time[64] ;//采集时间
  499. GetTotalInfo _total_voltage ;//统计电压
  500. GetTotalInfo _total_current ;//统计电流
  501. GetTotalInfo _total_power ;//统计功率
  502. GetTotalInfo _total_consumption ;//统计耗电量
  503. SystemTotalInfo _system_total_info ;//系统总数信息
  504. PowerTrendInfo _power_trend_info ;//功率趋势图
  505. struct list_head list;
  506. }GlobalPowerInfo;
  507. ///////////////////////////////////////////////////////////////////////////////////
  508. ///////////////////////////////////////////////////////////////////////////////////
  509. ///////////////////////////////////////////////////////////////////////////////////
  510. ///////////////////////////////////////////////////////////////////////////////////
  511. //传感器值
  512. typedef struct
  513. {
  514. char samp_time[64] ;//采集时间
  515. int product_id ;//产品ID
  516. char sensor_id ;//传感器ID
  517. float val1 ;//值1
  518. float val2 ;//值2
  519. time_t product_log_time; //存储最新时间
  520. struct list_head list;
  521. }GlobalSensorInfo;
  522. typedef struct
  523. {
  524. bool sensor_val1_upper_enabled ;//
  525. float sensor_val1_upper_threshold;
  526. char sensor_val1_upper_threshold_ctrl;
  527. char sensor_val1_upper_threshold_ctrl_para[512];
  528. bool sensor_val1_lower_enabled ;//
  529. float sensor_val1_lower_threshold;
  530. char sensor_val1_lower_threshold_ctrl;
  531. char sensor_val1_lower_threshold_ctrl_para[512];
  532. bool sensor_val2_upper_enabled ;//
  533. float sensor_val2_upper_threshold;
  534. char sensor_val2_upper_threshold_ctrl;
  535. char sensor_val2_upper_threshold_ctrl_para[512];
  536. bool sensor_val2_lower_enabled ;//
  537. float sensor_val2_lower_threshold;
  538. char sensor_val2_lower_threshold_ctrl;
  539. char sensor_val2_lower_threshold_ctrl_para[512];
  540. }GlobalSensorOverManage;
  541. //传感器报警状态表
  542. typedef struct
  543. {
  544. bool sensor_val1_upper;
  545. bool sensor_val1_lower;
  546. bool sensor_val2_upper;
  547. bool sensor_val2_lower;
  548. }GlobalSensorOverInfo;
  549. //传感器管理总表
  550. typedef struct
  551. {
  552. int product_id ;//产品ID
  553. char sensor_id ;//传感器ID
  554. char sensor_type ;//传感器类型
  555. char sensor_interface_type; //接口类型
  556. char sensor_name[64] ;//传感器名称
  557. char sensor_port_name[32] ;//传感器端口号
  558. int sensor_baud ;//波特率
  559. char sensor_addr ;//modbus地址
  560. int sensor_status ;//传感器状态
  561. char sensor_val_count ;//传感器值个数
  562. int sensor_node_number ;//节点号
  563. GlobalSensorInfo Cur_sensor_info ;
  564. GlobalSensorOverManage* over_manager ;//超限管理
  565. GlobalSensorOverInfo warning_status;//报警状态
  566. struct list_head list;
  567. }GlobalSensorManger;
  568. ///////////////////////////////////////////////////////////////////////////////////
  569. ///////////////////////////////////////////////////////////////////////////////////
  570. ///////////////////////////////////////////////////////////////////////////////////
  571. ///////////////////////////////////////////////////////////////////////////////////
  572. ///温湿度数据信息管理
  573. typedef struct
  574. {
  575. float temperature;
  576. float humidity;
  577. }SenorTempVal;
  578. //采集KB值
  579. typedef struct
  580. {
  581. float voltage_k;//电压K
  582. float voltage_b;//电压B
  583. float current_k;//电流K
  584. float current_b;//电流B
  585. }SwitchKbVal;
  586. ///////////////////////////////////////////////////////////////////////////////////
  587. ///////////////////////////////////////////////////////////////////////////////////
  588. //MAIL管理
  589. typedef struct
  590. {
  591. int idx;
  592. int use;
  593. char account[64];
  594. }mail_recv_t;
  595. typedef struct
  596. {
  597. int id;
  598. int mode; //0: open 1: close
  599. char account[64]; //用户账号
  600. char password[24]; //用户密码
  601. char server[64]; //服务器
  602. char port[10]; //端口
  603. char auth[16]; //认证方式
  604. char ca[8196]; //ca cert
  605. }mail_send_t;
  606. typedef struct
  607. {
  608. mail_send_t send;
  609. #define MAIL_RECV_MAX 10
  610. mail_recv_t recv[MAIL_RECV_MAX]; //接收账号列表
  611. }mail_info_t;
  612. enum {
  613. MQTT_EMQX=0,
  614. MQTT_ALIYUN,
  615. MQTT_MAX
  616. };
  617. typedef struct {
  618. int id; //id
  619. int type; //0: emqx 1: aliyun
  620. int mode; //0: open 1: close
  621. char proto[16]; //mqtt, mqtts, ws, wss
  622. char name[32]; //server name
  623. char server[512]; //server ip
  624. char port[10]; //server port
  625. char cid[64]; //client ID
  626. char user[64]; //
  627. char password[64];
  628. char cert[8192];
  629. int status;
  630. }mqtt_server_t;
  631. typedef struct {
  632. #define MQTT_SER_MAX 5
  633. mqtt_server_t ser[MQTT_SER_MAX];
  634. }mqtt_info_t;
  635. typedef struct
  636. {
  637. int product_id;
  638. int userID; //用户ID
  639. char* userName; //用户名-登录
  640. char* userPW; //用户密码
  641. char* nickName; //用户名称
  642. int roleID; //角色ID
  643. char* createTime; //用户创建时间
  644. struct list_head list;
  645. }UserInfo;
  646. typedef struct
  647. {
  648. int product_id;
  649. int roleID; //角色ID
  650. char* roleName; //角色名称
  651. int roleLevel; //角色等级
  652. char* roleAuthority; //角色权限
  653. int roleSort; //显示顺序
  654. char* createTime; //创建时间
  655. struct list_head list;
  656. }RoleInfo;
  657. typedef struct MenuAuthority{
  658. bool PowerEnabled;
  659. bool SensorEnabled;
  660. bool AlarmEnabled;
  661. bool LogEnabled;
  662. bool UserInfoEnabled;
  663. bool CommEnabled;
  664. bool ServiceEnabled;
  665. bool MaintenanceEnabled;
  666. //1、电源监控菜单
  667. bool MonitorallEnabled;
  668. bool ChannelstatusEnabled;
  669. bool SwitchtatusEnabled;
  670. bool MonitorhistoryEnabled;
  671. //2、传感器管理菜单
  672. bool SensorStatusEnabled;
  673. bool SensorHistorylEnabled;
  674. //3、告警管理菜单
  675. bool XtjgEnabled;
  676. bool YzszEnabled;
  677. //4、日志信息菜单
  678. bool CzrzEnabled;
  679. bool YxrzEnabled;
  680. //5、用户管理菜单
  681. bool UserEnabled;
  682. bool RoleEnabled;
  683. bool MenuEnabled;
  684. //6、通信管理菜单
  685. bool WkszEnabled;
  686. bool RsszEnabled;
  687. //7、服务管理菜单
  688. bool SmtpszEnabled;
  689. bool SnmpszEnabled;
  690. bool MqszEnabled;
  691. bool NtpszEnabled;
  692. bool DsszEnabled;
  693. bool SxszEnabled;
  694. bool FzszEnabled;
  695. //8、设备维护菜单
  696. bool SbcqEnabled;
  697. bool GjsjEnabled;
  698. bool HhccszEnabled;
  699. bool PzdrdcEnabled;
  700. bool ShfwEnabled;
  701. bool GysbEnabled;
  702. }MenuAuthority;
  703. typedef struct CascadeDevices{
  704. int product_id; //级联设备ID modbus地址
  705. int type; //
  706. char device_name[20]; //接连设备名;
  707. float valtage; //485
  708. float current;
  709. float power;
  710. float factor;
  711. float freq;
  712. float consumption;
  713. }CascadeDevices;
  714. struct modbus_slave_read_reg{
  715. uint16_t deveice_type; //设备类型
  716. uint16_t slave_addr; //设备地址
  717. uint32_t voltage; //当前电压
  718. uint32_t current; //当前电流
  719. uint32_t power; //当前功率
  720. uint32_t consumption_cur; //耗电量
  721. uint32_t freq; //频率
  722. uint32_t factor; //功率因素
  723. uint32_t temprature; //温度值
  724. uint32_t humidity; //湿度值
  725. uint32_t warning; //告警
  726. uint16_t power_supplay_status; //供电状态
  727. uint16_t sencor_status; //传感器状态
  728. uint32_t temprature2; //温度值2
  729. // uint32_t Warinfo;
  730. // uint16_t sercor_type;
  731. };
  732. typedef struct cascade_pw_info{
  733. uint32_t voltage ;//电压
  734. uint32_t current ;//电流
  735. uint32_t power ;//功率
  736. uint32_t freq ;//频率
  737. uint32_t consumption ;//耗电量
  738. uint32_t factor ;//功率因素
  739. }cascade_single_chn_info; //siggle hell
  740. typedef struct temp_hum{
  741. uint16_t temperature;
  742. uint16_t humidity;
  743. }temp_hum_t;
  744. #pragma pack (1)
  745. typedef struct cascade_single_mmap{
  746. uint16_t dev_chn;
  747. cascade_single_chn_info all_ch[128];
  748. uint32_t total_valtage;
  749. uint32_t total_current;
  750. uint32_t total_power;
  751. uint32_t total_consumer;
  752. temp_hum_t total_temp;
  753. uint16_t chn_status[64];
  754. uint16_t open_delay[64];
  755. uint16_t close_delay[64];
  756. uint16_t reservers[29];
  757. uint16_t set_chn_open_delay[64];
  758. uint16_t set_chn_close_delay[64];
  759. uint16_t set_chn_switch_status[64];
  760. uint16_t set_chn_all_switch_status;
  761. }cascade_single_mmap_t;
  762. #pragma pack ()
  763. #pragma pack (1)
  764. #define MAX_CHN 64
  765. typedef struct cascade_triphasic_pw_out{
  766. uint32_t o_voltage;
  767. uint32_t o_current;
  768. uint32_t o_power;
  769. uint32_t o_consumption;
  770. uint32_t o_status;
  771. }cascade_triphasic_pw_out_t;
  772. typedef struct cascade_triphasic_pw_o_chn{
  773. cascade_triphasic_pw_out_t chn[3]; // A B C
  774. }cascade_triphasic_pw_o_chn_t;
  775. typedef struct cascade_triphasic_pw_in{
  776. uint32_t i_total_voltage;
  777. uint32_t i_total_current;
  778. uint32_t i_total_power;
  779. uint32_t i_total_consumption;
  780. }cascade_triphasic_pw_in_t;
  781. typedef struct cascade_triphasic_pw_i_total{
  782. cascade_triphasic_pw_in_t pw_i_total[3]; // A B C
  783. }cascade_triphasic_pw_i_total_t;
  784. typedef struct cascade_triphasic_chn_info{
  785. uint32_t chn_power;
  786. uint32_t chn_freq;
  787. uint32_t chn_consumption;
  788. uint32_t chn_factor;
  789. uint32_t chn_type;
  790. }cascade_triphasic_chn_info_t;
  791. typedef struct cascade_triphasic_mmap{
  792. uint16_t dev_chn;
  793. cascade_triphasic_pw_i_total_t i_total;
  794. cascade_triphasic_chn_info_t chn_info[MAX_CHN];
  795. cascade_triphasic_pw_o_chn_t pw_o_chn[MAX_CHN];
  796. uint16_t temperature;
  797. uint16_t humidity;
  798. uint16_t chn_status[MAX_CHN];
  799. uint16_t open_delay[MAX_CHN];
  800. uint16_t close_delay[MAX_CHN];
  801. uint16_t set_chn_open_delay[MAX_CHN];
  802. uint16_t set_chn_close_delay[MAX_CHN];
  803. uint16_t set_chn_switch_status[MAX_CHN];
  804. uint16_t set_chn_all_switch_status;
  805. }cascade_triphasic_mmap_t;
  806. typedef struct
  807. {
  808. modbus_t *ctx;
  809. char ip[64];
  810. uint16_t port;
  811. uint8_t slave_id;
  812. uint8_t power_type;
  813. modbus_mapping_t *map;
  814. int connection;
  815. //pthread_mutex_t mutex;
  816. }Modbus_Manger_Tcp;
  817. #pragma pack ()
  818. struct modbus_slave_write_reg{
  819. uint16_t voltage_high_enable:1; //电压高阈值检测使能
  820. uint16_t voltage_low_enable:1; //电压低阈值检测使能
  821. uint16_t current_enable:1; //电流阈值使能
  822. uint16_t power_enable:1; //功率阈值使能
  823. uint16_t consumption_cur_enable:1; //功耗阈值使能
  824. uint32_t voltage_upper_threshold; //
  825. uint32_t voltage_lower_threshold;
  826. uint32_t current_upper_threshold;
  827. uint32_t power_upper_threshold;
  828. uint32_t consumption_threshold;
  829. };
  830. enum
  831. {
  832. DI,
  833. RS485
  834. };
  835. enum{
  836. SWITCH_CONNETCTED,
  837. SWITCH_DISCONNETCTED,
  838. SWITCH_OFFLINE
  839. };
  840. #define SWITCH_CHN_1_SET (1<<0)
  841. #define SWITCH_CHN_2_SET (1<<1)
  842. #define SWITCH_CHN_3_SET (1<<2)
  843. #define SWITCH_CHN_4_SET (1<<3)
  844. #define SWITCH_CHN_5_SET (1<<4)
  845. #define SWITCH_CHN_6_SET (1<<5)
  846. #define SWITCH_CHN_DI_1_SET (1<<6)
  847. #define SWITCH_CHN_DI_2_SET (1<<7)
  848. typedef struct breaker_switch_sta{
  849. uint16_t status;
  850. uint16_t gather;
  851. char com[30];
  852. uint8_t product_name[256];
  853. }breaker_switch_sta_t;
  854. typedef struct GlobalBreakerManger{
  855. uint32_t all_count;
  856. uint32_t online_count;
  857. uint32_t mask;
  858. uint8_t saddr;
  859. void *manager;
  860. breaker_switch_sta_t sta[8]; // 6 + 2
  861. pthread_mutex_t b_mutex;
  862. }GlobalBreakerManger;
  863. typedef struct GlobalBreakerManager{
  864. uint32_t product_id;
  865. uint32_t breaker_id;
  866. uint32_t breaker_chn;
  867. uint32_t breaker_status;
  868. uint32_t breaker_gather_addr;
  869. char breaker_switch_name[128];
  870. char breaker_com[64];
  871. char breaker_gather_type[64];
  872. char breaker_chns_name[128];
  873. struct list_head list;
  874. }GlobalBreakerManager;
  875. //设备总体管理
  876. typedef struct
  877. {
  878. sqlite3* db; //数据库接口
  879. pthread_mutex_t dbThreadlock; //写入线程锁
  880. pthread_t appweb_thread; //appweb线程
  881. pthread_t dev_samp_thread;//电源采集与控制线程
  882. pthread_t dev_update_thread;//电源采集与控制线程
  883. pthread_t io_thread; //IO采集线程
  884. pthread_t sensor_thread; //传感器采集现成
  885. bool dev_samp_flag ; //电源采集线程标志
  886. GlobalDeviceInfo _globalDevInfo; //设备管理信息
  887. GlobalPowerManger _globalPowerManger; //电源管理表
  888. GlobalSensorManger _globalSensorManger; //传感器管理表
  889. Modbus_Manger sensor_modbus_manager; //通信接口管理
  890. unsigned int useSlaveCount ; //有效地址
  891. Modbus_Manger _globalRelaySampManger; //内部继电器使用
  892. unsigned char uart_init_flag[6] ; //
  893. bool ws_isok; //ws准备好
  894. struct mg_connection *ws_client; //websocket管理信息
  895. HashTable* alarmTab; //全局的报警映射
  896. int nTreeACLshow; //三项总数据显示L1=0,L2=1,L3=2
  897. pthread_t snmp_agent_thread;//SNMP代理线程
  898. bool _ReloadChm_flag; //重新加载端口数据
  899. GlobalPowerCtrlBoard _all_ctrl_board[MAX_CHN_COUNT]; //所有控制板
  900. LowConf* _RS485LowConfData;//485Data
  901. //传感器
  902. struct modbus_slave_read_reg g_modebus_read;
  903. struct modbus_slave_write_reg g_modebus_write;
  904. smartPDU_shm_data* g_shm_Data;
  905. pthread_mutex_t _power_ds; //定时器锁
  906. pthread_mutex_t _power_update; //数据刷新锁
  907. pthread_mutex_t _sensor_update; //传感器刷新锁
  908. pthread_mutex_t _breaker_mutex; // breaker info
  909. cascade_single_mmap_t *single_mmap;
  910. cascade_triphasic_mmap_t *triphasic_mmap;
  911. GlobalBreakerManager g_new_global_breaker;
  912. mail_info_t mailInfo;
  913. mqtt_info_t mqttInfo;
  914. Modbus_Manger_Tcp *md_tcp;
  915. }GlobalDeviceManager;
  916. extern GlobalDeviceManager __globalDeviceManage;
  917. extern GlobalDeviceManager __globalDeviceManage2;
  918. extern int ntp_sync_flag;
  919. extern unsigned long __globalSxIdx;
  920. extern _User_Log _GlobalUserLoginInfo;//全局用户登录信息
  921. extern UserInfo _GlobalUserInfoNew;////最近登录的信息
  922. extern char _GlobalUserToken[64];////最近登录的信息
  923. extern MenuAuthority _GlobalMenuAuthority;////角色菜单权限
  924. extern NTPManager _globalNTPManager;
  925. void initLogger(void);
  926. //打印设备信息结构
  927. void printf_global_device_info(GlobalDeviceInfo* _globalDeviceInfo);
  928. //打印设备超限电源管理
  929. void printf_global_power_manage_info(GlobalPowerManger* _globalPowerManger);
  930. //打印设备超限电源通道管理
  931. //void printf_global_power_chn_manage_info(GlobalPowerChnManger* _globalPowerChnManger);
  932. void printf_power_info(GlobalPowerInfo* _globalPowerInfo);
  933. int getTotalMemorySize(unsigned int* totalSize);
  934. char* getLocalIpAddress(char* dev);
  935. char* get_mac_address(const char *iface_name) ;
  936. void get_disk_occupy(char ** reused);
  937. int set_zone(int zone);
  938. int set_time(struct tm *t, int zone);
  939. int set_utctime(time_t t);
  940. int set_time_zone(struct tm *t, int zone);
  941. int set_utctime_zone(time_t t, int zone);
  942. time_t mktime0(void);
  943. time_t mktime2(struct tm *t, int zone);
  944. int get_time_str(char *s, int len, time_t t);
  945. int get_time_str2(char *s, int len, time_t t);
  946. void print_time(char *s, struct tm *tm);
  947. void print_utc(char *s, time_t t);
  948. char* getUserNamebyToken(char* Token);
  949. int is_process_running(const char *process_name);
  950. bool CheckLoginStatus(char* Token);
  951. // 辅助函数:判断是否是闰年
  952. int is_leap_year(int year);
  953. // 辅助函数:获取某月的天数
  954. int days_in_month(int year, int month);
  955. // 函数:增加一天到日期
  956. void add_day_to_date(char *date_str, int *year, int *month, int *day);
  957. void SetMenuAuthority(int RoleID);
  958. #endif