Exemple #1
0
        private static SensorMessage[] DecodeBinaryMessage(byte[] rawData)
        {
            SensorMessage[] readings = null;

            if (rawData.Length % SensorMessage.incomingLength != 0)
            {
                Util.LoggingService.LogInfo($"Binary message recieved over MQTT is invalid. it must consist of sensor readings, " +
                                            $"{SensorMessage.incomingLength} bytes long each. It's length is {rawData.Length}, not divisble by {SensorMessage.incomingLength}"
                                            , Windows.Foundation.Diagnostics.LoggingLevel.Error);
            }
            else
            {
                var numberOfReadings = rawData.Length / SensorMessage.incomingLength;
                readings = new SensorMessage[numberOfReadings];
                //process each reading
                for (var i = 0; i < numberOfReadings; i++)
                {
                    readings[i].value        = BitConverter.ToSingle(rawData, i * SensorMessage.incomingLength);
                    readings[i].duration     = BitConverter.ToInt32(rawData, i * SensorMessage.incomingLength + 4);
                    readings[i].SensorTypeID = rawData[i * SensorMessage.incomingLength + 8];
                }
            }

            return(readings);
        }
Exemple #2
0
        private void MqttMessageRecieved(KeyValuePair <string, MqttMsgPublish> publishEvent)
        {
            //TODO move this GUID parsing code somewhere appropriate!
            Guid clientID;
            var  rawClientID = publishEvent.Key.Replace(":", string.Empty);

            if (Guid.TryParse(rawClientID, out clientID) == false)
            {
                Debug.WriteLine("recieved message with invalid ClientID");
            }
            else
            {
                SensorMessage[] readings;
                var             message = publishEvent.Value;

                if (message.Topic.Contains("reading"))
                {
                    var rawData = message.Message;
                    if (rawData.Length % SensorMessage.incomingLength != 0)
                    {
                        Debug.WriteLine("message recieved over MQTT has incorrect length!");
                    }
                    else
                    {
                        var numberOfReadings = rawData.Length / SensorMessage.incomingLength;
                        readings = new SensorMessage[numberOfReadings];
                        //process each reading
                        for (var i = 0; i < numberOfReadings; i++)
                        {
                            readings[i].value        = BitConverter.ToSingle(rawData, i * SensorMessage.incomingLength);
                            readings[i].duration     = BitConverter.ToInt32(rawData, i * SensorMessage.incomingLength + 4);
                            readings[i].SensorTypeID = rawData[i * SensorMessage.incomingLength + 8];
                        }
                        var toWrite = new KeyValuePair <Guid, SensorMessage[]>(clientID, readings);

                        _datapointsSaver.BufferAndSendReadings(toWrite);
                    }
                }
            }
        }