SServerManager.cs 5.7 KB
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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


        private static string Addr_VMICheckRLC = "/ESMTCommonInterface/CommonService.asmx?op=VMICheckRLC";
        private static string Addr_Return_Material = "/ReturnOne/WebServiceF.asmx?op=Return_Material";

        public static string Get_VMICheckRLC(string deviceName, string codeStr, out bool need)
        {
            //reelID:
            //partNum:
            //{"data":{"IFneed":"不需要","iftest":"","component":"","msg":"不需要测量RLC","status":1}}
            need = false;
            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.Post(server, "");
                LogUtil.info("Get_VMICheckRLC   " + FormUtil.GetSpanStr(DateTime.Now - startTime) + " 【" + server + "】【" + resultStr + "】");
                //{"data":{"IFneed":"不需要","iftest":"","component":"","msg":"不需要测量RLC","status":1}}
                CheckData dataResult = JsonHelper.DeserializeJsonToObject<CheckData>(resultStr);

                if (dataResult == null)
                {
                    return "获取到测值结果失败";
                }
                else if (dataResult.status.Equals(1) && dataResult.IFneed.Equals("不需要"))
                {
                    need = false;
                    return "";
                }
                else if (dataResult.status.Equals(1) && dataResult.IFneed.Equals("需要"))
                {
                    need = true;
                    return "";
                }
                else
                {
                    need = false;
                    return msg;
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(deviceName + " ", ex);
                return ex.ToString();
            }
        }


        public static string Return_Material(string deviceName, string codeStr, int count)
        {
            // Parameter Value
            //txtQty:
            // txtReelId:
            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("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.Post(server, "");
                LogUtil.info("Return_Material   " + FormUtil.GetSpanStr(DateTime.Now - startTime) + " 【" + server + "】【" + resultStr + "】");

                //   ServerData serverResult = JsonHelper.DeserializeJsonToObject<ServerData>(resultStr);

                if (!msg.Equals(""))
                {
                    LogUtil.error(msg);
                }
            }
            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 class CheckData
    {
        //{"data":{"IFneed":"不需要","iftest":"","component":"","msg":"不需要测量RLC","status":1}}
        public int IFneed { get; set; }
        public string iftest { get; set; }
        public string component { get; set; }
        public string msg { get; set; }
        public int status { get; set; }
    }
}