common.h 39 KB

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