common.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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. int rotate;
  317. }MiscInfo_t;
  318. typedef struct {
  319. char name[256];
  320. char site[256];
  321. char mail[256];
  322. char addr[256];
  323. char tele[32];
  324. }ServiceInfo_t;
  325. typedef struct
  326. {
  327. int voltage;
  328. int current;
  329. int power;
  330. int brdMax;
  331. }PowerTotal_t;
  332. typedef struct
  333. {
  334. ModbusInfo_t relay; //继电器控制端口
  335. NetworkInfo_t network; //网络信息
  336. ModbusInfo_t cascade; //级联端口
  337. ProductInfo_t product; //产品信息
  338. PowerTotal_t ptotal;
  339. WebcfgInfo_t webcfg;
  340. ServiceInfo_t service; //服务信息
  341. MiscInfo_t misc;
  342. }GlobalDeviceInfo;
  343. ///////////////////////////////////////////////////////////////////////////////////
  344. //自定义共享内存类
  345. typedef struct
  346. {
  347. //电源数据
  348. int bPDUSet;
  349. int nSelectPowerCH;
  350. int nMaxCh;
  351. float fV;
  352. float fA;
  353. float fW;
  354. float fF;
  355. float fS;
  356. int nStatus;
  357. //传感器数据
  358. int nSelectSensor;
  359. float fT;//温度
  360. float fH;//湿度
  361. //三项数据
  362. int bT_ACType;//三相类型
  363. int nSelectPh;//当前通道的三相ph参数:0:A相 1:B相 2:C相 -1通道总计
  364. } smartPDU_shm_data;
  365. ///////////////////////////////////////////////////////////////////////////////////
  366. ///////////////////////////////////////////////////////////////////////////////////
  367. //超限管理表
  368. typedef struct
  369. {
  370. int product_vol_upper_enable ;//电压上限使能
  371. float product_vol_upper_threshold ;//电压上限值
  372. int product_vol_over_upper_threshold_ctrl ;//电压超限动作
  373. int product_vol_over_upper_threshold_ctrl_para ;//电压超限关联参数
  374. int product_vol_lower_enable ;//电压下限使能
  375. float product_vol_lower_threshold ;//电压下限值
  376. int product_vol_over_lower_threshold_ctrl ;//电压超限动作
  377. int product_vol_over_lower_threshold_ctrl_para ;//电压超限动作
  378. int product_cur_upper_enable ;//电流下限使能
  379. float product_cur_upper_threshold ;//电流下限值
  380. float product_cur_lower_threshold; //
  381. int product_cur_over_upper_threshold_ctrl ;//电流超限动作
  382. int product_cur_over_upper_threshold_ctrl_para ;//电流超限动作
  383. int product_pwr_upper_enable ;//功率上限使能
  384. float product_pwr_upper_threshold ;//功率上限值
  385. int product_pwr_over_upper_threshold_ctrl ;//功率超限动作
  386. int product_pwr_over_upper_threshold_ctrl_para ;//功率超限动作
  387. int product_pwrcon_upper_enable ;//耗电量上限使能
  388. float product_pwrcon_upper_threshold ;//耗电量上限值
  389. int product_pwrcon_over_upper_threshold_ctrl ;//耗电量超限动作
  390. int product_pwrcon_over_upper_threshold_ctrl_para ;//耗电量超限动作
  391. }GlobalOverManager;
  392. /// @brief 定时管理
  393. typedef struct
  394. {
  395. char set_date[16]; //设置日期
  396. char ds_date[16]; //设置时间
  397. char ds_time[16]; //设置时间
  398. int set_stats; //状态
  399. int ds_is_triggered; //是否已触发
  400. struct list_head list;
  401. }_PowerDSManage_t;
  402. typedef struct
  403. {
  404. int firingSortno; //启动序号
  405. int closeSortno; //关闭序号
  406. }_PowerSXManage_t;
  407. typedef struct
  408. {
  409. int product_id;
  410. int mode;
  411. char ntpName[64];
  412. char ntpaddress[64];
  413. int port;
  414. char time[64];
  415. int weekday;
  416. int tzone;
  417. }NTPManager;
  418. enum {
  419. SNMP_AUTH_NONE=0,
  420. SNMP_AUTH_MD5,
  421. SNMP_AUTH_SHA,
  422. SNMP_AUTH_MAX
  423. };
  424. enum {
  425. SNMP_ENCYPT_NONE=0,
  426. SNMP_ENCYPT_DES,
  427. SNMP_ENCYPT_AES,
  428. SNMP_ENCYPT_MAX
  429. };
  430. typedef struct {
  431. char alg[12];
  432. char passwd[64];
  433. }crypt_data_t;
  434. typedef struct {
  435. char user[32];
  436. crypt_data_t auth;
  437. crypt_data_t priv;
  438. }crypt_t;
  439. typedef struct
  440. {
  441. int product_id;
  442. int mode;
  443. int version;
  444. char public[64];
  445. int trapmode;
  446. crypt_t crypt;
  447. char nmsIP[64];
  448. }SNMPManager;
  449. //分组管理
  450. typedef struct
  451. {
  452. int product_id;
  453. int GroupID;
  454. char GroupName[64];
  455. char GroupChList[512];
  456. struct list_head list;
  457. }GroupInfo;
  458. //电源部分
  459. typedef struct
  460. {
  461. float voltage ;//电压
  462. float current ;//电流
  463. float power ;//功率
  464. float freq ;//频率
  465. float consumption ;//耗电量
  466. float factor ;//功率因素
  467. unsigned short status ;//开关状态
  468. unsigned short NF_status ;//零线状态
  469. port_info_t port; //
  470. }PowerInfo;
  471. //电源部分报警信息
  472. typedef struct
  473. {
  474. bool w_voltage_up ;//电压上限报警
  475. bool w_voltage_down ;//电压下限报警
  476. bool w_current ;//电流上限报警
  477. bool w_power ;//功率
  478. bool w_consumption ;//耗电量
  479. bool w_phase_lossA; // 缺相报警
  480. bool w_phase_lossB; // 缺相报警
  481. bool w_phase_lossC; // 缺相报警
  482. }PowerWarningInfo;
  483. //三项数据
  484. typedef struct
  485. {
  486. int product_id;
  487. int product_ch_id;
  488. int product_ph_id;
  489. int product_ph_type;//0:A相 1:B相 2:C相
  490. int product_saddr;
  491. int product_ch_addr;
  492. int product_ph_outputType;//三相控制方式1,三项2单项
  493. int product_ph_outputStatus;//该相是否输出,1输出2不输出
  494. char samp_time[64]; //采集时间
  495. PowerInfo _PowerInfo; //存储最新的数据(兼容三项输入)
  496. GlobalOverManager global_over_manager; //超限管理
  497. PowerWarningInfo _PowerWarninginfo; //存储报警数据(兼容三项输入)
  498. time_t product_log_time; //存储最新时间
  499. struct list_head list_Tree_AC; //存储三项数据指针
  500. }GlobalTreeACManager;
  501. //电源通道管理总表(输出)
  502. typedef struct
  503. {
  504. int product_id ;//产品ID
  505. //20240225 add
  506. int product_saddr ;//modbus地址记录
  507. int product_ch_id ;//通道号
  508. int product_ch_addr ;//通道号实际地址
  509. char product_ch_name[32] ;//通道名称
  510. int product_ch_type ;//通道类型
  511. int product_ch_status ;//通道状态
  512. int product_ch_NF_status ;//通道零线状态(三相)
  513. int product_ch_start_delay ;//通道延时
  514. int product_ch_stop_delay ;//通道延时
  515. GlobalOverManager global_over_manager ;//超限管理
  516. struct list_head list_DS; //定时设置
  517. _PowerSXManage_t* _PowerSXManage; //时序管理
  518. PowerInfo _PowerInfo; //存储最新的数据
  519. PowerWarningInfo _PowerWarninginfo ; //存储报警数据
  520. time_t product_log_time; //存储最新时间
  521. struct list_head list_Tree_AC; //存储三项数据指针
  522. struct list_head list;
  523. } GlobalPowerManger;
  524. //电源通道管理总表(输入)
  525. typedef struct
  526. {
  527. int product_id ;//产品ID
  528. //20240225 add
  529. char product_saddr ;//modbus地址记录
  530. char product_saddr_name[32] ;//输入名称
  531. char product_saddr_type ;//输入类型
  532. GlobalOverManager global_over_manager ;//超限管理
  533. PowerInfo _PowerInfo[3]; //存储最新的数据(兼容三项输入)
  534. PowerWarningInfo _PowerWarninginfo[3]; //存储报警数据(兼容三项输入)
  535. time_t product_log_time; //存储最新时间
  536. int product_MissingPH[3]; //缺相报警
  537. }GlobalPowerInManger;
  538. //电源控制板计数
  539. typedef struct
  540. {
  541. char product_saddr ;//modbus地址记录
  542. int product_number; //数量
  543. int product_type; //类型
  544. PowerInfo _PowerInfo[AC_SINGLE_S_MAX_CHN_NUM]; //存储最新的数据
  545. PowerWarningInfo _PowerWarninginfo[AC_SINGLE_S_MAX_CHN_NUM] ;//存储报警数据
  546. struct list_head list; //存储具有的通道指针
  547. GlobalPowerInManger _globalPowerInManger; //总输入数据
  548. }GlobalPowerCtrlBoard;
  549. ///////////////////////////////////////////////////////////////////////////////////
  550. ///////////////////////////////////////////////////////////////////////////////////
  551. //电源信息统计
  552. typedef struct
  553. {
  554. float ydata[60]; //value
  555. char xAxis[60][32];//时间点
  556. }GetTotalInfo;
  557. //系统总数信息
  558. typedef struct
  559. {
  560. int channel_total ;//通道总数
  561. int warn_total ;//告警总数
  562. int sensor_total ;//传感器总数
  563. int log_total ;//运行日志总数
  564. }SystemTotalInfo;
  565. //功率趋势接口
  566. typedef struct
  567. {
  568. float ydata[3][60] ;//0:有功功率;1:无功功率;2:视在功率;
  569. char xAxis[60][32] ;//时间点
  570. }PowerTrendInfo;
  571. ///////////////////////////////////////////////////////////////////////////////////
  572. ///////////////////////////////////////////////////////////////////////////////////
  573. //电源监控
  574. typedef struct
  575. {
  576. int product_id ;//产品ID
  577. char product_ch_id ;//电源通道
  578. unsigned short product_status ;//当前状态
  579. PowerInfo _power_info ;//采集值
  580. char samp_time[64] ;//采集时间
  581. GetTotalInfo _total_voltage ;//统计电压
  582. GetTotalInfo _total_current ;//统计电流
  583. GetTotalInfo _total_power ;//统计功率
  584. GetTotalInfo _total_consumption ;//统计耗电量
  585. SystemTotalInfo _system_total_info ;//系统总数信息
  586. PowerTrendInfo _power_trend_info ;//功率趋势图
  587. struct list_head list;
  588. }GlobalPowerInfo;
  589. ///////////////////////////////////////////////////////////////////////////////////
  590. ///////////////////////////////////////////////////////////////////////////////////
  591. ///////////////////////////////////////////////////////////////////////////////////
  592. ///////////////////////////////////////////////////////////////////////////////////
  593. //传感器值
  594. typedef struct
  595. {
  596. char samp_time[64] ;//采集时间
  597. int product_id ;//产品ID
  598. char sensor_id ;//传感器ID
  599. float val1 ;//值1
  600. float val2 ;//值2
  601. time_t product_log_time; //存储最新时间
  602. struct list_head list;
  603. }GlobalSensorInfo;
  604. typedef struct
  605. {
  606. int sensor_val1_upper_enabled ;//
  607. float sensor_val1_upper_threshold;
  608. int sensor_val1_upper_threshold_ctrl;
  609. char sensor_val1_upper_threshold_ctrl_para[512];
  610. int sensor_val1_lower_enabled ;//
  611. float sensor_val1_lower_threshold;
  612. int sensor_val1_lower_threshold_ctrl;
  613. char sensor_val1_lower_threshold_ctrl_para[512];
  614. int sensor_val2_upper_enabled ;//
  615. float sensor_val2_upper_threshold;
  616. int sensor_val2_upper_threshold_ctrl;
  617. char sensor_val2_upper_threshold_ctrl_para[512];
  618. int sensor_val2_lower_enabled ;//
  619. float sensor_val2_lower_threshold;
  620. int sensor_val2_lower_threshold_ctrl;
  621. char sensor_val2_lower_threshold_ctrl_para[512];
  622. }GlobalSensorOverManage;
  623. //传感器报警状态表
  624. typedef struct
  625. {
  626. int sensor_val1_upper;
  627. int sensor_val1_lower;
  628. int sensor_val2_upper;
  629. int sensor_val2_lower;
  630. }GlobalSensorOverInfo;
  631. //传感器管理总表
  632. typedef struct
  633. {
  634. int product_id ;//产品ID
  635. int sensor_id ;//传感器ID
  636. int sensor_type ;//传感器类型
  637. int sensor_interface_type; //接口类型
  638. char sensor_name[64] ;//传感器名称
  639. char sensor_port_name[32] ;//传感器端口号
  640. int sensor_baud ;//波特率
  641. int sensor_addr ;//modbus地址
  642. int sensor_status ;//传感器状态
  643. int sensor_val_count ;//传感器值个数
  644. int sensor_node_number ;//节点号
  645. GlobalSensorInfo Cur_sensor_info ;
  646. GlobalSensorOverManage over_manager ;//超限管理
  647. GlobalSensorOverInfo warning_status;//报警状态
  648. struct list_head list;
  649. }GlobalSensorManger;
  650. ///////////////////////////////////////////////////////////////////////////////////
  651. ///////////////////////////////////////////////////////////////////////////////////
  652. ///////////////////////////////////////////////////////////////////////////////////
  653. ///////////////////////////////////////////////////////////////////////////////////
  654. ///温湿度数据信息管理
  655. typedef struct
  656. {
  657. float temperature;
  658. float humidity;
  659. }SenorTempVal;
  660. //采集KB值
  661. typedef struct
  662. {
  663. float voltage_k;//电压K
  664. float voltage_b;//电压B
  665. float current_k;//电流K
  666. float current_b;//电流B
  667. }SwitchKbVal;
  668. ///////////////////////////////////////////////////////////////////////////////////
  669. ///////////////////////////////////////////////////////////////////////////////////
  670. //MAIL管理
  671. typedef struct
  672. {
  673. int id;
  674. int use;
  675. char account[64];
  676. }mail_recv_t;
  677. typedef struct
  678. {
  679. int id;
  680. int mode; //0: open 1: close
  681. char account[64]; //用户账号
  682. char password[24]; //用户密码
  683. char server[64]; //服务器
  684. char port[10]; //端口
  685. char auth[16]; //认证方式
  686. char ca[8196]; //ca cert
  687. }mail_send_t;
  688. typedef struct
  689. {
  690. mail_send_t send;
  691. #define MAIL_RECV_MAX 10
  692. mail_recv_t recv[MAIL_RECV_MAX]; //接收账号列表
  693. }mail_info_t;
  694. enum {
  695. MQTT_PLAT_EMQX=0,
  696. MQTT_PLAT_ALIYUN,
  697. MQTT_PLAT_MAX
  698. };
  699. typedef struct {
  700. int id; //id
  701. int plat; //0: emqx 1: aliyun
  702. int mode; //0: open 1: close
  703. char proto[16]; //mqtt, mqtts, ws, wss
  704. char name[32]; //server name
  705. char server[512]; //server ip
  706. char port[10]; //server port
  707. char cid[64]; //client ID
  708. char user[64]; //
  709. char password[64];
  710. char cert[8200];
  711. int status;
  712. }mqtt_server_t;
  713. typedef struct {
  714. #define MQTT_SER_MAX 5
  715. mqtt_server_t ser[MQTT_SER_MAX];
  716. }mqtt_info_t;
  717. typedef struct
  718. {
  719. int product_id;
  720. int userID; //用户ID
  721. char userName[32]; //用户名-登录
  722. char userPW[32]; //用户密码
  723. char nickName[32]; //用户名称
  724. int roleID; //角色ID
  725. char createTime[32]; //用户创建时间
  726. struct list_head list;
  727. }UserInfo;
  728. typedef struct
  729. {
  730. int product_id;
  731. int roleID; //角色ID
  732. char roleName[32]; //角色名称
  733. int roleLevel; //角色等级
  734. char roleAuthority[32]; //角色权限
  735. int roleSort; //显示顺序
  736. char createTime[32]; //创建时间
  737. struct list_head list;
  738. }RoleInfo;
  739. typedef struct MenuAuthority{
  740. bool PowerEnabled;
  741. bool SensorEnabled;
  742. bool AlarmEnabled;
  743. bool LogEnabled;
  744. bool UserInfoEnabled;
  745. bool CommEnabled;
  746. bool ServiceEnabled;
  747. bool MaintenanceEnabled;
  748. //1、电源监控菜单
  749. bool MonitorallEnabled;
  750. bool ChannelstatusEnabled;
  751. bool SwitchtatusEnabled;
  752. bool MonitorhistoryEnabled;
  753. //2、传感器管理菜单
  754. bool SensorStatusEnabled;
  755. bool SensorHistorylEnabled;
  756. //3、告警管理菜单
  757. bool XtjgEnabled;
  758. bool YzszEnabled;
  759. //4、日志信息菜单
  760. bool CzrzEnabled;
  761. bool YxrzEnabled;
  762. //5、用户管理菜单
  763. bool UserEnabled;
  764. bool RoleEnabled;
  765. bool MenuEnabled;
  766. //6、通信管理菜单
  767. bool WkszEnabled;
  768. bool RsszEnabled;
  769. //7、服务管理菜单
  770. bool SmtpszEnabled;
  771. bool SnmpszEnabled;
  772. bool MqszEnabled;
  773. bool NtpszEnabled;
  774. bool DsszEnabled;
  775. bool SxszEnabled;
  776. bool FzszEnabled;
  777. //8、设备维护菜单
  778. bool SbcqEnabled;
  779. bool GjsjEnabled;
  780. bool KzbsjEnabled;
  781. bool HhccszEnabled;
  782. bool PzdrdcEnabled;
  783. bool ShfwEnabled;
  784. bool GysbEnabled;
  785. }MenuAuthority;
  786. typedef struct CascadeDevices{
  787. int product_id; //级联设备ID modbus地址
  788. int type; //
  789. char device_name[20]; //接连设备名;
  790. float valtage; //485
  791. float current;
  792. float power;
  793. float factor;
  794. float freq;
  795. float consumption;
  796. }CascadeDevices;
  797. struct modbus_slave_read_reg{
  798. uint16_t deveice_type; //设备类型
  799. uint16_t slave_addr; //设备地址
  800. uint32_t voltage; //当前电压
  801. uint32_t current; //当前电流
  802. uint32_t power; //当前功率
  803. uint32_t consumption_cur; //耗电量
  804. uint32_t freq; //频率
  805. uint32_t factor; //功率因素
  806. uint32_t temprature; //温度值
  807. uint32_t humidity; //湿度值
  808. uint32_t warning; //告警
  809. uint16_t power_supplay_status; //供电状态
  810. uint16_t sencor_status; //传感器状态
  811. uint32_t temprature2; //温度值2
  812. // uint32_t Warinfo;
  813. // uint16_t sercor_type;
  814. };
  815. typedef struct cascade_pw_info{
  816. uint32_t voltage ;//电压
  817. uint32_t current ;//电流
  818. uint32_t power ;//功率
  819. uint32_t freq ;//频率
  820. uint32_t consumption ;//耗电量
  821. uint32_t factor ;//功率因素
  822. }cascade_single_chn_info; //siggle hell
  823. typedef struct temp_hum{
  824. uint16_t temperature;
  825. uint16_t humidity;
  826. }temp_hum_t;
  827. typedef uint32_t sensor_t;
  828. typedef uint16_t dry_node_t;
  829. typedef struct cascade_sensor{
  830. sensor_t s_1;
  831. sensor_t s_2;
  832. }cascade_sensor_t;
  833. #pragma pack (1)
  834. typedef struct cascade_single_mmap{
  835. uint16_t dev_chn;
  836. cascade_single_chn_info all_ch[128];
  837. uint32_t total_valtage;
  838. uint32_t total_current;
  839. uint32_t total_power;
  840. uint32_t total_consumer;
  841. temp_hum_t total_temp;
  842. uint16_t chn_status[64];
  843. uint16_t open_delay[64];
  844. uint16_t close_delay[64];
  845. //uint16_t reservers[29];
  846. cascade_sensor_t cas_sensor[4];
  847. dry_node_t d_node[4];
  848. uint16_t reservers[9];
  849. uint16_t set_chn_open_delay[64];
  850. uint16_t set_chn_close_delay[64];
  851. uint16_t set_chn_switch_status[64];
  852. uint16_t set_chn_all_switch_status;
  853. }cascade_single_mmap_t;
  854. #pragma pack ()
  855. #pragma pack (1)
  856. #define MAX_CHN 64
  857. typedef struct cascade_triphasic_pw_out{
  858. uint32_t o_voltage;
  859. uint32_t o_current;
  860. uint32_t o_power;
  861. uint32_t o_consumption;
  862. uint32_t o_status;
  863. }cascade_triphasic_pw_out_t;
  864. typedef struct cascade_triphasic_pw_o_chn{
  865. cascade_triphasic_pw_out_t chn[3]; // A B C
  866. }cascade_triphasic_pw_o_chn_t;
  867. typedef struct cascade_triphasic_pw_in{
  868. uint32_t i_total_voltage;
  869. uint32_t i_total_current;
  870. uint32_t i_total_power;
  871. uint32_t i_total_consumption;
  872. }cascade_triphasic_pw_in_t;
  873. typedef struct cascade_triphasic_pw_i_total{
  874. cascade_triphasic_pw_in_t pw_i_total[3]; // A B C
  875. }cascade_triphasic_pw_i_total_t;
  876. typedef struct cascade_triphasic_chn_info{
  877. uint32_t chn_power;
  878. uint32_t chn_freq;
  879. uint32_t chn_consumption;
  880. uint32_t chn_factor;
  881. uint32_t chn_type;
  882. }cascade_triphasic_chn_info_t;
  883. typedef struct cascade_triphasic_mmap{
  884. uint16_t dev_chn;
  885. cascade_triphasic_pw_i_total_t i_total;
  886. cascade_triphasic_chn_info_t chn_info[MAX_CHN];
  887. cascade_triphasic_pw_o_chn_t pw_o_chn[MAX_CHN];
  888. uint16_t temperature;
  889. uint16_t humidity;
  890. uint16_t chn_status[MAX_CHN];
  891. uint16_t open_delay[MAX_CHN];
  892. uint16_t close_delay[MAX_CHN];
  893. cascade_sensor_t cas_sensor[4];
  894. dry_node_t d_node[4];
  895. uint16_t set_chn_open_delay[MAX_CHN];
  896. uint16_t set_chn_close_delay[MAX_CHN];
  897. uint16_t set_chn_switch_status[MAX_CHN];
  898. uint16_t set_chn_all_switch_status;
  899. }cascade_triphasic_mmap_t;
  900. typedef struct
  901. {
  902. modbus_t *ctx;
  903. char ip[64];
  904. uint16_t port;
  905. uint8_t slave_id;
  906. uint8_t power_type;
  907. modbus_mapping_t *map;
  908. int connection;
  909. //pthread_mutex_t mutex;
  910. }Modbus_Manger_Tcp;
  911. #pragma pack ()
  912. struct modbus_slave_write_reg{
  913. uint16_t voltage_high_enable:1; //电压高阈值检测使能
  914. uint16_t voltage_low_enable:1; //电压低阈值检测使能
  915. uint16_t current_enable:1; //电流阈值使能
  916. uint16_t power_enable:1; //功率阈值使能
  917. uint16_t consumption_cur_enable:1; //功耗阈值使能
  918. uint32_t voltage_upper_threshold; //
  919. uint32_t voltage_lower_threshold;
  920. uint32_t current_upper_threshold;
  921. uint32_t power_upper_threshold;
  922. uint32_t consumption_threshold;
  923. };
  924. enum
  925. {
  926. DI,
  927. RS485
  928. };
  929. enum{
  930. SWITCH_CONNETCTED,
  931. SWITCH_DISCONNETCTED,
  932. SWITCH_OFFLINE
  933. };
  934. #define SWITCH_CHN_1_SET (1<<0)
  935. #define SWITCH_CHN_2_SET (1<<1)
  936. #define SWITCH_CHN_3_SET (1<<2)
  937. #define SWITCH_CHN_4_SET (1<<3)
  938. #define SWITCH_CHN_5_SET (1<<4)
  939. #define SWITCH_CHN_6_SET (1<<5)
  940. #define SWITCH_CHN_DI_1_SET (1<<6)
  941. #define SWITCH_CHN_DI_2_SET (1<<7)
  942. typedef struct {
  943. int id;
  944. int type;
  945. char date[32];
  946. char content[128];
  947. }alarm_data_t;
  948. typedef struct {
  949. int ch;
  950. char date[16];
  951. char time[16];
  952. int status;
  953. int triggered;
  954. }timer_data_t;
  955. typedef struct {
  956. int addr;
  957. char name[32];
  958. char ver[32];
  959. char date[32];
  960. int dev_id;
  961. int brd_id;
  962. int channels;
  963. int status;
  964. }board_info_t;
  965. typedef struct breaker_switch_sta{
  966. uint16_t status;
  967. uint16_t gather;
  968. char com[30];
  969. uint8_t product_name[256];
  970. }breaker_switch_sta_t;
  971. typedef struct GlobalBreakerManger{
  972. uint32_t all_count;
  973. uint32_t online_count;
  974. uint32_t mask;
  975. uint8_t saddr;
  976. void *manager;
  977. breaker_switch_sta_t sta[8]; // 6 + 2
  978. pthread_mutex_t b_mutex;
  979. }GlobalBreakerManger;
  980. typedef struct GlobalBreakerManager{
  981. uint32_t product_id;
  982. uint32_t breaker_id;
  983. uint32_t breaker_chn;
  984. uint32_t breaker_status;
  985. uint32_t breaker_gather_addr;
  986. char breaker_switch_name[128];
  987. char breaker_com[64];
  988. char breaker_gather_type[64];
  989. char breaker_chns_name[128];
  990. struct list_head list;
  991. }GlobalBreakerManager;
  992. //设备总体管理
  993. typedef struct
  994. {
  995. sqlite3* db; //数据库接口
  996. pthread_mutex_t dbThreadlock; //写入线程锁
  997. pthread_t appweb_thread; //appweb线程
  998. pthread_t dev_samp_thread;//电源采集与控制线程
  999. pthread_t dev_update_thread;//电源采集与控制线程
  1000. pthread_t io_thread; //IO采集线程
  1001. pthread_t sensor_thread; //传感器采集现成
  1002. bool dev_samp_flag ; //电源采集线程标志
  1003. GlobalDeviceInfo _globalDevInfo; //设备管理信息
  1004. GlobalPowerManger _globalPowerManger; //电源管理表
  1005. GlobalSensorManger _globalSensorManger; //传感器管理表
  1006. Modbus_Manger sensor_modbus_manager; //通信接口管理
  1007. unsigned int useSlaveCount ; //有效地址
  1008. Modbus_Manger _globalRelaySampManger; //内部继电器使用
  1009. unsigned char uart_init_flag[6] ; //
  1010. bool ws_isok; //ws准备好
  1011. struct mg_connection *ws_client; //websocket管理信息
  1012. HashTable* alarmTab; //全局的报警映射
  1013. int nTreeACLshow; //三项总数据显示L1=0,L2=1,L3=2
  1014. pthread_t snmp_agent_thread;//SNMP代理线程
  1015. bool _ReloadChm_flag; //重新加载端口数据
  1016. int brdMax;
  1017. GlobalPowerCtrlBoard *pCtrlBoard; //所有控制板
  1018. LowConf* _RS485LowConfData;//485Data
  1019. //传感器
  1020. struct modbus_slave_read_reg g_modebus_read;
  1021. struct modbus_slave_write_reg g_modebus_write;
  1022. smartPDU_shm_data* g_shm_Data;
  1023. pthread_mutex_t _power_ds; //定时器锁
  1024. pthread_mutex_t _power_update; //数据刷新锁
  1025. pthread_mutex_t _sensor_update; //传感器刷新锁
  1026. pthread_mutex_t _breaker_mutex; // breaker info
  1027. cascade_single_mmap_t *single_mmap;
  1028. cascade_triphasic_mmap_t *triphasic_mmap;
  1029. GlobalBreakerManager g_new_global_breaker;
  1030. mail_info_t mailInfo;
  1031. mqtt_info_t mqttInfo;
  1032. Modbus_Manger_Tcp *md_tcp;
  1033. }GlobalDeviceManager;
  1034. extern GlobalDeviceManager __globalDeviceManage;
  1035. extern GlobalDeviceManager __globalDeviceManage2;
  1036. extern int ntp_sync_flag;
  1037. extern unsigned long __globalSxIdx;
  1038. extern _User_Log _GlobalUserLoginInfo;//全局用户登录信息
  1039. extern UserInfo _GlobalUserInfoNew;////最近登录的信息
  1040. extern char _GlobalUserToken[64];////最近登录的信息
  1041. extern MenuAuthority _GlobalMenuAuthority;////角色菜单权限
  1042. extern NTPManager _globalNTPManager;
  1043. extern SNMPManager _globalSNMPManager;
  1044. void initLogger(void);
  1045. //打印设备信息结构
  1046. void printf_global_device_info(GlobalDeviceInfo* _globalDeviceInfo);
  1047. //打印设备超限电源管理
  1048. void printf_global_power_manage_info(GlobalPowerManger* _globalPowerManger);
  1049. //打印设备超限电源通道管理
  1050. //void printf_global_power_chn_manage_info(GlobalPowerChnManger* _globalPowerChnManger);
  1051. void printf_power_info(GlobalPowerInfo* _globalPowerInfo);
  1052. int getTotalMemorySize(unsigned int* totalSize);
  1053. char* getLocalIpAddress(char* dev);
  1054. char* get_mac_address(const char *iface_name) ;
  1055. void get_disk_occupy(char ** reused);
  1056. int get_tzone(void);
  1057. int set_tzone(int tzone);
  1058. int set_time(struct tm *t, int tzone);
  1059. int set_utctime(time_t t);
  1060. int set_time_zone(struct tm *t, int tzone);
  1061. int set_utctime_zone(time_t t, int tzone);
  1062. time_t mktime0(void);
  1063. time_t mktime2(struct tm *t, int tzone);
  1064. int get_time_str(char *s, int len, time_t t);
  1065. int get_time_str2(char *s, int len, time_t t);
  1066. int get_curtime_str(char *s, int len);
  1067. int get_curtime_str2(char *s, int len);
  1068. int get_bldtime(char *buf, int len);
  1069. int get_bldtime2(char *dst, char *ori);
  1070. void print_time(char *s, struct tm *tm);
  1071. void print_utc(char *s, time_t t);
  1072. char* getUserNamebyToken(char* Token);
  1073. int is_process_running(const char *process_name);
  1074. bool CheckLoginStatus(char* Token);
  1075. // 辅助函数:判断是否是闰年
  1076. int is_leap_year(int year);
  1077. // 辅助函数:获取某月的天数
  1078. int days_in_month(int year, int month);
  1079. // 函数:增加一天到日期
  1080. void add_day_to_date(char *date_str, int *year, int *month, int *day);
  1081. void SetMenuAuthority(int RoleID);
  1082. #endif