Services.cs 6.2 KB
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
using System;
using System.IO;
using System.Runtime.Serialization;
using Common;
using DeviceLibrary.Models.Service.Request;

namespace DeviceLibrary.Service
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    internal class WebService : IService
    {
        static log4net.ILog Log = log4net.LogManager.GetLogger("Service");

        public Result Leave(Stream stream)
        {
            Result result = new Result();
            StreamReader sr = new StreamReader(stream);
            string s = sr.ReadToEnd();
            DoorInfo doorInfo = JsonHelper.DeserializeJsonToObject<DoorInfo>(s);
            if(doorInfo==null)
            {
                result.code = -1;
                result.msg = $"解析异常:{s}";
                Log.Error($"Leave 解析异常:{s}");
            }
            else
            {
                Context.LiftContext.AGVLeave(doorInfo,out string msg);
                Log.Info($"AGVLeave接口被调用:{doorInfo},rtn_msg={msg}");
                result.msg = msg;
            }
            return result;
        }

        public Result Call(Stream stream)
        {
            Result result = new Result();
            StreamReader sr = new StreamReader(stream);
            string s = sr.ReadToEnd();
            DoorInfo doorInfo = JsonHelper.DeserializeJsonToObject<DoorInfo>(s);
            if (doorInfo == null)
            {
                result.code = -1;
                result.msg = $"解析异常:{s}";
                Log.Error($"Call 解析异常:{s}");
            }
            else
            {
                Context.LiftContext.AGVCall(doorInfo, out string msg);
                Log.Info($"AGVCall接口被调用:{doorInfo},rtn_msg={msg}");
                result.msg = msg;
            }
            return result;
        }
        string preLogInfo = "";
        string curLogInfo = "";
        void LogInfo(string txt)
        {
            curLogInfo = txt;
            if(!curLogInfo.Equals(preLogInfo))
            {
                Log.Info(curLogInfo);
                preLogInfo = curLogInfo;
            }
        }
        public Result sendIn(Stream stream)
        {
            Result result = new Result();
            StreamReader sr = new StreamReader(stream);
            string s = sr.ReadToEnd();
            SendInInfo sendInInfo = JsonHelper.DeserializeJsonToObject<SendInInfo>(s);
            if(sendInInfo ==null)
            {
                result.code = -1;
                result.msg = $"解析异常:{s}";
                Log.Error($"sendIn 解析异常:{s}");
            }
            else
            {
                bool idle= Context.LiftContext.RequestSendIn(sendInInfo,out string msg);
                LogInfo($"请求接口被调用:{sendInInfo},[{msg}]");
                result.msg = msg;
            }
            return result;
        }

        //public Result status(Stream stream)
        //{
        //    Result result = new Result();
        //    StreamReader sr = new StreamReader(stream);
        //    string s = sr.ReadToEnd();
        //    result.data = Common.JsonHelper.SerializeObject(new Models.Service.Response.Status());
        //    return result;
        //}

        public StatusResult status(Stream stream)
        {
            StatusResult result = new StatusResult();
            StreamReader sr = new StreamReader(stream);
            string s = sr.ReadToEnd();
            ClientStatus clientStatus = JsonHelper.DeserializeJsonToObject<ClientStatus>(s);
            if(clientStatus==null)
            {
                result.code = -1;
                result.msg = $"解析异常:{s}";
                Log.Error($"status 解析异常:{s}");
            }
            else
            {
                string msg = "";
                Context.LiftContext.UpdateClient(clientStatus,out msg);
                result.data = Context.LiftContext.GetLiftStatus(clientStatus,out msg);
                result.msg = msg;
            }
            return result;
        }

        public Result finishedProdcut(Stream stream)
        {
            Result result = new Result();
            StreamReader sr = new StreamReader(stream);
            string s = sr.ReadToEnd();
            FinishedInfo info = JsonHelper.DeserializeJsonToObject<FinishedInfo>(s);
            if (info == null)
            {
                result.code = -1;
                result.msg = $"解析异常:{s}";
                Log.Error($"sendIn 解析异常:{s}");
            }
            else
            {
                Context.LiftContext.UpdateFinishedInfo(info, out string msg);
                LogInfo($"成品入库状态接口被调用:{info},[{msg}]");
                result.msg = msg;
            }
            return result;
        }
    }

    public class LiftService
    {
        private WebServiceHost serviceHost;
        public bool State = false;
        public void Open(string baseUrl)
        {
            try
            {
                if (State)
                {
                    LogUtil.error("Web Service has been opened");
                    return;
                }
                WebService service = new WebService();
                serviceHost = new WebServiceHost(service, new Uri(baseUrl));//service, new Uri(url)
                serviceHost.Open();
                State = true;
                LogUtil.info("Web Service Opened.");
            }
            catch (Exception ex)
            {
                State = false;
                LogUtil.error("Web Service Open", ex);
            }
        }

        public void Close()
        {
            try
            {
                if (serviceHost != null)//判断服务是否关闭
                    serviceHost.Close();//关闭服务
                State = false;
                LogUtil.info("Web Service Closed.");
            }
            catch (Exception ex)
            {
                LogUtil.error("Web Service Close", ex);
            }

        }

    }



}