public async void Init(int port) { Port = port; StartServer(); var gpio = GpioController.GetDefault(); _led1In = gpio.OpenPin(LED1); _led2In = gpio.OpenPin(LED2); _led3In = gpio.OpenPin(LED3); _led4In = gpio.OpenPin(LED4); _led5In = gpio.OpenPin(LED5); _btnCntrOut = gpio.OpenPin(CENTER_BUTTON); _btnDownOut = gpio.OpenPin(DOWN_BUTTON); _btnLeftOut = gpio.OpenPin(LEFT_BUTTON); _btnRightOut = gpio.OpenPin(RIGHT_BUTTON); _btnUpOut = gpio.OpenPin(UP_BUTTON); _led1In.SetDriveMode(GpioPinDriveMode.Input); _led2In.SetDriveMode(GpioPinDriveMode.Input); _led3In.SetDriveMode(GpioPinDriveMode.Input); _led4In.SetDriveMode(GpioPinDriveMode.Input); _led5In.SetDriveMode(GpioPinDriveMode.Input); _btnCntrOut.SetDriveMode(GpioPinDriveMode.Output); _btnDownOut.SetDriveMode(GpioPinDriveMode.Output); _btnLeftOut.SetDriveMode(GpioPinDriveMode.Output); _btnUpOut.SetDriveMode(GpioPinDriveMode.Output); _btnRightOut.SetDriveMode(GpioPinDriveMode.Output); _btnCntrOut.Write(GpioPinValue.Low); _btnDownOut.Write(GpioPinValue.Low); _btnUpOut.Write(GpioPinValue.Low); _btnRightOut.Write(GpioPinValue.Low); _btnLeftOut.Write(GpioPinValue.Low); _mqttClient = SLWIOC.Create <IMQTTDeviceClient>(); _mqttClient.ShowDiagnostics = true; _mqttClient.BrokerHostName = "mqttdev.nuviot.com"; _mqttClient.BrokerPort = 1883; _mqttClient.DeviceId = [ADD as UserInfo NAME TO GET IT TO COMPILE !]; Hint...typical one _mqttClient.Password = [ADD as PASSWORD TO GET IT TO COMPILE !]; Hint...easy unsecure one for dev _mqttClient.ConnectionStateChanged += _mqttClient_ConnectionStateChanged; var result = await _mqttClient.ConnectAsync(); if (result.Result == ConnAck.Accepted) { await _mqttClient.SubscribeAsync(new Core.Networking.Models.MQTTSubscription() { Topic = "mancave/blinds/+/+", QOS = EntityHeader <QOS> .Create(QOS.QOS0) }); _mqttClient.MessageReceived += _mqttClient_MessageReceived; } }
public async void Connect() { try { IsBusy = true; switch (Model.DefaultTransport.Value) { /* case TransportTypes.AMQP: * { * var connectionString = $"Endpoint=sb://{Model.DefaultEndPoint}.servicebus.windows.net/;SharedAccessKeyName={Model.AccessKeyName};SharedAccessKey={Model.AccessKey}"; * var bldr = new EventHubsConnectionStringBuilder(connectionString) * { * EntityPath = Model.HubName * }; * * _isConnected = true; * } * * break;*/ case TransportTypes.AzureIoTHub: #if IOTHUB var connectionString = $"HostName={Model.DefaultEndPoint};DeviceId={Model.DeviceId};SharedAccessKey={Model.AccessKey}"; _azureIoTHubClient = DeviceClient.CreateFromConnectionString(connectionString, Microsoft.Azure.Devices.Client.TransportType.Amqp_Tcp_Only); await _azureIoTHubClient.OpenAsync(); ReceivingTask = Task.Run(ReceiveDataFromAzure); SetConnectedState(); #endif break; case TransportTypes.MQTT: _mqttClient = SLWIOC.Create <IMQTTDeviceClient>(); _mqttClient.ShowDiagnostics = true; _mqttClient.BrokerHostName = Model.DefaultEndPoint; _mqttClient.BrokerPort = Model.DefaultPort; _mqttClient.DeviceId = Model.UserName; _mqttClient.Password = Model.Password; var result = await _mqttClient.ConnectAsync(); if (result.Result == ConnAck.Accepted) { _isConnected = true; if (!String.IsNullOrEmpty(Model.Subscription)) { var subscription = new MQTTSubscription() { Topic = Model.Subscription.Replace("~deviceid~", Model.DeviceId), QOS = EntityHeader <QOS> .Create(QOS.QOS2) }; await _mqttClient.SubscribeAsync(subscription); _mqttClient.MessageReceived += _mqttClient_CommandReceived; } SetConnectedState(); } else { await Popups.ShowAsync($"{Resources.SimulatorCoreResources.Simulator_ErrorConnecting}: {result.Result.ToString()}"); } break; case TransportTypes.TCP: _tcpClient = SLWIOC.Create <ITCPClient>(); await _tcpClient.ConnectAsync(Model.DefaultEndPoint, Model.DefaultPort); StartReceiveThread(); SetConnectedState(); break; case TransportTypes.UDP: _udpClient = SLWIOC.Create <IUDPClient>(); await _udpClient.ConnectAsync(Model.DefaultEndPoint, Model.DefaultPort); StartReceiveThread(); SetConnectedState(); break; } RightMenuIcon = Client.Core.ViewModels.RightMenuIcon.None; } catch (Exception ex) { Debug.WriteLine(ex.Message); await Popups.ShowAsync(ex.Message); if (_mqttClient != null) { _mqttClient.Dispose(); _mqttClient = null; } #if IOTHUB if (_azureIoTHubClient != null) { await _azureIoTHubClient.CloseAsync(); _azureIoTHubClient.Dispose(); _azureIoTHubClient = null; } #endif if (_tcpClient != null) { await _tcpClient.DisconnectAsync(); _tcpClient.Dispose(); _tcpClient = null; } if (_udpClient != null) { await _udpClient.DisconnectAsync(); _udpClient.Dispose(); _udpClient = null; } SetDisconnectedState(); } finally { IsBusy = false; } }