public static void Run( [ EventHubTrigger("%IssueTicketEventHubName%", Connection = "receiverConnectionString") ] EventData[] eventHubMessages, ILogger log) { // process messages foreach (EventData message in eventHubMessages) { string messagePayload = Encoding.UTF8.GetString(message.Body.Array); // process each message IssueTicketRequest ticketRequestMessage = JsonConvert.DeserializeObject <IssueTicketRequest>(messagePayload); try { string methodName = ticketRequestMessage.MethodName; string deviceId = ticketRequestMessage.DeviceId; string transactionId = ticketRequestMessage.TransactionId; var payload = new IssueTicketRequest() { TransactionId = transactionId, DeviceId = ticketRequestMessage.DeviceId, DeviceType = ticketRequestMessage.DeviceType }; log.LogInformation($"Response Method: {methodName}"); } catch (Exception ex) { log.LogError(ex.Message); } } }
private bool SendTicketIssuedMessageToCloud(PurchaseTicketPayload requestpayload) { this.TicketStockCount--; IssueTicketRequest issueTicketRequest = new IssueTicketRequest() { DeviceId = this.deviceId, DeviceType = this.deviceType, TransactionId = requestpayload.TransactionId, CreateTime = System.DateTime.UtcNow }; var messageString = JsonConvert.SerializeObject(issueTicketRequest); SendMessageToCloud(messageString); Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString); Console.WriteLine(); // if we've fallen below the threshold, send notification if (this.TicketStockCount < (int)this.deviceConfig.LowStockThreshold && !this.LowStockNotificationSent) { SendTLowStockMessageToCloud(); } 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; } } } }