SServerManager.cs 8.2 KB
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    public class SServerManager
    {

        //是否测值:
        //http://10.85.17.233/ESMTCommonInterface/CommonService.asmx?op=VMICheckRLC
        //结果上传:
        //http://10.85.17.233/ReturnOne/WebServiceF.asmx?op=Return_Material

        //  http://10.85.17.233/ESMTCommonInterface/CommonService.asmx/VMICheckRLC?reelID=R014212020051100876&partNum=6C.R0034.1D1
        private static string Addr_VMICheckRLC = "/ESMTCommonInterface/CommonService.asmx/VMICheckRLC";
        private static string Addr_Return_Material = "/ReturnOne/WebServiceF.asmx/Return_Material";

        public static string Get_VMICheckRLC(string deviceName, string codeStr, out int targetP)
        {
            //reelID:
            //partNum:
            //{"data":{"IFneed":"不需要","iftest":"","component":"","msg":"不需要测量RLC","status":1}}
            targetP = 0;
            string msg = "";
            try
            {
                string[] codeArray = codeStr.Split(';');
                if (codeArray.Length != 2)
                {
                    return msg = deviceName + "【" + codeStr + "】未找到有效条码";
                }
                Dictionary<string, string> paramMap = new Dictionary<string, string>();
                paramMap.Add("reelID", codeArray[1]);
                paramMap.Add("partNum", codeArray[0]);

                string server = GetAddr(Addr_VMICheckRLC, paramMap);
                if (server.Equals(""))
                {
                    return "获取是否测值失败";
                }
                DateTime startTime = DateTime.Now;
                string resultStr = HttpHelper.Get(server);
                LogUtil.info(deviceName + "Get_VMICheckRLC   " + FormUtil.GetSpanStr(DateTime.Now - startTime) + " 【" + server + "】【" + resultStr + "】");
                //{"data":{"IFneed":"不需要","iftest":"","component":"","msg":"不需要测量RLC","status":1}}
                ResultData strData = JsonHelper.DeserializeJsonToObject<ResultData>(resultStr);
                if (strData != null && strData.data != null)
                {
                    if (strData.data.status.Equals(0) && strData.data.IFneed.Equals("不需要"))
                    {
                        targetP = 1;
                        return "";
                    }
                    else if (strData.data.status.Equals(1) && strData.data.IFneed.Equals("需要"))
                    {
                        //需要时,component是电容 或者电阻
                        if (strData.data.component.Equals("电容"))
                        {
                            targetP = 2;
                        }
                        else
                        {
                            targetP = 3;
                        }
                        return "";
                    }
                    else
                    {
                        msg = strData.data.msg;
                        return msg;
                    }
                }
                else
                {
                    return "获取是否测值失败";
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(deviceName + " ", ex);
                return ex.ToString();
            }
            return msg;
        }


        public static string Return_Material(string deviceName, string codeStr, int count, out string FactoryCode)
        {
            // Parameter Value
            //txtQty:
            // txtReelId:
            string msg = "";
            FactoryCode = "";
            try
            {
                string[] codeArray = codeStr.Split(';');
                if (codeArray.Length != 2)
                {
                    return msg = deviceName + "【" + codeStr + "】未找到有效条码";
                }

                Dictionary<string, string> paramMap = new Dictionary<string, string>();
                paramMap.Add("txtQty", count.ToString());
                paramMap.Add("txtReelId", codeArray[1]);
                string server = GetAddr(Addr_Return_Material, paramMap);
                if (server.Equals(""))
                {
                    return "";
                }
                DateTime startTime = DateTime.Now;
                string resultStr = HttpHelper.Get(server).Replace("\r\n", "  ");
                //【<?xml version="1.0" encoding="utf-8"?>< string xmlns = "http://tempuri.org/" > NG 不可退料 厂别:ST </ string >】 
                LogUtil.info(deviceName + "Return_Material   " + FormUtil.GetSpanStr(DateTime.Now - startTime) + " 【" + server + "】【" + resultStr + "】");

                //resultStr = "<?xml version=\"1.0\" encoding=\"utf - 8\"?>< string xmlns = \"http://tempuri.org/\" > NG 不可退料 厂别:ST </ string >";
                if (resultStr.Contains("NG"))
                {
                    int index = resultStr.IndexOf("NG");
                    msg = resultStr.Substring(index, resultStr.Length - index).Replace("</ string >", "");
                }

                var m = Regex.Match(resultStr, "厂别[::\\s]*(.*?)<");
                if (m.Success)
                {
                    FactoryCode = m.Groups[1].Value.Trim();
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(deviceName + " ", ex);
            }
            return msg;
        }

        private static string GetAddr(string addr, Dictionary<string, string> paramsMap)
        {
            string server = ConfigAppSettings.GetValue(Setting_Init.ServerAddr).Trim();
            if (server.Equals(""))
            {
                return "";
            }
            if (server.EndsWith("/"))
            {
                server = server.Substring(0, server.Length - 1);
            }
            string path = server + addr.Trim() + "?";
            foreach (string paramName in paramsMap.Keys)
            {
                string par = System.Web.HttpUtility.UrlEncode(paramsMap[paramName], System.Text.Encoding.UTF8);
                path += paramName + "=" + par + "&";
            }
            path = path.Substring(0, path.Length - 1);
            return path;
        }

        private static string server = ConfigAppSettings.GetValue(Setting_Init.ServerAddr).Trim();
        public static bool CanConnect()
        {
            return !String.IsNullOrEmpty(server);
        }
        public static bool GetReekinfo(string codeStr, out ReelResult reelResult) {

            string[] codeArray = codeStr.Split(';');
            if (codeArray.Length != 2)
            {
                reelResult = null;
                return false;
            }

            string resultStr = HttpHelper.Get("http://10.85.199.25/myproject/rest/api/qisda/device/resolveCode?code=" + codeArray[1], Encoding.UTF8,1000);
            ServerResult serverResult = JsonHelper.DeserializeJsonToObject<ServerResult>(resultStr);
            if (serverResult.code == 0) {
                reelResult = serverResult.data;
                return true;
            } else {
                reelResult = null;
                return false;
            }
        }
    }
    // http://10.85.17.233/ESMTCommonInterface/CommonService.asmx/VMICheckRLC?reelID=R014212020051100876&partNum=6C.R0034.1D1
    // {"data":{"IFneed":"需要","iftest":"未测值","component":"","msg":"未获取到测量RLC时间","status":1}}
    public class ResultData
    {
        public CheckData data { get; set; }
    }
    public class CheckData
    {
        //{"data":{"IFneed":"不需要","iftest":"","component":"","msg":"不需要测量RLC","status":1}}
        public string IFneed { get; set; }
        public string iftest { get; set; }
        public string component { get; set; }
        public string msg { get; set; }
        public int status { get; set; }
    }

    public class ServerResult {
        public int code;
        public string msg;
        public ReelResult data;

    }
    public class ReelResult
    {
        public int qty;
        public int w;
        public int h;
        public string pn;
    }
}