public static void Run( [ EventHubTrigger("lowstockeventhub", Connection = "receiverConnectionString") ] EventData[] eventHubMessages, ILogger log) { foreach (var message in eventHubMessages) { string messagePayload = Encoding.UTF8.GetString(message.Body.Array); LowStockRequest ticketRequestMessage = JsonConvert.DeserializeObject <LowStockRequest>(messagePayload); log.LogWarning($" !!!!! Low Stock Alert !!!! \n\t DeviceID - {ticketRequestMessage.DeviceId} " + $"\n\t Time - {ticketRequestMessage.CreateTime}" + $"\n\t Current Stock Level - {ticketRequestMessage.StockLevel}"); } }
private bool SendTLowStockMessageToCloud() { LowStockRequest lowStockNotification = new LowStockRequest() { DeviceId = this.deviceId, DeviceType = this.deviceType, StockLevel = this.TicketStockCount, CreateTime = System.DateTime.UtcNow }; var messageString = JsonConvert.SerializeObject(lowStockNotification); SendMessageToCloud(messageString); this.LowStockNotificationSent = true; Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString); Console.WriteLine(); return(false); // don't restart timer }
public static void Run( [ EventHubTrigger("%noactionneededEventhubName%", Connection = "receiverConnectionString") ] EventData[] eventHubMessages, ILogger log) { foreach (var message in eventHubMessages) { string messagePayload = Encoding.UTF8.GetString(message.Body.Array); // note, this approaches parses the JSON twice, which isn't very efficient but does help // demonstrate what we're trying to accomplish var obj = JObject.Parse(messagePayload); // get a dynamic object based on the msg payload string msgType = (string)obj["MessageType"]; if (message.Properties.ContainsKey("opType") && (message.Properties["opType"].ToString() == "updateTwin")) { log.LogInformation($" Device Twin update event."); string deviceId = message.Properties["deviceId"].ToString(); // are there desired properties present? if (obj["properties"]["reported"] != null) { if (obj["properties"]["reported"]["GateDirection"] != null) { string reportedGateDirection = obj["properties"]["reported"]["GateDirection"].ToString(); log.LogInformation($" Gate Direction on Device {deviceId} reported change to {reportedGateDirection}."); } if (obj["properties"]["reported"]["status"] != null) { log.LogInformation($" Desired Status of Device {deviceId} reported as {obj["properties"]["reported"]["status"].ToString()}."); } } else if (obj["properties"]["desired"] != null) { if (obj["properties"]["desired"]["status"] != null) { log.LogInformation($" Desired Status of Device {deviceId} set to {obj["properties"]["desired"]["status"].ToString()}."); } } } else { switch (msgType) { case MessageType.eventLowStock: // parse a low stock message LowStockRequest lowStockMsg = JsonConvert.DeserializeObject <LowStockRequest>(messagePayload); log.LogWarning($" !!!!! Low Stock Alert !!!! \n\t DeviceID - {lowStockMsg.DeviceId} " + $"\n\t Time - {lowStockMsg.CreateTime}" + $"\n\t Current Stock Level - {lowStockMsg.StockLevel}"); break; case MessageType.eventTicketIssued: // parse a ticket issued event IssueTicketRequest ticketIssued = JsonConvert.DeserializeObject <IssueTicketRequest>(messagePayload); log.LogInformation($" Ticket Issued by device {ticketIssued.DeviceId} "); break; case MessageType.eventGateOpened: // parse a GateOpened GateOpenedNotification gateOpenedMsg = JsonConvert.DeserializeObject <GateOpenedNotification>(messagePayload); log.LogInformation($" Gate Opened on Device {gateOpenedMsg.DeviceId} "); break; default: log.LogError($" Unknown message recieved !!!! \n\t {messagePayload} "); break; } } } }
public void TestLowStock() { FakeDeviceClient fakeDeviceClient = new FakeDeviceClient(); FakeEventScheduler fakeScheduler = new FakeEventScheduler(); TestContext.WriteLine(">> Testing the Kiosk Device's Low Stock notification.."); PurchaseTicketPayload approvePurchaseMethodkRequest = new PurchaseTicketPayload() { DeviceId = deviceconfig.DeviceId, DeviceType = deviceconfig.DeviceType, MessageType = MessageType.cmdPurchaseTicket, IsApproved = true }; // create our test device KioskDevice device = new KioskDevice(deviceconfig, fakeDeviceClient, fakeScheduler); device.InitializeAsync().Wait(); TestContext.WriteLine(string.Empty); TestContext.WriteLine(">> Purchasing tickets, shouldn't throw event"); string requestString = JsonConvert.SerializeObject(approvePurchaseMethodkRequest); MethodRequest methodRequest = new MethodRequest("ReceivePurchaseTicketResponse", Encoding.UTF8.GetBytes(requestString)); // fire events to bring the count down to right at threshold for (long count = this.deviceconfig.InitialStockCount; count > this.deviceconfig.LowStockThreshold; count--) { MethodResponse myresult = fakeDeviceClient.directMethods[0](methodRequest, null).Result; TestContext.WriteLine(">> Current stock level: " + device.CurrentStockLevel); } // now that we're at the threshold, lets clear all previous events fakeDeviceClient.sendMessageLog.Clear(); // clear out all messages to this point TestContext.WriteLine(string.Empty); TestContext.WriteLine(">> Purchasing 1 more ticket. Should send low stock notification"); // purchase one more ticket. MethodResponse methodresult = fakeDeviceClient.directMethods[0](methodRequest, null).Result; // we expect 2 messages to have been sent Assert.AreEqual(fakeDeviceClient.sendMessageLog.Count, 2); // second message should make expected result LowStockRequest expectedRequest = new LowStockRequest() { DeviceId = deviceconfig.DeviceId, DeviceType = deviceconfig.DeviceType, StockLevel = (deviceconfig.LowStockThreshold - 1), }; // get actual message into an object so we can compare it LowStockRequest actualRequest = JsonConvert.DeserializeObject <LowStockRequest>(fakeDeviceClient.sendMessageLog[1]); TestContext.WriteLine(fakeDeviceClient.sendMessageLog[1]); // compare properties to make sure they're valid. Assert.AreEqual(actualRequest.DeviceId, expectedRequest.DeviceId); Assert.AreEqual(actualRequest.DeviceType, expectedRequest.DeviceType); Assert.AreEqual(actualRequest.MessageType, expectedRequest.MessageType); Assert.AreEqual(actualRequest.StockLevel, expectedRequest.StockLevel); TestContext.WriteLine(string.Empty); TestContext.WriteLine(">> Testing to make sure we don't have a second low stock warning .."); // make sure we don't throw the low stock warning again fakeDeviceClient.sendMessageLog.Clear(); // clear out all messages to this point // purchase one more ticket. methodresult = fakeDeviceClient.directMethods[0](methodRequest, null).Result; // we expect 2 messages to have been sent Assert.AreEqual(fakeDeviceClient.sendMessageLog.Count, 1); // reset the device to make sure ticket stock is reset using the desired property callback option device.SetDeviceStatusAsync(DeviceStatus.disabled).Wait(); // disable the device device.SetDeviceStatusAsync(DeviceStatus.enabled).Wait(); // enable the device // check the stock level Assert.AreEqual(deviceconfig.InitialStockCount, device.CurrentStockLevel, "Device stock levels were not reset back to initial after device rest"); }