Ejemplo n.º 1
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;
	}
}
Ejemplo n.º 2
0
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 = "";
		
	}
}
Ejemplo n.º 3
0
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;
		
	}
}