appl.h 4.6 KB

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