Ejemplo n.º 1
0
        public LogicalBinaryStateActuator WithActuator(IStateMachine actuator)
        {
            if (actuator == null)
            {
                throw new ArgumentNullException(nameof(actuator));
            }

            actuator.StateChanged += (s, e) =>
            {
                var oldState = _state;
                _state = GetStateInternal();

                if (oldState.Equals(_state))
                {
                    return;
                }

                OnActiveStateChanged(oldState, _state);
            };

            Actuators.Add(actuator);
            _state = GetStateInternal();

            return(this);
        }
Ejemplo n.º 2
0
        protected void OnActiveStateChanged(IComponentState oldState, IComponentState newState)
        {
            _stateLastChanged = DateTime.Now;

            Log.Info($"Component '{Id}' updated state from '{oldState}' to '{newState}'");
            StateChanged?.Invoke(this, new ComponentStateChangedEventArgs(oldState, newState));
        }
Ejemplo n.º 3
0
 private void ThrowIfStateNotSupported(IComponentState stateId)
 {
     if (!GetSupportsState(stateId))
     {
         throw new NotSupportedException($"State '{stateId}' is not supported.");
     }
 }
Ejemplo n.º 4
0
        public override void SetState(IComponentState state, params IHardwareParameter[] parameters)
        {
            if (state == null)
            {
                throw new ArgumentNullException(nameof(state));
            }

            if (state.Equals(_state))
            {
                return;
            }

            if (state.Equals(RollerShutterStateId.Off))
            {
                _endpoint.Stop(parameters);
            }
            else if (state.Equals(RollerShutterStateId.MovingUp))
            {
                _endpoint.StartMoveUp(parameters);
                RestartTracking();
            }
            else if (state.Equals(RollerShutterStateId.MovingDown))
            {
                _endpoint.StartMoveDown(parameters);
                RestartTracking();
            }

            var oldState = _state;

            _state = state;

            OnActiveStateChanged(oldState, _state);
        }
Ejemplo n.º 5
0
 public void Add(IComponentState <TJob> state)
 {
     if (list.Count == capcity)
     {
         list.Dequeue();
     }
     list.Enqueue(state);
 }
Ejemplo n.º 6
0
 public void SaveState <T> (IComponentState <T> state)
 {
     using (var textWriter = new StreamWriter(path, true))
     {
         var csv = new CsvWriter(textWriter);
         csv.WriteRecord(state);
     }
 }
Ejemplo n.º 7
0
        public StateMachineState(IComponentState id)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            Id = id;
        }
        public bool Equals(IComponentState otherState)
        {
            if (ReferenceEquals(otherState, this))
            {
                return(true);
            }

            return(Equals(otherState as StatefulComponentState));
        }
        public StateNotSupportedException(IComponentState state)
            : base($"State '{state}' is not supported.")
        {
            if (state == null)
            {
                throw new ArgumentNullException(nameof(state));
            }

            State = state;
        }
Ejemplo n.º 10
0
        public Frame WithTargetState(IActuator actuator, IComponentState state)
        {
            if (actuator == null)
            {
                throw new ArgumentNullException(nameof(actuator));
            }

            _states.Add(new PendingActuatorState().WithActuator(actuator).WithState(state));
            return(this);
        }
Ejemplo n.º 11
0
 public void SetState(IComponentState state, object obj)
 {
     foreach (var propertyInfo in GetStatePropertyInfo(obj))
     {
         if (!state.ContainsKey(propertyInfo.Name))
         {
             continue;
         }
         var propertyType = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType;
         propertyInfo.SetValue(this, Convert.ChangeType(state[propertyInfo.Name], propertyType), null);
     }
 }
Ejemplo n.º 12
0
        public static bool GetSupportsState(this IComponent component, IComponentState componentState)
        {
            if (componentState == null)
            {
                throw new ArgumentNullException(nameof(componentState));
            }
            if (component == null)
            {
                throw new ArgumentNullException(nameof(component));
            }

            return(component.GetSupportedStates().Any(s => s.Equals(componentState)));
        }
Ejemplo n.º 13
0
        protected void SetState(IComponentState newState)
        {
            if (newState.Equals(_state))
            {
                return;
            }

            var oldValue = _state;

            _state = newState;

            OnActiveStateChanged(oldValue, newState);
        }
        public ComponentIsInStateCondition(IComponent component, IComponentState state)
        {
            if (component == null)
            {
                throw new ArgumentNullException(nameof(component));
            }
            if (state == null)
            {
                throw new ArgumentNullException(nameof(state));
            }

            WithExpression(() => component.GetState().Equals(state));
        }
Ejemplo n.º 15
0
        public void AddSynonymsForComponentState(IComponentState componentState, params string[] synonyms)
        {
            if (componentState == null)
            {
                throw new ArgumentNullException(nameof(componentState));
            }
            if (synonyms == null)
            {
                throw new ArgumentNullException(nameof(synonyms));
            }

            AddSynonyms(_componentStateSynonyms, componentState, synonyms);
            _storage.PersistComponentStateSynonyms(_componentStateSynonyms);
        }
Ejemplo n.º 16
0
 public void GetState(object obj, IComponentState state)
 {
     foreach (var propertyInfo in GetStatePropertyInfo(obj))
     {
         if (state.ContainsKey(propertyInfo.Name))
         {
             state[propertyInfo.Name] = propertyInfo.GetValue(obj);
         }
         else
         {
             state.Add(propertyInfo.Name, propertyInfo.GetValue(obj));
         }
     }
 }
Ejemplo n.º 17
0
        public IComponentState GetNextState(IComponentState baseStateId)
        {
            if (baseStateId.Equals(BinaryStateId.Off))
            {
                return(BinaryStateId.On);
            }

            if (baseStateId.Equals(BinaryStateId.On))
            {
                return(BinaryStateId.Off);
            }

            throw new StateNotSupportedException(baseStateId);
        }
Ejemplo n.º 18
0
        private IStateMachineState GetState(IComponentState id)
        {
            IStateMachineState state = _states.FirstOrDefault(s => s.Id.Equals(id));

            if (state == null && _stateAlias.TryGetValue(id, out id))
            {
                state = _states.FirstOrDefault(s => s.Id.Equals(id));
            }

            if (state == null)
            {
                throw new InvalidOperationException("State machine state is unknown.");
            }

            return(state);
        }
Ejemplo n.º 19
0
        public bool Equals(IComponentState otherState)
        {
            var other = otherState as NumericSensorValue;

            if (other == null)
            {
                return(false);
            }

            if (ReferenceEquals(other, this))
            {
                return(true);
            }

            return(other.Value.Equals(Value));
        }
Ejemplo n.º 20
0
        public bool GetSupportsState(IComponentState stateId)
        {
            if (stateId == null)
            {
                throw new ArgumentNullException(nameof(stateId));
            }

            if (_states.Any(s => s.Id.Equals(stateId)))
            {
                return(true);
            }

            if (!_stateAlias.TryGetValue(stateId, out stateId))
            {
                return(false);
            }

            return(_states.Any(s => s.Id.Equals(stateId)));
        }
Ejemplo n.º 21
0
        public IComponentState GetNextState(IComponentState stateId)
        {
            if (stateId == null)
            {
                throw new ArgumentNullException(nameof(stateId));
            }

            ThrowIfStateNotSupported(stateId);

            IStateMachineState startState = GetState(stateId);

            int indexOfStartState = _states.IndexOf(startState);

            if (indexOfStartState == _states.Count - 1)
            {
                return(_states.First().Id);
            }

            return(_states[indexOfStartState + 1].Id);
        }
Ejemplo n.º 22
0
        public void SaveState <T> (IComponentState <T> state)
        {
            var cmd = connection.CreateCommand();

            cmd.CommandText =
                "insert into {0} (lbs_statusdate,loadballancer_machines_lbm_id," +
                "lbs_cpuussage,lbs_freememory,lbs_freediskspace) " +
                "values ($1,$2,$3,$4,$5)";
            IDbDataParameter param = cmd.CreateParameter();

            param.DbType        = DbType.DateTime;
            param.ParameterName = "1";
            param.Value         = DateTime.UtcNow;
            cmd.Parameters.Add(param);

            param               = cmd.CreateParameter();
            param.DbType        = DbType.UInt64;
            param.ParameterName = "2";
            param.Value         = state.MachineNumber;
            cmd.Parameters.Add(param);

            param               = cmd.CreateParameter();
            param.DbType        = DbType.Double;
            param.ParameterName = "3";
            param.Value         = state.CpuUsages.Average(x => (double)x.Value);
            cmd.Parameters.Add(param);

            param               = cmd.CreateParameter();
            param.DbType        = DbType.UInt64;
            param.ParameterName = "4";
            param.Value         = state.AviableMemory;
            cmd.Parameters.Add(param);

            param               = cmd.CreateParameter();
            param.DbType        = DbType.UInt64;
            param.ParameterName = "5";
            param.Value         = state.FreeDiskSpace;
            cmd.Parameters.Add(param);

            cmd.ExecuteNonQuery();
        }
Ejemplo n.º 23
0
        public override void SetState(IComponentState id, params IHardwareParameter[] parameters)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            ThrowIfNoStatesAvailable();

            IStateMachineState oldState = _activeState;
            IStateMachineState newState = GetState(id);

            if (newState.Id.Equals(_activeState?.Id))
            {
                if (_turnOffIfStateIsAppliedTwice && GetSupportsState(BinaryStateId.Off) && !GetState().Equals(BinaryStateId.Off))
                {
                    SetState(BinaryStateId.Off, parameters);
                    return;
                }

                if (!parameters.Any(p => p is ForceUpdateStateParameter))
                {
                    return;
                }
            }

            oldState?.Deactivate(parameters);
            newState.Activate(parameters);

            if (parameters.Any(p => p is IsPartOfPartialUpdateParameter))
            {
                return;
            }

            _activeState = newState;
            OnActiveStateChanged(oldState, newState);
        }
Ejemplo n.º 24
0
        public override void SetState(IComponentState state, params IHardwareParameter[] parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            ////var animationParameter = parameters.SingleOrDefault(p => p is AnimateParameter) as AnimateParameter;
            ////if (animationParameter != null)
            ////{
            ////    Animate(animationParameter, state);
            ////    return;
            ////}

            foreach (var actuator in Actuators)
            {
                actuator.SetState(state, parameters);
            }

            ////foreach (var actuator in Actuators)
            ////{
            ////    actuator.SetState(state, HardwareParameter.IsPartOfPartialUpdate);
            ////}

            ////bool commit = !parameters.Any(p => p is IsPartOfPartialUpdateParameter);
            ////bool forceUpdate = parameters.Any(p => p is ForceUpdateStateParameter);
            ////if (!commit && !forceUpdate)
            ////{
            ////    return;
            ////}

            ////foreach (var actuator in Actuators)
            ////{
            ////    actuator.SetState(state, parameters);
            ////}
        }
 public DirectionAnimation WithTargetOffState()
 {
     _targetState = BinaryStateId.Off;
     return(this);
 }
 public DirectionAnimation WithTargetState(IComponentState state)
 {
     _targetState = state;
     return(this);
 }
 public ComponentStateChangedEventArgs(IComponentState oldState, IComponentState newState)
 {
     OldState = oldState;
     NewState = newState;
 }
Ejemplo n.º 28
0
 public abstract void SetState(IComponentState state, params IHardwareParameter[] parameters);
Ejemplo n.º 29
0
 public void SetStateIdAlias(IComponentState id, IComponentState alias)
 {
     throw new NotSupportedException();
 }
Ejemplo n.º 30
0
 public void SetStateIdAlias(IComponentState stateId, IComponentState alias)
 {
     _stateAlias[alias] = stateId;
 }