appl.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #ifndef __APPL_H__
  2. #define __APPL_H__
  3. #include "modbus.h"
  4. #include "MQTTClient.h"
  5. #include "MQTTClientPersistence.h"
  6. enum CommState_t
  7. {
  8. ST_COMM_ERR = 0,
  9. ST_COMM_NORM = 1,
  10. };
  11. enum chan485_state_t
  12. {
  13. ST_485_INIT = 0,
  14. ST_485_RUN,
  15. ST_485_ERR,
  16. };
  17. enum chan485_err_t{
  18. ERR_485_NONE = 0,
  19. ERR_485_INIT_FAIL,
  20. };
  21. enum chan485_cmd_t
  22. {
  23. CMD_485_DONE = 0,
  24. CMD_485_RESET = 1,
  25. };
  26. #define CHAN485_NBR 8
  27. struct chan485_t{
  28. pthread_mutex_t mutex;
  29. char szinfo[128];
  30. int state;
  31. char szstate[32];
  32. int err;
  33. char szerr[64];
  34. int Cmd;
  35. int CmdParam;
  36. int baud;
  37. char szdev[32];
  38. char parity;
  39. int64_t reqcnt;
  40. int64_t failcnt;
  41. int64_t loopcnt;
  42. int64_t looptime;
  43. };
  44. enum chancan_state_t
  45. {
  46. ST_CAN_INIT = 1,
  47. ST_CHANCAN_RUN,
  48. ST_CHANCAN_ERR,
  49. };
  50. enum chancan_err_t{
  51. ERR_CAN_NONE = 0,
  52. ERR_CAN_INIT_FAIL,
  53. };
  54. enum chancan_cmd_t
  55. {
  56. CMD_CHANCAN_DONE = 0,
  57. CMD_CHANCAN_RESET = 1,
  58. };
  59. #define CHANCAN_NBR 3
  60. struct chancan_t{
  61. int State;
  62. char szState[32];
  63. int Err;
  64. char szErr[128];
  65. int Cmd;
  66. int sock;
  67. char szdev[32];
  68. int64_t RdCnt;
  69. int64_t RdFailcnt;
  70. int64_t WrCnt;
  71. int64_t WrFailcnt;
  72. int64_t Loopcnt;
  73. int64_t LoopTime;
  74. char szinfo[128];
  75. pthread_mutex_t mutex;
  76. };
  77. #define CHAN_MQTT_NBR 4
  78. struct chanmqtt_t{
  79. char szs_url[128];
  80. char szs_sub_topic[16][128];
  81. char szs_pub_topic[16][128];
  82. char szusrname[128];
  83. char szpasswd[128];
  84. char szclientid[128];
  85. int s_qos; // MQTT QoS
  86. struct mg_connection* s_conn; // Client connection
  87. MQTTClient s_paho_client;
  88. char szState[64];
  89. int bConnected;
  90. int64_t LastFastUpload;
  91. int64_t LastMediumUpload;
  92. int64_t LastSlowUpload;
  93. int Cmd;
  94. int CmdParam;
  95. int64_t TotalReconn;
  96. int64_t TotalSend;
  97. int64_t TotalRecv;
  98. int MaxIntv;
  99. int AvgIntv;
  100. int64_t TotalIntv;
  101. int64_t LastRecv;
  102. };
  103. enum MqttCmd_t{
  104. CMD_MQTT_DONE = 0,
  105. CMD_MQTT_REGISTER = 1,
  106. };
  107. // !!! 注意,不要插入
  108. struct Settings_t{
  109. long long chksum;
  110. int bErr;
  111. char szState[32];
  112. char szCloudUserName[128];
  113. char szCloudPasswd[128];
  114. char szCloudUrl[128];
  115. char szClientId[128];
  116. int DataKeepDay;
  117. int UploadHighSpeed; // ms
  118. int UploadMediumSpeed; //ms
  119. int UploadSlowSpeed; //ms
  120. char szCloudLocalUserName[128];
  121. char szCloudLocalPasswd[128];
  122. char szCloudLocalUrl[128];
  123. char szLocalClientId[128];
  124. char meterDataLoadFileName[128];
  125. };
  126. union UnSettings_t{
  127. struct Settings_t s;
  128. char buf[1024*64];
  129. };
  130. enum SettingsCmd_t{
  131. SETTINGS_CMD_SET_CLOUD_USERNAME = 101,
  132. SETTINGS_CMD_SET_CLOUD_PASSWD = 102,
  133. SETTINGS_CMD_SET_CLOUD_URL = 103,
  134. SETTINGS_CMD_REGISTER = 104,
  135. SETTINGS_CMD_SET_CLOUD_CLIENTID = 105,
  136. SETTINGS_CMD_SET_CLOUD_UPLOADINTV = 106,
  137. SETTINGS_CMD_SET_DATAKEEPDAY = 200,
  138. SETTINGS_CMD_SET_UPLOADHIHGSPEED = 201,
  139. SETTINGS_CMD_SET_UPLOADMEDIUMSPEED = 202,
  140. SETTINGS_CMD_SET_UPLOADSLOWSPEED = 203,
  141. SETTINGS_CMD_SET_LOCAL_USERNAME = 401,
  142. SETTINGS_CMD_SET_LOCAL_PASSWD = 402,
  143. SETTINGS_CMD_SET_LOCAL_URL = 403,
  144. SETTINGS_CMD_SET_LOCAL_CLIENTID = 405,
  145. SETTINGS_CMD_LOAD_METER_CSV = 410,
  146. SEETINGS_CMD_METER_DATA_FILE_NAME = 420,
  147. SETTINGS_CMD_TEST = 1000,
  148. };
  149. enum Metersettings{
  150. PT = 201,
  151. CT = 202,
  152. };
  153. struct Snap_t{
  154. int enable;
  155. int timer;
  156. int intv;
  157. int bStart;
  158. int bErr;
  159. char szState[32];
  160. char szcurrDatePath[32];
  161. int datechk_timer;
  162. int KeepDay;
  163. FILE* fpcs;
  164. FILE* fbms;
  165. FILE* fac;
  166. FILE* fctl;
  167. FILE* fmisc;
  168. FILE* f[8];
  169. int64_t LastSnap;
  170. };
  171. #define DTSD1352_NBR 8
  172. struct Dtsd1352_t{
  173. char szinfo[128];
  174. int Adr;
  175. modbus_t* ctx;
  176. double com_active_e;
  177. double com_active_p;
  178. double com_ractive_p;
  179. double pos_active_e;
  180. double pos_active_dem;
  181. double neg_active_dem;
  182. double neg_active_e;
  183. double pwr_factor;
  184. int PT;
  185. int CT;
  186. double ua;
  187. double ub;
  188. double uc;
  189. double ia;
  190. double ib;
  191. double ic;
  192. double freq;
  193. int64_t LastUpdate;
  194. char szLastUpdate[32];
  195. int CommState;
  196. char szCommState[32];
  197. int64_t CommFailTotalCnt;
  198. };
  199. struct MeterDayData_t{
  200. double com_active_e[24*60*60/5];
  201. double com_active_p[24*60*60/5];
  202. double com_ractive_p[24*60*60/5];
  203. double pos_active_e[24*60*60/5];
  204. double pos_active_dem[24*60*60/5];
  205. double neg_active_dem[24*60*60/5];
  206. double neg_active_e[24*60*60/5];
  207. double pwr_factor[24*60*60/5];
  208. int PT[24*60*60/5];
  209. int CT[24*60*60/5];
  210. double ua[24*60*60/5];
  211. double ub[24*60*60/5];
  212. double uc[24*60*60/5];
  213. double ia[24*60*60/5];
  214. double ib[24*60*60/5];
  215. double ic[24*60*60/5];
  216. double freq[24*60*60/5];
  217. };
  218. struct appl_t{
  219. struct chan485_t chan485[CHAN485_NBR + 1];
  220. struct chancan_t chancan[CHANCAN_NBR + 1];
  221. struct chanmqtt_t chanmqtt[CHAN_MQTT_NBR + 1];
  222. union UnSettings_t Set;
  223. struct Dtsd1352_t Dtsd1352[DTSD1352_NBR + 1];
  224. struct Snap_t Snap;
  225. struct MeterDayData_t MeterDayData;
  226. };
  227. typedef void *(*Thread_485)(void* param);
  228. struct MeterConfig_t{
  229. uint32_t ch_idx;
  230. uint32_t adr;
  231. char szTopic[32];
  232. char szinfo[32];
  233. };
  234. struct Chan485Config_t{
  235. uint32_t used;
  236. char szDev[32];
  237. uint32_t baud;
  238. char parity;
  239. pthread_t* hthrd;
  240. Thread_485 thread;
  241. char szInfo[32];
  242. };
  243. extern char* VERSION;
  244. extern char* CFG_FN;
  245. extern struct appl_t APPL;
  246. extern void appl_chan485_lock(int idx);
  247. extern void appl_chan485_unlock(int idx);
  248. int appl_load_meter_data_from_file(void);
  249. double appl_meter_com_ap_get_from_file(void);
  250. uint32_t appl_get_meter_config_nbr(void);
  251. #endif