public static RoasterSettings FromPacket(Packet packet)
        {
            var settings = new RoasterSettings();

            settings.FanSpeed     = (int)packet.FanSpeed[0];
            settings.TimerMinutes = ((int)packet.Timer[0] / 10f);

            var _tmpHeatLevel = (int)packet.HeatLevel[0];

            switch (_tmpHeatLevel)
            {
            case 0:
                settings.HeatLevel = HeatLevel.OFF;
                break;

            case 1:
                settings.HeatLevel = HeatLevel.LOW;
                break;

            case 2:
                settings.HeatLevel = HeatLevel.MEDIUM;
                break;

            case 3:
                settings.HeatLevel = HeatLevel.HIGH;
                break;
            }

            if (packet.Control[0] == 0x02 && packet.Control[1] == 0x01)
            {
                settings.RunningState = RunningState.IDLE;
            }
            if (packet.Control[0] == 0x04 && packet.Control[1] == 0x02)
            {
                settings.RunningState = RunningState.ROASTING;
            }
            if (packet.Control[0] == 0x04 && packet.Control[1] == 0x04)
            {
                settings.RunningState = RunningState.COOLING;
            }
            if (packet.Control[0] == 0x08 && packet.Control[1] == 0x01)
            {
                settings.RunningState = RunningState.STOPPED;
            }

            return(settings);
        }
Example #2
0
        private void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (!_serialPort.IsOpen)
            {
                return;
            }
            byte[] buffer = new byte[_serialPort.BytesToRead];
            _serialPort.Read(buffer, 0, buffer.Length);

            if (buffer.Length == 14)
            {
                var packet   = Packet.FromByteArray(buffer);
                var settings = RoasterSettings.FromPacket(packet);
                if (this._state == InternalState.INITIALIZED)
                {
                    this.ActiveSettings = settings;
                    this.Temperature    = packet.TemperatureValue();
                    OnDataReceived(new DataReceivedEventArgs {
                        Temperature = this.Temperature, Settings = this.Settings
                    });
                }

                if (this._state == InternalState.INITIALIZING)
                {
                    this.ActiveSettings = settings;
                    this._state         = InternalState.RECEIVING_RECIPE;
                }

                if (this._state == InternalState.RECEIVING_RECIPE)
                {
                    this.CurrentRecipe.Add(settings);
                    if (buffer[4] == 0xAF)
                    {
                        this._state = InternalState.INITIALIZED;
                    }
                }

                //Console.WriteLine(packet.ToString() + ": -- packet");
            }
            string dat = BitConverter.ToString(buffer).Replace("-", "").ToUpper();

            //Console.WriteLine(dat + ":" + DateTime.Now.Ticks);
        }