LineWebService.cs 7.9 KB
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Runtime.Serialization;
using System.ServiceModel.Activation;
using System;
using AGVControl;
using System.IO;
using System.Xml;
using System.Text;

namespace BLL
{

    [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("4CFeeder"))
                {
                    emptyStation = "4CFeederOut";
                }
                if (Common.GetNodeNameByLineName(emptyStation, out string value))
                {
                    if (!Common.AddEmptyShelfTask(value))
                    {
                        Common.log.Error("CreateEmptyRecycleTask(POST) 节点[" + value + "]不存在");
                        res = new Result() { Succeed = false, ResultData = null, ErrorMessage = "CreateEmptyRecycleTask failed: " + emptyStation };
                    }
                    else
                    {
                        if (rfid == null)
                        {
                            res = new Result() { Succeed = true, ResultData = null, ErrorMessage = "" };
                            Log.Info(string.Format("WebService Request(POST) emptyStation={0},rfid=null", emptyStation));
                            Common.LogInfo("任务[POST]:" + value + " 出空料架 [" + emptyStation + "]");
                        }
                        else
                        {
                            res = new Result() { Succeed = true, ResultData = rfid, ErrorMessage = "" };
                            Log.Info(string.Format("WebService Request(POST) emptyStation={0},rfid={1}", emptyStation, rfid));
                            Common.LogInfo("任务[POST]:" + value + " 出空料架 [location=" + emptyStation + ",rfid=" + rfid + "]");
                        }
                    }
                   // AGVControl.Common.log.Debug("WebService POST Response OK");
                }
                else
                {
                    res = new Result() { Succeed = false, ResultData = null, ErrorMessage = "Not find " + emptyStation };
                    AGVControl.Common.log.Error("WebService 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("4CFeeder"))
            {
                line = "4CFeederOut";
            }
            if (Common.GetNodeNameByLineName(line, out string value))
            {

                if (!Common.AddEmptyShelfTask(value))
                {
                    Common.log.Error("CreateEmptyRecycleTask 节点[" + value + "]不存在");
                    res = new Result() { Succeed = false, ResultData = null, ErrorMessage = "CreateEmptyRecycleTask failed: " + line };
                }
                else
                {
                    res = new Result() { Succeed = true, ResultData = RFID, ErrorMessage = "" };
                    Log.Info(string.Format("WebService Request(GET) emptyStation={0},rfid={1}", line, RFID));
                    Common.LogInfo("任务[GET]:" + value + " 出空料架 [emptyStation=" + line + ",rfid="+RFID+"]");
                }
            }
            else
            {
                res = new Result() { Succeed = false, ResultData = null, ErrorMessage = "Not find " + line };
                AGVControl.Common.LogInfo("WebService 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();
                AGVControl.Common.LogInfo("Web服务已开启");
            }
            catch (Exception ex)
            {
                AGVControl.Common.log.Error("Open", ex);
            }
        }

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

        }

        //public void RecycleTask(string lineName)
        //{
        //    if (AGVControl.Common.agvProductionLine.TryGetValue(lineName, out string value))
        //    {
        //        AGVControl.Common.LogInfo("WebService Response OK");

        //        //加到任务
        //        //int idx = AGVControl.Common.nodeInfo.FindIndex(s => s.Name == value);
        //        //if (idx > -1)
        //        //    AGVControl.Common.linePlace.Add(value);
        //        //else
        //        //    AGVControl.Common.log.Error("CreateEmptyRecycleTask " + value + "不存在");
        //        if (!Common.AddLinePlace(value))
        //            Common.log.Error("CreateEmptyRecycleTask 节点【" + value + "】不存在");
        //        else
        //        {
        //            Common.LogInfo("任务:" + value + " 出空料架 【" + lineName + "】");
        //        }
        //        //System.IO.File.WriteAllLines(Common.CONFIG_PATH + "LinePlace.txt", Common.linePlace);
        //    }
        //    else
        //    {
        //        AGVControl.Common.LogInfo("WebService Response false");
        //    }
        //}
    }



}