src/VirtualShield.cpp
/// <summary>
/// Event called when a valid json message was received. 
/// Dispatches to added sensors that match the incoming Type.
/// </summary>
/// <param name="root">The root json object.</param>
/// <param name="shieldEvent">The shield event.</param>

Event called when a valid json message was received.
Dispatches to added sensors that match the incoming Type.

The root json object.
The shield event.
Event called when a valid json message was received . Dispatches to added sensors that match the incoming Type . The root json object . The shield event .
void VirtualShield::onJsonReceived(JsonObject& root, ShieldEvent* shieldEvent) {
	const char* sensorType = static_cast<const char *>(root["Type"]);
	shieldEvent->tag = static_cast<const char*>(root["Tag"]);
	shieldEvent->id = static_cast<int>(root["Pid"]);
	if (shieldEvent->id == 0) {
		shieldEvent->id = static_cast<int>(root["Id"]);
	}
	shieldEvent->resultId = static_cast<long>(root["ResultId"]);
	shieldEvent->result = static_cast<const char *>(root["Result"]);
	shieldEvent->resultHash = hash(shieldEvent->result);
	shieldEvent->action = static_cast<const char *>(root["Action"]);
	shieldEvent->actionHash = hash(shieldEvent->action);
	shieldEvent->value = static_cast<double>(root["Value"]);
	if (sensorType) {
		if (sensorType[0] == SYSTEM_EVENT)
		{
			shieldEvent->cargo = &root;
			bool refresh = false;
			switch (shieldEvent->resultHash)
			{
			case PING_HASH:
				sendPingBack(shieldEvent);
				break;
			case REFRESH_HASH:
				refresh = true;
				break;
			case CONNECT_HASH:
				refresh = true;
				if (onConnect)
				{
					onConnect(shieldEvent);
				}
				break;
			case SUSPEND_HASH:
				if (onSuspend)
				{
					onSuspend(shieldEvent);
				}
				break;
			case RESUME_HASH:
				refresh = true;
				if (onResume)
				{
					onResume(shieldEvent);
				}
				break;
			}
			if (refresh && onRefresh)
			{
				onRefresh(shieldEvent);
			}					  
		} 
		else
		{
			const char sensorTypeChar = sensorType[0];
			for (int i = 0; i < sensorCount; i++)
			{
				if (sensors[i]->sensorType == sensorTypeChar) {
					sensors[i]->onJsonReceived(root, shieldEvent);
					if (shieldEvent->shieldEventType == SensorShieldEventType) {
						SensorEvent* sensorEvent = static_cast<SensorEvent*>(shieldEvent);
						sensorEvent->sensor = sensors[i];
					}
					break;
				}
			}
		}
	}
	if (onEvent)
	{
		onEvent(shieldEvent);
	}
}
void VirtualShield :: onJsonReceived ( JsonObject & root , ShieldEvent * shieldEvent ) { const char * sensorType = static_cast < const char * > ( root [ " " ] ) ; shieldEvent -> tag = static_cast < const char * > ( root [ " " ] ) ; shieldEvent -> id = static_cast < int > ( root [ " " ] ) ; if ( shieldEvent -> id == 0 ) { shieldEvent -> id = static_cast < int > ( root [ " " ] ) ; } shieldEvent -> resultId = static_cast < long > ( root [ " " ] ) ; shieldEvent -> result = static_cast < const char * > ( root [ " " ] ) ; shieldEvent -> resultHash = hash ( shieldEvent -> result ) ; shieldEvent -> action = static_cast < const char * > ( root [ " " ] ) ; shieldEvent -> actionHash = hash ( shieldEvent -> action ) ; shieldEvent -> value = static_cast < double > ( root [ " " ] ) ; if ( sensorType ) { if ( sensorType [ 0 ] == SYSTEM_EVENT ) { shieldEvent -> cargo = & root ; bool refresh = false ; switch ( shieldEvent -> resultHash ) { case PING_HASH : sendPingBack ( shieldEvent ) ; break ; case REFRESH_HASH : refresh = true ; break ; case CONNECT_HASH : refresh = true ; if ( onConnect ) { onConnect ( shieldEvent ) ; } break ; case SUSPEND_HASH : if ( onSuspend ) { onSuspend ( shieldEvent ) ; } break ; case RESUME_HASH : refresh = true ; if ( onResume ) { onResume ( shieldEvent ) ; } break ; } if ( refresh && onRefresh ) { onRefresh ( shieldEvent ) ; } } else { const char sensorTypeChar = sensorType [ 0 ] ; for ( int i = 0 ; i < sensorCount ; i ++ ) { if ( sensors [ i ] -> sensorType == sensorTypeChar ) { sensors [ i ] -> onJsonReceived ( root , shieldEvent ) ; if ( shieldEvent -> shieldEventType == SensorShieldEventType ) { SensorEvent * sensorEvent = static_cast < SensorEvent * > ( shieldEvent ) ; sensorEvent -> sensor = sensors [ i ] ; } break ; } } } } if ( onEvent ) { onEvent ( shieldEvent ) ; } }
<summary>
Event called when a valid json message was received.
<summary > Event called when a valid json message was received .
// special '!' Type which means remote device just connected/reconnected // check each sensor for matching Type