using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Message;
namespace UnNomalized_node.communication
{
    class unnormalized_communication :Communication_socket_base
    {
        /// <summary>
        /// 单例
        /// </summary>
        class Nested
        {
            // 创建模板类实例，参数2设为true表示支持私有构造函数
            internal static readonly unnormalized_communication instance = Activator.CreateInstance(typeof(unnormalized_communication), true) as unnormalized_communication;
        }
        private static unnormalized_communication instance = null;
        public static unnormalized_communication Instance
        {
            get
            {
                return Nested.instance;
            }
        }

        private unnormalized_communication()
        {
           
        }
        public void unnormalized_communication_init()
        {
            base.communication_init();
        }
        public void unnormalized_communication_uninit()
        {
            base.communication_uninit();
        }
        //重载检查消息--检查是否给终端的消息
        public override bool check_msg(byte[] msg)
        {
            return true;
            //Message.Base_info base_Info = Message.Base_msg.Parser.ParseFrom(msg).BaseInfo;
            //if (base_Info.Receiver == Message.Communicator.ETerminor ||
            //    base_Info.Receiver == Message.Communicator.EEmpty||
            //    base_Info.Receiver == Message.Communicator.EMain)//消息类型为空一般表示状态消息
            //{
            //    return true;
            //}
            //else
            //{
            //    //无效的消息
            //    return false;
            //}
        }
        //重载检查执行者状态
        public override bool check_executer(byte[] msg)
        {
            return true;
        }
        //重载处理消息
        public override void execute_msg(byte[] msg)
        {
            Message.Message_type type = Message.Base_msg.Parser.ParseFrom(msg).BaseInfo.MsgType;
            switch (type)
            {
                //雷达状态消息
                case Message.Message_type.EGroundStatusMsg:
                    {
                        Message.Ground_status_msg t_ground_status_msg = Message.Ground_status_msg.Parser.ParseFrom(msg);
                        communication_data.Instance.lidar_statu = t_ground_status_msg;

                        /*if (t_ground_status_msg.IdStruct.TerminalId == communication_data.Instance.ID)
                        {
                            Console.WriteLine("终端："+ communication_data.Instance.ID.ToString()+", " +
                                t_ground_status_msg.GroundStatus.ToString() +
                                "," + DateTime.Now.ToString());
                        }*/
                    }
                    break;
                //车位状态消息
                case Message.Message_type.EParkspaceAllocationStatusMsg:
                    {
                        Message.Parkspace_allocation_status_msg t_parkspace_status_msg = Message.Parkspace_allocation_status_msg.Parser.ParseFrom(msg);
                        communication_data.Instance.parkspace_statu = t_parkspace_status_msg;
                    }
                    break;
                case Message.Message_type.ETerminalStatusMsg:
                    {
                        Message.terminal_status_msg t_terminal_statu_msg = Message.terminal_status_msg.Parser.ParseFrom(msg);
                        communication_data.Instance.terminal_statu = t_terminal_statu_msg;

                        /*if (t_terminal_statu_msg.IdStruct.TerminalId == communication_data.Instance.ID)
                            Console.WriteLine("终端：" + communication_data.Instance.ID.ToString() + ", " 
                                + t_terminal_statu_msg.SinglechipData.OverBorderStatus+
                                ","+ t_terminal_statu_msg.SinglechipData.InsideExistenceFlag.ToString()
                                +","+DateTime.Now.ToString());*/
                    }
                    break;
                default:
                    break;
            }
        }
        //封装发送消息
        public override void encapsulate_msg(byte[] msg)
        {
            base.encapsulate_msg(msg);
        }
    }
}
