Esempio n. 1
0
        /// <summary>
        /// Start the Can2Mqtt service.
        /// </summary>
        /// <param name="config">Commandline parameters</param>
        /// <returns></returns>
        public async Task Start(DaemonConfig config)
        {
            _MqttTopic          = config.MqttTopic;
            _CanTranslator      = config.MqttTranslator;
            _CanForwardWrite    = config.CanForwardWrite;
            _CanForwardRead     = config.CanForwardRead;
            _CanForwardResponse = config.CanForwardResponse;
            _NoUnits            = config.NoUnits;

            // Create a new MQTT client.
            var mqttFactory = new MqttFactory();

            _MqttClient = mqttFactory.CreateMqttClient();

            // Create TCP based options using the builder.
            _MqttClientOptions = new MqttClientOptionsBuilder()
                                 .WithClientId(config.MqttClientId)
                                 .WithTcpServer(config.MqttServer)
                                 //.WithCredentials("bud", "%spencer%")
                                 //.WithTls()
                                 .WithCleanSession()
                                 .Build();

            //Handle reconnect on loosing connection to MQTT Server
            _MqttClient.UseDisconnectedHandler(async e =>
            {
                Console.WriteLine("DISCONNECTED FROM MQTT BROKER {0}", config.MqttServer);
                while (!_MqttClient.IsConnected)
                {
                    await Task.Delay(TimeSpan.FromSeconds(5));

                    try
                    {
                        await _MqttClient.ConnectAsync(_MqttClientOptions);
                        Console.WriteLine("CONNECTED TO MQTT BROKER {0} using ClientId {1}", config.MqttServer, config.MqttClientId);
                    }
                    catch
                    {
                        Console.WriteLine("RECONNECTING TO MQTT BROKER {0} FAILED", config.MqttServer);
                    }
                }
            });

            //Connect the MQTT Client to the MQTT Broker
            await _MqttClient.ConnectAsync(_MqttClientOptions);

            if (_MqttClient.IsConnected)
            {
                Console.WriteLine("CONNECTED TO MQTT BROKER {0} using ClientId {1}", config.MqttServer, config.MqttClientId);
            }

            //Start listening on canlogservers port
            await ConnectTcpCanBus(config.CanServer, config.CanServerPort);
        }
Esempio n. 2
0
        /// <summary>
        /// Start the Can2Mqtt service.
        /// </summary>
        /// <param name="config">Commandline parameters</param>
        /// <returns></returns>
        public async Task Start(DaemonConfig config)
        {
            _MqttTopic          = config.MqttTopic;
            _CanTranslator      = config.MqttTranslator;
            _CanForwardWrite    = config.CanForwardWrite;
            _CanForwardRead     = config.CanForwardRead;
            _CanForwardResponse = config.CanForwardResponse;
            _NoUnits            = config.NoUnits;
            _CANInterface       = config.CANInterface == "auto" || config.CANInterface == "" ? "can" : config.CANInterface;

            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();

            var adapter_cnt = adapters.Length;


            foreach (NetworkInterface adapter in adapters)
            {
                IPInterfaceProperties properties = adapter.GetIPProperties();


                if (adapter.Name.Contains(_CANInterface))
                {
                    Console.WriteLine("Will use {0} interface.", adapter.Name);
                    _CANInterface = adapter.Name;
                    break;
                }

                adapter_cnt--;
            }

            if (adapter_cnt == 0)
            {
                Console.WriteLine("No matching CAN interface found in the system. Program abort...");
                return;
            }

            // Create a new MQTT client.
            var mqttFactory = new MqttFactory();

            _MqttClient = mqttFactory.CreateMqttClient();

            // Create TCP based options using the builder.
            _MqttClientOptions = new MqttClientOptionsBuilder()
                                 .WithClientId(config.MqttClientId)
                                 .WithTcpServer(config.MqttServer)
                                 //.WithCredentials("bud", "%spencer%")
                                 //.WithTls()
                                 .WithCleanSession()
                                 .Build();

            //Handle reconnect on loosing connection to MQTT Server
            _MqttClient.UseDisconnectedHandler(async e =>
            {
                Console.WriteLine("DISCONNECTED FROM MQTT BROKER {0}", config.MqttServer);
                while (!_MqttClient.IsConnected)
                {
                    try
                    {
                        await _MqttClient.ConnectAsync(_MqttClientOptions);
                        Console.WriteLine("CONNECTED TO MQTT BROKER {0} using ClientId {1}", config.MqttServer, config.MqttClientId);
                    }
                    catch
                    {
                        Console.WriteLine("RECONNECTING TO MQTT BROKER {0} FAILED", config.MqttServer);
                        System.Threading.Thread.Sleep(10000); //Wait 10 seconds
                    }
                }
            });

            //Connect the MQTT Client to the MQTT Broker
            await _MqttClient.ConnectAsync(_MqttClientOptions);

            if (_MqttClient.IsConnected)
            {
                Console.WriteLine("CONNECTED TO MQTT BROKER {0} using ClientId {1}", config.MqttServer, config.MqttClientId);
            }

            //Start listening on canlogservers port
            await ConnectTcpCanBus(config.CanServer, config.CanServerPort, _CANInterface);
        }