void DeviceBitTcpClient::sendUserSwitchState()
{
	UserSwitchNode *currentSwitch = switchHead;                    
	boolean bFirstNode = true;
	//{\"id\":\"m\",\"name\":\"m\",\"value\":\"0\",\"status\":\"ok\"}
	String stateStr = "[";
	while(currentSwitch != NULL)
	{
		if(!bFirstNode)stateStr+=",";
		stateStr+="{\"id\":\"";
		stateStr+=currentSwitch->userSwitchId;
		stateStr+="\",\"name\":\"";
		stateStr+=currentSwitch->userSwitchId;
		stateStr+="\",\"value\":\"";
		stateStr+=currentSwitch->userSwitchState;
		stateStr+="\",\"status\":\"ok\"}";
		//Serial.println(functionName);
		//Serial.println(currentSwitch->userSwitchId);
		//Serial.println(currentSwitch->userSwitchState);     
		bFirstNode = false;
		currentSwitch = currentSwitch->next;
	}
	stateStr+="]";
	//Serial.println(stateStr);
	
	if(!bFirstNode)
	{
		setRevCtrlMsg("true","ok");
		char * msg = strToChar(stateStr);
		setRevCtrlData(msg);
		//msg = NULL;
	}
	stateStr = "";
	//stateStr = NULL;
	if(strlen(_revCtrlData)>0)
	{
		int len=strlen(_revCtrlResult)+strlen(_revCtrlMsg)+strlen(_revCtrlData)+71;
		commandString=(char *)malloc(len);	
		snprintf(commandString, len, "{\"method\":\"response\",\"result\":{\"successful\":%s,\"message\":\"%s\",\"data\":%s}}&^!", _revCtrlResult, _revCtrlMsg,_revCtrlData);
		
		_clientRevCtrl.print(commandString);
		free(commandString);
		commandString = NULL;
		free(_revCtrlData);
		_revCtrlData = NULL;
		setRevCtrlMsg("true","ok");
		setRevCtrlData("");
		_clientStr = "";
	}
}
void LeweiTcpClient::setupDefaultValue()
{
	//checkFreeMem();
	head = NULL;
	switchHead = NULL;
	
	byte _mac[] = {0x74, 0x69, 0x69, 0x2D, 0x30, 0x31};
	_postInterval = 30000;//server setting is 60000
	_starttime = millis();
	
	int len=strlen(_gatewayNo)+32+51;//api-key length:32
	
	aliveString=(char *)malloc(len);	
	snprintf(aliveString, len, "{\"method\":\"update\",\"gatewayNo\":\"%s\",\"userkey\":\"%s\"}&^!", _gatewayNo, _userKey);
	

	setRevCtrlMsg("false","NotBind");
	

	setRevCtrlData("");
	_bEasyMode = false;
	startCommPost = 75;
	currentPos = 0;
	

}
void LeweiTcpClient::setupDefaultValue()
{
	head = NULL;
	//tcpServer = IPAddress(42,121,128,216); //tcp.lewei50.com's ip
	//uploadServer = IPAddress(121,197,10,140);// "open.lewei50.com";
	//char tcpServer[] = "tcp.lewei50.com";
	//char uploadServer[] = "open.lewei50.com";
	
	byte _mac[] = {0x74, 0x69, 0x69, 0x2D, 0x30, 0x31};
	_postInterval = 10000;//server setting is 60000
	_starttime = millis();
	String tcpServerStr = "tcp.lewei50.com";
	tcpServerStr.toCharArray(tcpServer,16);
	tcpServerStr = NULL;
	String uploadServerStr = "open.lewei50.com";
	uploadServerStr.toCharArray(uploadServer,17);
	uploadServerStr = NULL;
	bIsConnecting = false;
	
	int len=strlen(_gatewayNo)+strlen(_userKey)+51;
	aliveString=(char *)malloc(len);	
	snprintf(aliveString, len, "{\"method\":\"update\",\"gatewayNo\":\"%s\",\"userkey\":\"%s\"}&^!", _gatewayNo, _userKey);
	

	setRevCtrlMsg("false","function not binded");

}
Esempio n. 4
0
void LeweiTcpClient::setupDefaultValue()
{
	head = NULL;
	
	_postInterval = 10000;//server setting is 60000
	_starttime = millis();
	
	int len=strlen(_gatewayNo)+strlen(_userKey)+51;
	aliveString=(char *)malloc(len);	
	snprintf(aliveString, len, "{\"method\":\"update\",\"gatewayNo\":\"%s\",\"userkey\":\"%s\"}&^!", _gatewayNo, _userKey);
	
	setRevCtrlMsg("false","function not bind");

}
void LeweiTcpClient::getResponse()
{
	if (_clientRevCtrl.available())
	{
		currentPos ++;
		char c = _clientRevCtrl.read();
		if(currentPos >startCommPost)
		{
			//Serial.print(c);checkFreeMem();
		_clientStr += c;
		}
	}
	else if(_clientStr.length()>0)
	{
		currentPos = 0;
		//Serial.print("FrmSrv:");
		//Serial.println(_clientStr);
		//if(_clientStr.indexOf("&^!")<0)
		//{
			//Serial.println("no end!");
			//return;
		//}
		//checkFreeMem();
		
		String functionName = getParaValueStr(_clientStr,"f");
			char* p1 = getParaValue(_clientStr,"p1");
			char* p2 = getParaValue(_clientStr,"p2");
			char* p3 = getParaValue(_clientStr,"p3");
			char* p4 = getParaValue(_clientStr,"p4");
			char* p5 = getParaValue(_clientStr,"p5");
			_clientStr = "";
			
		if(!functionName.equals(""))//here comes user defined command
		{
  		//Serial.print("f:");
			//Serial.println(functionName);
			if(functionName.equals("getAllSensors"))//try to return the switch status list to server
			{
				sendUserSwitchState();
				free(p1);free(p2);free(p3);free(p4);free(p5);
				p1=p2=p3=p4=p5=NULL;
				free(commandString);
				commandString = NULL;
				
				return;
				
			}
			else if(functionName.equals("updateSensor"))
			{
				//Serial.println("updateSensor");
				
				UserSwitchNode *currentSwitch = switchHead;  
				while(currentSwitch != NULL)
				{ 
					if(String(p1).equals(String(currentSwitch->userSwitchId)))
					{
						
						if(String(p2).equals("0"))
						{
								currentSwitch->userSwitchState = false;
								//execute(currentSwitch->userSwitchOnFunctionAddr);
						}
						else
						{
								currentSwitch->userSwitchState = true;
								//execute(currentSwitch->userSwitchOffFunctionAddr);
						}
						
						execute(currentSwitch->userSwitchFunctionAddr,p2);
						updateUserSwitchState(p1,p2);
						return;
					}
					currentSwitch = currentSwitch->next;
				}
			}
			UserFunctionNode * current = head;
			while(current!=NULL)
			{
//				Serial.println(functionName);
//				Serial.println(current->userFunctionName);
				if(functionName.equals(current->userFunctionName))
				{
		//checkFreeMem();
					//setRevCtrlMsg("true","execute function");
					
					if(p5!=NULL)
					{
						//Serial.println(5);
						execute(current->userFunctionAddr5,p1,p2,p3,p4,p5);
					}
					else 
						if(p4!=NULL)
					{
						//Serial.println(4);
						execute(current->userFunctionAddr4,p1,p2,p3,p4);
					}
					else 
						if(p3!=NULL)
					{
						//Serial.println(3);
						execute(current->userFunctionAddr3,p1,p2,p3);
					}
					else 
						if(p2!=NULL)
					{
						//Serial.println(2);
						execute(current->userFunctionAddr2,p1,p2);
					}
					else 
					if(p1!=NULL)
					{
						//Serial.println(1);
						execute(current->userFunctionAddr1,p1);
					}
					else
					{
						//Serial.println(0);
						execute(current->userFunctionAddr0);
					}
					//execute(current->userFunctionAddr);
					break;
				}
				current = current->next;
			}
			
		}
			free(p1);free(p2);free(p3);free(p4);free(p5);
			p1=p2=p3=p4=p5=NULL;
			//free(p1);free(p2);
			//p1=p2=NULL;

			functionName = "";
			if(strlen(_revCtrlData)>0)
			{
				int len=strlen(_revCtrlResult)+strlen(_revCtrlMsg)+strlen(_revCtrlData)+71;
				commandString=(char *)malloc(len);	
				snprintf(commandString, len, "{\"method\":\"response\",\"result\":{\"successful\":%s,\"message\":\"%s\",\"data\":%s}}&^!", _revCtrlResult, _revCtrlMsg,_revCtrlData);
				
			}
			else
			{
				int len=strlen(_revCtrlResult)+strlen(_revCtrlMsg)+63;
				commandString=(char *)malloc(len);	
				snprintf(commandString, len, "{\"method\":\"response\",\"result\":{\"successful\":%s,\"message\":\"%s\"}}&^!", _revCtrlResult, _revCtrlMsg);
			}
			//Serial.println(commandString);
			_clientRevCtrl.print(commandString);
			free(commandString);
			commandString = NULL;
		setRevCtrlMsg("false","NotBind");
		setRevCtrlData("");
		_clientStr = "";
		
	}
}
Esempio n. 6
0
void LeweiTcpClient::getResponse()
{
  while (Serial.available()>0) {
    // get the new byte:
    char inChar = (char)Serial.read(); 
    // add it to the inputString:
    _clientStr += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
  }
  if(_clientStr.indexOf('&^!')>0)
  {
	  if(_clientStr.length()>0)
		{
			String functionName = getParaValueStr(_clientStr,"f");
			if(!functionName.equals(""))//here comes user defined command
			{
				char* p1 = getParaValue(_clientStr,"p1");
				char* p2 = getParaValue(_clientStr,"p2");
				//char* p3 = getParaValue(_clientStr,"p3");
				//char* p4 = getParaValue(_clientStr,"p4");
				//char* p5 = getParaValue(_clientStr,"p5");
				UserFunctionNode * current = head;
				while(current!=NULL)
				{
					if(functionName.equals(current->userFunctionName))
					{
						/*
						if(p5!=NULL)
						{
							//Serial.println(5);
							execute(current->userFunctionAddr5,p1,p2,p3,p4,p5);
						}
						else 
							if(p4!=NULL)
						{
							//Serial.println(4);
							execute(current->userFunctionAddr4,p1,p2,p3,p4);
						}
						else 
							if(p3!=NULL)
						{
							//Serial.println(3);
							execute(current->userFunctionAddr3,p1,p2,p3);
						}
						else */
							if(p2!=NULL)
						{
							//Serial.println(2);
							execute(current->userFunctionAddr2,p1,p2);
						}
						else if(p1!=NULL)
						{
							//Serial.println(1);
							execute(current->userFunctionAddr1,p1);
						}
						else
						{
							//Serial.println(0);
							execute(current->userFunctionAddr0);
						}
						//execute(current->userFunctionAddr);
						break;
					}
					current = current->next;
				}
				//free(p1);free(p2);free(p3);free(p4);free(p5);
				//p1=p2=p3=p4=p5=NULL;
				free(p1);free(p2);
				p1=p2=NULL;
			}
	
			functionName = NULL;
		
			int len=strlen(_revCtrlResult)+strlen(_revCtrlMsg)+63;
			commandString=(char *)malloc(len);	
			snprintf(commandString, len, "{\"method\":\"response\",\"result\":{\"successful\":%s,\"message\":\"%s\"}}&^!", _revCtrlResult, _revCtrlMsg);
			
			Serial.print(commandString);
			free(commandString);
			commandString = NULL;
			
			setRevCtrlMsg("false","function not set");
			
			
		}
		_clientStr = "";
		_clientStr = NULL;
	}
}
void LeweiTcpClient::getResponse()
{
	if (_clientRevCtrl.available())
	{
		char c = _clientRevCtrl.read();
		_clientStr += c;
	}
	else if(_clientStr.length()>0)
	{
		//char* retMessage = "function not binded";
		Serial.print("message from server:");
		Serial.println(_clientStr);
		checkFreeMem();
		String functionName = getParaValueStr(_clientStr,"f");
			char* p1 = getParaValue(_clientStr,"p1");
			char* p2 = getParaValue(_clientStr,"p2");
			char* p3 = getParaValue(_clientStr,"p3");
			char* p4 = getParaValue(_clientStr,"p4");
			char* p5 = getParaValue(_clientStr,"p5");
			_clientStr = NULL;
		checkFreeMem();
		if(!functionName.equals(""))//here comes user defined command
		{
  		Serial.println("functionName is:");
			Serial.println(functionName);
			//countUserFunction();
			UserFunctionNode * current = head;
			while(current!=NULL)
			{
//				Serial.println(functionName);
//				Serial.println(current->userFunctionName);
				if(functionName.equals(current->userFunctionName))
				{
					setRevCtrlMsg("true","execute function");
					if(p5!=NULL)
					{
						//Serial.println(5);
						execute(current->userFunctionAddr5,p1,p2,p3,p4,p5);
					}
					else 
						if(p4!=NULL)
					{
						//Serial.println(4);
						execute(current->userFunctionAddr4,p1,p2,p3,p4);
					}
					else 
						if(p3!=NULL)
					{
						//Serial.println(3);
						execute(current->userFunctionAddr3,p1,p2,p3);
					}
					else 
						if(p2!=NULL)
					{
						//Serial.println(2);
						execute(current->userFunctionAddr2,p1,p2);
					}
					else if(p1!=NULL)
					{
						//Serial.println(1);
						execute(current->userFunctionAddr1,p1);
					}
					else
					{
						//Serial.println(0);
						execute(current->userFunctionAddr0);
					}
					//execute(current->userFunctionAddr);
					break;
				}
				current = current->next;
			}
		}
  		Serial.println("4");
			free(p1);free(p2);free(p3);free(p4);free(p5);
			p1=p2=p3=p4=p5=NULL;
			//free(p1);free(p2);
			//p1=p2=NULL;

			functionName = NULL;
		
			int len=strlen(_revCtrlResult)+strlen(_revCtrlMsg)+63;
			//Serial.println(len);
			commandString=(char *)malloc(len);	
			snprintf(commandString, len, "{\"method\":\"response\",\"result\":{\"successful\":%s,\"message\":\"%s\"}}&^!", _revCtrlResult, _revCtrlMsg);
			//Serial.println(commandString);
			_clientRevCtrl.print(commandString);
			free(commandString);
			commandString = NULL;
	/*
		_clientStr="";
		_clientStr+=;
		_clientStr+="";
		if(_revCtrlMsg!="")_clientStr+=_revCtrlMsg;
		else _clientStr+=retMessage;
		_clientStr+="\"}}&^!";
						Serial.println(10);
		char* c = strToChar(_clientStr);
						Serial.println(11);
		if(c)
		{
			_clientRevCtrl.print(c);
			Serial.print(c);
		}
			free(c);
			c = NULL;
						Serial.println(12);
						*/
		/*
		char* c = (char*)malloc(_clientStr.length()+1);
		_clientStr.toCharArray(c,_clientStr.length()+1);
		_clientRevCtrl.print(c);
		Serial.print(c);
		free(c);
		c = NULL;
		*/
		
		Serial.println("response to server.");
		
		setRevCtrlMsg("false","function not binded");
		_clientStr = NULL;
		
	}
}