common.h 37 KB

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