using ProjectManagementSystem.Device.Extenions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManagementSystem.Device.CommandCallback
{
    public class InfoAgv1064 : InfoAgv
    {
        public override void Deserialize(byte[] data)
        {
            base.Deserialize(data);

            AgvId = data[2];
            Mark = Convert.ToInt32(data[4] | ((data[3] & 0x1F) * 256));
            Route = Convert.ToInt32(data[9] | ((data[8] & 0x1F) * 256));

            FPS = (int)((data[3] & 0xE0) >> 5);

            switch (FPS)
            {
                case 1:
                    {
                        Status = (((data[8] & 0xE0) >> 5) + 0x40);
                        Alarm = data[6];

                        StatusText = Status.GetAgvStatusText();
                        AlarmText = Alarm.GetAgvAlarmText();

                        int SpeedGrade = (int)data[7];
                        if (SpeedGrade > 0 && SpeedGrade < 6)
                        {
                            SpeedLevel = SpeedGrade;
                        }
                    }
                    break;
                case 2:
                    {
                        int BatteryEleInt = (int)(data[5] << 8 | data[6]);
                        BatteyCurrent = ((decimal)BatteryEleInt) / 100;
                        BatteyPower = data[7];
                    }
                    break;
                case 3:
                    {
                        int BatteryVolInt = (int)((data[5] << 4) | (data[6] >> 4));
                        BatteyVoltage = ((decimal)BatteryVolInt) / 100;

                        if (BatteyVoltage > 0)
                        {
                            //BatteyVoltage += 40.96m;
                        }

                        int BatteryTemInt = (int)(((data[6] & 0x0F) << 8) | (data[7]));
                        BatteyTemperature = ((decimal)BatteryTemInt) / 100;
                    }
                    break;
                default:
                    break;
            }

            LastUpdateTime = DateTime.Now;
        }
    }
}
