public static async Task SendEvent(DeviceClient client, IotMessage payload) { try { var messageString = JsonConvert.SerializeObject(payload); var message = new Message(Encoding.ASCII.GetBytes(messageString)); // Add a custom application property to the message. // An IoT hub can filter on these properties without access to the message body. //message.Properties.Add("temperatureAlert", (currentTemperature > 30) ? "true" : "false"); // Send the tlemetry message await client.SendEventAsync(message).ConfigureAwait(false); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString); Console.ResetColor(); await Task.Delay(500); } catch (Exception e) { Console.WriteLine(e); throw; } }
static async Task Execute() { try { GetClient(new HandlerConfig().WithElement("SetVariableAsync", SetVariableAsync).Elements); CustomLogger logger = new CustomLogger(); var opcConnectionString = @"opc.tcp://127.0.0.1:49320"; MyClient = new OPCClient(opcConnectionString, "", "", logger, "Mytest2", SecurityPolicy.Basic128, MessageSecurity.Sign); R1 = (OPCTag)MyClient.AddTag("RAMP1", "ns=2;s=TEST_OPC.RAMP.RAMP1", typeof(float)); T0 = (OPCTag)MyClient.AddTag("WRITE1", "ns=2;s=TEST_OPC.WRITE.WRITE1", typeof(float)); T1 = (OPCTag)MyClient.AddTag("WRITE2", "ns=2;s=TEST_OPC.WRITE.WRITE2", typeof(float)); T2 = (OPCTag)MyClient.AddTag("WRITE3", "ns=2;s=TEST_OPC.WRITE.WRITE3", typeof(float)); var T3 = (OPCTag)MyClient.AddTag("Cast", "ns=2;s=TEST_OPC.WRITE.WRITESLOW1", typeof(double)); T3.ReadItem(); List <string> TagNameList = new List <string>(); TagNameList.Add(T0.Name); TagNameList.Add(T1.Name); TagNameList.Add(T2.Name); TagNameList.Add(R1.Name); var baseName1 = "ns=2;s=TEST_OPC.RAMP.RAMP"; var baseName2 = "ns=2;s=TEST_OPC.RAND.RAND"; for (int i = 1; i <= 150; i++) { var tagName1 = $"{baseName1}{i}"; MyClient.AddTag(tagName1, tagName1, typeof(float)); TagNameList.Add(tagName1); var tagName2 = $"{baseName2}{i}"; MyClient.AddTag(tagName2, tagName2, typeof(int)); TagNameList.Add(tagName2); } while (true) { MyClient.ReadTags(TagNameList); IotMessage message = new IotMessage(); message.Datetime = DateTime.Now; message.DeviceId = "PLC0001"; message.Data = new List <Payload>(); foreach (var x in TagNameList) { double y = 0; if (MyClient.GetTag(x)?.Value != null) { y = Double.Parse(MyClient.GetTag(x).Value.ToString()); } message.Data.Add(new Payload() { datetime = DateTime.Now, name = MyClient.GetTag(x).Name, value = y }); Console.WriteLine(MyClient.GetTag(x).Name + " | " + MyClient.GetTag(x).Value); } SendEvent(_deviceClient, message).GetAwaiter().GetResult(); Thread.Sleep(100); Console.WriteLine("----"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }