using Message;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Communication
{
    class GroundDIsplayCommuncator : Communication.Communicator
    {
        protected GroundDIsplayCommuncator():base()
        {
        }

        /// <summary>
        /// 单例访问
        /// </summary>
        public new static Communicator GetInstance()
        {
            if (instance == null)
            {
                lock (lockObj)
                {
                    if (instance == null)
                    {
                        instance = new GroundDIsplayCommuncator();
                    }
                }
            }
            return instance;
        }

        /// <summary>
        /// 检查消息
        /// </summary>
        /// <param name="header"></param>
        /// <returns></returns>
        public override bool CheckMsg(Base_info header)
        {
            if (header.HasMsgType && header.HasSender && header.HasTimeoutMs
                && header.MsgType == Message_type.EGroundStatusMsg && header.Sender == Message.Communicator.EGroundMeasurer)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 检查执行器状态
        /// </summary>
        /// <param name="header"></param>
        /// <param name="receiveTime"></param>
        /// <returns></returns>
        public override CheckExecuterReturn CheckExecuter(Base_info header, DateTime receiveTime)
        {
            if ((DateTime.Now - receiveTime).Milliseconds > header.TimeoutMs)
                return CheckExecuterReturn.MSG_TIMEOUT;
            else
                return CheckExecuterReturn.EXECUTER_READY;
        }

        /// <summary>
        /// 执行消息
        /// </summary>
        /// <param name="msgStamped"></param>
        /// <returns></returns>
        public override bool ExecuteMsg(MsgStamped msgStamped)
        {
            Ground_status_msg msg = Ground_status_msg.Parser.ParseFrom(msgStamped.msg);
            Console.WriteLine(Ground_status_msg.Parser.ParseJson(msg.ToString()));
            return true;
        }
    }
}
