Un esempio che mostra come pubblicare messaggi sul cloud Amazon AWS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace TestMQTTPublisherDriver { class Program { static void Main(string[] args) { bool exit = false; IToolS.Components.ComponentBase.RaiseEventsOnMainThread = false; IToolS.Components.Communication.Client client = null; IToolS.Components.Communication.Variable var1 = null; var1 = new IToolS.Components.Communication.Variable() { VariableName = "Temperature1" }; var1.VariableType = "String"; var1.PublishName = "/itools/temperature"; var1.Changed += (sender, e) => { Console.WriteLine("Topic: {0}; Value: {1}", var1.Address, e.NewValue); }; client = new IToolS.Components.Communication.Client(); IToolS.Components.Communication.Group group = new IToolS.Components.Communication.Group(); group.Items.Add(var1); client.Group = group; client.IOServer = new IToolS.Components.IOServers.IOServer() { Name = "MQTTPublisher" }; client.IOServer.NetConfig.Address = "az1r3jlpm8xaj.iot.eu-west-1.amazonaws.com"; client.IOServer.NetConfig.Port = 8883; client.IOServer.AdvancedProperties.Add(new IToolS.Data.Base.AdvancedProperty("QoSLevel", 1)); client.IOServer.AdvancedProperties.Add(new IToolS.Data.Base.AdvancedProperty("CaCertificateFileName", "root-cert.pem")); client.IOServer.AdvancedProperties.Add(new IToolS.Data.Base.AdvancedProperty("ClientCertificateFileName", "4b2686f67a.pfx")); client.IOServer.AdvancedProperties.Add(new IToolS.Data.Base.AdvancedProperty("CertificatePassword", "xxxxx")); client.Start(); Thread t = new Thread(() => { Random r = new Random(); while (!exit) { var1.PublishName = "/itools/temperature/" + DateTime.Now.Millisecond.ToString(); var1.Value = DateTime.Now.ToString(); Thread.Sleep(2000); } }); t.Start(); Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); exit = true; t.Join(5000); client.Stop(); client.StopIOServer(); } } } |
Un esempio che mostra come sottoscrivere messaggi dal cloud Amazon AWS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestMQTTSubscriberDriver { class Program { static void Main(string[] args) { IToolS.Components.ComponentBase.RaiseEventsOnMainThread = false; IToolS.Components.Communication.Client client = null; IToolS.Components.Communication.Variable var1 = null; var1 = new IToolS.Components.Communication.Variable(); var1.VariableType = "String"; var1.Address = "/itools/temperature"; var1.Changed += (sender, e) => { Console.WriteLine("Topic: {0}; Value: {1}", var1.Address, e.NewValue); }; client = new IToolS.Components.Communication.Client(); IToolS.Components.Communication.Group group = new IToolS.Components.Communication.Group(); group.Items.Add(var1); client.Group = group; client.IOServer = new IToolS.Components.IOServers.IOServer() { Name = "MQTTSubscriber" }; client.IOServer.NetConfig.Address = "az1r3jlpm8xaj.iot.eu-west-1.amazonaws.com"; client.IOServer.NetConfig.Port = 8883; client.IOServer.AdvancedProperties.Add(new IToolS.Data.Base.AdvancedProperty("QoSLevel", 1)); client.IOServer.AdvancedProperties.Add(new IToolS.Data.Base.AdvancedProperty("CaCertificateFileName", "root-cert.pem")); client.IOServer.AdvancedProperties.Add(new IToolS.Data.Base.AdvancedProperty("ClientCertificateFileName", "4b2686f67a.pfx")); client.IOServer.AdvancedProperties.Add(new IToolS.Data.Base.AdvancedProperty("CertificatePassword", "xxxxx")); client.Start(); Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); } } } |