Exemple #1
0
        /// <summary>
        /// Runs the function associated with this button/type. One of the following strings:
        /// Pressed, Released, Tapped, DoubleTapped, Held, HeldReleased
        /// </summary>
        /// <param name="number"></param>
        /// <param name="type"></param>
        public void Press(uint number, string type)
        {
            if (!Buttons.ContainsKey(number))
            {
                return;
            }
            var but = Buttons[number];

            if (but.EventTypes.ContainsKey(type))
            {
                foreach (var a in but.EventTypes[type])
                {
                    DeviceJsonApi.DoDeviceAction(a);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Runs the function associated with this button/type. One of the following strings:
        /// Pressed, Released, Tapped, DoubleTapped, Held, HeldReleased
        /// </summary>
        /// <param name="number"></param>
        /// <param name="type"></param>
        public void Press(string number, string type)
        {
            // TODO: In future, consider modifying this to generate actions at device activation time
            //       to prevent the need to dynamically call the method via reflection on each button press
            if (!_Buttons.ContainsKey(number))
            {
                return;
            }
            var but = _Buttons[number];

            if (but.EventTypes.ContainsKey(type))
            {
                foreach (var a in but.EventTypes[type])
                {
                    DeviceJsonApi.DoDeviceAction(a);
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ParseStreamRx(string message)
        {
            if (string.IsNullOrEmpty(message))
            {
                return;
            }

            if (!message.Contains("/system/heartbeat"))
            {
                Debug.Console(1, this, "Message RX: {0}", message);
            }
            else
            {
                LastAckMessage = DateTime.Now;
            }

            try
            {
                var messageObj = JObject.Parse(message);

                var type = messageObj["type"].Value <string>();

                if (type == "hello")
                {
                    SendInitialMessage();
                    ResetOrStartHearbeatTimer();
                }
                else if (type == "/system/heartbeat")
                {
                    HandleHeartBeat(messageObj["content"]);
                }
                else if (type == "raw")
                {
                    var wrapper = messageObj["content"].ToObject <DeviceActionWrapper>();
                    DeviceJsonApi.DoDeviceAction(wrapper);
                }
                else if (type == "close")
                {
                    Debug.Console(1, this, "Received close message from server.");
                    // DisconnectWebsocketClient();

                    if (ServerHeartbeatCheckTimer != null)
                    {
                        ServerHeartbeatCheckTimer.Stop();
                    }
                }
                else
                {
                    // Check path against Action dictionary
                    if (ActionDictionary.ContainsKey(type))
                    {
                        var action = ActionDictionary[type];

                        if (action is Action)
                        {
                            (action as Action)();
                        }
                        else if (action is PressAndHoldAction)
                        {
                            var stateString = messageObj["content"]["state"].Value <string>();

                            // Look for a button press event
                            if (!string.IsNullOrEmpty(stateString))
                            {
                                switch (stateString)
                                {
                                case "true":
                                {
                                    if (!PushedActions.ContainsKey(type))
                                    {
                                        PushedActions.Add(type, new CTimer(o =>
                                            {
                                                (action as PressAndHoldAction)(false);
                                                PushedActions.Remove(type);
                                            }, null, ButtonHeartbeatInterval, ButtonHeartbeatInterval));
                                    }
                                    // Maybe add an else to reset the timer
                                    break;
                                }

                                case "held":
                                {
                                    if (PushedActions.ContainsKey(type))
                                    {
                                        PushedActions[type].Reset(ButtonHeartbeatInterval, ButtonHeartbeatInterval);
                                    }
                                    return;
                                }

                                case "false":
                                {
                                    if (PushedActions.ContainsKey(type))
                                    {
                                        PushedActions[type].Stop();
                                        PushedActions.Remove(type);
                                    }
                                    break;
                                }
                                }

                                (action as PressAndHoldAction)(stateString == "true");
                            }
                        }
                        else if (action is Action <bool> )
                        {
                            var stateString = messageObj["content"]["state"].Value <string>();

                            if (!string.IsNullOrEmpty(stateString))
                            {
                                (action as Action <bool>)(stateString == "true");
                            }
                        }
                        else if (action is Action <ushort> )
                        {
                            (action as Action <ushort>)(messageObj["content"]["value"].Value <ushort>());
                        }
                        else if (action is Action <string> )
                        {
                            (action as Action <string>)(messageObj["content"]["value"].Value <string>());
                        }
                        else if (action is Action <SourceSelectMessageContent> )
                        {
                            (action as Action <SourceSelectMessageContent>)(messageObj["content"]
                                                                            .ToObject <SourceSelectMessageContent>());
                        }
                    }
                    else
                    {
                        Debug.Console(1, this, "-- Warning: Incoming message has no registered handler");
                    }
                }
            }
            catch (Exception err)
            {
                //Debug.Console(1, "SseMessageLengthBeforeFailureCount: {0}", SseMessageLengthBeforeFailureCount);
                //SseMessageLengthBeforeFailureCount = 0;
                Debug.Console(1, this, "Unable to parse message: {0}", err);
            }
        }