Nel primo esempio viene mostrato come creare un driver IToolS MQTT Publisher per scrivere su un broker MQTT dei topic legati alle variabili IToolS
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 |
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.PublishName = "/itools/temperature1"; 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 = "broker.hivemq.com"; client.IOServer.NetConfig.Port = 1883; client.Start(); Thread t = new Thread(() => { Random r = new Random(); while (!exit) { var1.Value = r.Next(1000, 5000); Thread.Sleep(2000); } }); t.Start(); Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); exit = true; t.Join(5000); client.Stop(); client.StopIOServer(); } } } |
Nel secondo esempio viene mostrato come creare un driver IToolS MQTT Subscriber per leggere dei topic da un broker MQTT legati alle variabili IToolS
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 |
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.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 = "broker.mqttdashboard.com"; client.IOServer.NetConfig.Port = 1883; client.Start(); Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); } } } |
Nel terzo esempio viene mostrato come creare un driver modbus IToolS e pubblicare il valore di una variabile IToolS su un topic MQTT:
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 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace TestMQTTPublisherDriverModbusClient { using IToolS.Components; using IToolS.Components.Communication; using IToolS.Components.IOServers; class Program { static void Main(string[] args) { ComponentBase.RaiseEventsOnMainThread = false; Client client = null; Variable var1 = null; IOServer ioServer = new IOServer(); ioServer = new IOServer() { Name = "MQTTPublisher" }; ioServer.NetConfig.Address = "broker.mqttdashboard.com"; ioServer.NetConfig.Port = 1883; var1 = new Variable() { VariableName = "var1", Address = "100", Area = "HR" }; var1.PublishName = "/itools/temperature"; var1.Changed += (sender, e) => { Console.WriteLine("Topic: {0}; Value: {1}", var1.PublishName, e.NewValue); }; client = new Client(); Group group = new Group(); group.Items.Add(var1); group.IOServer = ioServer; client.Group = group; client.IOServer = new IOServer() { Name = "ModnetMaster" }; client.Start(); group.StartIOServer(); Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); client.Stop(); client.StopIOServer(); group.StopIOServer(); } } } |