using CatLib;
using Highway.Assemble.common;
using Highway.Assemble.Common;
using Highway.Assemble.EquipClient;
using Highway.Assemble.Sql;
using log4net;
using Mesnac.DeviceAdapter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Rpc;
using TouchSocket.WebApi;

namespace HighWayAssemble.Protocol
{
    [Router("/[api]/[action]")]
    public class ApiServer : SingletonRpcServer
    {
        IEquipClient m_EquipClient = null;
        ISql Sql = null;
        public ApiServer()
        {
            m_EquipClient = App.Make<IEquipClient>();
            Sql = App.Make<ISql>(System.Configuration.ConfigurationManager.AppSettings["SqlString"]);
        }

        [WebApi(Method = HttpMethodType.Post)]
        public async Task<ApiResponse<MesParaData>> RFIDcontrol(MesParaData mesParaData)
        {

            return new ApiResponse<MesParaData>
            {
                Code = 200,
                Data = null,
                Msg = "sucess",
            };
        }
        [WebApi(Method = HttpMethodType.Post)]
        public async Task<RFIDDeviceApiResponse<RFIDDeviceInfo>> basedata(string Requesttime)
        {
           List<RFIDDeviceInfo> rFIDDeviceInfos =  Sql.GetRFIDEquipList();
            return new RFIDDeviceApiResponse<RFIDDeviceInfo>
            {
                Code = 200,
                Data = rFIDDeviceInfos,
                Msg = "sucess",
            };
        }

        /// <summary>
        /// MES主动请求读取标签
        /// </summary>
        /// <param name="EQUID"></param>
        /// <param name="requesttime"></param>
        /// <returns></returns>
        [WebApi(Method = HttpMethodType.Post)]
        public async Task<ApiResponse<MesReadRespon>> readAsync(MesReadPara mesReadPara)
        {
            FrmDisplayView.LogInfo.InfoFormat("接收MES主动读取指令{0},{1}",mesReadPara.EQUID ,mesReadPara.requesttime);
            MesReadRespon mesReadRespon = new MesReadRespon();
            List<TagInfo> rfid = null;
            m_EquipClient.ReadRFID(Int32.Parse(mesReadPara.EQUID), out rfid);
            if (rfid != null && rfid.Count > 0)
            {
                if (rfid.Count > 1)
                {
                    if (rfid != null && rfid.Any())
                    {
                        mesReadRespon.EPCID = string.Join(",", rfid.Select(item => item.EPCstring));
                    }
                }
                else
                {
                    mesReadRespon.EPCID = rfid[0].EPCstring;
                }
               
                mesReadRespon.Respontime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }
            return new ApiResponse<MesReadRespon>
            {
                Code = 200,
                Data = mesReadRespon,
                Msg = "sucess",
            };
        }


        /// <summary>
        /// MES主动回复处理结果，控制报警灯
        /// </summary>
        /// <param name="EQUID"></param>
        /// <param name="requesttime"></param>
        /// <returns></returns>
        [WebApi(Method = HttpMethodType.Post)]
        public async Task<ApiResponse<MesParaData>> revresult(MesAlarmLightPara mesAlarmLightPara)
        { 
            FrmDisplayView.LogInfo.InfoFormat("接收MES控制报警灯指令{0},{1}", mesAlarmLightPara.EQUID, mesAlarmLightPara.Sendtime);

            m_EquipClient.WriteAlarmLight(Int32.Parse(mesAlarmLightPara.EQUID), mesAlarmLightPara.Code);
            return new ApiResponse<MesParaData>
            {
                Code = 200,
                Data = null,
                Msg = "sucess",
            };
        }
    }
}
