Beispiel #1
0
        public void SetService()
        {
            if (appSettings.ConnectionIndex < 0)
            {
                return;
            }

            ServiceBase.OnConnectHandler OnConnection = async connection =>
            {
                await SendResult(new SystemResultMessage("CONNECT"), "!!");
            };

            ServiceBase.OnConnectHandler OnDisconnected = connection =>
            {
                this.Disconnect();
            };

            if (service != null)
            {
                service.OnConnect      -= OnConnection;
                service.OnDisconnected -= OnDisconnected;
            }

            if (appSettings.ConnectionIndex == 0)
            {
                service = services.ContainsKey("Bluetooth") ? services["Bluetooth"] : new Bluetooth();
                services["Bluetooth"] = service;
            }
            else
            {
                service          = services.ContainsKey("Wifi") ? services["Wifi"] : new Wifi();
                services["Wifi"] = service;

                if (appSettings.ConnectionIndex == 2 && !string.IsNullOrWhiteSpace(appSettings.Hostname))
                {
                    service.SetClient("added",
                                      new Connection("added",
                                                     new RemotePeer(null, new HostName(appSettings.Hostname), "1235")));
                }
            }

            service.OnConnect      += OnConnection;
            service.OnDisconnected += OnDisconnected;

            service.Initialize(!appSettings.MissingBackButton);

            service.ListenForBeacons();
            RefreshConnections();
        }
Beispiel #2
0
        public async void SetService()
        {
            if (this.appSettings.ConnectionIndex < 0)
            {
                return;
            }

            ServiceBase.OnConnectHandler OnConnection = async connection =>
            {
                if (this.IsWelcomeMessageShowing)
                {
                    this.IsWelcomeMessageShowing = false;
                    await
                    this.dispatcher.RunAsync(
                        CoreDispatcherPriority.Normal,
                        () => { this.backgroundImage.ClearValue(Image.SourceProperty); });
                }

                await this.SendResult(new SystemResultMessage("CONNECT"), "!!");
            };

            ServiceBase.OnConnectHandler OnDisconnected = connection => { this.Disconnect(); };

            if (this.lastConnection != this.appSettings.ConnectionIndex)
            {
                await
                this.dispatcher.RunAsync(
                    CoreDispatcherPriority.Normal,
                    () => { this.appSettings.CurrentConnectionState = (int)ConnectionState.Disconnecting; });

                this.Disconnect();
                this.lastConnection = this.appSettings.ConnectionIndex;

                await
                this.dispatcher.RunAsync(
                    CoreDispatcherPriority.Normal,
                    () => { this.appSettings.CurrentConnectionState = (int)ConnectionState.NotConnected; });
            }

            if (this.service != null)
            {
                this.service.OnConnect      -= OnConnection;
                this.service.OnDisconnected -= OnDisconnected;
            }

            switch (this.appSettings.ConnectionIndex)
            {
            case AppSettings.CONNECTION_BLUETOOTH:
            {
                this.service = this.services.ContainsKey("Bluetooth")
                                           ? this.services["Bluetooth"]
                                           : new Bluetooth();
                this.services["Bluetooth"] = this.service;
                App.Telemetry.Context.Properties["connection.type"] = "Bluetooth";

                break;
            }

            case AppSettings.CONNECTION_WIFI:
            case AppSettings.CONNECTION_MANUAL:
            {
                this.service = this.services.ContainsKey("Wifi")
                                           ? this.services["Wifi"]
                                           : new Wifi(AppSettings.BroadcastPort);
                this.services["Wifi"] = this.service;
                App.Telemetry.Context.Properties["connection.type"] = "Wifi";

                if (this.appSettings.ConnectionIndex == 2 &&
                    !string.IsNullOrWhiteSpace(this.appSettings.Hostname))
                {
                    this.service.SetClient(
                        "added",
                        new Connection(
                            "added",
                            new RemotePeer(
                                null,
                                new HostName(this.appSettings.Hostname),
                                AppSettings.BroadcastPort.ToString())));
                }

                break;
            }

            case AppSettings.CONNECTION_USB:
            {
                this.service = new USB();
                App.Telemetry.Context.Properties["connection.type"] = "USB";
                break;
            }

            default:
            {
                throw new NotImplementedException(
                          "Connection type (" + this.appSettings.ConnectionIndex + ") not supported");
            }
            }

            this.service.OnConnect         += OnConnection;
            this.service.OnDisconnected    += OnDisconnected;
            this.service.ThreadedException += this.Service_ThreadedException;

            if (!this.service.IsConnected)
            {
                this.service.Initialize(!this.appSettings.MissingBackButton);
            }

            this.service.ListenForBeacons();
            this.RefreshConnections();
        }