예제 #1
0
        private async void OnTimer(object state)
        {
            var    geolocator = (Geolocator)state;
            string message;

            if (geolocator == null)
            {
                message = "No geolocator";
                DisplayToast(message);
            }
            else
            {
                Geoposition geoposition = await geolocator.GetGeopositionAsync();

                if (geoposition == null)
                {
                    message = "Cannot get current location";
                    DisplayToast(message);
                }
                else
                {
                    Geocoordinate    gc            = geoposition.Coordinate;
                    BasicGeoposition basicPosition = gc.Point.Position;

                    //message = "{\"_type": "location", "lat": "' + basicPosition.Latitude + '", "lon": "' + basicPosition.Longitude + '", "tst": "' + timestamp + '"';
                    OwntracksLocationMessage locmsg = new OwntracksLocationMessage(basicPosition.Latitude, basicPosition.Longitude, gc.Accuracy);
                    message = JsonConvert.SerializeObject(locmsg);
                    //no toast, just send it to the MQTT broker.
                    //this.client.Publish("home-assistant/jeroen", System.Text.Encoding.UTF8.GetBytes("{'message': 'barf'}"));
                    try
                    {
                        MqttBroker mqttbroker = new MqttBroker(this.broker, this.port, this.secure, this.sslprotocol, this.protocolversion, this.username, this.password);
                        mqttbroker.Connect();

                        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                        {
                            mqttbroker.Publish(tb_topic.Text, System.Text.Encoding.UTF8.GetBytes(message));
                        });

                        counter++;
                        NotifyUser("Total location messages sent: " + counter.ToString(), NotifyType.StatusMessage);
                    }
                    catch (Exception ex)
                    {
                        NotifyUser(ex.Message, NotifyType.ErrorMessage);
                    }
                }
            }
        }
예제 #2
0
        private void bt_connect_Click(object sender, RoutedEventArgs e)
        {
            var broker = tb_broker.Text.Trim();

            if (broker.Length == 0)
            {
                //no broker given
                NotifyUser("Enter a hostname for the MQTT broker.", NotifyType.ErrorMessage);
            }
            else
            {
                int port = -1;
                if (!(int.TryParse(tb_port.Text.Trim(), out port)))
                {
                    //invalid port
                    NotifyUser("Enter a valid port number", NotifyType.ErrorMessage);
                }
                else
                {
                    if (cb_protocol_version.SelectedItem == null)
                    {
                        //no protocol version selected
                        NotifyUser("Select a protocol", NotifyType.ErrorMessage);
                    }
                    else
                    {
                        if (cb_ssl_protocol_version.SelectedItem == null)
                        {
                            //no ssl protocol selected
                            NotifyUser("Select a protocol version", NotifyType.ErrorMessage);
                        }
                        else
                        {
                            uint interval = 15;
                            if (!uint.TryParse(tb_interval.Text.Trim(), out interval) || interval <= 0)
                            {
                                //invalid interval
                                NotifyUser("Enter a valid interval", NotifyType.ErrorMessage);
                            }
                            else
                            {
                                if (tb_topic.Text.Trim().Length == 0)
                                {
                                    //invalid topic
                                    NotifyUser("Enter a valid topic", NotifyType.ErrorMessage);
                                }
                                else
                                {
                                    //parse the input values
                                    var sslprotocol     = (MqttSslProtocols)Enum.Parse(typeof(MqttSslProtocols), cb_ssl_protocol_version.SelectedItem.ToString());
                                    var protocolversion = (MqttProtocolVersion)Enum.Parse(typeof(MqttProtocolVersion), cb_protocol_version.SelectedItem.ToString());
                                    this._interval = interval;
                                    //connect
                                    try
                                    {
                                        //store all settings
                                        StoreAllSettings();
                                        this.broker          = tb_broker.Text;
                                        this.port            = port;
                                        this.secure          = cb_secure.IsChecked.Value;
                                        this.sslprotocol     = sslprotocol;
                                        this.protocolversion = protocolversion;
                                        this.username        = tb_username.Text;
                                        this.password        = tb_password.Password;
                                        MqttBroker mqttbroker = new MqttBroker(this.broker, this.port, this.secure, this.sslprotocol, this.protocolversion, this.username, this.password);
                                        mqttbroker.Connect();

                                        //register extended execution.
                                        BeginExtendedExecution(_interval);
                                    }
                                    catch (Exception ex)
                                    {
                                        NotifyUser(ex.Message + ": " + ex.InnerException, NotifyType.ErrorMessage);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }