/// <inheritdoc/> public long GetTimestampFor(LoraMessage payload) { string time = payload.UplinkMessage.ReceivedAt; long epochTimestamp = DateTimeOffset.Parse(time).ToUnixTimeMilliseconds(); return(epochTimestamp); }
/// <inheritdoc/> public bool CanParse(LoraMessage payload) { var validPayload = payload.UplinkMessage.DecodedPayload != null; if (!validPayload) { var devEui = GetDeviceIdFor(payload); _logger.Warning($"'{devEui}': does not contain decoded payload"); } return(validPayload); }
async Task IEventProcessor.ProcessEventsAsync(PartitionContext context, IEnumerable <EventData> messages) { LoraMessage message = new LoraMessage(); _client = new MongoClient(); _database = _client.GetDatabase("lockD"); foreach (EventData eventData in messages) { string data = Encoding.UTF8.GetString(eventData.GetBytes()); //"{\n \"LockId\":\"1C8779C0000000EC\",\n \"Latitude\":\"1.0\",\n \"Longitude\":\"1.0\",\n \"Timestamp\":\"0\"\n}" Console.WriteLine(string.Format("Message received. Partition: '{0}', Data: '{1}'", context.Lease.PartitionId, data)); try { message = JsonConvert.DeserializeObject <LoraMessage>(data); var document = new BsonDocument { { "LockId", message.LockId }, { "Latitude", message.Latitude }, { "Longitude", message.Longitude }, { "Timestamp", DateTime.Now.ToLocalTime() } }; string LastLocation = message.Latitude + "," + message.Longitude; var loraMessages = _database.GetCollection <BsonDocument>("loraMessages"); await loraMessages.InsertOneAsync(document); var bikes = _database.GetCollection <BsonDocument>("bikes"); var filter = Builders <BsonDocument> .Filter.Eq("LockId", message.LockId); var update = Builders <BsonDocument> .Update .Set("LastLocation", LastLocation); var result = await bikes.UpdateOneAsync(filter, update); } catch { Console.WriteLine("Failed To Deserialize"); } } //Call checkpoint every 5 minutes, so that worker can resume processing from 5 minutes back if it restarts. if (this.checkpointStopWatch.Elapsed > TimeSpan.FromMinutes(5)) { await context.CheckpointAsync(); this.checkpointStopWatch.Restart(); } }
/// <inheritdoc/> public Dictionary <string, dynamic> GetDecodedPayloadFor(LoraMessage payload) => payload.UplinkMessage.DecodedPayload;
/// <inheritdoc/> public string GetDeviceIdFor(LoraMessage payload) => payload.EndDeviceIds.DevEui;
/// <inheritdoc/> public string GetApplicationIdFor(LoraMessage payload) => payload.EndDeviceIds.ApplicationIds.ApplicationId;