コード例 #1
0
        protected void websocket_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
#if DEBUG
            Console.WriteLine("Received message: " + e.Message);
#endif
            if (isPairing)
            {
                var definition = new { id = "", method = "", payload = "", type = "", version = 0 };
                var dynObj     = JsonConvert.DeserializeAnonymousType(e.Message, definition);
                if (dynObj.method != null)
                {
                    string method = dynObj.method;

                    if (method == PairingCodeMessage.METHOD)
                    {
                        PairingCodeMessage pcm = JsonUtils.Deserialize <PairingCodeMessage>(dynObj.payload);
                        if (config.OnPairingCode != null)
                        {
                            config.OnPairingCode(pcm.pairingCode);
                        }
                        else
                        {
                            throw new Exception("OnPairingCode handler not set");
                        }
                    }
                    else if (method == PairingResponse.METHOD)
                    {
                        PairingResponse pr = JsonUtils.Deserialize <PairingResponse>(dynObj.payload);
                        // TODO: There was a bug in the Device that sent INITIAL instead of PAIRED, fix committed around Aug 1, 2018 in Aug AppsCut and rolled to customers ??? thereafter - fall/winter '18.
                        //       This was actually the only time INITIAL was sent; once the bug is fixed there is no further expected need for INITIAL. When bug clears back compat for customers, we can clean this workaround up.
                        if (pr.pairingState == PairingResponse.PAIRED || pr.pairingState == PairingResponse.INITIAL)
                        {
                            isPairing        = false;
                            pairingAuthToken = pr.authenticationToken;
                            config.OnPairingSuccess?.Invoke(pr.authenticationToken);
                            onDeviceReady();
                        }
                        else if (pr.pairingState == PairingResponse.FAILED)
                        {
                            pairingAuthToken = null;
                            SendPairingRequest();
                        }
                        else if (pr.pairingState == PairingResponse.AUTHENTICATING)
                        {
                            config.OnPairingState?.Invoke(PairingResponse.AUTHENTICATING, "Enter security pin on device to begin pairing");
                        }
                        else
                        {
                            // Pass anything else from the device up to the user
                            config.OnPairingState?.Invoke(pr.pairingState, "");
                        }
                    }
                }
            }
            else
            {
                onMessage(e.Message);
            }
        }
コード例 #2
0
        protected void websocket_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
#if DEBUG
            Console.WriteLine("Received message: " + e.Message);
#endif
            if (isPairing)
            {
                var definition = new { id = "", method = "", payload = "", type = "", version = 0 };
                var dynObj     = JsonConvert.DeserializeAnonymousType(e.Message, definition);
                if (dynObj.method != null)
                {
                    string method = dynObj.method;

                    if (PairingCodeMessage.METHOD.Equals(method))
                    {
                        PairingCodeMessage pcm = JsonUtils.deserialize <PairingCodeMessage>(dynObj.payload);
                        if (config.OnPairingCode != null)
                        {
                            config.OnPairingCode(pcm.pairingCode);
                        }
                        else
                        {
                            throw new Exception("OnPairingCode handler not set");
                        }
                    }
                    else if (PairingResponse.METHOD.Equals(method))
                    {
                        PairingResponse pr = JsonUtils.deserialize <PairingResponse>(dynObj.payload);
                        if (PairingResponse.PAIRED.Equals(pr.pairingState) || PairingResponse.INITIAL.Equals(pr.pairingState))
                        {
                            isPairing        = false;
                            pairingAuthToken = pr.authenticationToken;
                            if (config.OnPairingSuccess != null)
                            {
                                config.OnPairingSuccess(pr.authenticationToken);
                            }
                            onDeviceReady();
                        }
                        else if (PairingResponse.FAILED.Equals(pr.pairingState))
                        {
                            pairingAuthToken = null; //
                            SendPairingRequest();
                        }
                    }
                }
            }
            else
            {
                onMessage(e.Message);
            }
        }