public async Task Consume(ConsumeContext <CreateTk103Gps> context) { using (var contextFScope = _dbContextScopeFactory.Create()) { _db = contextFScope.DbContexts.Get <SmartFleetObjectContext>(); var box = await _db.Boxes.FirstOrDefaultAsync(x => x.SerialNumber == context.Message.SerialNumber) .ConfigureAwait(false); if (box == null) { box = new Box(); box.Id = Guid.NewGuid(); box.BoxStatus = BoxStatus.Prepared; box.CreationDate = DateTime.UtcNow; box.LastGpsInfoTime = context.Message.TimeStampUtc; box.Icci = String.Empty; box.PhoneNumber = String.Empty; box.Vehicle = null; box.Imei = context.Message.IMEI; box.SerialNumber = context.Message.SerialNumber; try { _db.Boxes.Add(box); } catch (Exception e) { Trace.WriteLine(e); } } if (box.BoxStatus == BoxStatus.WaitInstallation) { box.BoxStatus = BoxStatus.Prepared; } box.LastGpsInfoTime = context.Message.TimeStampUtc; var address = await _geoCodingService.ExecuteQueryAsync(context.Message.Latitude, context.Message.Longitude); Position position = new Position { Box_Id = box.Id, Altitude = 0, Direction = 0, Lat = context.Message.Latitude, Long = context.Message.Longitude, Speed = context.Message.Speed, Id = Guid.NewGuid(), Priority = 0, Satellite = 0, Timestamp = context.Message.TimeStampUtc, Address = address.display_name, MotionStatus = (int)context.Message.Speed > 2? MotionStatus.Moving: MotionStatus.Stopped }; _db.Positions.Add(position); await contextFScope.SaveChangesAsync().ConfigureAwait(false); } }
public async Task Consume(ConsumeContext <CreateTeltonikaGps> context) { try { var item = await Item(context.Message).ConfigureAwait(false); if (item != null) { using (var contextFScope = _dbContextScopeFactory.Create()) { _db = contextFScope.DbContexts.Get <SmartFleetObjectContext>(); var position = new Position(); position.Box_Id = item?.Id; position.Altitude = context.Message.Altitude; position.Direction = context.Message.Direction; position.Lat = context.Message.Lat; position.Long = context.Message.Long; position.Speed = context.Message.Speed; position.Id = Guid.NewGuid(); position.Priority = context.Message.Priority; position.Satellite = context.Message.Satellite; position.Timestamp = context.Message.DateTimeUtc; Semaphore.WaitOne(); var address = await _geoCodingService.ExecuteQueryAsync(context.Message.Lat, context.Message.Long).ConfigureAwait(false); position.Address = address.display_name; Semaphore.Release(); position.MotionStatus = !context.Message.IsStop ? MotionStatus.Moving : MotionStatus.Stopped; _db.Positions.Add(position); await contextFScope.SaveChangesAsync().ConfigureAwait(false); } } } catch (Exception e) { Trace.WriteLine(e); //throw; } // return Task.FromResult(false); }