Exemplo n.º 1
0
        // POST /api/Beacon - check user status and user exists
        public async Task<UserDataModel> PostBeaconDataAsync(UserDataModel user, BeaconDataModel beacon)
        {
            string url = AppConstants.ApiBaseUrl + "/Beacon";

            // TODO PostBeaconDataModel implementationa
            PostBeaconDataModel postBeaconDataModel = new PostBeaconDataModel();
            postBeaconDataModel.BeaconUuid = beacon.BeaconUuid;
            postBeaconDataModel.Count = beacon.Count;
            postBeaconDataModel.Distance = beacon.Distance;
            postBeaconDataModel.ElaspedTime = beacon.ElaspedTime;
            postBeaconDataModel.FirstDetectTime = beacon.FirstDetectTime;
            postBeaconDataModel.LastDetectTime = beacon.LastDetectTime;
            postBeaconDataModel.Major = beacon.Major;
            postBeaconDataModel.Minor = beacon.Minor;
            postBeaconDataModel.Rssi = beacon.Rssi;
            postBeaconDataModel.TXPower = beacon.TXPower;
            postBeaconDataModel.KeyTime = beacon.KeyTime;
            postBeaconDataModel.Id = beacon.Id;
            postBeaconDataModel.UserMajor = user.Major;
            postBeaconDataModel.UserMinor = user.Minor;
            postBeaconDataModel.UserUuid = user.UserUuid;

            HttpContent content = new StringContent(Utils.SerializeToJson(postBeaconDataModel), Encoding.UTF8, "application/json");
            var result = await Post(url, content);
            if (result != null)
            {
                return Utils.DeserializeFromJson<UserDataModel>(result);
            }
            return null;
        }
Exemplo n.º 2
0
        private async void DidRangeBeaconsInRegionComplete(object sender, RangeEventArgs rangeEventArgs)
        {
            System.Diagnostics.Debug.WriteLine("DidRangeBeaconsInRegionComplete");
            var now     = DateTime.UtcNow;
            var keyTime = now.ToString("yyyyMMddHH");

            ICollection <Beacon> beacons = rangeEventArgs.Beacons;

            if (beacons != null && beacons.Count > 0)
            {
                var foundBeacons = beacons.ToList();
                foreach (Beacon beacon in foundBeacons)
                {
                    var key = $"{beacon.Id1}{beacon.Id2}{beacon.Id3}.{keyTime}";
                    lock (dataLock)
                    {
                        var result = _connection.Table <BeaconDataModel>().SingleOrDefault(x => x.Id == key);
                        if (result == null)
                        {
                            // New
                            BeaconDataModel data = new BeaconDataModel();
                            data.Id         = key;
                            data.Count      = 0;
                            data.BeaconUuid = beacon.Id1.ToString();
                            data.Major      = beacon.Id2.ToString();
                            data.Minor      = beacon.Id3.ToString();
                            data.Distance   = beacon.Distance;
                            data.Rssi       = beacon.Rssi;
                            //                       data.TXPower = beacon.TxPower;
                            data.ElaspedTime     = new TimeSpan();
                            data.LastDetectTime  = now;
                            data.FirstDetectTime = now;
                            data.KeyTime         = keyTime;
                            data.IsSentToServer  = false;
                            _connection.Insert(data);
                        }
                        else
                        {
                            // Update
                            BeaconDataModel data = result;
                            data.Id = key;
                            data.Count++;
                            data.BeaconUuid = beacon.Id1.ToString();
                            data.Major      = beacon.Id2.ToString();
                            data.Minor      = beacon.Id3.ToString();
                            data.Distance  += (beacon.Distance - data.Distance) / data.Count;
                            data.Rssi       = beacon.Rssi;
                            //                        data.TXPower = beacon.TxPower;
                            data.ElaspedTime   += now - data.LastDetectTime;
                            data.LastDetectTime = now;
                            data.KeyTime        = keyTime;
                            data.IsSentToServer = false;
                            _connection.Update(data);
                            System.Diagnostics.Debug.WriteLine(Utils.SerializeToJson(data));
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        private async void DidRangeBeconsInRegionComplete(object sender, CLRegionBeaconsRangedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("HandleDidDetermineState");
            var now     = DateTime.UtcNow;
            var keyTime = now.ToString("yyyyMMddHH");

            foreach (var beacon in e.Beacons)
            {
                if (beacon.Proximity == CLProximity.Unknown)
                {
                    return;
                }

                var key = $"{beacon.Uuid}{beacon.Major}{beacon.Minor}.{keyTime}";
                lock (dataLock)
                {
                    var result = _connection.Table <BeaconDataModel>().SingleOrDefault(x => x.Id == key);
                    if (result == null)
                    {
                        BeaconDataModel data = new BeaconDataModel();
                        data.Id         = key;
                        data.Count      = 0;
                        data.BeaconUuid = beacon.Uuid.ToString();
                        data.Major      = beacon.Major.ToString();
                        data.Minor      = beacon.Minor.ToString();
                        data.Distance   = beacon.Accuracy;
                        data.Rssi       = (short)beacon.Rssi;
                        //                        data.TXPower = beacon.tr;
                        data.ElaspedTime     = new TimeSpan();
                        data.LastDetectTime  = now;
                        data.FirstDetectTime = now;
                        data.KeyTime         = keyTime;
                        data.IsSentToServer  = false;
                        _connection.Insert(data);
                    }
                    else
                    {
                        BeaconDataModel data = result;
                        data.Id = key;
                        data.Count++;
                        data.BeaconUuid = beacon.Uuid.ToString();
                        data.Major      = beacon.Major.ToString();
                        data.Minor      = beacon.Minor.ToString();
                        data.Distance  += (beacon.Accuracy - data.Distance) / data.Count;
                        data.Rssi       = (short)beacon.Rssi;
                        //                        data.TXPower = beacon.tr;
                        data.ElaspedTime   += now - data.LastDetectTime;
                        data.LastDetectTime = now;
                        data.KeyTime        = keyTime;
                        data.IsSentToServer = false;
                        _connection.Update(data);
                        System.Diagnostics.Debug.WriteLine(Utils.SerializeToJson(data));
                    }
                }
            }
        }
Exemplo n.º 4
0
        private async Task <IActionResult> Get(HttpRequest req, ILogger log)
        {
            // get name from query
            string name = req.Query["name"];

            // get BeaconDataModel from DB
            await Task.CompletedTask;
            var result = new BeaconDataModel();

            return((ActionResult) new OkObjectResult(result));
        }
Exemplo n.º 5
0
        private async void DidRangeBeconsInRegionComplete(object sender, CLRegionBeaconsRangedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("HandleDidDetermineState");

            foreach (var beacon in e.Beacons)
            {
                if (beacon.Proximity == CLProximity.Unknown)
                {
                    return;
                }

                var key    = beacon.Uuid.ToString() + beacon.Major.ToString() + beacon.Minor.ToString();
                var result = _connection.Table <BeaconDataModel>().SingleOrDefault(x => x.Id == key);
                if (result == null)
                {
                    BeaconDataModel data = new BeaconDataModel();
                    data.Id         = key;
                    data.Count      = 0;
                    data.BeaconUuid = beacon.Uuid.ToString();
                    data.Major      = beacon.Major.ToString();
                    data.Minor      = beacon.Minor.ToString();
                    data.Distance   = beacon.Accuracy;
                    data.Rssi       = (short)beacon.Rssi;
                    //                        data.TXPower = beacon.tr;
                    data.ElaspedTime    = new TimeSpan();
                    data.LastDetectTime = DateTime.Now;
                    _connection.Insert(data);
                    if (data.ElaspedTime > TimeSpan.FromMinutes(AppConstants.ELAPSED_TIME_OF_TRANSMISSION_START))
                    {
                        await _httpDataService.PostBeaconDataAsync(_userData, data);
                    }
                }
                else
                {
                    BeaconDataModel data = result;
                    data.Id = key;
                    data.Count++;
                    data.BeaconUuid = beacon.Uuid.ToString();
                    data.Major      = beacon.Major.ToString();
                    data.Minor      = beacon.Minor.ToString();
                    data.Distance  += (beacon.Accuracy - data.Distance) / data.Count;
                    data.Rssi       = (short)beacon.Rssi;
                    //                        data.TXPower = beacon.tr;
                    data.ElaspedTime    = new TimeSpan();
                    data.LastDetectTime = DateTime.Now;
                    _connection.Update(data);
                    System.Diagnostics.Debug.WriteLine(Utils.SerializeToJson(data));
                }
            }
        }
Exemplo n.º 6
0
        private void DidRangeBeaconsInRegionComplete(object sender, RangeEventArgs rangeEventArgs)
        {
            System.Diagnostics.Debug.WriteLine("DidRangeBeaconsInRegionComplete");

            ICollection <Beacon> beacons = rangeEventArgs.Beacons;

            if (beacons != null && beacons.Count > 0)
            {
                var foundBeacons = beacons.ToList();
                foreach (Beacon beacon in foundBeacons)
                {
                    var             key  = beacon.Id1.ToString() + beacon.Id2.ToString() + beacon.Id3.ToString();
                    BeaconDataModel data = new BeaconDataModel();
                    if (!_dictionaryOfBeaconData.ContainsKey(key))
                    {
                        data.UUID           = beacon.Id1.ToString();
                        data.Major          = beacon.Id2.ToString();
                        data.Minor          = beacon.Id3.ToString();
                        data.Distance       = beacon.Distance;
                        data.Rssi           = beacon.Rssi;
                        data.TXPower        = beacon.TxPower;
                        data.ElaspedTime    = new TimeSpan();
                        data.LastDetectTime = DateTime.Now;
                    }
                    else
                    {
                        data                = _dictionaryOfBeaconData.GetValueOrDefault(key);
                        data.UUID           = beacon.Id1.ToString();
                        data.Major          = beacon.Id2.ToString();
                        data.Minor          = beacon.Id3.ToString();
                        data.Distance       = beacon.Distance;
                        data.Rssi           = beacon.Rssi;
                        data.TXPower        = beacon.TxPower;
                        data.ElaspedTime   += DateTime.Now - data.LastDetectTime;
                        data.LastDetectTime = DateTime.Now;
                        _dictionaryOfBeaconData.Remove(key);
                    }

                    _dictionaryOfBeaconData.Add(key, data);
                    System.Diagnostics.Debug.WriteLine(key.ToString());
                    System.Diagnostics.Debug.WriteLine(data.Distance);
                    System.Diagnostics.Debug.WriteLine(data.ElaspedTime.TotalSeconds);
                }
            }
        }
Exemplo n.º 7
0
        // POST /api/Beacon - check user status and user exists
        public async Task <bool> PostBeaconDataAsync(UserDataModel user, BeaconDataModel beacon)
        {
            string url = AppConstants.ApiBaseUrl + "/Beacon";

            // TODO PostBeaconDataModel implementationa
            PostBeaconDataModel postBeaconDataModel = new PostBeaconDataModel();

            postBeaconDataModel.BeaconUuid      = beacon.BeaconUuid;
            postBeaconDataModel.Count           = beacon.Count;
            postBeaconDataModel.Distance        = beacon.Distance;
            postBeaconDataModel.ElaspedTime     = beacon.ElaspedTime;
            postBeaconDataModel.FirstDetectTime = beacon.FirstDetectTime;
            postBeaconDataModel.LastDetectTime  = beacon.LastDetectTime;
            postBeaconDataModel.Major           = beacon.Major;
            postBeaconDataModel.Minor           = beacon.Minor;
            postBeaconDataModel.Rssi            = beacon.Rssi;
            postBeaconDataModel.TXPower         = beacon.TXPower;
            postBeaconDataModel.KeyTime         = beacon.KeyTime;
            postBeaconDataModel.Id        = beacon.Id;
            postBeaconDataModel.UserMajor = user.Major;
            postBeaconDataModel.UserMinor = user.Minor;
            postBeaconDataModel.UserUuid  = user.UserUuid;

            HttpContent content = new StringContent(Utils.SerializeToJson(postBeaconDataModel), Encoding.UTF8, "application/json");

            try
            {
                HttpResponseMessage result = await httpClient.PostAsync(url, content);

                await result.Content.ReadAsStringAsync();

                if (result.IsSuccessStatusCode)
                {
                    return(true);
                }
            }
            catch { }
            return(false);
        }