Example #1
0
int main(int argc, char *argv[]) {
  char *cmd = NULL;
  char **cmdTokens = NULL;

  while (1) {
    cmd = Readline();
    if (!cmd || !*cmd) continue;
    
    // Add to readline history
    add_history(cmd);
    
    // Parse and execute
    int tokencnt = Tokenize(cmd, &cmdTokens);
    ASTTree astTree = ParseCommand(cmdTokens, tokencnt);
    EvalCommand(astTree);
    WaitChildren();
    
    FreeCmd(cmd);
    FreeCmdTokens(cmdTokens);
  }

  return 0;
}
/**
 * Flow interface thread :-
 * 1. Registers a message callback for any message received.
 * 2. Handles all the commands coming from controller thread.
 */
void FlowInterfaceThread(FlowThread thread, void *taskParameters)
{
	Controller *me = taskParameters;

	FlowInterfaceCmd *cmd = NULL;

	_receiveMsgQueue = &me->receiveMsgQueue;
	RegisterCallbackForReceivedMsg(MessageReceivedCallBack);

	for (;;)
	{
		cmd = FlowQueue_DequeueWaitFor(me->sendMsgQueue,QUEUE_WAITING_TIME);
		if (cmd != NULL)
		{
			switch (cmd->cmdType)
			{
				case FlowInterfaceCmd_SendMessageToUser:
				{
					SendMessage(me->userId, cmd->details,SendMessage_ToUser);
					FreeCmd(cmd);
					break;
				}
				case FlowInterfaceCmd_SendMessageToActuator:
				{
					if (me->actuatorConfig.actuatorId)
					{
						SendMessage(me->actuatorConfig.actuatorId, cmd->details,SendMessage_ToDevice);
						ControllerLog(ControllerLogLevel_Debug, DEBUG_PREFIX "Sending relay command to actuator" );
					}
					else
					{
						ControllerLog(ControllerLogLevel_Debug, DEBUG_PREFIX "Actuator id is NOT known to us" );
					}
					FreeCmd(cmd);
					break;
				}
				case FlowInterfaceCmd_SendMessageToSensor:
				{
					if (me->sensorConfig.sensorId)
					{
						SendMessage(me->sensorConfig.sensorId, cmd->details,SendMessage_ToDevice);
						ControllerLog(ControllerLogLevel_Debug, DEBUG_PREFIX "Sending update settings to sensor" );
					}
					else
					{
						ControllerLog(ControllerLogLevel_Debug, DEBUG_PREFIX "Sensor id is NOT known to us" );
					}
					FreeCmd(cmd);
					break;
				}
				case FlowInterfaceCmd_GetSetting:
				{
					char *data = NULL;

					if (GetSetting(CONTROLLER_CONFIG_NAME, &data))
					{
						if (!PostControllerEventSetting(&me->receiveMsgQueue, data))
						{
							Flow_MemFree((void **)&data);
							ControllerLog(ControllerLogLevel_Error, ERROR_PREFIX "Posting settings event to controller thread failed");
						}
					}
					else
					{
						if (!PostControllerEventSetting(&me->receiveMsgQueue, NULL))
						{
							ControllerLog(ControllerLogLevel_Error, ERROR_PREFIX "Posting settings event to controller thread failed");
						}
					}
					FreeCmd(cmd);
					break;
				}
				case FlowInterfaceCmd_SetSetting:
				{
					if (!SetSetting(CONTROLLER_CONFIG_NAME, cmd->details))
					{
						ControllerLog(ControllerLogLevel_Error, ERROR_PREFIX "Error in saving settings" );
					}
					FreeCmd(cmd);
					break;
				}
				default:
				{
					ControllerLog(ControllerLogLevel_Debug, DEBUG_PREFIX "Received unknown command" );
					FreeCmd(cmd);
					break;
				}
			}
		}
	}
}