Exemple #1
0
        public async Task <SensorNotification> SimulateCustomerAsync(CancellationToken cancellationToken)
        {
            // simulate delay between customer movements
            await SimulateDelay(cancellationToken);

            // prevent event from being sent if properties have not been initialized (desired property)
            if (!SensorId.HasValue || !MaxCapacity.HasValue)
            {
                Console.WriteLine($"{DateTime.Now.ToString("yyyyMMdd hh:mm:ss:ffffff")} - Sensor Id and/or MaxCapacity not set. Skipping event publication.");
                return(new SensorNotification {
                    IsInitialized = false
                });
            }

            Console.WriteLine($"{DateTime.Now.ToString("yyyyMMdd hh:mm:ss:ffffff")} - Customer detected. Sending message ...");

            // determine notificationtype
            NotificationType?notificationType = DetermineNotificationType();

            if (notificationType == null)
            {
                return(new SensorNotification {
                    IsInitialized = false
                });
            }

            //create event
            SensorNotification e = new SensorNotification
            {
                DeviceId         = _deviceId,
                ModuleId         = _moduleId,
                SensorId         = SensorId.Value,
                MaxCapacity      = MaxCapacity.Value,
                NotificationType = notificationType.Value,
                CustomerCount    = _customerCount,
                StoreStatus      = StoreStatus
            };

            return(e);
        }
Exemple #2
0
        /// <summary>
        /// Message loop that simulates people entering the store.
        /// </summary>
        /// <param name="cancellationToken">The cancellation,token for gracefully handling cancellation.</param>
        private static async Task MessageLoopAsync(CancellationToken cancellationToken)
        {
            int retryCount = 0;

            while (!cancellationToken.IsCancellationRequested)
            {
                // simulate customer
                SensorNotification notification = await _customersSimulation.SimulateCustomerAsync(cancellationToken);

                // prevent event from being sent if module has not yet been initialized properly (desired property)
                if (!notification.IsInitialized)
                {
                    Log("Sensor module not yet initialized properly. Skipping event publication.");
                    continue;
                }

                try
                {
                    Log("Customer entered. Sending message ...");

                    string messageString = JsonConvert.SerializeObject(notification);
                    var    message       = new Message(Encoding.UTF8.GetBytes(messageString));

                    // send event
                    await _moduleClient.SendEventAsync("sensorOutput", message);

                    Log($"Message sent: {messageString}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    retryCount++;
                    if (retryCount == 10)
                    {
                        break;
                    }
                }
            }
        }