static PromoPackage CreatePromoPackage(char selection)
        {
            PromoPackage promoPackage = null;

            if (selection == '1')
            {
                promoPackage = new PromoPackage()
                {
                    ImageUri     = GetUriForPromoImage("Soda.png"),
                    Price        = 0.99,
                    ProductId    = 1,
                    ProductTitle = "soda"
                };
            }
            else
            {
                promoPackage = new PromoPackage()
                {
                    ImageUri     = GetUriForPromoImage("Water.png"),
                    Price        = 0.75,
                    ProductId    = 2,
                    ProductTitle = "water"
                };
            }

            return(promoPackage);
        }
        static void Main(string[] args)
        {
            string deviceId    = "112358";
            bool   keepLooping = true;

            while (keepLooping)
            {
                Console.WriteLine("Which promo would you like to push out?");
                Console.WriteLine("1. Soda");
                Console.WriteLine("2. Water");
                Console.WriteLine("Press any other key to quit.");

                char selection = Console.ReadKey().KeyChar;
                Console.WriteLine();

                if (selection == '1' || selection == '2')
                {
                    PromoPackage promoPackage = CreatePromoPackage(selection);

                    PushPromo(deviceId, promoPackage).Wait();

                    Console.WriteLine("Command sent");
                }

                else
                {
                    keepLooping = false;
                }
            }
        }
Exemple #3
0
        private async void ListenForControlMessages()
        {
            Task.Delay(3000).Wait();

            while (true)
            {
                //TODO: 1. Receive messages intended for the device via the instance of _deviceClient.
                Microsoft.Azure.Devices.Client.Message receivedMessage = await _deviceClient.ReceiveAsync();

                // 5.Replace TODO 2 with the following:
                //TODO: 2. A null message may be received if the wait period expired, so ignore and call the receive operation again
                if (receivedMessage == null)
                {
                    continue;
                }

                //6.Replace TODO 3 with the following:
                //TODO: 3. Deserialize the received binary encoded JSON message into an instance of PromoPackage.
                string receivedJSON = Encoding.ASCII.GetString(receivedMessage.GetBytes());
                System.Diagnostics.Trace.TraceInformation("Received message: {0}", receivedJSON);
                PromoPackage promo = Newtonsoft.Json.JsonConvert.DeserializeObject <PromoPackage>(receivedJSON);

                //7.Replace TODO 4 with the following:
                //TODO: 4. Acknowledge receipt of the message with IoT Hub
                await _deviceClient.CompleteAsync(receivedMessage);
            }
        }
Exemple #4
0
        private async void ApplyPromoPackageAsync(PromoPackage promo)
        {
            CloudBlockBlob blob = new CloudBlockBlob(new Uri(promo.ImageUri));
            string         path = System.IO.Path.GetFullPath(String.Format("{0}-{1}.png", promo.ProductId, promo.ProductTitle));

            if (!File.Exists(path))
            {
                await blob.DownloadToFileAsync(path, FileMode.Create);
            }

            SetPromo(promo.ProductTitle, promo.Price, promo.ProductId, path);
        }
        static async Task PushPromo(string deviceId, PromoPackage promoPackage)
        {
            //TODO: 1. Create a Service Client instance provided the _IoTHubConnectionString
            _serviceClient = /*Complete this*/;

            var promoPackageJson = JsonConvert.SerializeObject(promoPackage);

            Console.WriteLine("Sending Promo Package:");
            Console.WriteLine(promoPackageJson);

            var commandMessage = new Message(Encoding.ASCII.GetBytes(promoPackageJson));

            //TODO: 2. Send the command
            await _serviceClient./*Complete this*/;
        }