|
| 1 | +/* Purpose: This is a simple application that acts as an ISBM publication Provider. |
| 2 | + * It demonstrates an IoT device using an ISBM Client Adapter to post-publication |
| 3 | + * to an ISBM Server Adapter. It is interoperable with any ISBM compatible adapters |
| 4 | + * regardless of the actual service bus that delivers the messages. |
| 5 | + * |
| 6 | + * Remarks: 1. This is an .Net Core 3.1 project that targets Raspberry Pi OS for deployment. |
| 7 | + * 2. This demo does not close publication session for simpliclty. It behaves as power |
| 8 | + * loss when the program exits. |
| 9 | + * |
| 10 | + * Author: Pak Wong |
| 11 | + * Date Created: 2022/08/31 |
| 12 | + * |
| 13 | + * (c) 2022 |
| 14 | + * This code is licensed under MIT license |
| 15 | +*/ |
| 16 | + |
| 17 | +using System; |
| 18 | +using System.Threading; |
| 19 | +using ISBM20ClientAdapter; |
| 20 | +using ISBM20ClientAdapter.ResponseType; |
| 21 | +using Newtonsoft.Json; |
| 22 | +using Newtonsoft.Json.Linq; |
| 23 | + |
| 24 | +namespace ISBM20Pi3TestCore21 |
| 25 | +{ |
| 26 | + class Program |
| 27 | + { |
| 28 | + static string _hostName = ""; |
| 29 | + static string _channelId = ""; |
| 30 | + static string _topic = ""; |
| 31 | + static string _sessionId = ""; |
| 32 | + |
| 33 | + static Boolean _authentication; |
| 34 | + static string _username = ""; |
| 35 | + static string _password = ""; |
| 36 | + |
| 37 | + static string _bodTemplate = ""; |
| 38 | + |
| 39 | + static ProviderPublicationService _myProviderPublicationService = new ProviderPublicationService(); |
| 40 | + |
| 41 | + static void Main(string[] args) |
| 42 | + { |
| 43 | + |
| 44 | + SetConfigurations(); |
| 45 | + GetBODTemplate(); |
| 46 | + |
| 47 | + //Open an Provider Publication Session |
| 48 | + OpenPublicationSessionResponse myOpenPublicationSessionResponse = _myProviderPublicationService.OpenPublicationSession(_hostName, _channelId); |
| 49 | + Console.WriteLine("Host Address " + _hostName); |
| 50 | + Console.WriteLine("Channel Id " + _channelId); |
| 51 | + |
| 52 | + |
| 53 | + if (myOpenPublicationSessionResponse.StatusCode == 201) |
| 54 | + { |
| 55 | + //SessionID is stored in a class level valuable for repeatedly used in every BPD post publication. |
| 56 | + _sessionId = myOpenPublicationSessionResponse.SessionID; |
| 57 | + Console.WriteLine("Publication Session " + _sessionId); |
| 58 | + Console.WriteLine("ISBM 2.0 IoT Device is ready!!"); |
| 59 | + } |
| 60 | + else |
| 61 | + { |
| 62 | + Console.WriteLine(myOpenPublicationSessionResponse.StatusCode + " " + myOpenPublicationSessionResponse.ISBMHTTPResponse); |
| 63 | + Console.WriteLine("Please check configurations!!"); |
| 64 | + } |
| 65 | + |
| 66 | + Thread.Sleep(1000); |
| 67 | + |
| 68 | + while (true) |
| 69 | + { |
| 70 | + PublishBOD(); |
| 71 | + Thread.Sleep(5000); |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + private static void SetConfigurations() |
| 78 | + { |
| 79 | + //Read application configurations from Configs.json |
| 80 | + string filename = "Configs.json"; |
| 81 | + |
| 82 | + string JsonFromFile = System.IO.File.ReadAllText(filename); |
| 83 | + |
| 84 | + JObject JObjectConfigs = JObject.Parse(JsonFromFile); |
| 85 | + _hostName = JObjectConfigs["hostName"].ToString(); |
| 86 | + _channelId = JObjectConfigs["channelId"].ToString(); |
| 87 | + _topic = JObjectConfigs["topic"].ToString(); |
| 88 | + |
| 89 | + _authentication = (Boolean)JObjectConfigs["authentication"]; |
| 90 | + if (_authentication == true) |
| 91 | + { |
| 92 | + _username = JObjectConfigs["userName"].ToString(); |
| 93 | + _password = JObjectConfigs["password"].ToString(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private static void GetBODTemplate() |
| 98 | + { |
| 99 | + string filename = "SyncMeasurements.json"; |
| 100 | + |
| 101 | + string JsonFromFile = System.IO.File.ReadAllText(filename); |
| 102 | + |
| 103 | + _bodTemplate = JsonFromFile; |
| 104 | + } |
| 105 | + |
| 106 | + private static void PublishBOD() |
| 107 | + { |
| 108 | + |
| 109 | + //Create new BOD message from SyncMeasurements use case template |
| 110 | + string bodMessage = FillBODFields(_bodTemplate); |
| 111 | + |
| 112 | + //Post Publication - BOD message |
| 113 | + PostPublicationResponse myPostPublicationResponse = _myProviderPublicationService.PostPublication(_hostName, _sessionId, _topic, bodMessage); |
| 114 | + |
| 115 | + string MessageId = ""; |
| 116 | + if (myPostPublicationResponse.StatusCode == 201) |
| 117 | + { |
| 118 | + MessageId = myPostPublicationResponse.MessageID; |
| 119 | + Console.WriteLine("Message " + MessageId + " has been pusblished!!"); |
| 120 | + } |
| 121 | + else |
| 122 | + { |
| 123 | + Console.WriteLine(myPostPublicationResponse.StatusCode + " " + myPostPublicationResponse.ISBMHTTPResponse); |
| 124 | + } |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + private static string FillBODFields(string bodTemplate) |
| 129 | + { |
| 130 | + |
| 131 | + //Create a sim value |
| 132 | + //------------------------------------------------- |
| 133 | + Random myRandom = new Random(); |
| 134 | + double randomValue = (double)myRandom.Next(1, 100); |
| 135 | + |
| 136 | + double simMeasurement = randomValue / 100 + 8; |
| 137 | + //------------------------------------------------- |
| 138 | + |
| 139 | + JObject objBOD = JObject.Parse(_bodTemplate); |
| 140 | + |
| 141 | + objBOD["syncMeasurements"]["applicationArea"]["bODID"] = System.Guid.NewGuid().ToString(); |
| 142 | + objBOD["syncMeasurements"]["applicationArea"]["creationDateTime"] = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"); |
| 143 | + |
| 144 | + objBOD["syncMeasurements"]["dataArea"]["measurements"][0]["measurementLocation"]["UUID"] = "6EEBDB09-39EC-4E40-9FD9-3864FD49B0DB"; |
| 145 | + objBOD["syncMeasurements"]["dataArea"]["measurements"][0]["measurementLocation"]["shortName"] = "Headwater Elevation"; |
| 146 | + objBOD["syncMeasurements"]["dataArea"]["measurements"][0]["measurementLocation"]["infoSource"]["UUID"] = "9B005C91-A36F-4D69-A3ED-CC51E78E3A66"; |
| 147 | + |
| 148 | + objBOD["syncMeasurements"]["dataArea"]["measurements"][0]["measurement"][0]["UUID"] = System.Guid.NewGuid().ToString(); |
| 149 | + objBOD["syncMeasurements"]["dataArea"]["measurements"][0]["measurement"][0]["recorded"]["dateTime"] = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"); |
| 150 | + objBOD["syncMeasurements"]["dataArea"]["measurements"][0]["measurement"][0]["infoSource"]["UUID"] = "9B005C91-A36F-4D69-A3ED-CC51E78E3A66"; |
| 151 | + |
| 152 | + objBOD["syncMeasurements"]["dataArea"]["measurements"][0]["measurement"][0]["data"]["measure"]["value"] = simMeasurement; |
| 153 | + objBOD["syncMeasurements"]["dataArea"]["measurements"][0]["measurement"][0]["data"]["measure"]["unitOfMeasure"]["UUID"] = "73e8145d-7a92-49ef-9b10-6162a06f7876"; |
| 154 | + objBOD["syncMeasurements"]["dataArea"]["measurements"][0]["measurement"][0]["data"]["measure"]["unitOfMeasure"]["shortName"] = "Feet"; |
| 155 | + objBOD["syncMeasurements"]["dataArea"]["measurements"][0]["measurement"][0]["data"]["measure"]["unitOfMeasure"]["infoSource"]["UUID"] = "cf3f3a8a-1e42-4f15-9288-9cf2241e163d"; |
| 156 | + |
| 157 | + return objBOD.ToString(Newtonsoft.Json.Formatting.None); |
| 158 | + } |
| 159 | + |
| 160 | + |
| 161 | + } |
| 162 | +} |
0 commit comments