FixtureService.cs 11.3 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.manager;
using DeviceLibrary.manager;

namespace DeviceLibrary
{

    [ServiceContract(Name = "FixtureServices")]
    internal interface IWebService
    {
        [OperationContract]
        [WebInvoke(UriTemplate = "warehouse", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Xml)]
        string WarehouseRequestByPost(Stream stream);
        [OperationContract]
        [WebInvoke(UriTemplate = "warehouse?name={name}&targetline={targetline}&type={type}", Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Xml)]
        string WarehouseRequestByGet(string name, string targetline,int type);
        [OperationContract]
        [WebInvoke(UriTemplate = "line", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Xml)]
        string LineRequestByPost(Stream stream);
        [OperationContract]
        [WebInvoke(UriTemplate = "line?name={name}&targetwarehouse={targetwarehouse}&type={type}", Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Xml)]
        string LineRequestByGet(string name, string targetwarehouse, int type);
    }

    [DataContract]
    internal class Result
    {
        /// <summary>
        /// 呼叫成功;false:呼叫失败
        /// </summary>
        [DataMember]
        public bool Succeed { get; set; }
        /// <summary>
        /// 返回对应接口呼叫的参数信息;
        /// </summary>
        [DataMember]
        public string Data { get; set; }
        /// <summary>
        /// 呼叫成功,为空。呼叫失败会返回原因
        /// </summary>
        [DataMember]
        public string Msg { 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("FixtureServices");
        public string LineRequestByGet(string name, string targetwarehouse, int type)
        {
            Result res;
            name = name.ToUpper();
            targetwarehouse = SettingString .Warehouse;
            manager.MissionInfo missionInfo = new manager.MissionInfo(name, (manager.FixType)type, targetwarehouse, manager.MissionType.Line);
            if (!NodeManager.HasNode(name))
            {
                res = new Result() { Succeed = false, Data = $"name:{name},targetwarehouse:{targetwarehouse},type:{type}", Msg = $"节点名{name}不存在" };
                Log.Error("产线呼叫任务失败[GET](节点不存在):" + missionInfo.ToString());

            }
            else if(!NodeManager.HasNode(targetwarehouse))
            {
                res = new Result() { Succeed = false, Data = $"name:{name},targetwarehouse:{targetwarehouse},type:{type}", Msg = $"节点名{targetwarehouse}不存在" };
                Log.Error("产线呼叫任务失败[GET](节点不存在):" + missionInfo.ToString());
            }
            else
            {
                if (!FixMissionManager.Contains(missionInfo))
                {
                    FixMissionManager.Add(missionInfo);
                    res = new Result() { Succeed = true, Data = $"name:{name},targetwarehouse:{targetwarehouse},type:{type}", Msg = "" };
                    Log.Info("产线呼叫任务成功[GET]:" + missionInfo.ToString());
                }
                else
                {
                    res = new Result() { Succeed = false, Data = $"name:{name},targetwarehouse:{targetwarehouse},type:{type}", Msg = "该任务已存在" };
                    Log.Error("产线呼叫任务失败[GET](已存在相同任务):" + missionInfo.ToString());
                }
            }

            return JsonHelper.SerializeObject(res);
        }

        public string LineRequestByPost(Stream stream)
        {
            Result res;
            StreamReader sr = new StreamReader(stream);
            string s = sr.ReadToEnd();
            System.Collections.Specialized.NameValueCollection nvc = System.Web.HttpUtility.ParseQueryString(s);
            string name = nvc["name"].ToUpper();
            string targetwarehouse = SettingString.Warehouse;//nvc["targetwarehouse"].ToUpper();
            int type = int.Parse(nvc["type"]);
            manager.MissionInfo missionInfo = new manager.MissionInfo(name, (manager.FixType)type, targetwarehouse, manager.MissionType.Line);
            if (!NodeManager.HasNode(name))
            {
                res = new Result() { Succeed = false, Data = $"name:{name},targetwarehouse:{targetwarehouse},type:{type}", Msg = $"节点名{name}不存在" };
                Log.Error("产线呼叫任务失败[POST](节点不存在):" + missionInfo.ToString());

            }
            else if (!NodeManager.HasNode(targetwarehouse))
            {
                res = new Result() { Succeed = false, Data = $"name:{name},targetwarehouse:{targetwarehouse},type:{type}", Msg = $"节点名{targetwarehouse}不存在" };
                Log.Error("产线呼叫任务失败[POST](节点不存在):" + missionInfo.ToString());
            }
            else
            {
                if (!FixMissionManager.Contains(missionInfo))
                {
                    FixMissionManager.Add(missionInfo);
                    res = new Result() { Succeed = true, Data = $"name:{name},targetwarehouse:{targetwarehouse},type:{type}", Msg = "" };
                    Log.Info("产线呼叫任务成功[POST]:" + missionInfo.ToString());
                }
                else
                {
                    res = new Result() { Succeed = false, Data = $"name:{name},targetwarehouse:{targetwarehouse},type:{type}", Msg = "该任务已存在" };
                    Log.Error("产线呼叫任务失败[POST](已存在相同任务):" + missionInfo.ToString());
                }
            }

            return JsonHelper.SerializeObject(res);
        }

        public string WarehouseRequestByGet(string name, string targetline, int type)
        {
            Result res;
            name = SettingString.Warehouse;//name.ToUpper();
            targetline = targetline.ToUpper();
            manager.MissionInfo missionInfo = new manager.MissionInfo(name, (manager.FixType)type, targetline, manager.MissionType.Warehouse);
            if (!NodeManager.HasNode(name))
            {
                res = new Result() { Succeed = false, Data = $"name:{name},targetwarehouse:{targetline},type:{type}", Msg = $"节点名{name}不存在" };
                Log.Error("立库呼叫任务失败[GET](节点不存在):" + missionInfo.ToString());

            }
            else if (!NodeManager.HasNode(targetline))
            {
                res = new Result() { Succeed = false, Data = $"name:{name},targetwarehouse:{targetline},type:{type}", Msg = $"节点名{targetline}不存在" };
                Log.Error("立库呼叫任务失败[GET](节点不存在):" + missionInfo.ToString());
            }
            else
            {
                if (!FixMissionManager.Contains(missionInfo))
                {
                    FixMissionManager.Add(missionInfo);
                    res = new Result() { Succeed = true, Data = $"name:{name},targetline:{targetline},type:{type}", Msg = "" };
                    Log.Info("立库呼叫任务成功[GET]:" + missionInfo.ToString());
                }
                else
                {
                    res = new Result() { Succeed = false, Data = $"name:{name},targetline:{targetline},type:{type}", Msg = "该任务已存在" };
                    Log.Error("立库呼叫任务失败[GET](已存在相同任务):" + missionInfo.ToString());
                }
            }

            return JsonHelper.SerializeObject(res);
        }

        public string WarehouseRequestByPost(Stream stream)
        {
            Result res;
            StreamReader sr = new StreamReader(stream);
            string s = sr.ReadToEnd();
            System.Collections.Specialized.NameValueCollection nvc = System.Web.HttpUtility.ParseQueryString(s);
            string name = SettingString.Warehouse;//nvc["name"].ToUpper();
            string targetline = nvc["targetline"].ToUpper();
            int type = int.Parse(nvc["type"]);
            manager.MissionInfo missionInfo = new manager.MissionInfo(name, (manager.FixType)type, targetline, manager.MissionType.Warehouse);
            if (!NodeManager.HasNode(name))
            {
                res = new Result() { Succeed = false, Data = $"name:{name},targetwarehouse:{targetline},type:{type}", Msg = $"节点名{name}不存在" };
                Log.Error("立库呼叫任务失败[POST](节点不存在):" + missionInfo.ToString());

            }
            else if (!NodeManager.HasNode(targetline))
            {
                res = new Result() { Succeed = false, Data = $"name:{name},targetwarehouse:{targetline},type:{type}", Msg = $"节点名{targetline}不存在" };
                Log.Error("立库呼叫任务失败[POST](节点不存在):" + missionInfo.ToString());
            }
            else
            {
                if (!FixMissionManager.Contains(missionInfo))
                {
                    FixMissionManager.Add(missionInfo);
                    res = new Result() { Succeed = true, Data = $"name:{name},targetline:{targetline},type:{type}", Msg = "" };
                    Log.Info("立库呼叫任务成功[POST]:" + missionInfo.ToString());
                }
                else
                {
                    res = new Result() { Succeed = false, Data = $"name:{name},targetline:{targetline},type:{type}", Msg = "该任务已存在" };
                    Log.Error("立库呼叫任务失败[POST](已存在相同任务):" + missionInfo.ToString());
                }
            }

            return JsonHelper.SerializeObject(res);
        }
    }

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

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

        }

    }



}