appl.h 4.6 KB

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