common.h 35 KB

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