public async Task QueueShelf(Shelf productData)
        {
            try
            {
                _logger.LogInformation($"Number of products on shelf: {productData.Products.Length}");

                if (shelfQueue.Count() > 120)
                {
                    while (shelfQueue.Count() > 120)
                    {
                        await shelfQueue.DequeueAsync(new CancellationToken()); //throw away result
                    }
                }

                // exit early if we dont have a deserialized element and a serial number
                if (productData != null)
                {
                    _logger.LogInformation("Shelf Data Generator queueing in progress...");
                    shelfQueue.QueueShelf(productData);
                }
            }
            catch (AggregateException ex)
            {
                _logger.LogError($"Error processing message: {ex.Flatten()}");
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error processing message: {ex}");
            }
        }
Esempio n. 2
0
        public void QueueShelf(Shelf shelf)
        {
            if (shelf == null)
            {
                throw new ArgumentNullException(nameof(shelf));
            }

            _shelves.Enqueue(shelf);
            _signal.Release();
        }
Esempio n. 3
0
        /*The PipeMessage method is what will connect to the IoT Edge Hub and will queue the shelf data
         * for processing.
         *
         * It will be registered as an inputMessageHandler with the IoT Edge Hub in the ExecuteAsync method
         * that follows.
         */
        public async Task <MessageResponse> PipeMessage(Message message, object userContext)
        {
            var moduleClient = userContext as ModuleClient;

            if (moduleClient == null)
            {
                throw new InvalidOperationException("UserContext doesn't contain " + "expected values");
            }

            /*This section receives the data from the IoT Edge Hub and converts it to a Shelf object
             * for processing through signalR to the WebApp module.
             */
            byte[] messageBytes  = message.GetBytes();
            string messageString = Encoding.UTF8.GetString(messageBytes);

            if (!string.IsNullOrEmpty(messageString))
            {
                try{
                    Console.WriteLine($"Receiving Message: {messageString.TrimStart('"').TrimEnd('"').Replace('\\', ' ')}");
                    Shelf productData = JsonConvert.DeserializeObject <Shelf>
                                            (messageString.TrimStart('"').TrimEnd('"').Replace("\\", String.Empty));

                    /*If we have more than 120 shelves to process, remove down to the limit*/

                    if (shelfQueue.Count() > 120)
                    {
                        while (shelfQueue.Count() > 120)
                        {
                            await shelfQueue.DequeueAsync(new CancellationToken()); //throw away result

                            Console.WriteLine("Dequeing Extra live shelves.");
                        }
                    }
                    if (productData != null)
                    {
                        // Use appropriate shelfQueue based on count.
                        if (productData.Products.Count() == 0)
                        {
                            Console.WriteLine("Queuing 'Live' Feed for displaying in WebApp.");
                            shelfQueue.QueueShelf(productData);
                        }
                    }
                    // catch and swallow exceptions
                } catch (AggregateException ex) {
                    Console.WriteLine($"Error processing message: {ex.Flatten()}");
                } catch (Exception ex) {
                    Console.WriteLine($"Error processing message: {ex}");
                }
            }
            return(MessageResponse.Completed);
        }