private void ProcessMessageFromThermostat(string receivedMessage)
        {
            HeatmiserState state = new HeatmiserState();

            byte[] dcbExtracted = new byte[292];
            Array.Copy(Encoding.ASCII.GetBytes(receivedMessage), 7, dcbExtracted, 0, 292);

            state.SetFromDCB(dcbExtracted);
            this.currentState = state;
        }
        public HeatmiserState PostData(HeatmiserState state)
        {
            HeatmiserState stat = null;

            if (state.GetChangedValues().Count > 0)
            {
                foreach (HeatmiserStateValueChange stateChange in state.GetChangedValues())
                {
                    stat = SendMessageToThermostat(WriteDCBCommand(stateChange));
                }
            }
            return(stat);
        }
        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            HeatmiserState state = RequestData();

            if (logState)
            {
                PostLogToDatabase(state);
            }

            if (state != null)
            {
                ResetReceiverIfPerceivedHanging();
            }
        }
        public HeatmiserState RequestData()
        {
            HeatmiserState state = SendMessageToThermostat(ReadDCBCommand());

            if (state != null)
            {
                this.currentState = state;
                if (StateChanged != null)
                {
                    StateChanged(this, new DeviceStateChangedEventArgs(this.currentState));
                }
            }
            return(currentState);
        }
        public HeatmiserState SendMessageToThermostat(byte[] message)
        {
            byte[] recBuffer = SocketsHelper.SendMessageToSocketAndReadSynchroniously(this.localEndPoint, this.targetEndPoint,
                                                                                      message, ProtocolType.Tcp, false);

            if (recBuffer != null)
            {
                HeatmiserState state        = new HeatmiserState();
                byte[]         dcbExtracted = new byte[292];
                Array.Copy(recBuffer, 7, dcbExtracted, 0, 292);

                state.SetFromDCB(dcbExtracted);

                return(state);
            }
            return(null);
        }
        public void ResetReceiverIfPerceivedHanging()
        {
            HeatmiserState currentState = CurrentState;

            byte currentTemp = (byte)(currentState.CurrentAirTemp / 10);
            byte setRoomTemp = currentState.IsOnHoliday ? currentState.FrostProtectTemperature : currentState.SetRoomTemp;

            if (Math.Abs(currentTemp - setRoomTemp) > 1)
            {
                DateTime pastReadingDate         = DateTime.Now.AddMinutes(currentState.RateOfChange * -1);
                List <HeatmiserTempReading> list = GetListOfReadings(pastReadingDate);

                if (list != null)
                {
                    bool heatingThroughout = true;
                    bool idleThroughout    = true;
                    foreach (HeatmiserTempReading reading in list)
                    {
                        if (!reading.IsHeating)
                        {
                            heatingThroughout = false;
                        }
                        else
                        {
                            idleThroughout = false;
                        }
                    }

                    if (
                        (heatingThroughout &&
                         (currentState.CurrentAirTemp - list[0].CurrentAirTemp) < 7)
                        ||
                        (idleThroughout &&
                         (currentState.CurrentAirTemp - list[0].CurrentAirTemp) > 7)
                        )
                    {
                        ResetHangingReceiver();
                    }
                }
            }
        }
        private void PostLogToDatabase(HeatmiserState state)
        {
            try {
                SqlConnection connection = new SqlConnection(LocalSettings.SqlConnectionString);
                connection.Open();

                SqlCommand cmd = new SqlCommand("PostHeatmiserLog", connection);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@DateTime", SqlDbType.DateTime).Value       = DateTime.Now;
                cmd.Parameters.Add("@RateOfChange", SqlDbType.TinyInt).Value    = state.RateOfChange;
                cmd.Parameters.Add("@SetRoomTemp", SqlDbType.TinyInt).Value     = state.SetRoomTemp;
                cmd.Parameters.Add("@RunMode", SqlDbType.TinyInt).Value         = state.RunMode;
                cmd.Parameters.Add("@CurrentAirTemp", SqlDbType.SmallInt).Value = state.CurrentAirTemp;
                cmd.Parameters.Add("@IsHeating", SqlDbType.Bit).Value           = state.IsHeating;
                cmd.Parameters.Add("@IsHotWater", SqlDbType.Bit).Value          = state.IsHotWater;
                cmd.ExecuteNonQuery();

                connection.Close();
            }
            catch (Exception ex) {
                LoggingHelper.WriteExceptionLogEntry(ex.Source, ex);
            }
        }