Пример #1
0
        public static Measurement Convert(ControlMessage message)
        {
            var builder = new MeasurementBuilder();
            var token   = JToken.Parse(message.Data);

            if (token["latitude"] != null && token["longitude"] != null)
            {
                var lat = token["latitude"].ToObject <double>();
                var lon = token["longitude"].ToObject <double>();

                builder.WithCoordinates(lon, lat);
            }

            if (token["telegram"] == null)
            {
                throw new InvalidOperationException("Unable to parse telegram without a telegram.");
            }

            builder.WithTimestamp(token["timestamp"].ToObject <DateTime>());
            builder.WithSensorId(message.SensorId);

            var base64 = token["telegram"].ToString();
            var bytes  = System.Convert.FromBase64String(base64);

            var tmp = Encoding.UTF8.GetString(bytes);

            tmp = tmp.Replace(Needle, "");
            var data = JToken.Parse(tmp);

            builder.WithDataPoint("pressure", "Pa", data["p"].ToObject <decimal>(), 0.1);
            builder.WithDataPoint("temperature", "C", data["t"].ToObject <decimal>(), 0.1);
            builder.WithDataPoint("rh", "%", data["rh"].ToObject <decimal>(), 0.00001);

            return(builder.Build());
        }
        private MeasurementBuilder createMeasurementBuilder(Telegram telegram)
        {
            var builder = new MeasurementBuilder();

            if (telegram.Latitude.HasValue && telegram.Longitude.HasValue)
            {
                builder.WithCoordinates(telegram.Longitude.Value, telegram.Latitude.Value);
            }

            builder.WithTimestamp(DateTime.UtcNow);

            return(builder);
        }