Esempio n. 1
0
        // Private Methods (1) 

        void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            _timer.Stop();

            if (_onTimeout != null)
            {
                _onTimeout.Invoke();
            }
            if (OnTimeout != null)
            {
                OnTimeout();
            }
        }
Esempio n. 2
0
 private void Update()
 {
     if (Time.time - startTime > time && time > 0)
     {
         if (TimeoutEvent != null)
         {
             TimeoutEvent.Invoke(this, parentWeapon);
         }
         Destroy(this.gameObject, 0.4f);
         if (DestroyEvent != null)
         {
             DestroyEvent.Invoke(this);
         }
     }
 }
Esempio n. 3
0
    // Update is called once per frame
    void Update()
    {
        if (stopTimer == false)
        {
            time -= Time.deltaTime;
        }

        time = Mathf.Max(time, 0f);

        timerText.text = time.ToString("f1");

        if (time <= 0f & timeOut == false)
        {
            TimeoutEvent?.Invoke();
            timeOut = true;
        }
    }
Esempio n. 4
0
        /// <summary>
        /// Callback method for when the client receives a login response from a server connection.
        /// </summary>
        /// <param name="loginResponse">The LoginResponse packet data.</param>
        private void OnConnect(LoginResponse loginResponse)
        {
            Logger.Get().Info(this, "Connection to server success");

            // De-register the connect failed and register the actual timeout handler if we time out
            UpdateManager.OnTimeout -= OnConnectTimedOut;
            UpdateManager.OnTimeout += () => {
                ThreadUtil.RunActionOnMainThread(() => {
                    TimeoutEvent?.Invoke();
                });
            };

            // Invoke callback if it exists on the main thread of Unity
            ThreadUtil.RunActionOnMainThread(() => {
                ConnectEvent?.Invoke(loginResponse);
            });

            IsConnected = true;
        }
Esempio n. 5
0
 protected virtual void onTimeoutEvent()
 {
     TimeoutEvent?.Invoke(this, EventArgs.Empty);
 }
Esempio n. 6
0
        private void SetRoomState(RoomState roomState)
        {
            LogInfoJson(hassEventArgs, data: roomState.ToString());
            var _attributes = SetAttributes(roomState);

            switch (roomState)
            {
            case RoomState.Idle:
                SetRoomStateIdle();
                break;

            case RoomState.Active:
                SetRoomStateActive();
                break;

            case RoomState.Disabled:
                SetRoomStateDisabled();
                break;

            case RoomState.Override:
                SetRoomStateOverride();
                break;

            case RoomState.RandomWait:
                SetRoomStateRandom();
                break;
            }

            void SetTimer(TimeSpan ts, Action action)
            {
                DisposeTimer();
                Timer = _app.RunIn(ts, action);
            }

            void DisposeTimer()
            {
                Timer?.Dispose();
                Timer = null;
            }

            void SetRoomStateRandom()
            {
                var waitDuration = _controller.GetRandomDuration();

                SetRandomOnTimer(waitDuration);
                _attributes = SetAttributes(RoomState.RandomWait, waitDuration);
                _app.SetState(_roomConfig.RoomPresenceEntityId, RoomState.RandomWait.ToString().ToLower(), _attributes);
            }

            void SetRandomOnTimer(int duration)
            {
                SetTimer(TimeSpan.FromMinutes(duration), () =>
                {
                    if (!LuxBelowThreshold())
                    {
                        return;
                    }
                    var onDuration = _controller.GetRandomDuration();

                    TurnOnControlEntities();
                    TurnOffRandomAndReset(onDuration);

                    _attributes = SetAttributes(RoomState.RandomActive, onDuration);
                    _app.SetState(_roomConfig.RoomPresenceEntityId, RoomState.RandomActive.ToString().ToLower(), _attributes);
                });
            }

            void TurnOffRandomAndReset(int duration)
            {
                SetTimer(TimeSpan.FromMinutes(duration), () =>
                {
                    var offDuration = _controller.GetRandomDuration();
                    SetRoomStateIdle();
                    SetRandomOnTimer(offDuration);

                    _attributes = SetAttributes(RoomState.RandomWait, offDuration);
                    _app.SetState(_roomConfig.RoomPresenceEntityId, RoomState.RandomWait.ToString().ToLower(), _attributes);
                });
            }

            void SetRoomStateOverride()
            {
                if (Timer != null && (RoomIs(RoomState.Active) || RoomIs(RoomState.RandomActive)))
                {
                    return;
                }

                SetTimer(_overrideTimeout, () => TimeoutEvent.Invoke(this, new HassEventArgs(_roomConfig.Name, nameof(SetRoomStateOverride))
                {
                    EntityId = _eventEntity
                }));
                _app.SetState(_roomConfig.RoomPresenceEntityId, RoomState.Override.ToString().ToLower(), _attributes);
            }

            void SetRoomStateDisabled()
            {
                DisposeTimer();
                _brightnessTimer?.Dispose();
                _brightnessTimer = null;
                _app.SetState(_roomConfig.RoomPresenceEntityId, RoomState.Disabled.ToString().ToLower(), null);
            }

            void SetRoomStateActive()
            {
                SetTimer(Timeout, () => TimeoutEvent.Invoke(this, new HassEventArgs(_roomConfig.Name, nameof(SetRoomStateActive))
                {
                    EntityId = _eventEntity
                }));
                _app.SetState(_roomConfig.RoomPresenceEntityId, RoomState.Active.ToString().ToLower(), _attributes);
                TurnOnControlEntities();
            }

            void SetRoomStateIdle()
            {
                TurnOffControlEntities();
                _brightnessTimer?.Dispose();
                _brightnessTimer = null;
                DisposeTimer();
                _app.Delay(TimeSpan.FromSeconds(1));
                _app.SetState(_roomConfig.RoomPresenceEntityId, RoomState.Idle.ToString().ToLower(), _attributes);
                EnableCircadian();
            }

            object SetAttributes(RoomState state, int?randomDuration = null)
            {
                string expiryAtt = "";

                if (randomDuration == null)
                {
                    expiryAtt =
                        state == RoomState.Override
                            ? DateTime.Now.AddSeconds(_overrideTimeout.TotalSeconds).ToString("yyyy-MM-dd HH:mm:ss")
                            : Expiry;
                }
                else
                {
                    expiryAtt = DateTime.Now.AddMinutes((int)randomDuration).ToString("yyyy-MM-dd HH:mm:ss");
                }


                var attrs = new
                {
                    EventEntity = _eventEntity,
                    ActiveEntities,
                    PresenceEntityIds = _presenceEntityIds,
                    KeepAliveEntities,
                    ControlEntityIds      = _controlEntityIds,
                    NightControlEntityIds = _nightControlEntityIds,
                    _roomConfig.RandomEntityId,
                    _roomConfig.RandomStates,
                    RandomDuration = randomDuration ?? 0,
                    Expiry         = expiryAtt
                };

                LogVerboseJson(hassEventArgs, data: attrs);
                return(attrs);
            }
        }