|  | @@ -11,55 +11,51 @@
 | 
	
		
			
				|  |  |  #include "tcp_server.h"
 | 
	
		
			
				|  |  |  #include "log.h"
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -void protocolsModeFunc(GATEWAY_PARAMS* current_device, char* buf, char* string);
 | 
	
		
			
				|  |  | +void protocolsModeFunc(GATEWAY_PARAMS* current_device, char* string);
 | 
	
		
			
				|  |  |  void transparentModeFunc(DEVICE_PARAMS* current_device);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -int ID = 1;
 | 
	
		
			
				|  |  | -uint8_t count = 0;
 | 
	
		
			
				|  |  | +uint8_t recv_state = 0;
 | 
	
		
			
				|  |  | +uint8_t mode = 0;
 | 
	
		
			
				|  |  | +uint8_t def = 0;
 | 
	
		
			
				|  |  |  uint8_t startFlag = 0;// 读取数据初始标志位
 | 
	
		
			
				|  |  | -int time1,time2;
 | 
	
		
			
				|  |  | +int time1,time2,size;
 | 
	
		
			
				|  |  |  void data_task(void *pdata)
 | 
	
		
			
				|  |  |  {			
 | 
	
		
			
				|  |  |  		dlt645_init(1);	// 若读不到数据,则延时 参数 秒
 | 
	
		
			
				|  |  |  		mmodbus_init(10);// 若读不到数据,则延时 参数 秒
 | 
	
		
			
				|  |  | +	
 | 
	
		
			
				|  |  |  		GATEWAY_PARAMS *get;
 | 
	
		
			
				|  |  | -		// 如果flash有config数据,则解析,进行下一步;若flash无config数据,则等待上位机发送数据
 | 
	
		
			
				|  |  | -		do{
 | 
	
		
			
				|  |  | -				char *device_config_json=pvPortMalloc( 10 *1024 );
 | 
	
		
			
				|  |  | -				memset(device_config_json,0,strlen(device_config_json));
 | 
	
		
			
				|  |  | -				portENTER_CRITICAL();
 | 
	
		
			
				|  |  | -				read_data_from_flash(device_config_json);		
 | 
	
		
			
				|  |  | -				addGatewayParams(device_config_json);
 | 
	
		
			
				|  |  | -				vPortFree(device_config_json);
 | 
	
		
			
				|  |  | -				portEXIT_CRITICAL();
 | 
	
		
			
				|  |  | -				get= get_gateway_config_params();
 | 
	
		
			
				|  |  | -				delay_ms(100);
 | 
	
		
			
				|  |  | -		}while(get->device_params == NULL);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		char *device_config_json=pvPortMalloc( 10 *1024 );
 | 
	
		
			
				|  |  | +		memset(device_config_json,0,strlen(device_config_json));
 | 
	
		
			
				|  |  | +		portENTER_CRITICAL();
 | 
	
		
			
				|  |  | +		read_data_from_flash(device_config_json);	
 | 
	
		
			
				|  |  | +		addGatewayParams(device_config_json);
 | 
	
		
			
				|  |  | +		vPortFree(device_config_json);
 | 
	
		
			
				|  |  | +		portEXIT_CRITICAL();
 | 
	
		
			
				|  |  | +	
 | 
	
		
			
				|  |  | +		get= get_gateway_config_params();
 | 
	
		
			
				|  |  |  		LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"device params not empty");
 | 
	
		
			
				|  |  |  		DEVICE_PARAMS *current_device=get->device_params;
 | 
	
		
			
				|  |  |  		
 | 
	
		
			
				|  |  | -		char *buf = pvPortMalloc(1024);			// 接收读取的全部数据
 | 
	
		
			
				|  |  | -		char *string = pvPortMalloc(1024);	// 接收读取的不同数据
 | 
	
		
			
				|  |  | -		if(buf == NULL || string == NULL)
 | 
	
		
			
				|  |  | +		char *string = pvPortMalloc(3 * 1024);	// 接收读取数据
 | 
	
		
			
				|  |  | +		if(string == NULL)
 | 
	
		
			
				|  |  |  		{
 | 
	
		
			
				|  |  |  			LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"buf or string malloc fail");
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  | -		memset(buf,0,strlen(buf));
 | 
	
		
			
				|  |  |  		memset(string,0,strlen(string));
 | 
	
		
			
				|  |  |  		while (current_device!=NULL)
 | 
	
		
			
				|  |  | -		{				
 | 
	
		
			
				|  |  | +		{					
 | 
	
		
			
				|  |  |  			if(ProtocolsModeFlag)
 | 
	
		
			
				|  |  |  			{		
 | 
	
		
			
				|  |  | -				protocolsModeFunc(get, buf, string);	
 | 
	
		
			
				|  |  | +				protocolsModeFunc(get,string);	
 | 
	
		
			
				|  |  |  			}
 | 
	
		
			
				|  |  |  			else if(TransparentModeFlag)
 | 
	
		
			
				|  |  |  			{
 | 
	
		
			
				|  |  |  				transparentModeFunc(current_device);
 | 
	
		
			
				|  |  |  			}
 | 
	
		
			
				|  |  |  			current_device=get->device_params;	
 | 
	
		
			
				|  |  | -			vTaskDelay(1000);
 | 
	
		
			
				|  |  | +			vTaskDelay(500);
 | 
	
		
			
				|  |  |  		}	
 | 
	
		
			
				|  |  | -		vPortFree(buf);
 | 
	
		
			
				|  |  |  		vPortFree(string);
 | 
	
		
			
				|  |  |  		LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"data_task return");
 | 
	
		
			
				|  |  |  }
 | 
	
	
		
			
				|  | @@ -88,7 +84,7 @@ int compareArrays(uint8_t arr1[], uint8_t arr2[], int size) {
 | 
	
		
			
				|  |  |  *	返 回 值: 1: 成功 0:失败
 | 
	
		
			
				|  |  |  *********************************************************************************************************
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | -int read_device_data(DEVICE_PARAMS *device, char* buf, char* string)
 | 
	
		
			
				|  |  | +int read_device_data(DEVICE_PARAMS *device, char* string)
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  |  		DEVICE_PARAMS *current_device=device;	
 | 
	
		
			
				|  |  |  		GATEWAY_READ_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_read_modbus_command;
 | 
	
	
		
			
				|  | @@ -98,40 +94,8 @@ int read_device_data(DEVICE_PARAMS *device, char* buf, char* string)
 | 
	
		
			
				|  |  |  				gd_eval_led_toggle(LED_485TX);
 | 
	
		
			
				|  |  |  				if (current_device->protocol == MODBUS_READ)
 | 
	
		
			
				|  |  |  				{
 | 
	
		
			
				|  |  | -						uint8_t cmd;	//开关水阀命令
 | 
	
		
			
				|  |  | -						uint8_t state;	// 水阀状态
 | 
	
		
			
				|  |  | -						uint16_t data[currentModbusParams->registerByteNum /2]; // modbus寄存器长度
 | 
	
		
			
				|  |  | -						
 | 
	
		
			
				|  |  | +						uint16_t data[currentModbusParams->registerByteNum /2]; // modbus寄存器长度				
 | 
	
		
			
				|  |  |  						mmodbus_set16bitOrder(current_device->MDBbigLittleFormat);
 | 
	
		
			
				|  |  | -						// 读水阀状态
 | 
	
		
			
				|  |  | -						if(currentModbusParams->functionCode == 0x01)
 | 
	
		
			
				|  |  | -						{
 | 
	
		
			
				|  |  | -							bool success = mmodbus_readCoil(currentModbusParams->slaveAddress,
 | 
	
		
			
				|  |  | -																		 currentModbusParams->registerByteNum /2,
 | 
	
		
			
				|  |  | -																		 &state);
 | 
	
		
			
				|  |  | -							if(success)
 | 
	
		
			
				|  |  | -							{
 | 
	
		
			
				|  |  | -								if(state == 0)
 | 
	
		
			
				|  |  | -								{
 | 
	
		
			
				|  |  | -									sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":close},", 
 | 
	
		
			
				|  |  | -																									current_device->deviceID, currentModbusParams->keyword);
 | 
	
		
			
				|  |  | -								}else{
 | 
	
		
			
				|  |  | -									sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":open},", 
 | 
	
		
			
				|  |  | -																									current_device->deviceID, currentModbusParams->keyword);
 | 
	
		
			
				|  |  | -								}
 | 
	
		
			
				|  |  | -							}
 | 
	
		
			
				|  |  | -							currentModbusParams = currentModbusParams->nextParams;
 | 
	
		
			
				|  |  | -								if (currentModbusParams == NULL)	
 | 
	
		
			
				|  |  | -								{
 | 
	
		
			
				|  |  | -										current_device = current_device->nextDevice;
 | 
	
		
			
				|  |  | -										currentModbusParams = current_device->params->gateway_read_modbus_command;
 | 
	
		
			
				|  |  | -										if(current_device == NULL)
 | 
	
		
			
				|  |  | -										{
 | 
	
		
			
				|  |  | -												sprintf(buf + strlen(buf) - 1, ""); 
 | 
	
		
			
				|  |  | -												return 1;
 | 
	
		
			
				|  |  | -										}
 | 
	
		
			
				|  |  | -								}		
 | 
	
		
			
				|  |  | -						}
 | 
	
		
			
				|  |  |  						// 读单个寄存器
 | 
	
		
			
				|  |  |  						if (currentModbusParams->functionCode == 0x03)
 | 
	
		
			
				|  |  |  						{
 | 
	
	
		
			
				|  | @@ -151,21 +115,25 @@ int read_device_data(DEVICE_PARAMS *device, char* buf, char* string)
 | 
	
		
			
				|  |  |  										{
 | 
	
		
			
				|  |  |  												value = data[0];
 | 
	
		
			
				|  |  |  										}
 | 
	
		
			
				|  |  | -										if((value - currentModbusParams->value) != 0)
 | 
	
		
			
				|  |  | +										
 | 
	
		
			
				|  |  | +										if(mode == 0)// all
 | 
	
		
			
				|  |  |  										{
 | 
	
		
			
				|  |  | -												count++;
 | 
	
		
			
				|  |  | -												sprintf(string + strlen(string), "{\"deviceId\":\"%s\",\"%s\":%d},", 
 | 
	
		
			
				|  |  | +											sprintf(string + strlen(string), "{\"deviceId\":\"%s\",\"%s\":%d},", 
 | 
	
		
			
				|  |  |  																									current_device->deviceID, currentModbusParams->keyword, value);
 | 
	
		
			
				|  |  |  										}
 | 
	
		
			
				|  |  | -										else
 | 
	
		
			
				|  |  | -										{									
 | 
	
		
			
				|  |  | -												sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":%d},", 
 | 
	
		
			
				|  |  | -																									current_device->deviceID, currentModbusParams->keyword, value);
 | 
	
		
			
				|  |  | +										else if(mode == 1)// def
 | 
	
		
			
				|  |  | +										{
 | 
	
		
			
				|  |  | +											if((value - currentModbusParams->value) != 0)
 | 
	
		
			
				|  |  | +											{																
 | 
	
		
			
				|  |  | +													sprintf(string + strlen(string), "{\"deviceId\":\"%s\",\"%s\":%d},", 
 | 
	
		
			
				|  |  | +																										current_device->deviceID, currentModbusParams->keyword, value);
 | 
	
		
			
				|  |  | +													def = 1;
 | 
	
		
			
				|  |  | +											}
 | 
	
		
			
				|  |  |  										}
 | 
	
		
			
				|  |  | +										
 | 
	
		
			
				|  |  |  										if (currentModbusParams->decimalPoint == 0)
 | 
	
		
			
				|  |  |  										{
 | 
	
		
			
				|  |  |  												currentModbusParams->value = value;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  										}
 | 
	
		
			
				|  |  |  										else
 | 
	
		
			
				|  |  |  										{ 	
 | 
	
	
		
			
				|  | @@ -180,64 +148,11 @@ int read_device_data(DEVICE_PARAMS *device, char* buf, char* string)
 | 
	
		
			
				|  |  |  										currentModbusParams = current_device->params->gateway_read_modbus_command;
 | 
	
		
			
				|  |  |  										if(current_device == NULL)
 | 
	
		
			
				|  |  |  										{
 | 
	
		
			
				|  |  | -												sprintf(buf + strlen(buf) - 1, ""); 
 | 
	
		
			
				|  |  |  												sprintf(string + strlen(string) - 1, ""); 
 | 
	
		
			
				|  |  |  												return 1;
 | 
	
		
			
				|  |  |  										}
 | 
	
		
			
				|  |  |  								}												
 | 
	
		
			
				|  |  |  						}
 | 
	
		
			
				|  |  | -						// 开关水阀
 | 
	
		
			
				|  |  | -						if(currentModbusParams->functionCode == 0x05)
 | 
	
		
			
				|  |  | -						{
 | 
	
		
			
				|  |  | -							bool success = mmodbus_writeCoil(currentModbusParams->slaveAddress,
 | 
	
		
			
				|  |  | -																							 currentModbusParams->registerByteNum /2,
 | 
	
		
			
				|  |  | -																							 cmd);
 | 
	
		
			
				|  |  | -							if(success)
 | 
	
		
			
				|  |  | -							{
 | 
	
		
			
				|  |  | -								
 | 
	
		
			
				|  |  | -									sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\": success},", 
 | 
	
		
			
				|  |  | -																									current_device->deviceID, currentModbusParams->keyword);
 | 
	
		
			
				|  |  | -							}
 | 
	
		
			
				|  |  | -							else{
 | 
	
		
			
				|  |  | -									sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\": fail},", 
 | 
	
		
			
				|  |  | -																									current_device->deviceID, currentModbusParams->keyword);
 | 
	
		
			
				|  |  | -							}
 | 
	
		
			
				|  |  | -							currentModbusParams = currentModbusParams->nextParams;
 | 
	
		
			
				|  |  | -							if (currentModbusParams == NULL)	
 | 
	
		
			
				|  |  | -							{
 | 
	
		
			
				|  |  | -									current_device = current_device->nextDevice;
 | 
	
		
			
				|  |  | -									currentModbusParams = current_device->params->gateway_read_modbus_command;
 | 
	
		
			
				|  |  | -									if(current_device == NULL)
 | 
	
		
			
				|  |  | -									{
 | 
	
		
			
				|  |  | -											sprintf(buf + strlen(buf) - 1, ""); 
 | 
	
		
			
				|  |  | -											return 1;
 | 
	
		
			
				|  |  | -									}
 | 
	
		
			
				|  |  | -							}
 | 
	
		
			
				|  |  | -						}
 | 
	
		
			
				|  |  | -						// 写单个寄存器
 | 
	
		
			
				|  |  | -						if(currentModbusParams->functionCode == 0x06)
 | 
	
		
			
				|  |  | -						{
 | 
	
		
			
				|  |  | -							bool success = mmodbus_writeHoldingRegisters16i(currentModbusParams->slaveAddress,
 | 
	
		
			
				|  |  | -																		 currentModbusParams->registerAddress,
 | 
	
		
			
				|  |  | -																		 currentModbusParams->registerByteNum /2,
 | 
	
		
			
				|  |  | -																		 data);
 | 
	
		
			
				|  |  | -							if(success)
 | 
	
		
			
				|  |  | -							{
 | 
	
		
			
				|  |  | -								sprintf(buf + strlen(buf), "{\"deviceId\":\"%s\",\"%s\":write success},", 
 | 
	
		
			
				|  |  | -																									current_device->deviceID, currentModbusParams->keyword);
 | 
	
		
			
				|  |  | -							}
 | 
	
		
			
				|  |  | -							currentModbusParams = currentModbusParams->nextParams;
 | 
	
		
			
				|  |  | -							if (currentModbusParams == NULL)	
 | 
	
		
			
				|  |  | -							{
 | 
	
		
			
				|  |  | -									current_device = current_device->nextDevice;
 | 
	
		
			
				|  |  | -									currentModbusParams = current_device->params->gateway_read_modbus_command;
 | 
	
		
			
				|  |  | -									if(current_device == NULL)
 | 
	
		
			
				|  |  | -									{
 | 
	
		
			
				|  |  | -											sprintf(buf + strlen(buf) - 1, ""); 
 | 
	
		
			
				|  |  | -											return 1;
 | 
	
		
			
				|  |  | -									}
 | 
	
		
			
				|  |  | -							}							
 | 
	
		
			
				|  |  | -						}
 | 
	
		
			
				|  |  |  				}
 | 
	
		
			
				|  |  |  				else if (current_device->protocol == DLT645_2007 || current_device->protocol == DLT645_97)
 | 
	
		
			
				|  |  |  				{
 | 
	
	
		
			
				|  | @@ -260,7 +175,7 @@ int read_device_data(DEVICE_PARAMS *device, char* buf, char* string)
 | 
	
		
			
				|  |  |  						}
 | 
	
		
			
				|  |  |  						if (rs != -1)
 | 
	
		
			
				|  |  |  						{
 | 
	
		
			
				|  |  | -							if(compareArrays(read_buf,currentDLT645Params->data,10))// 不相同1,相同0
 | 
	
		
			
				|  |  | +							if(mode == 0)// all
 | 
	
		
			
				|  |  |  								{
 | 
	
		
			
				|  |  |  										if (rs <= 4)
 | 
	
		
			
				|  |  |  										{
 | 
	
	
		
			
				|  | @@ -285,10 +200,11 @@ int read_device_data(DEVICE_PARAMS *device, char* buf, char* string)
 | 
	
		
			
				|  |  |  																										currentDLT645Params->deviceID645[1],currentDLT645Params->deviceID645[2],
 | 
	
		
			
				|  |  |  																										currentDLT645Params->deviceID645[3],currentDLT645Params->deviceID645[4],
 | 
	
		
			
				|  |  |  																										currentDLT645Params->deviceID645[5],currentDLT645Params->Identification,dltValue);
 | 
	
		
			
				|  |  | -										count++;
 | 
	
		
			
				|  |  |  								}
 | 
	
		
			
				|  |  | -								else
 | 
	
		
			
				|  |  | +								else if(mode == 1)//def
 | 
	
		
			
				|  |  |  								{
 | 
	
		
			
				|  |  | +									if(compareArrays(read_buf,currentDLT645Params->data,10))// 不相同1,相同0
 | 
	
		
			
				|  |  | +									{
 | 
	
		
			
				|  |  |  										if (rs <= 4)
 | 
	
		
			
				|  |  |  										{
 | 
	
		
			
				|  |  |  												memcpy(currentDLT645Params->data, read_buf, 4);
 | 
	
	
		
			
				|  | @@ -307,12 +223,14 @@ int read_device_data(DEVICE_PARAMS *device, char* buf, char* string)
 | 
	
		
			
				|  |  |  										dltValue = currentDLT645Params->data[0] << 24 | currentDLT645Params->data[1] << 16|
 | 
	
		
			
				|  |  |  																				currentDLT645Params->data[2] << 8  | currentDLT645Params->data[3];
 | 
	
		
			
				|  |  |  										
 | 
	
		
			
				|  |  | -										sprintf(buf + strlen(buf), "{\"identifier\":\"%s\",\"deviceID645\":\"%02x%02x%02x%02x%02x%02x\",\"identifier645\":%d,\"value\":%X}",
 | 
	
		
			
				|  |  | +										sprintf(string + strlen(string), "{\"identifier\":\"%s\",\"deviceID645\":\"%02x%02x%02x%02x%02x%02x\",\"identifier645\":%d,\"value\":%X}",
 | 
	
		
			
				|  |  |  																								currentDLT645Params->keyword, currentDLT645Params->deviceID645[0],
 | 
	
		
			
				|  |  |  																								currentDLT645Params->deviceID645[1],currentDLT645Params->deviceID645[2],
 | 
	
		
			
				|  |  |  																								currentDLT645Params->deviceID645[3],currentDLT645Params->deviceID645[4],
 | 
	
		
			
				|  |  |  																								currentDLT645Params->deviceID645[5],currentDLT645Params->Identification,dltValue);
 | 
	
		
			
				|  |  | -								}
 | 
	
		
			
				|  |  | +										def = 1;
 | 
	
		
			
				|  |  | +									}
 | 
	
		
			
				|  |  | +							}
 | 
	
		
			
				|  |  |  						}
 | 
	
		
			
				|  |  |  		
 | 
	
		
			
				|  |  |  								currentDLT645Params = currentDLT645Params->nextParams;		
 | 
	
	
		
			
				|  | @@ -322,7 +240,7 @@ int read_device_data(DEVICE_PARAMS *device, char* buf, char* string)
 | 
	
		
			
				|  |  |  										currentDLT645Params = current_device->params->gateway_read_dlt645_command;
 | 
	
		
			
				|  |  |  										if(current_device == NULL)
 | 
	
		
			
				|  |  |  										{
 | 
	
		
			
				|  |  | -												sprintf(buf + strlen(buf) - 1, ""); 
 | 
	
		
			
				|  |  | +												sprintf(string + strlen(string) - 1, ""); 
 | 
	
		
			
				|  |  |  												return 1;
 | 
	
		
			
				|  |  |  										}
 | 
	
		
			
				|  |  |  								}										
 | 
	
	
		
			
				|  | @@ -340,15 +258,21 @@ int read_device_data(DEVICE_PARAMS *device, char* buf, char* string)
 | 
	
		
			
				|  |  |  *	返 回 值: 无
 | 
	
		
			
				|  |  |  *********************************************************************************************************
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | -void send_mqtt(char*buf){
 | 
	
		
			
				|  |  | -		LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"send to mqtt");
 | 
	
		
			
				|  |  | -		GATEWAY_PARAMS *get;
 | 
	
		
			
				|  |  | -		get= get_gateway_config_params();
 | 
	
		
			
				|  |  | -		char *pubJsonString = pvPortMalloc(700);
 | 
	
		
			
				|  |  | -	  sprintf(pubJsonString,"ID: %d {\"DEVICEID\":\"%s\",\"data\":[%s]}",ID, get->deviceId, buf);	// 组成要发送的json语句	
 | 
	
		
			
				|  |  | -		mqtt_publish_data(pubJsonString, QOS0, strlen(pubJsonString), (char*)&get->messageTopic);	
 | 
	
		
			
				|  |  | -		vPortFree(pubJsonString);
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | +//int size;
 | 
	
		
			
				|  |  | +//void send_mqtt(char*buf){
 | 
	
		
			
				|  |  | +//		LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"send to mqtt");
 | 
	
		
			
				|  |  | +//		GATEWAY_PARAMS *get;
 | 
	
		
			
				|  |  | +//		get= get_gateway_config_params();
 | 
	
		
			
				|  |  | +//	size = xPortGetFreeHeapSize();
 | 
	
		
			
				|  |  | +//		char *pubJsonString = pvPortMalloc(5 * 1024);
 | 
	
		
			
				|  |  | +//		if(pubJsonString == NULL)
 | 
	
		
			
				|  |  | +//		{
 | 
	
		
			
				|  |  | +//			LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"mqtt_data malloc fail");
 | 
	
		
			
				|  |  | +//		}
 | 
	
		
			
				|  |  | +//	  sprintf(pubJsonString,"{\"DEVICEID\":\"%s\",\"data\":[%s]}",get->deviceId, buf);	// 组成要发送的json语句	
 | 
	
		
			
				|  |  | +//		mqtt_publish_data(pubJsonString, QOS0, strlen(pubJsonString), (char*)&get->messageTopic);	
 | 
	
		
			
				|  |  | +//		vPortFree(pubJsonString);
 | 
	
		
			
				|  |  | +//}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /*
 | 
	
		
			
				|  |  |  *********************************************************************************************************
 | 
	
	
		
			
				|  | @@ -364,62 +288,83 @@ void write_modbus_data(char* JSON_STRING)
 | 
	
		
			
				|  |  |  		GATEWAY_PARAMS* get;
 | 
	
		
			
				|  |  |  		get = get_gateway_config_params();
 | 
	
		
			
				|  |  |  		DEVICE_PARAMS* current_device = get->device_params;
 | 
	
		
			
				|  |  | -	
 | 
	
		
			
				|  |  | -		parseStringField(JSON_STRING, "\"deviceId\":\"", jsonMsg.deviceId);
 | 
	
		
			
				|  |  | -    jsonMsg.function = parseIntField(JSON_STRING, "\"function\":");
 | 
	
		
			
				|  |  | -		jsonMsg.cmd = parseIntField(JSON_STRING, "\"cmd\":");
 | 
	
		
			
				|  |  | -		jsonMsg.power = parseIntField(JSON_STRING, "\"power\":");
 | 
	
		
			
				|  |  | -		jsonMsg.temp = parseIntField(JSON_STRING, "\"temp\":");
 | 
	
		
			
				|  |  | -		jsonMsg.mode = parseIntField(JSON_STRING, "\"mode\":");
 | 
	
		
			
				|  |  | -		jsonMsg.fan = parseIntField(JSON_STRING, "\"fan\":");
 | 
	
		
			
				|  |  | -		
 | 
	
		
			
				|  |  | -		while(current_device)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		jsonMsg.parameter =parseIntField(JSON_STRING, "\"parameter\":");
 | 
	
		
			
				|  |  | +		parseStringField(JSON_STRING, "\"deviceId\":\"", (char*)&jsonMsg.deviceId);
 | 
	
		
			
				|  |  | +		parseStringField(JSON_STRING, "\"identifier\":\"", (char*)&jsonMsg.identifier);
 | 
	
		
			
				|  |  | +		parseStringField(JSON_STRING, "\"messageId\":\"", (char*)&jsonMsg.messageId);
 | 
	
		
			
				|  |  | +		parseStringField(JSON_STRING, "\"action\":\"", (char*)&jsonMsg.action);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"write to mqtt");
 | 
	
		
			
				|  |  | +	while(current_device)
 | 
	
		
			
				|  |  |  		{
 | 
	
		
			
				|  |  | -				char* device_ID = (char*)current_device->deviceID;
 | 
	
		
			
				|  |  | -				GATEWAY_WRITE_MODBUS_COMMAND *currentModbusParams = current_device->params->gateway_write_modbus_command;
 | 
	
		
			
				|  |  | -				if(!strcmp(device_ID,jsonMsg.deviceId)) //匹配ID
 | 
	
		
			
				|  |  | -				{
 | 
	
		
			
				|  |  | -					portENTER_CRITICAL();
 | 
	
		
			
				|  |  | -					delay_ms(50);
 | 
	
		
			
				|  |  | -					if(jsonMsg.function == 5)
 | 
	
		
			
				|  |  | -					// 开关阀门
 | 
	
		
			
				|  |  | -					{			
 | 
	
		
			
				|  |  | -						mmodbus_writeCoil(jsonMsg.slaveAddress,jsonMsg.registerAddress,jsonMsg.cmd);
 | 
	
		
			
				|  |  | -					}
 | 
	
		
			
				|  |  | -					if(jsonMsg.function == 6)
 | 
	
		
			
				|  |  | +			char* device_ID = (char*)current_device->deviceID;
 | 
	
		
			
				|  |  | +			GATEWAY_WRITE_MODBUS_COMMAND *currentModbusWriteParams = current_device->params->gateway_write_modbus_command;
 | 
	
		
			
				|  |  | +			GATEWAY_READ_MODBUS_COMMAND *currentModbusReadParams = current_device->params->gateway_read_modbus_command;
 | 
	
		
			
				|  |  | +			char* pubJsonString = pvPortMalloc(150);
 | 
	
		
			
				|  |  | +			switch(atoi((char*)&jsonMsg.action))
 | 
	
		
			
				|  |  | +			{
 | 
	
		
			
				|  |  | +				case 0:/* write */
 | 
	
		
			
				|  |  | +					if(!strcmp(device_ID,(char*)&jsonMsg.deviceId))
 | 
	
		
			
				|  |  |  					{
 | 
	
		
			
				|  |  | -					/* 写入寄存器操作 */
 | 
	
		
			
				|  |  | -						if(jsonMsg.power)
 | 
	
		
			
				|  |  | -						{
 | 
	
		
			
				|  |  | -								mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress, 
 | 
	
		
			
				|  |  | -																								currentModbusParams->registerAddress, jsonMsg.power);
 | 
	
		
			
				|  |  | -						}
 | 
	
		
			
				|  |  | -						delay_ms(10);
 | 
	
		
			
				|  |  | -						if(jsonMsg.temp)
 | 
	
		
			
				|  |  | -						{
 | 
	
		
			
				|  |  | -								currentModbusParams = currentModbusParams->nextParams;							
 | 
	
		
			
				|  |  | -								mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress, 
 | 
	
		
			
				|  |  | -																								currentModbusParams->registerAddress, jsonMsg.temp);	
 | 
	
		
			
				|  |  | -						}
 | 
	
		
			
				|  |  | -						delay_ms(10);
 | 
	
		
			
				|  |  | -						if(jsonMsg.mode)
 | 
	
		
			
				|  |  | +						while(currentModbusWriteParams != NULL)
 | 
	
		
			
				|  |  |  						{
 | 
	
		
			
				|  |  | -								currentModbusParams = currentModbusParams->nextParams;
 | 
	
		
			
				|  |  | -								mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress, 
 | 
	
		
			
				|  |  | -																								currentModbusParams->registerAddress, jsonMsg.mode);
 | 
	
		
			
				|  |  | +							if(!strcmp((char*)¤tModbusWriteParams->keyword,(char*)&jsonMsg.identifier)) //匹配ID和属性
 | 
	
		
			
				|  |  | +							{
 | 
	
		
			
				|  |  | +								recv_state = 0;
 | 
	
		
			
				|  |  | +								delay_ms(100);
 | 
	
		
			
				|  |  | +								mmodbus_writeHoldingRegister16i(currentModbusWriteParams->slaveAddress, currentModbusWriteParams->registerAddress, jsonMsg.parameter);
 | 
	
		
			
				|  |  | +//								sprintf(pubJsonString,"{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d}",
 | 
	
		
			
				|  |  | +//													jsonMsg.action,jsonMsg.identifier,jsonMsg.deviceId,jsonMsg.messageId,recv_state);	// 组成要发送的json语句
 | 
	
		
			
				|  |  | +//								mqtt_publish_data(pubJsonString, QOS0, strlen(pubJsonString), (char*)&get->messageTopic);	
 | 
	
		
			
				|  |  | +								delay_ms(100);
 | 
	
		
			
				|  |  | +							}
 | 
	
		
			
				|  |  | +							currentModbusWriteParams = currentModbusWriteParams->nextParams;
 | 
	
		
			
				|  |  |  						}
 | 
	
		
			
				|  |  | -						delay_ms(10);
 | 
	
		
			
				|  |  | -					  if(jsonMsg.fan)
 | 
	
		
			
				|  |  | +					}
 | 
	
		
			
				|  |  | +					break;
 | 
	
		
			
				|  |  | +				case 1:/* read  */
 | 
	
		
			
				|  |  | +					if(!strcmp(device_ID,(char*)&jsonMsg.deviceId)) 
 | 
	
		
			
				|  |  | +					{
 | 
	
		
			
				|  |  | +						while(currentModbusReadParams != NULL)
 | 
	
		
			
				|  |  |  						{
 | 
	
		
			
				|  |  | -								currentModbusParams = currentModbusParams->nextParams;
 | 
	
		
			
				|  |  | -								mmodbus_writeHoldingRegister16i(currentModbusParams->slaveAddress, 
 | 
	
		
			
				|  |  | -																								currentModbusParams->registerAddress, jsonMsg.fan);	
 | 
	
		
			
				|  |  | +							if(!strcmp((char*)¤tModbusReadParams->keyword,(char*)&jsonMsg.identifier)) //匹配ID和属性
 | 
	
		
			
				|  |  | +							{
 | 
	
		
			
				|  |  | +								delay_ms(100);
 | 
	
		
			
				|  |  | +								recv_state = 0;
 | 
	
		
			
				|  |  | +								uint16_t data[currentModbusReadParams->registerByteNum /2]; // modbus寄存器长度
 | 
	
		
			
				|  |  | +								bool success = mmodbus_readHoldingRegisters16i(currentModbusReadParams->slaveAddress,currentModbusReadParams->registerAddress,
 | 
	
		
			
				|  |  | +																																	currentModbusReadParams->registerByteNum /2,data);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +									if (success)
 | 
	
		
			
				|  |  | +									{
 | 
	
		
			
				|  |  | +											recv_state = 1;
 | 
	
		
			
				|  |  | +											uint32_t value;
 | 
	
		
			
				|  |  | +											if (currentModbusReadParams->registerByteNum == 4)
 | 
	
		
			
				|  |  | +											{
 | 
	
		
			
				|  |  | +													value = (uint32_t)data[0] | data[1];
 | 
	
		
			
				|  |  | +											}
 | 
	
		
			
				|  |  | +											else if (currentModbusReadParams->registerByteNum == 2)
 | 
	
		
			
				|  |  | +											{
 | 
	
		
			
				|  |  | +													value = data[0];
 | 
	
		
			
				|  |  | +											}
 | 
	
		
			
				|  |  | +											sprintf(pubJsonString,"{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d,\"parameter\":%d}",
 | 
	
		
			
				|  |  | +														jsonMsg.action,jsonMsg.identifier,jsonMsg.deviceId,jsonMsg.messageId,recv_state,value);	// 组成要发送的json语句
 | 
	
		
			
				|  |  | +											mqtt_publish_data(pubJsonString, QOS0, strlen(pubJsonString), (char*)&get->messageTopic);	
 | 
	
		
			
				|  |  | +											delay_ms(100);
 | 
	
		
			
				|  |  | +									}
 | 
	
		
			
				|  |  | +								}
 | 
	
		
			
				|  |  | +							currentModbusReadParams = currentModbusReadParams->nextParams;
 | 
	
		
			
				|  |  |  						}
 | 
	
		
			
				|  |  | -					}	
 | 
	
		
			
				|  |  | -					delay_ms(10);
 | 
	
		
			
				|  |  | -					portEXIT_CRITICAL();
 | 
	
		
			
				|  |  | -				}
 | 
	
		
			
				|  |  | +					}
 | 
	
		
			
				|  |  | +					break;
 | 
	
		
			
				|  |  | +				case 3:/* reboot */
 | 
	
		
			
				|  |  | +					__set_PRIMASK(1);
 | 
	
		
			
				|  |  | +					NVIC_SystemReset();
 | 
	
		
			
				|  |  | +					break;
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  |  				current_device = current_device->nextDevice;
 | 
	
		
			
				|  |  | +				vPortFree(pubJsonString);
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  // 重定义pow函数
 | 
	
	
		
			
				|  | @@ -431,32 +376,35 @@ uint32_t my_pow(int base, int exponent) {
 | 
	
		
			
				|  |  |      return result;
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -void protocolsModeFunc(GATEWAY_PARAMS* get, char* buf, char* string)
 | 
	
		
			
				|  |  | +void protocolsModeFunc(GATEWAY_PARAMS* get, char* string)
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  |  		if(mqtt_connectFlag)
 | 
	
		
			
				|  |  |  		{
 | 
	
		
			
				|  |  |  				time1 = GetCurrentTime();
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +				sprintf(string,"{\"deviceId\":\"%s\",\"data\":[",get->deviceId);	// 组成要发送的json语句	
 | 
	
		
			
				|  |  |  				if(startFlag && time2  <= time1 - ( 10 * 1000))// 60s进行一次全数据发送
 | 
	
		
			
				|  |  |  				{
 | 
	
		
			
				|  |  | +						mode = 0;//all
 | 
	
		
			
				|  |  |  						LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"protocolsMode:All data");
 | 
	
		
			
				|  |  | -						read_device_data(get->device_params, buf, string);
 | 
	
		
			
				|  |  | -						send_mqtt(buf);				
 | 
	
		
			
				|  |  | +						read_device_data(get->device_params,string);
 | 
	
		
			
				|  |  | +						sprintf(string + strlen(string),"]}");
 | 
	
		
			
				|  |  | +						mqtt_publish_data(string, QOS0, strlen(string), (char*)&get->messageTopic);					
 | 
	
		
			
				|  |  |  						time2 = GetCurrentTime();
 | 
	
		
			
				|  |  |  				}
 | 
	
		
			
				|  |  |  				else
 | 
	
		
			
				|  |  |  				{
 | 
	
		
			
				|  |  | -						read_device_data(get->device_params, buf, string);
 | 
	
		
			
				|  |  | -						if(count > 0)// count检测string是否含有数据
 | 
	
		
			
				|  |  | +						mode = 1;// def
 | 
	
		
			
				|  |  | +						read_device_data(get->device_params, string);
 | 
	
		
			
				|  |  | +						if(def)// 检测string是否含有数据
 | 
	
		
			
				|  |  |  						{
 | 
	
		
			
				|  |  | +							def = 0;
 | 
	
		
			
				|  |  |  							LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"protocolsMode:Different data");
 | 
	
		
			
				|  |  | -							count = 0;
 | 
	
		
			
				|  |  | -							send_mqtt(string);	
 | 
	
		
			
				|  |  | +							sprintf(string + strlen(string),"]}");
 | 
	
		
			
				|  |  | +							mqtt_publish_data(string, QOS0, strlen(string), (char*)&get->messageTopic);			
 | 
	
		
			
				|  |  |  							time2 = GetCurrentTime();		
 | 
	
		
			
				|  |  |  						}					
 | 
	
		
			
				|  |  |  				}
 | 
	
		
			
				|  |  | -				startFlag = 1;	
 | 
	
		
			
				|  |  | -				memset(buf,0,strlen(buf));				
 | 
	
		
			
				|  |  | +				startFlag = 1;				
 | 
	
		
			
				|  |  |  				memset(string,0,strlen(string));	
 | 
	
		
			
				|  |  |  				
 | 
	
		
			
				|  |  |  		}				
 | 
	
	
		
			
				|  | @@ -496,7 +444,6 @@ int transparent_data(DEVICE_PARAMS *device)
 | 
	
		
			
				|  |  |  												mmodbus.rxBuf[i + 3 + 1] = H;
 | 
	
		
			
				|  |  |  											}
 | 
	
		
			
				|  |  |  											gd_com_485_send(mmodbus.rxBuf,mmodbus.rxIndex);
 | 
	
		
			
				|  |  | -											count++;
 | 
	
		
			
				|  |  |  											vTaskDelay(100);
 | 
	
		
			
				|  |  |  										}									
 | 
	
		
			
				|  |  |  								}					
 |