private static void Parser_MessageReceived(object sender, ProtocolEventArgs e) { ReceiverMessage rcvMessage = null; RFMMessage meshMessage = null; // Message was successfully parsed GPRSMessage receivedGPRSMessage = (e.Message as GPRSMessage); // Because GPRS protocol requires acknowledgments. Send acknowledge message. GPRSMessage ack = GPRSMessage.GenerateGPRSAcknowledgeMessage(receivedGPRSMessage.MessageNumber); ((sender as GPRSParser).Info as IScsServerClient).SendMessage(new ScsRawDataMessage(ack.Bytes)); // Handle received message. It can be mesh protocol message or old protocol receiver message try { meshMessage = MessageFactory.ParseMeshMessage(receivedGPRSMessage.Body) as RFMMessage; } catch { try { meshMessage = null; rcvMessage = MessageFactory.ParseReceiverMessage(receivedGPRSMessage.Body) as ReceiverMessage; } catch { rcvMessage = null; } } if (meshMessage != null) { // Mesh protocol // Print receiver details such as IMEI and Address Console.WriteLine(string.Format("GPRS Message # {0}, IMEI: {1}, Address: {2}", receivedGPRSMessage.MessageNumber, receivedGPRSMessage.MainReceiverID, receivedGPRSMessage.ReceiverID)); // !!! Here you can implement your custom logics according to message type. Console.WriteLine(meshMessage.ToString()); // Use ToString method to print inner fields // Or handle message according its type as follows if (meshMessage.GetType() == typeof(DiagnosticGetShortAddressMessage)) { DiagnosticGetShortAddressMessage diagnosticGetShorAddressMessage = meshMessage as DiagnosticGetShortAddressMessage; Console.WriteLine(string.Format("Tag {0} DiagnosticGetShortAddressMessage", diagnosticGetShorAddressMessage.DeviceID)); } //// Additional messages. Use as shown before: // GetRegisterRequestMessage // GetRegisterResponseMessage // SetRegisterRequestMessage // SetRegisterResponseMessage // GetTimeRequestMessage // GetTimeResponseMessage // TesterCommandMessage // AssociationRequestEventMessage // CheckInEventMessage // DataInMessage // DataRawInMessage // DataOutMessage // DataSensorMessage // ErrorEventMessage //// Sensor messages: // GPIOSensorMessage // AccelerometerSensorMessage // AnalogSensorMessage // BME280SensorMessage // DS18B20SensorMessage // SI7021SensorMessage } else if (rcvMessage != null) { // Old protocol receiver message // Print receiver details such as IMEI and Address Console.WriteLine(string.Format("GPRS Message # {0}, IMEI: {1}, Address: {2}, Inner message type: {3}", receivedGPRSMessage.MessageNumber, receivedGPRSMessage.MainReceiverID, receivedGPRSMessage.ReceiverID, rcvMessage.GetType().ToString())); if (rcvMessage.MessageType == ReceiverMessage.MessageTypes.PowerAlert) { // Receiver power alert. Is sent from the gateway when external power is connected or disconnected // Note: Available only on gateways with the inner battery ReceiverPowerAlertMessage pam = rcvMessage as ReceiverPowerAlertMessage; Console.WriteLine(string.Format("Gateway (IMEI: {0}) External power {1}", receivedGPRSMessage.MainReceiverID, pam.ExternalPowerConnected ? "connected" : "disconnected")); } else if (rcvMessage.MessageType == ReceiverMessage.MessageTypes.ModemVoltage) { // Modem voltage. Actually sends voltage mesured inside GPRS modem module ReceiverModemVoltageMessage mv = rcvMessage as ReceiverModemVoltageMessage; Console.WriteLine(string.Format("Gateway (IMEI: {0}) Modem voltage message: {1}", receivedGPRSMessage.MainReceiverID, mv.ToString())); } else if (rcvMessage.MessageType == ReceiverMessage.MessageTypes.ReceiverMessage) { // Receiver configuration message. ReceiverConfigurationMessage rcm = rcvMessage as ReceiverConfigurationMessage; if (rcvMessage.GetType() == typeof(ReceiverConfigurationVoltageMessage)) { ReceiverConfigurationVoltageMessage cvm = rcvMessage as ReceiverConfigurationVoltageMessage; Console.WriteLine(string.Format("Gateway (IMEI: {0}) Radio module voltage message: {1}", receivedGPRSMessage.MainReceiverID, cvm.ToString())); } else if (rcvMessage.GetType() == typeof(ReceiverConfigurationExternalVoltageMessage)) { ReceiverConfigurationExternalVoltageMessage cevm = rcvMessage as ReceiverConfigurationExternalVoltageMessage; Console.WriteLine(string.Format("Gateway (IMEI: {0}) External voltage message: {1}", receivedGPRSMessage.MainReceiverID, cevm.ToString())); } else if (rcvMessage.GetType() == typeof(ReceiverConfigurationTemperatureMessage)) { ReceiverConfigurationTemperatureMessage ctm = rcvMessage as ReceiverConfigurationTemperatureMessage; Console.WriteLine(string.Format("Gateway (IMEI: {0}) Radio module temperature message: {1}", receivedGPRSMessage.MainReceiverID, ctm.ToString())); } else { Console.WriteLine(string.Format("Gateway (IMEI: {0}) Radio module configuration message: {1}", receivedGPRSMessage.MainReceiverID, rcm.ToString())); } } else { Console.WriteLine(rcvMessage.ToString()); } } }
private static void Parser_MessageReceived(object sender, ProtocolEventArgs e) { // Message was successfully parsed GPRSMessage receivedGPRSMessage = (e.Message as GPRSMessage); // Because GPRS protocol requires acknowledgments. Send acknowledge message. GPRSMessage ack = GPRSMessage.GenerateGPRSAcknowledgeMessage(receivedGPRSMessage.MessageNumber); ((sender as GPRSParser).Info as IScsServerClient).SendMessage(new ScsRawDataMessage(ack.Bytes)); // Handle received message ReceiverMessage rcvMessage = MessageFactory.ParseReceiverMessage(receivedGPRSMessage.Body) as ReceiverMessage; // Print receiver details such as IMEI and Address Console.WriteLine(string.Format("GPRS Message # {0}, IMEI: {1}, Address: {2}, Inner message type: {3}", receivedGPRSMessage.MessageNumber, receivedGPRSMessage.MainReceiverID, receivedGPRSMessage.ReceiverID, rcvMessage.GetType().ToString())); if (rcvMessage.MessageType == ReceiverMessage.MessageTypes.TagDataMessages) { foreach (TagMessage tm in (rcvMessage as TagDataMessage).TagMessages) { // Handle tag messages according its type //if (tm.GetType() == typeof(TagTemperatureMessage)) //{ // // Temperature message // TagTemperatureMessage tempMsg = tm as TagTemperatureMessage; // Console.WriteLine(string.Format("Tag ID {0}, Temperature: {1:0.00}", tempMsg.TagID, tempMsg.Temperature)); //} //else if (tm.GetType() == typeof(TagPeriodicMessage)) //{ // // Periodic message // TagPeriodicMessage periodicMsg = tm as TagPeriodicMessage; // Console.WriteLine(string.Format("Tag ID {0}, Voltage: {1:0.00}", periodicMsg.TagID, periodicMsg.Voltage)); //} //else if (tm.GetType() == typeof(TagCounterMessage)) //{ // // Counter // TagCounterMessage counterMsg = tm as TagCounterMessage; // Console.WriteLine(string.Format("Tag ID {0}, Counter: {1}, Voltage: {2:0.00}", counterMsg.TagID, counterMsg.Counter, counterMsg.Voltage)); //} //else if (tm.GetType() == typeof(TagActivateMessage)) //{ // // Activate message // TagActivateMessage activateMsg = tm as TagActivateMessage; // Console.WriteLine(string.Format("Tag ID {0}, Activator: {1}", activateMsg.TagID, activateMsg.Activator)); //} //else if (tm.GetType() == typeof(TagPressureMessage)) //{ // // Pressure message // TagPressureMessage pressureMsg = tm as TagPressureMessage; // Console.WriteLine(string.Format("Tag ID {0}, Pressure: {1:0.00}, Humidity: {2:0.00}, Temperature: {3:0.00}", pressureMsg.TagID, pressureMsg.Pressure, pressureMsg.Humidity, pressureMsg.Temperature)); //} //else if (tm.GetType() == typeof(TagTemperatureRHMessage)) //{ // // Temperature RH message // TagTemperatureRHMessage temperatureRHMsg = tm as TagTemperatureRHMessage; // Console.WriteLine(string.Format("Tag ID {0}, Temperature: {1:0.00}, Humidity: {2:0.00}, Voltage: {3:0.00}", temperatureRHMsg.TagID, temperatureRHMsg.Temperature, temperatureRHMsg.Humidity, temperatureRHMsg.Voltage)); //} //// Additional messages. Use as shown before //// TagPushButtonGPIO9Message //// TagAngleCalibrationMessage //// TagAgroMessage //// TagMovementMessage //// TagAngleCalibrationRecalibrationMessage //// TagMovementAverageMessage //// TagPushButtonGPIO8Message //// TagDistanceMessage //// TagAnalogMessage //// TagNoMovementMessage //// TagAnalogCounterMessage //// TagAngleMessage //// TagIButtonMessage //// TagMagnetDetectionGPIO8Message //// TagProductionMessage //// TagAveragePulsesPerSecondMessage // Print message details by using ToString() method Console.WriteLine(tm.ToString()); } } else if (rcvMessage.MessageType == ReceiverMessage.MessageTypes.Transient) { byte[] transientData = (rcvMessage as ReceiverTransientMessage).TransientData; if ((transientData != null) && (transientData.Length > 0) && (transientData[0] == 0x96)) { // New protocol message ClientInfo info = infos[((sender as GPRSParser).Info as IScsServerClient).ClientId]; if (info != null && info.new_parser != null) { info.new_parser.AppendBytes(RFPPFHelper.BitStuffEncode(transientData)); } } else { Console.WriteLine(RFPPFHelper.ByteArrayToHexString((rcvMessage as ReceiverTransientMessage).TransientData)); } } else if (rcvMessage.MessageType == ReceiverMessage.MessageTypes.PowerAlert) { // Receiver power alert. Is sent from the gateway when external power is connected or disconnected // Note: Available only on gateways with the inner battery ReceiverPowerAlertMessage pam = rcvMessage as ReceiverPowerAlertMessage; Console.WriteLine(string.Format("Gateway (IMEI: {0}) External power {1}", receivedGPRSMessage.MainReceiverID, pam.ExternalPowerConnected? "connected" : "disconnected")); } else if (rcvMessage.MessageType == ReceiverMessage.MessageTypes.ModemVoltage) { // Modem voltage. Actually sends voltage mesured inside GPRS modem module ReceiverModemVoltageMessage mv = rcvMessage as ReceiverModemVoltageMessage; Console.WriteLine(string.Format("Gateway (IMEI: {0}) Modem voltage message: {1}", receivedGPRSMessage.MainReceiverID, mv.ToString())); } else if (rcvMessage.MessageType == ReceiverMessage.MessageTypes.ReceiverMessage) { // Receiver configuration message. ReceiverConfigurationMessage rcm = rcvMessage as ReceiverConfigurationMessage; if (rcvMessage.GetType() == typeof(ReceiverConfigurationVoltageMessage)) { ReceiverConfigurationVoltageMessage cvm = rcvMessage as ReceiverConfigurationVoltageMessage; Console.WriteLine(string.Format("Gateway (IMEI: {0}) Radio module voltage message: {1}", receivedGPRSMessage.MainReceiverID, cvm.ToString())); } else if (rcvMessage.GetType() == typeof(ReceiverConfigurationExternalVoltageMessage)) { ReceiverConfigurationExternalVoltageMessage cevm = rcvMessage as ReceiverConfigurationExternalVoltageMessage; Console.WriteLine(string.Format("Gateway (IMEI: {0}) External voltage message: {1}", receivedGPRSMessage.MainReceiverID, cevm.ToString())); } else if (rcvMessage.GetType() == typeof(ReceiverConfigurationTemperatureMessage)) { ReceiverConfigurationTemperatureMessage ctm = rcvMessage as ReceiverConfigurationTemperatureMessage; Console.WriteLine(string.Format("Gateway (IMEI: {0}) Radio module temperature message: {1}", receivedGPRSMessage.MainReceiverID, ctm.ToString())); } else { Console.WriteLine(string.Format("Gateway (IMEI: {0}) Radio module configuration message: {1}", receivedGPRSMessage.MainReceiverID, rcm.ToString())); } } else { Console.WriteLine(rcvMessage.ToString()); } // To send SMS via GPRS gateway use following code // Change <Phone Number> to phone number string // Change <Message> to message string // client is an instance of IScsServerClient (in this case - client = ((sender as GPRSParser).Info as IScsServerClient) ) // //GPRSMessage smsMsg = GPRSMessage.GenerateSendSMSMessage(<Phone Number>, <Message>); //client.SendMessage(new ScsRawDataMessage(smsMsg.Bytes)); // To set register on mesh tag use following code // Change <DeviceID> to required tag ID // Change <RegisterNumber> to required register number // Change <RegisterValue> to required register value // client is an instance of IScsServerClient (in this case - client = ((sender as GPRSParser).Info as IScsServerClient) ) //RFPPF.Messages.RFM.Configuration.SetRegisterRequestMessage getRegMsg = RFPPF.Messages.MessageFactory.CreateMessage(typeof(RFPPF.Messages.RFM.Configuration.SetRegisterRequestMessage), new RFPPF.Messages.RFM.Configuration.SetRegisterRequestMessageBuilder() //{ // Version = 1, // DeviceID = <DeviceID>, // MessageNumber = 0, // RegisterNumber = <RegisterNumber>, // RegisterValue = <RegisterValue> //}) as RFPPF.Messages.RFM.Configuration.SetRegisterRequestMessage; //GPRSMessage transientMsg = GPRSMessage.GenerateTransientMessage(getRegMsg.Bytes); //client.SendMessage(new ScsRawDataMessage(transientMsg.Bytes)); }