public void ReadMessageData() { string filePath = Path.Combine(Application.streamingAssetsPath, "AppMessages.json"); if (File.Exists(filePath)) { string dataAsJson = File.ReadAllText(filePath); jsonMessages = JsonUtility.FromJson <JsonMessages>(dataAsJson); } else { Debug.LogError("Cannot load messsages."); } }
/// <summary> /// Obtiene el mensaje desde el Json. /// </summary> /// <param name="codigo"></param> /// <param name="parametros"></param> /// <returns></returns> public static DTMessage ObtenerJsonMensajes(int codigo, params string[] parametros) { JsonMessages ListMensaje = new JsonMessages(); DTMessage objmensaje = new DTMessage(); string rutaArchivo = ObtenerArchivo(ARCHIVO_MENSAJES); var json = File.ReadAllText(rutaArchivo); ListMensaje = JsonConvert.DeserializeObject <JsonMessages>(json); objmensaje = ListMensaje.Messages.Where(m => m.Code == codigo).FirstOrDefault(); if (parametros != null) { objmensaje.Text = string.Format(objmensaje.Text, parametros); } return(objmensaje); }
// Process a received message private void processReceivedDatagram(ref JsonMessages.AmqpResponse jsonMsg) { unsafe { try { fixed (byte* pBuffer = &(jsonMsg.Data[0])) { MessageCodec_dll.CSDecodeResult decodeResult; byte* pNext = pBuffer; byte** ppNext = &pNext; updateDataAfterTx((UInt32)jsonMsg.Data.Length); while (pNext < pBuffer + jsonMsg.Data.Length) { decodeResult = messageCodec.decodeUlMsgType(pNext, (UInt32)jsonMsg.Data.Length); switch (decodeResult) { case (MessageCodec_dll.CSDecodeResult.DECODE_RESULT_INIT_IND_UL_MSG): { UInt32 wakeUpCode; UInt32 revisionLevel; decodeResult = messageCodec.decodeUlMsgInitInd(ppNext, (UInt32)jsonMsg.Data.Length, &wakeUpCode, &revisionLevel); testOnConsoleTrace(String.Format("Message decode: InitIndUlMsg, wake-up code {0}, revisionLevel {1}.", wakeUpCode.ToString(), revisionLevel.ToString())); if (revisionLevel != messageCodec.revisionLevel()) { testOnConsoleTrace(String.Format ("!!!!! tedI is at revision level {0} whereas this codec is revision {1}!!!!!", (int) revisionLevel, (int) messageCodec.revisionLevel())); } /* Device has (re)started so sync with it's settings */ resetSettings(); if (!parent.testMode) { sendIntervalsGetReqDlMsg(); } // Clear stored data parent._clearSensorDataCallback(); parent._clearTrafficDataCallback(); powerLastReportTime = 0; UpdateInitIndCallback(wakeUpCodeString(wakeUpCode)); updateControlText(this, commsFormTitleSyncing); } break; case (MessageCodec_dll.CSDecodeResult.DECODE_RESULT_INTERVALS_GET_CNF_UL_MSG): { UInt32 reportingIntervalMinutes; UInt32 heartbeatSeconds; decodeResult = messageCodec.decodeUlMsgIntervalsGetCnf(ppNext, (UInt32)jsonMsg.Data.Length, &reportingIntervalMinutes, &heartbeatSeconds); testOnConsoleTrace(String.Format("Message decode: IntervalsGetCnfUlMsg, reporting {0} min(s), reading {1} secs.", reportingIntervalMinutes.ToString(), heartbeatSeconds.ToString())); gotReportingIntervalMinutes = true; updateControlText(textBoxReportingIntervalMinutes, reportingIntervalMinutes.ToString()); UpdateReportingIntervalCallback(reportingIntervalMinutes); gotHeartbeatSeconds = true; updateControlText(textBoxHeartbeatSeconds, heartbeatSeconds.ToString()); UpdateHeartbeatCallback(heartbeatSeconds); } break; case (MessageCodec_dll.CSDecodeResult.DECODE_RESULT_REPORTING_INTERVAL_SET_CNF_UL_MSG): { UInt32 reportingIntervalMinutes; decodeResult = messageCodec.decodeUlMsgReportingIntervalSetCnf(ppNext, (UInt32)jsonMsg.Data.Length, &reportingIntervalMinutes); testOnConsoleTrace(String.Format("Message decode: ReportingIntervalSetCnfUlMsg, reporting {0} min(s).", reportingIntervalMinutes.ToString())); gotReportingIntervalMinutes = true; updateControlText(textBoxReportingIntervalMinutes, reportingIntervalMinutes.ToString()); UpdateReportingIntervalCallback(reportingIntervalMinutes); } break; case (MessageCodec_dll.CSDecodeResult.DECODE_RESULT_HEARTBEAT_SET_CNF_UL_MSG): { UInt32 heartbeatSeconds; decodeResult = messageCodec.decodeUlMsgHeartbeatSetCnf(ppNext, (UInt32)jsonMsg.Data.Length, &heartbeatSeconds); testOnConsoleTrace(String.Format("Message decode: HeartbeatSetCnfUlMsg, reading {0} secs.", heartbeatSeconds.ToString())); gotHeartbeatSeconds = true; updateControlText(textBoxHeartbeatSeconds, heartbeatSeconds.ToString()); UpdateHeartbeatCallback(heartbeatSeconds); } break; case (MessageCodec_dll.CSDecodeResult.DECODE_RESULT_POLL_IND_UL_MSG): { decodeResult = messageCodec.decodeUlMsgPollInd(ppNext, (UInt32)jsonMsg.Data.Length); testOnConsoleTrace(String.Format("Message decode: PollIndUlMsg.")); UpdatePollIndCallback(); } break; case (MessageCodec_dll.CSDecodeResult.DECODE_RESULT_SENSORS_REPORT_GET_CNF_UL_MSG): case (MessageCodec_dll.CSDecodeResult.DECODE_RESULT_SENSORS_REPORT_IND_UL_MSG): { UInt32 time; Boolean gpsPositionPresent; Int32 gpsPositionLatitude; Int32 gpsPositionLongitude; Int32 gpsPositionElevation; Int32 gpsPositionSpeed; Boolean lclPositionPresent; UInt32 lclPositionOrientation; UInt32 lclPositionHugsThisPeriod; UInt32 lclPositionSlapsThisPeriod; UInt32 lclPositionDropsThisPeriod; UInt32 lclPositionNudgesThisPeriod; Boolean soundLevelPresent; UInt32 soundLevel; Boolean luminosityPresent; UInt32 luminosity; Boolean temperaturePresent; Int32 temperature; Boolean rssiPresent; UInt32 rssi; Boolean powerStatePresent; UInt32 powerStateChargeState; UInt32 powerStateBatteryMV; UInt32 powerStateEnergyUWH; // Add a new data point to the collection DataRow newRow = parent.sensorDataTable.NewRow(); decodeResult = messageCodec.decodeUlMsgSensorsReportxxx(ppNext, (UInt32)jsonMsg.Data.Length, &time, &gpsPositionPresent, &gpsPositionLatitude, &gpsPositionLongitude, &gpsPositionElevation, &gpsPositionSpeed, &lclPositionPresent, &lclPositionOrientation, &lclPositionHugsThisPeriod, &lclPositionSlapsThisPeriod, &lclPositionDropsThisPeriod, &lclPositionNudgesThisPeriod, &soundLevelPresent, &soundLevel, &luminosityPresent, &luminosity, &temperaturePresent, &temperature, &rssiPresent, &rssi, &powerStatePresent, &powerStateChargeState, &powerStateBatteryMV, &powerStateEnergyUWH); testOnConsoleTrace(String.Format("Message decode: SensorReportxxxMsg, contents...")); UInt32 hh = time / 3600; UInt32 mm = (time % 3600) / 60; UInt32 ss = (time % 3600) % 60; newRow[parent.dataTableRowTimeName] = new DateTime(1970, 1, 1).ToLocalTime().AddSeconds(time); testOnConsoleTrace(String.Format("Time: {0} seconds, UTC {1:D2}:{2:D2}:{3:D2}, date/time {4}.", time, hh, mm, ss, newRow[parent.dataTableRowTimeName])); if (gpsPositionPresent) { newRow[parent.dataTableRowLatitudeName] = Convert.ToDouble(gpsPositionLatitude) / 60000; newRow[parent.dataTableRowLongitudeName] = Convert.ToDouble(gpsPositionLongitude) / 60000; newRow[parent.dataTableRowElevationName] = gpsPositionElevation; newRow[parent.dataTableRowSpeedName] = gpsPositionSpeed; testOnConsoleTrace(String.Format("GPS: lat/long {0:N4}/{1:N4} degrees, elev {2} metres, speed {3} km/h.", newRow[parent.dataTableRowLatitudeName], newRow[parent.dataTableRowLongitudeName], newRow[parent.dataTableRowElevationName], newRow[parent.dataTableRowSpeedName])); } if (lclPositionPresent) { newRow[parent.dataTableRowOrientationName] = lclPositionOrientation; newRow[parent.dataTableRowHugsName] = lclPositionHugsThisPeriod; newRow[parent.dataTableRowSlapsName] = lclPositionSlapsThisPeriod; newRow[parent.dataTableRowDropsName] = lclPositionDropsThisPeriod; newRow[parent.dataTableRowNudgesName] = lclPositionNudgesThisPeriod; testOnConsoleTrace(String.Format("LCL: orientation {0}, hugs {1}, slaps {2}, drops {3}, nudges {4}.", newRow[parent.dataTableRowOrientationName], newRow[parent.dataTableRowHugsName], newRow[parent.dataTableRowSlapsName], newRow[parent.dataTableRowDropsName], newRow[parent.dataTableRowNudgesName])); } if (soundLevelPresent) { newRow[parent.dataTableRowSoundLevelName] = soundLevel; testOnConsoleTrace(String.Format("Sound: {0}.", newRow[parent.dataTableRowSoundLevelName])); } if (luminosityPresent) { newRow[parent.dataTableRowLuminosityName] = luminosity; testOnConsoleTrace(String.Format("Luminosity: {0}.", newRow[parent.dataTableRowLuminosityName])); } if (temperaturePresent) { newRow[parent.dataTableRowTemperatureName] = temperature; testOnConsoleTrace(String.Format("Temperature: {0} C.", newRow[parent.dataTableRowTemperatureName])); } if (rssiPresent) { newRow[parent.dataTableRowRssiName] = rssi; rssidBm = inventRssi(rssi); newRow[parent.dataTableRowRssidBmName] = rssidBm; testOnConsoleTrace(String.Format("RSSI: {0}, {1} dBm.", newRow[parent.dataTableRowRssiName], newRow[parent.dataTableRowRssidBmName])); } if (powerStatePresent) { newRow[parent.dataTableRowChargeStateName] = powerStateChargeState; newRow[parent.dataTableRowBatterymVName] = powerStateBatteryMV; newRow[parent.dataTableRowEnergyuWhName] = powerStateEnergyUWH; if (powerLastReportTime > 0) { newRow[parent.dataTableRowEnergymWPerHourName] = (double)powerStateEnergyUWH * 3600 / (double)(time - powerLastReportTime) / 1000; } powerLastReportTime = time; powerUWHTotal += powerStateEnergyUWH; newRow[parent.dataTableRowEnergyTotalmWhName] = powerUWHTotal / 1000; testOnConsoleTrace(String.Format("Pwr State: charging {0}, voltage {1} mV, energy {2} uWh, mW in an hour {3}, total since reset {4} uWh.", newRow[parent.dataTableRowChargeStateName], newRow[parent.dataTableRowBatterymVName], newRow[parent.dataTableRowEnergyuWhName], newRow[parent.dataTableRowEnergymWPerHourName], newRow[parent.dataTableRowEnergyTotalmWhName])); } // Update the data table UpdateSensorDataCallback(newRow); } break; case (MessageCodec_dll.CSDecodeResult.DECODE_RESULT_TRAFFIC_REPORT_GET_CNF_UL_MSG): { UInt32 numDatagramsSent; UInt32 numBytesSent; UInt32 numDatagramsReceived; UInt32 numBytesReceived; // Add a new data point to the collection DataRow newRow = parent.trafficDataTable.NewRow(); decodeResult = messageCodec.decodeUlMsgTrafficReportGetCnf(ppNext, (UInt32)jsonMsg.Data.Length, &numDatagramsSent, &numBytesSent, &numDatagramsReceived, &numBytesReceived); newRow[parent.dataTableRowUplinkDatagramsName] = numDatagramsSent; newRow[parent.dataTableRowUplinkBytesName] = numBytesSent; newRow[parent.dataTableRowDownlinkDatagramsName] = numDatagramsReceived; newRow[parent.dataTableRowDownlinkBytesName] = numBytesReceived; testOnConsoleTrace(String.Format("Message decode: TrafficReportGetCnfUlMsg, reporting {0} bytes sent ({1} datagrams), {2} bytes received ({3} datagrams).", newRow[parent.dataTableRowUplinkBytesName].ToString(), newRow[parent.dataTableRowUplinkDatagramsName].ToString(), newRow[parent.dataTableRowDownlinkBytesName].ToString(), newRow[parent.dataTableRowDownlinkDatagramsName].ToString())); // Update the data table UpdateTrafficDataCallback(newRow); } break; case (MessageCodec_dll.CSDecodeResult.DECODE_RESULT_TRAFFIC_REPORT_IND_UL_MSG): { UInt32 numDatagramsSent; UInt32 numBytesSent; UInt32 numDatagramsReceived; UInt32 numBytesReceived; // Add a new data point to the collection DataRow newRow = parent.trafficDataTable.NewRow(); decodeResult = messageCodec.decodeUlMsgTrafficReportInd(ppNext, (UInt32)jsonMsg.Data.Length, &numDatagramsSent, &numBytesSent, &numDatagramsReceived, &numBytesReceived); newRow[parent.dataTableRowUplinkDatagramsName] = numDatagramsSent; newRow[parent.dataTableRowUplinkBytesName] = numBytesSent; newRow[parent.dataTableRowDownlinkDatagramsName] = numDatagramsReceived; newRow[parent.dataTableRowDownlinkBytesName] = numBytesReceived; testOnConsoleTrace(String.Format("Message decode: TrafficReportIndUlMsg, reporting {0} bytes sent ({1} datagrams), {2} bytes received ({3} datagrams).", newRow[parent.dataTableRowUplinkBytesName].ToString(), newRow[parent.dataTableRowUplinkDatagramsName].ToString(), newRow[parent.dataTableRowDownlinkBytesName].ToString(), newRow[parent.dataTableRowDownlinkDatagramsName].ToString())); // Update the data table UpdateTrafficDataCallback(newRow); } break; case (MessageCodec_dll.CSDecodeResult.DECODE_RESULT_DEBUG_IND_UL_MSG): { UInt32 sizeOfString; byte[] debugString = new byte[messageCodec.maxDebugStringSize()]; fixed (byte * pDebugString = &debugString[0]) { decodeResult = messageCodec.decodeUlMsgDebugInd(ppNext, (UInt32)jsonMsg.Data.Length, &sizeOfString, pDebugString); testOnConsoleTrace(String.Format("Message decode: DebugIndUlMsg \"{0}\".", System.Text.Encoding.Default.GetString(debugString, 0, (int) sizeOfString))); } } break; case (MessageCodec_dll.CSDecodeResult.DECODE_RESULT_FAILURE): { (*ppNext)++; // Move pNext on. testOnConsoleTrace("Message decode: failure."); } break; case (MessageCodec_dll.CSDecodeResult.DECODE_RESULT_INPUT_TOO_SHORT): { (*ppNext)++; // Move pNext on. testOnConsoleTrace("Message decode: input too short."); } break; case (MessageCodec_dll.CSDecodeResult.DECODE_RESULT_OUTPUT_TOO_SHORT): { (*ppNext)++; // Move pNext on. testOnConsoleTrace("Message decode: output too short."); } break; case (MessageCodec_dll.CSDecodeResult.DECODE_RESULT_BAD_MSG_FORMAT): { (*ppNext)++; // Move pNext on. testOnConsoleTrace("Message decode: bad message format."); } break; case (MessageCodec_dll.CSDecodeResult.DECODE_RESULT_UNKNOWN_MSG_ID): { (*ppNext)++; // Move pNext on. testOnConsoleTrace("Message decode: unknown message."); } break; default: { (*ppNext)++; // Move pNext on. testOnConsoleTrace("Message decode: unknown decode result."); } break; } } } } catch (IndexOutOfRangeException) { testOnConsoleTrace("JSON contained no data"); } } // Check if we've got all the stuff from the device if (gotReportingIntervalMinutes && gotHeartbeatSeconds) { updateControlText (this, commsFormTitleConnected); } }