common.h 35 KB

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