using ProjectBase.Data.Logs;
using SIASUN.Autopilot.Device;
using SIASUN.Autopilot.Device.Command;
using SIASUN.Autopilot.Device.Message;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIASUN.Autopilot.BLL
{
    public class BlCanBusPerception 
    {
        public static BlCanBusPerception Instance { get; private set; }



        /// <summary>
        /// 车辆状态1事件
        /// </summary>
        private VehicleStatus1 comVehicleStatus1;

        /// <summary>
        /// 车辆状态2事件
        /// </summary>
        private VehicleStatus2 comVehicleStatus2;

        /// <summary> 
        /// 车辆状态3事件
        /// </summary>
        private VehicleStatus3 comVehicleStatus3;

        // 实际车速
        private long realSpeed = 0;
        // 获取当前实际速度
        public long GetRealSpeed  
        {
            get { return realSpeed; }
        }

        // 实际角度
        private long realAng = 0;
        // 获取当前实际角度
        public long GetRealAng 
        {
            get { return realAng; }
        }

        static BlCanBusPerception()
        {
            if (Instance != null)
                return;
            Instance = new BlCanBusPerception();
            CanFactory.Instance.Initialize();
        }

        public void Init()
        {
            comVehicleStatus1 = new VehicleStatus1();
            comVehicleStatus1.ReceiveMessage += ComVehicleStatus1_ReceiveMessage; 
            comVehicleStatus1.Subscribe();

            comVehicleStatus2 = new VehicleStatus2();
            comVehicleStatus2.ReceiveMessage += ComVehicleStatus2_ReceiveMessage;
            comVehicleStatus2.Subscribe();

            comVehicleStatus3 = new VehicleStatus3();
            comVehicleStatus3.ReceiveMessage += ComVehicleStatus3_ReceiveMessage;
            comVehicleStatus3.Subscribe();
        }

       

        /// <summary>
        /// 车辆状态1响应函数
        /// </summary>
        /// <param name="e"></param>
        private void ComVehicleStatus1_ReceiveMessage(ReceiveMessageEventArgs e)
        {
            try
            {
                var m = e.Message as 车辆状态1;
            }
            catch (Exception ex)
            {
                LogHelper.log.Error("接收到车辆状态1的数据感知层出现错误",ex);
            }
        }

        /// <summary>
        /// 车辆状态2响应函数
        /// </summary>
        /// <param name="e"></param>
        private void ComVehicleStatus2_ReceiveMessage(ReceiveMessageEventArgs e)
        {
            try
            {
                var m = e.Message as 车辆状态2;
                realSpeed = Convert.ToInt64(m.实际车速);
                realAng = Convert.ToInt64(m.实际方向盘转角);
            }
            catch (Exception ex)
            {
                LogHelper.log.Error("接收到车辆状态2的数据感知层出现错误", ex);
            }       
        }

        /// <summary>
        /// 车辆状态3响应函数
        /// </summary>
        /// <param name="e"></param>
        private void ComVehicleStatus3_ReceiveMessage(ReceiveMessageEventArgs e)
        {
            try
            {
                var m = e.Message as 车辆状态3;
            }
            catch (Exception ex)
            {
                LogHelper.log.Error("接收到车辆状态3的数据感知层出现错误", ex);
            }
        }



        /// <summary>
        /// 输入加速度，调用加速度接口
        /// </summary>
        /// <param name="accelerate">计算的加速度</param>
        public void SetAccelerate(double accelerate)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="angle"></param>
        public void Angle(double angle)
        {

        }
        // 制动
        public void Brake(double brake)
        {

        }
    }
}
