Exemplo n.º 1
0
        public void HandleCircuitChange(bool state, InputPin inputPin)
        {
            LogHelper.LogMessage("state: {0}, inputPin: {1}", state, inputPin);

            var circuit = _mapper.MapInputPinToCircuit(inputPin);

            if (state)
            {
                //when circuit is turned on
                var newRow = new CircuitStateHistoryEntity
                {
                    Circuit        = circuit,
                    TurnOnTime     = _dateProvider.GetUtcDateTimeDate(),
                    TurnedOnLength = 0
                };
                _circuitsDalHelper.InsertHistory(newRow);
            }
            else
            {
                var turnedOnEntry = _circuitsDalHelper.GetLastRowHistoricalState(circuit);
                if (turnedOnEntry != null)
                {
                    var now          = _dateProvider.GetUtcDateTimeDate();
                    var turnedOnTime = (int)Math.Round((now - turnedOnEntry.TurnOnTime).TotalSeconds);
                    turnedOnEntry.TurnedOnLength = turnedOnTime;

                    _circuitsDalHelper.UpdateHistory(turnedOnEntry);
                }
            }

            _circuitsDalHelper.UpdateCircuitState(circuit, state);
            _eventBroadcaster.BroadcastCircuitStateChange(new StateChange {
                Circuit = circuit, State = state
            });
        }