/// <summary>
        /// Save all beacons in a log table
        /// </summary>
        /// <param name="beacon"></param>
        private static void SaveInLogTable(BeaconData beacon)
        {
            var partitionKey = DateTime.Now.ToString("ddMMyyyy");

            var item = new BeaconDataStorage {
                PartitionKey = partitionKey, RowKey = beacon.Identifier, Timestamp = DateTimeOffset.Now, Data = Newtonsoft.Json.JsonConvert.SerializeObject(beacon)
            };

            var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("AzureWebJobsStorage"));

            var tableClient = storageAccount.CreateCloudTableClient();

            var table = tableClient.GetTableReference("BeaconLogTable");

            var operation = TableOperation.InsertOrReplace(item);

            table.Execute(operation);
        }
        private static bool IsAlreadyDiscovered(BeaconData beacon)
        {
            var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("AzureWebJobsStorage"));

            var tableClient = storageAccount.CreateCloudTableClient();

            var table = tableClient.GetTableReference("BeaconLogTable");

            var query = new TableQuery <LocationStorage>()
                        .Where(
                TableQuery.CombineFilters(
                    TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, DateTimeOffset.Now.ToString("ddMMyyyy")),
                    TableOperators.And,
                    TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, beacon.Identifier)));

            var result = table.ExecuteQuery(query);

            return(result.Any(x => DateTimeOffset.UtcNow.Subtract(x.Timestamp).Minutes < 5));
        }
        private static string GetBeaconIdentifier(BeaconData beacon)
        {
            var result = new StringBuilder();

            if (beacon.BeaconType.Equals("ibeacon", StringComparison.InvariantCultureIgnoreCase))
            {
                result.Append("1!");

                result.Append(beacon.IbeaconData.Uuid.ToString("N").ToLower())
                .Append(beacon.IbeaconData.MajorHex.ToLower())
                .Append(beacon.IbeaconData.MinorHex.ToLower());
            }
            else if (beacon.BeaconType.Equals("eddystone_uid", StringComparison.InvariantCultureIgnoreCase) && beacon.EddyStoneData != null)
            {
                result.Append("3!")
                .Append(beacon.EddyStoneData.NamespaceId.Replace("0x", ""))
                .Append(beacon.EddyStoneData.InstanceId.Replace("0x", ""));
            }
            return(result.ToString());
        }
Пример #4
0
        private static async Task <LocationInfo> GetInfo(TraceWriter log, BeaconData beacon, ExecutionContext context)
        {
            LogHelper.Log(log, $"GetBeaconIdentifier called: {beacon.Identifier}");

            if (string.IsNullOrEmpty(beacon.Identifier))
            {
                return(null);
            }
            var path = $"{context.FunctionAppDirectory}\\beacons.csv";

            using (TextReader reader = File.OpenText(path))
            {
                var configuration = new Configuration {
                    HasHeaderRecord = true,
                };

                var doubleConverterOptions = new TypeConverterOptions {
                    CultureInfo = CultureInfo.InvariantCulture
                };

                var csv = new CsvReader(reader, configuration);
                csv.Configuration.TypeConverterOptionsCache.AddOptions <double>(doubleConverterOptions);

                await csv.ReadAsync(); // skip first header line

                while (await csv.ReadAsync())
                {
                    var name = csv.GetField <string>(0);
                    if (!string.IsNullOrEmpty(name) && name.Equals(beacon.Identifier, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(new LocationInfo
                        {
                            Name = csv.GetField <string>(1),
                            Coordinates = new GeoCoordinates(csv.GetField <double>(4), csv.GetField <double>(5)),
                            Beacon = beacon
                        });
                    }
                }
            }
            return(null);
        }
Пример #5
0
        public static async Task <LocationInfo> Run(
            [QueueTrigger("beacon-received-queue", Connection = "AzureWebJobsStorage")] BeaconData beacon,
            TraceWriter log,
            ExecutionContext context)
        {
            var info = await GetInfo(log, beacon, context);

            if (info != null)
            {
                info.Address = await GetAddress(info.Coordinates);

                info.Date = DateTime.UtcNow;

                LogHelper.Log(log, $"ProcessBeaconFunction called: beacon: {Newtonsoft.Json.JsonConvert.SerializeObject(info)}");

                return(info);
            }
            else
            {
                LogHelper.Log(log, "ProcessBeaconFunction called: unkown beacon");
            }
            return(null);
        }