Exemplo n.º 1
0
 public async Task InsertData(InformationBuffer _informationBuffer)
 {
     try
     {
         await BlobCache.UserAccount.InsertObject("buffer", _informationBuffer);
     }
     catch { }
 }
        private async Task ClearBufferAsync(AccessRepo accessRepo, InformationBuffer _informationBuffer)
        {
            try
            {
                await accessRepo.DumpCache("buffer");

                _informationBuffer.Buffer.RemoveRange(0, _informationBuffer.Buffer.Count() - 1);
                await accessRepo.InsertData(_informationBuffer);
            }
            catch {; }
        }
Exemplo n.º 3
0
        public async Task <InformationBuffer> ReadData()
        {
            try
            {
                var _informationBuffer = new InformationBuffer();

                _informationBuffer = await BlobCache.UserAccount.GetObject <InformationBuffer>("buffer");

                return(_informationBuffer);
            }
            catch
            {
                return(null);
            }
        }
        private async void ClearBuffer()
        {
            var _informationBuffer = new InformationBuffer();

            _informationBuffer.Buffer = new List <DeviceInformation>();

            await AccessRepo.Semaphore.WaitAsync();

            try
            {
                _informationBuffer = await BlobCache.UserAccount.GetObject <InformationBuffer>("buffer");
            }
            catch
            {
                _informationBuffer.Buffer.Clear();
            }

            _informationBuffer.Buffer.Clear();
            await accessRepo.DumpCache("buffer");
        }
        private async Task SendData(string message = "")
        {
            DeviceClient _deviceClient = null;

            try
            {
                SendDataFinished = false;

                var _informationBuffer = new InformationBuffer();
                _informationBuffer.Buffer = new List <DeviceInformation>();

                await AccessRepo.Semaphore.WaitAsync();

                try
                {
                    _informationBuffer = await BlobCache.UserAccount.GetObject <InformationBuffer>("buffer");
                }
                catch
                {
                    _informationBuffer.Buffer.Clear();
                }

                if (_informationBuffer.Buffer.Count() > 0)
                {
                    try
                    {
                        var bufferJson = Serialize(JsonConvert.SerializeObject(_informationBuffer));

                        var msg = new Message(bufferJson);
                        msg.Properties.Add(AppConstants.DEVICE_APP_PROP_MAP_SYSORI, AppConstants.DEVICE_DEFAULT_NAME);
                        msg.Properties.Add(AppConstants.DEVICE_APP_PROP_MAP_CODINT, AppConstants.DEVICE_DEFAULT_MSG_ID);

                        _deviceClient = DeviceClient.CreateFromConnectionString(ConnectionString, TransportType.Http1);
                        int count = _informationBuffer.Buffer.Count();

                        await _deviceClient.SendEventAsync(msg);

                        if (_informationBuffer.Buffer.Count() == count)
                        {
                            _informationBuffer.Buffer.Clear();
                            await accessRepo.DumpCache("buffer");

                            if (SendMsgFail > 0)
                            {
                                SendMsgFail = 0;
                                NotSentMsg  = "Aguard. Envio: " + SendMsgFail;
                            }
                        }
                        else
                        {
                            _informationBuffer.Buffer.RemoveRange(0, count);
                            await accessRepo.InsertData(_informationBuffer);

                            SendMsgFail = _informationBuffer.Buffer.Count();
                            NotSentMsg  = "Aguard. Envio: " + SendMsgFail;
                        }
                    }
                    catch
                    {
                        WakeUp();

                        SendMsgFail = _informationBuffer.Buffer.Count();
                        NotSentMsg  = "Aguard. Envio: " + SendMsgFail;
                    }
                }
            }
            finally
            {
                if (_deviceClient != null)
                {
                    await _deviceClient.CloseAsync();

                    _deviceClient.Dispose();
                }

                AccessRepo.Semaphore.Release();

                SendDataFinished = true;

                GC.Collect();
            }
        }
        public async Task AcquireData(bool endTrip = false)
        {
            DataAquisitionFinished = false;

            try
            {
                await AccessRepo.Semaphore.WaitAsync();

                try
                {
                    await accessRepo.SaveState(AquisitionStart);
                }
                finally
                {
                    AccessRepo.Semaphore.Release();
                }

                await AccessRepo.Semaphore.WaitAsync();
            }
            catch
            {
                WakeUp();
                return;
            }

            var _informationBuffer = new InformationBuffer();

            try
            {
                _informationBuffer = await BlobCache.UserAccount.GetObject <InformationBuffer>("buffer");
            }
            catch
            {
                _informationBuffer.Buffer.Clear();
            }

            try
            {
                var latLongPosition = await GetPosition();

                //Caso não consiga obter localização envia 500 como código de erro.
                var Latitude  = 0d;
                var Longitude = 0d;
                var speed     = 0d;
                var status    = 500;

                if (latLongPosition != null)
                {
                    Latitude  = latLongPosition.Latitude;
                    Longitude = latLongPosition.Longitude;
                    speed     = latLongPosition.Speed;
                    status    = 200; //200 é o código de conseguiu obter a localização.
                    UpdateScrnInfo(latLongPosition);
                }

                var info = new DeviceInformation()
                {
                    EndTrip        = endTrip,
                    AquisitionDate = DateTime.Now,
                    Latitude       = Latitude,
                    Longitude      = Longitude,
                    Speed          = speed,
                    Battery        = CrossBattery.Current.RemainingChargePercent,
                    DeviceId       = SimpleIoc.Default.GetInstance <IDeviceId>().GetDeviceId(),
                    Status         = status
                };

                _informationBuffer.Buffer.Add(info);

                await accessRepo.InsertData(_informationBuffer);
            }
            finally
            {
                AccessRepo.Semaphore.Release();
            }

            DataAquisitionFinished = true;
        }