common.h 37 KB

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