task.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include "task.h"
  2. #include "cjson.h"
  3. #include "myFile.h"
  4. #include "gateway_message.h"
  5. #include "log.h"
  6. #include "malloc.h"
  7. #include "sx1276.h"
  8. #include "protocol.h"
  9. #include "usart.h"
  10. #include "node_data_acquisition.h"
  11. #include "sys_mqtt.h"
  12. #include "sys_http.h"
  13. #include "node_message.h"
  14. #include "usart.h"
  15. #include "mmodbus.h"
  16. #include "sys_mqtt.h"
  17. #include "gateway_message.h"
  18. #include "MQTTClient.h"
  19. #include "cJSON.h"
  20. void master_task(uint8_t *string,uint16_t stringlength);
  21. void slave_task();
  22. int READ_MODBUS_DATA(DEVICE_PARAMS *current_device);
  23. uint16_t BufferSize;
  24. uint8_t Buffer[256];
  25. uint32_t rx_num = 0;
  26. uint8_t PingMsg[] = "PING\0";
  27. uint8_t PongMsg[] = "PONG\0";
  28. tRadioDriver *Radio=NULL;
  29. /*
  30. *********************************************************************************************************
  31. * �� �� ��: void data_task(void *pdata)
  32. * ����˵��: ��Ҫ��data_task�����̣߳����ȼ��ߡ��������߼��ǽ�nandflash�е����ݽ���������ѯ��������
  33. * �� ����
  34. * �� �� ֵ: ��
  35. *********************************************************************************************************
  36. */
  37. void data_task(void *pdata)
  38. {
  39. OS_CPU_SR cpu_sr;
  40. pdata = pdata;
  41. // Radio = RadioDriverInit();
  42. // Radio->Init();
  43. mmodbus_init(1);
  44. char *device_config_json = mymalloc(SRAMEX, 9 * 1024);
  45. read_file("device.txt", device_config_json);
  46. addGatewayParams(device_config_json);
  47. myfree(SRAMEX, device_config_json);
  48. GATEWAY_PARAMS *get;
  49. get= get_gateway_config_params();
  50. int deviceIndex=0;
  51. DEVICE_PARAMS *current_device=get->device_params;
  52. uint32_t baudrate = get->baudrate;
  53. uint8_t databits = get->dataBits;
  54. uint8_t stopbits = get->stopBit;
  55. uint8_t parity = get->parity;
  56. uint8_t flowcontrol = get->flowControl;
  57. Config_485_Port(baudrate, databits, stopbits, parity, flowcontrol);
  58. uint8_t string[256];
  59. uint16_t bufferLength;
  60. OS_Q_DATA Qnum;
  61. StringInfo message;
  62. char *mqttRecv;
  63. uint8_t err;
  64. while (current_device!=NULL)
  65. {
  66. READ_MODBUS_DATA(current_device);
  67. if(current_device->nextDevice!=NULL)
  68. {
  69. current_device=current_device->nextDevice;
  70. }
  71. else
  72. {
  73. int msg = MBOX_USER_PUBLISHQOS2;
  74. if(mqtt_connectFlag==1) OSMboxPost(mqtt_sendMseeageMbox, &msg);
  75. memset(pubJsonString,0,strlen(pubJsonString));
  76. current_device=get->device_params;
  77. }
  78. }
  79. }
  80. #if 0 //���Դ��벻����ת��ֱ�ӽ�����Ӧ�Ľ���
  81. SlaveProtocolAnalysis(string,bufferLength);
  82. data_acquisition();
  83. uint8_t node_string[256];
  84. uint16_t node_string_Length;
  85. nodeSendReaddValue(node_string,&node_string_Length);
  86. GatewayProtocolAnalysis(node_string,node_string_Length);
  87. #endif
  88. /*
  89. *********************************************************************************************************
  90. * �� �� ��: int READ_MODBUS_DATA(DEVICE_PARAMS *device)
  91. * ����˵��: ��ȡ��ǰ�ڵ��ϵ�modbus����
  92. * �� �Σ�DEVICE_PARAMS *device ��ǰ�豸
  93. * �� �� ֵ: 1 ���ɹ� 0��ʧ��
  94. *********************************************************************************************************
  95. */
  96. int READ_MODBUS_DATA(DEVICE_PARAMS *device)
  97. {
  98. DEVICE_PARAMS *current_device=device;
  99. GATEWAY_READ_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_read_modbus_command;
  100. GATEWAY_READ_DLT645_COMMAND *currentDLT645Params = current_device->params->gateway_read_dlt645_command;
  101. while(current_device->params != NULL)
  102. {
  103. if (current_device->protocol == MODBUS_READ)
  104. {
  105. protocol_485=1;
  106. uint16_t data[currentModbusParams->registerByteNum /2]; // modbus�Ĵ�������
  107. mmodbus_set16bitOrder(current_device->MDBbigLittleFormat);
  108. if (currentModbusParams->functionCode == 0x03 | currentModbusParams->functionCode == 0x01)
  109. {
  110. bool success = mmodbus_readHoldingRegisters16i(currentModbusParams->slaveAddress,
  111. currentModbusParams->registerAddress,
  112. currentModbusParams->registerByteNum /2,
  113. data);
  114. if (success)
  115. {
  116. uint32_t value;
  117. if (currentModbusParams->registerByteNum == 4)
  118. {
  119. value = (uint32_t)data[0] | data[1];
  120. }
  121. else if (currentModbusParams->registerByteNum == 2)
  122. {
  123. value = data[0];
  124. }
  125. if (currentModbusParams->decimalPoint == 0)
  126. {
  127. currentModbusParams->value[0] = value;
  128. currentModbusParams->value[1] = value << 8;
  129. currentModbusParams->value[2] = value << 16;
  130. currentModbusParams->value[3] = value << 24;
  131. }
  132. else
  133. {
  134. float convertedValue = (float)value / pow(10, currentModbusParams->decimalPoint);
  135. memcpy(currentModbusParams->value, &convertedValue, 4);
  136. }
  137. sprintf(pubJsonString + strlen(pubJsonString), "{\"data\":[{\"deviceId\":\"%s\",\"%s\":%d}]},",
  138. current_device->deviceID, currentModbusParams->keyword, value);
  139. currentModbusParams = currentModbusParams->nextParams;
  140. if (currentModbusParams == NULL)
  141. {
  142. sprintf(pubJsonString + strlen(pubJsonString) - 1, "");
  143. return 0;
  144. }
  145. }
  146. }
  147. }
  148. else if (current_device->protocol == MODBUS_WRITE)
  149. {
  150. protocol_485=1;
  151. }
  152. else if (current_device->protocol == DLT645_07 || current_device->protocol == DLT645_97)
  153. {
  154. protocol_485=2;
  155. uint8_t read_buf[10];
  156. currentDLT645Params->rxLen = 0;
  157. memset(read_buf, 0, 10);
  158. memset(currentDLT645Params->data, 0, 10);
  159. dlt645_set_addr(&dlt645, current_device->params->gateway_read_dlt645_command->deviceID645);
  160. int8_t rs;
  161. if (current_device->protocol == DLT645_07)
  162. {
  163. rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_2007);
  164. }
  165. else if (current_device->protocol == DLT645_97)
  166. {
  167. rs = dlt645_read_data(&dlt645, currentDLT645Params->Identification, read_buf, DLT645_1997);
  168. }
  169. if (rs != -1)
  170. {
  171. if (rs <= 4)
  172. {
  173. memcpy(currentDLT645Params->data, read_buf, 4);
  174. currentDLT645Params->rxLen = 4;
  175. }
  176. else if (rs == 5)
  177. {
  178. memcpy(currentDLT645Params->data, read_buf, 5);
  179. currentDLT645Params->rxLen = 5;
  180. }
  181. else if (rs > 5)
  182. {
  183. memcpy(currentDLT645Params->data, read_buf, 9);
  184. currentDLT645Params->rxLen = 9;
  185. }
  186. }
  187. else
  188. {
  189. currentDLT645Params->rxLen =0;
  190. }
  191. sprintf(pubJsonString + strlen(pubJsonString), "{\"data\":[{\"deviceId\":\"%s\",\"%s\":%s},]}",
  192. current_device->deviceID, currentDLT645Params->keyword, currentDLT645Params->data);
  193. currentDLT645Params = currentDLT645Params->nextParams;
  194. if (currentDLT645Params == NULL)
  195. {
  196. return 0;
  197. }
  198. }
  199. }
  200. return 1;
  201. }
  202. /*
  203. *********************************************************************************************************
  204. * �� �� ��:void WRITE_MODBUS_DATA(char* cJSONstring)
  205. * ����˵��: ����mqtt���ݲ�д��modbus�Ĵ���
  206. * �� �Σ�char* cJSONstring mqtt���յ�������
  207. * �� �� ֵ: ��
  208. *********************************************************************************************************
  209. */
  210. void WRITE_MODBUS_DATA(char* cJSONstring){
  211. GATEWAY_PARAMS* get;
  212. get = get_gateway_config_params();
  213. DEVICE_PARAMS* current_device = get->device_params;
  214. cJSON *root = cJSON_Parse(cJSONstring);
  215. const char *deviceId = cJSON_GetStringValue(cJSON_GetObjectItem(root, "deviceId"));
  216. const cJSON *power = cJSON_GetObjectItemCaseSensitive(root, "power");
  217. const cJSON *temp = cJSON_GetObjectItemCaseSensitive(root, "temp");
  218. const cJSON *mode = cJSON_GetObjectItemCaseSensitive(root, "mode");
  219. const cJSON *fan = cJSON_GetObjectItemCaseSensitive(root, "fan");
  220. uint16_t slaveAddress;
  221. uint16_t number;
  222. uint16_t data;
  223. while(current_device)
  224. {
  225. char* device_ID = (char*)current_device->deviceID;
  226. GATEWAY_WRITE_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_write_modbus_command;
  227. if(!strcmp(device_ID,deviceId)){
  228. OSTimeDlyHMSM(0, 0, 0, 60);
  229. OSIntEnter();
  230. if(power){
  231. slaveAddress = currentModbusParams->slaveAddress;
  232. number =currentModbusParams->registerAddress;
  233. data = power->valueint;
  234. mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
  235. }
  236. if(temp){
  237. currentModbusParams = currentModbusParams->nextParams;
  238. slaveAddress = currentModbusParams->slaveAddress;
  239. number =currentModbusParams->registerAddress;
  240. data = temp->valueint;
  241. mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
  242. }
  243. if(mode){
  244. currentModbusParams = currentModbusParams->nextParams;
  245. slaveAddress = currentModbusParams->slaveAddress;
  246. number =currentModbusParams->registerAddress;
  247. data = mode->valueint;
  248. mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
  249. }
  250. if(fan){
  251. currentModbusParams = currentModbusParams->nextParams;
  252. slaveAddress = currentModbusParams->slaveAddress;
  253. number =currentModbusParams->registerAddress;
  254. data = fan->valueint;
  255. mmodbus_writeHoldingRegister16i(slaveAddress, number, data);
  256. }
  257. OSIntExit();
  258. }
  259. current_device = current_device->nextDevice;
  260. }
  261. cJSON_Delete(root);
  262. }