LineWebService.cs 7.3 KB
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
using System;
using System.IO;
using System.Runtime.Serialization;
using Common;

namespace DeviceLibrary
{

    [ServiceContract(Name = "Services")]
    internal interface IWebService
    {
        [OperationContract]
        [WebInvoke(UriTemplate = "CreateEmptyRecycleTask", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Xml)]
        string CreateEmptyRecycleTask(Stream stream);//string line
                                                     //?emptyStation={line}
        [OperationContract]
        [WebInvoke(UriTemplate = "CreateEmptyRecycleTask?emptyStation={line}&rfid={RFID}", Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Xml)]
        string CreateEmptyRecycleTaskGET(string line, string RFID = "");

    }

    [DataContract]
    internal class Result
    {
        [DataMember]
        public bool Succeed { get; set; }

        [DataMember]
        public string ResultData { get; set; }

        [DataMember]
        public string ErrorMessage { get; set; }

    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    internal class ClsWebService : IWebService
    {
        static log4net.ILog Log = log4net.LogManager.GetLogger("LineWebService");
        internal ClsWebService()
        {

        }

        public string CreateEmptyRecycleTask(Stream stream)
        {
            Result res;
            StreamReader sr = new StreamReader(stream);
            string s = sr.ReadToEnd();
            System.Collections.Specialized.NameValueCollection nvc = System.Web.HttpUtility.ParseQueryString(s);
            string emptyStation = nvc["emptyStation"];
            string rfid = nvc["rfid"];
            if (emptyStation == null)
                res = new Result() { Succeed = false, ResultData = null, ErrorMessage = "emptyStation =null " };
            else
            {
                if (emptyStation.Equals("Feeder"))
                {
                    emptyStation = "FeederOut";
                }
                else if (emptyStation.Equals("3CFeeder"))
                {
                    emptyStation = "3CFeederOut";
                }
                if (AGVManager.GetNodeNameByLineName(emptyStation, out string value))
                {
                    if (rfid == null)
                    {
                        res = new Result() { Succeed = false, ResultData = null, ErrorMessage = "rfid=null" };
                        Log.Error(string.Format("Unlock Request(POST) Failed [emptyStation={0},rfid=null]", emptyStation));
                    }
                    else
                    {
                        res = new Result() { Succeed = true, ResultData = rfid, ErrorMessage = "" };
                        Log.Debug(string.Format("Unlock Request(POST) [emptyStation={0},rfid={1}]", emptyStation, rfid.ToUpper()));
                        if (!AGVManager.unlockManager.AddMission(value, rfid.ToUpper()))
                        {
                            if(rfid.Equals(""))
                            {
                                Log.Error(string.Format("添加空架任务失败 节点[{0}] RFID=null", value));
                                res = new Result() { Succeed = false, ResultData = null, ErrorMessage = "CreateEmptyRecycleTask failed: emptyStation=" + emptyStation + " rfid=" + rfid };
                            }
                            else
                            {
                                Log.Error(string.Format("添加空架任务失败 节点[{0}]RFID={1} 重复", value,rfid.ToUpper()));
                                res = new Result() { Succeed = false, ResultData = null, ErrorMessage = "CreateEmptyRecycleTask failed due it has been created: emptyStation=" + emptyStation + " rfid=" + rfid};
                            }
                        }
                        else
                            Log.Info("任务[POST]:" + value + " 出空料架 [location=" + emptyStation + ",rfid=" + rfid.ToUpper() + "]");
                    }
                    // AGVControl.Common.log.Debug("WebService POST Response OK");
                }
                else
                {
                    res = new Result() { Succeed = false, ResultData = null, ErrorMessage = "Not find " + emptyStation };
                    Log.Error("Unlock POST Response false:" + "Not find " + emptyStation);
                }
            }
            return JsonHelper.SerializeObject(res);
        }

        public string CreateEmptyRecycleTaskGET(string line, string RFID)
        {
            Result res;
            if (line.Equals("Feeder"))
            {
                line = "FeederOut";
            }
            else if (line.Equals("3CFeeder"))
            {
                line = "3CFeederOut";
            }
            if (AGVManager.GetNodeNameByLineName(line, out string value))
            {
                Log.Debug(string.Format("Unlock Request(GET) [emptyStation={0},rfid={1}]", line, RFID.ToUpper()));
                if (!AGVManager.unlockManager.AddMission(value,RFID.ToUpper()))
                {
                    Log.Error(string.Format("添加空架任务失败 节点[{0}]RFID={1} 重复", value, RFID.ToUpper()));
                    res = new Result() { Succeed = false, ResultData = null, ErrorMessage = "CreateEmptyRecycleTask failed due it has been created: emptyStation=" + line + " rfid=" + RFID };
                }          
                else
                {
                    res = new Result() { Succeed = true, ResultData = RFID, ErrorMessage = "" };
                    Log.Info("任务[GET]:" + value + " 出空料架 [emptyStation=" + line + ",rfid=" + RFID.ToUpper() + "]");
                }
            }
            else
            {
                res = new Result() { Succeed = false, ResultData = null, ErrorMessage = "Not find " + line };
                Log.Error("Unlock GET Response false " + "Not find " + line);
            }
            //Log.Info(string.Format("WebService GET Request emptyStation={0},rfid={1}", line, RFID));
            return JsonHelper.SerializeObject(res);
        }
    }

    public class WebService
    {
        private WebServiceHost _serviceHost;

        public void Open(string url)
        {
            try
            {
                ClsWebService service = new ClsWebService();
                _serviceHost = new WebServiceHost(service, new Uri(url));//service, new Uri(url)
                _serviceHost.Open();
                LogUtil.info("Web服务已开启");
            }
            catch (Exception ex)
            {
                LogUtil.error("Web服务开启", ex);
            }
        }

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

        }

    }



}