Exemplo n.º 1
0
        public static async Task InsertEvent([EventHubTrigger("waterlyeventhub", Connection = "str")] EventData eventData, ILogger log)
        {
            log.LogInformation("C# event hub trigger function processed events.");
            string messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);

            log.LogInformation($"Event message is : {messageBody}");
            EventItem dataJson = JsonConvert.DeserializeObject <EventItem>(messageBody);

            dataJson.id = Guid.NewGuid().ToString();
            await Resources.events_container.CreateItemAsync(dataJson);


            //now updates the devices table (the last water read)
            long   last_water_read       = dataJson.water_read;
            long   last_update_timestamp = dataJson.timestamp;
            string device_id             = dataJson.device_id;

            log.LogInformation("C# event hub trigger function update rows last_water_read.");
            var option = new FeedOptions {
                EnableCrossPartitionQuery = true
            };

            DeviceItem deviceItem = Resources.docClient.CreateDocumentQuery <DeviceItem>(
                UriFactory.CreateDocumentCollectionUri("waterly_db", "waterly_devices"), option)
                                    .Where(deviceItem => deviceItem.id.Equals(device_id))
                                    .AsEnumerable()
                                    .First();

            // Correcting out of orderness
            if (deviceItem.last_update_timestamp < last_update_timestamp)
            {
                deviceItem.last_water_read       = last_water_read;
                deviceItem.last_update_timestamp = last_update_timestamp;
            }


            ResourceResponse <Document> response = await Resources.docClient.ReplaceDocumentAsync(
                UriFactory.CreateDocumentUri("waterly_db", "waterly_devices", deviceItem.id),
                deviceItem);

            var updated = response.Resource;

            //execute detector
            await Detector.executeDetectionLogic(dataJson, deviceItem.userId, log);
        }
Exemplo n.º 2
0
        public static async Task createAlert(EventItem eventItem, string type, string userId, string evidence)
        {
            logger.LogInformation("Creating alert...");

            var    sqlQueryText = $"SELECT * FROM c WHERE c.id = '{eventItem.device_id}'";
            string device_name  = null;

            QueryDefinition           queryDefinition        = new QueryDefinition(sqlQueryText);
            FeedIterator <DeviceItem> queryResultSetIterator = Resources.devices_container.GetItemQueryIterator <DeviceItem>(queryDefinition);
            FeedResponse <DeviceItem> currentResultSet;

            while (queryResultSetIterator.HasMoreResults)
            {
                currentResultSet = await queryResultSetIterator.ReadNextAsync();

                DeviceItem device = currentResultSet.FirstOrDefault <DeviceItem>();
                device_name = device.name;
            }

            AlertItem alert = new AlertItem
            {
                id          = Guid.NewGuid().ToString(),
                device_id   = eventItem.device_id,
                created_at  = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(),
                type        = type,
                user_id     = userId,
                status      = true,
                evidence    = evidence,
                device_name = device_name,
                message     = "Please contact with your local service center"
            };

            // Create an item in the container representing alert.
            ItemResponse <AlertItem> alertResponse = await Resources.alert_container.CreateItemAsync <AlertItem>(alert);

            // Note that after creating the item, we can access the body of the item with the Resource property off the ItemResponse.
            Console.WriteLine("Created alert in database with id: {0}\n", alertResponse.Resource.id);

            EmailSender.sendMailNewAlert(alert, userId);
        }