public TelldusTemperatureDatapoint Create(string input) { var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, }; var message = JsonSerializer.Deserialize <TelldusMessageRaw>(input, options); var temperature = message.Data.First(item => item.Name.Equals("temp", StringComparison.OrdinalIgnoreCase)); var humidity = message.Data.First(item => item.Name.Equals("humidity", StringComparison.OrdinalIgnoreCase)); var epochStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc); var localTimestamp = epochStart.AddSeconds(temperature.LastUpdated).ToLocalTime(); var datapoint = new TelldusTemperatureDatapoint(); datapoint.Name = message.Name; datapoint.Battery = int.Parse(message.Battery); datapoint.Timestamp = localTimestamp.Ticks; datapoint.Datestamp = localTimestamp; datapoint.Temperature = double.Parse(temperature.Value.Replace(",", "."), NumberStyles.Any, CultureInfo.InvariantCulture); datapoint.Humidity = double.Parse(humidity.Value.Replace(",", "."), NumberStyles.Any, CultureInfo.InvariantCulture); return(datapoint); }
public async Task Save(TelldusTemperatureDatapoint entity) { var mongoclient = new MongoClient(new MongoUrl("mongodb://hemmaserver2")); var database = mongoclient.GetDatabase("Telldus"); var datas = database.GetCollection <TelldusTemperatureDatapoint>("LoggedData"); var filter = "{ Timestamp: " + entity.Timestamp + "}"; var existingItem = await datas.FindAsync <TelldusTemperatureDatapoint>(filter); if (existingItem.FirstOrDefault() == null) { datas.InsertOne(entity); } }