WebService.cs 8.0 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}", Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Xml)]
        string CreateEmptyRecycleTaskGET(string line);

    }

    [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
    {
        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"];
            if(emptyStation ==null)
                res = new Result() { Succeed = false, ResultData = null, ErrorMessage = "emptyStation =null "};
            else
            {
                if(emptyStation.Equals("Feeder"))
                {
                    emptyStation = "FeederOut";
                }
                AGVControl.Common.LogInfo("WebService Request emptyStation=" + emptyStation,false);
            }
            if (AGVControl.Common.agvProductionLine.TryGetValue(emptyStation, out string value))
            {
                res = new Result() { Succeed = true, ResultData = null, ErrorMessage = "" };
                AGVControl.Common.log.Debug("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 + " 出空料架 【" + emptyStation + "】");
                }
                //System.IO.File.WriteAllLines(Common.CONFIG_PATH + "LinePlace.txt", Common.linePlace);
            }
            else
            {
                res = new Result() { Succeed = false, ResultData = null, ErrorMessage = "Not find " + emptyStation };
                AGVControl.Common.LogInfo("WebService Response false "+ "Not find " + emptyStation);
            }
           
            return JsonHelper.SerializeObject(res);
        }

        public string CreateEmptyRecycleTaskGET(string line)
        {
            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"];
            if (line.Equals("Feeder"))
            {
                line = "FeederOut";
            }
            else if (line.Equals("4CFeeder"))
            {
                line = "4CFeederOut";
            }
            AGVControl.Common.LogInfo("WebService Request emptyStation=" + line,false);
            //if (line == null)
            //    res = new Result() { Succeed = "false", ResultData = null, ErrorMessage = "line =null " };
            if (AGVControl.Common.agvProductionLine.TryGetValue(line, out string value))
            {
                res = new Result() { Succeed = true, ResultData = null, ErrorMessage = "" };
                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 + " 出空料架 【" + line + "】");
                }
                //System.IO.File.WriteAllLines(Common.CONFIG_PATH + "LinePlace.txt", Common.linePlace);
            }
            else
            {
                res = new Result() { Succeed = false, ResultData = null, ErrorMessage = "Not find " + line };
                AGVControl.Common.LogInfo("WebService Response false " + "Not find " + line);
            }

            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.State != CommunicationState.Closed)//判断服务是否关闭
                {
                    _serviceHost.Close();//关闭服务
                }
                //_serviceHost.Close();
                AGVControl.Common.LogInfo("Web服务已关闭");
            }
            catch (Exception ex)
            {
                AGVControl.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");
        //    }
        //}
    }



}