using ProjectBase.Data.Logs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIASUN.Autopilot.Device.Common
{
    /// <summary>
    /// 简单命令
    /// </summary>
    public class SimpleCommand<T>
    {
        /// <summary>
        /// 一次发送一个命令
        /// 大于0返回发送的字节数，-1超过最大重发次数；-2发送时出现异常；-3指令被取消（与对方的指令冲突）；-4服务器未连接；
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public int Execute(T message, string id)
        {
            int iRet = 1;
            try
            {
                Package_Data_Msg<T> p = new Package_Data_Msg<T>();
                p.Message = new List<T>() { message };
                byte[] bytes = p.Package();
                CanFactory.Instance.Write(bytes, id);
            }
            catch (Exception ex)
            {
                iRet = 0;
                LogHelper.log.Error("发送数据失败,错误为.",ex);
            }
            LogHelper.log.Info("发送通信命令已完成.");
            return iRet;
        }
    }
}
