AGVManager.cs 6.3 KB
using BLL.BLL;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace BLL
{
    public class AGVManager
    {

        //public static string Add_emptyMsg = "http://10.85.199.1/BenQMIR/Webservice/AGVService.asmx/CreateEmptyRecycleTask";//"http://localhost:11111/BenQMIR/Webservice/AGVService.asmx/CreateEmptyRecycleTask";//
        public static string CreateEmptyTask(string Add_emptyMsg, string line)
        {
            try
            {
                //Dictionary<string, string> paramMap = new Dictionary<string, string>();
                //paramMap.Add("emptyStation", "D1");
                string path = Add_emptyMsg;
                //foreach (string paramName in paramMap.Keys)
                //{
                //    string par = System.Web.HttpUtility.UrlEncode(paramMap[paramName], System.Text.Encoding.UTF8);
                //    path += paramName + "=" + par + "&";
                //}
                //path = path.Substring(0, path.Length - 1);

                string resultStr = HttpUtil.Post(path, "emptyStation=" + line);//HttpHelper.Post(path, "");
                //Common.log.Debug("【" + path + "】【" + resultStr + "】");

                //Result data = JsonHelper.DeserializeJsonToObject<Result>(resultStr);
                return resultStr;
                //if (data == null)
                //{
                //    return " updateDeviceAlarmMsg 没有收到服务器反馈";
                //}
                //else
                //{
                //    return data.Succeed;
                //}
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }



        public static Result GetWebservice(string Add_emptyMsg,string id)
        {
            Result rb = new Result();
            try
            {
                //http://10.85.36.156/BenQMIR/Webservice/AGVService.asmx
                string strURL = Add_emptyMsg + "?emptyStation=" + id;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
                //request.Method="get";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream s = response.GetResponseStream();
                XmlTextReader Reader = new XmlTextReader(s);
                Reader.MoveToContent();
                string strValue = Reader.ReadInnerXml();
                strValue = strValue.Replace("&lt;", "<");
                strValue = strValue.Replace("&gt;", ">");
                //{"Succeed":false,"ResultData":null,"ErrorMessage":"无法通过[Feederaaaa]lineCode找到小车停靠点"}
                rb = JsonConvert.DeserializeObject<Result>(strValue);
                Reader.Close();
            }
            catch (Exception e)
            {
               System.Windows.Forms.MessageBox.Show(e.Message);
            }
            return rb;
        }
        /// <summary>
        /// 获取大料架解绑情况
        /// </summary>
        /// <param name="rfid"></param>
        /// <param name="lineName"></param>
        /// <returns></returns>
        public static bool GetRackBy(string rfid, out string lineName)
        {
            lineName = "";
            //GET /ESMTCommonInterface/CommonService.asmx/GetRackBy?id=string HTTP/1.1
            //Host: 10.85.17.233
            string addr = "http://10.85.17.233/ESMTCommonInterface/CommonService.asmx/GetRackBy?id=" + rfid;
            //[{"msg":"1OKD1"}]
            //[{"msg":"0NGFeeder"}]
            try
            {
                string result = HttpUtil.Get(addr);
                if (!result.Equals(""))
                {
                    List<Msg> msgs = JsonHelper.DeserializeJsonToList<Msg>(result);
                    if (msgs == null || msgs.Count == 0)
                        return false;
                    int.TryParse(msgs[0].msg.Substring(0, 1), out int resCode);
                    string resStr = msgs[0].msg.Substring(1, 2);
                    lineName = msgs[0].msg.Substring(3);
                    //Common.log.Debug(string.Format("resCode={0},resStr={1},lineName={2}", resCode, resStr, lineName));
                    if (lineName.Equals("").Equals(true))
                        return false;
                    return true;
                }
            }
            catch { return false; }
            return false;
        }

        /// <summary>
        /// 物料状态上报
        /// </summary>
        /// <param name="rfid"></param>
        /// <param name="lineName"></param>
        /// <returns></returns>
        public static bool UpdateStatus(string rfid, string lineName)
        {
            //GET /ESMTCommonInterface/CommonService.asmx/UpdateStatusBy?id=string&location=string HTTP/1.1
            //Host: 10.85.17.233
            string addr = "http://10.85.17.233/ESMTCommonInterface/CommonService.asmx/UpdateStatusBy?id=" + rfid + "&location=" + lineName;
            //[{"msg":"1更新成功"}]
            try
            {
                string result = HttpUtil.Get(addr);
                if (!result.Equals(""))
                {
                    List<Msg> msgs = JsonHelper.DeserializeJsonToList<Msg>(result);
                    if (msgs == null || msgs.Count == 0)
                        return false;
                    bool resRtn = int.TryParse(msgs[0].msg.Substring(0, 1), out int resCode);
                    if (resRtn)
                    {
                        if (resCode.Equals(1))
                        {
                            System.Windows.Forms.MessageBox.Show(string.Format("{0}物料状态更新成功[{1}]", lineName, rfid));
                            return true;
                        }

                        else
                            return false;
                    }
                    return false;
                }
            }
            catch { return false; }
            return false;
        }
        private class Msg
        {
            public string msg { get; set; }
        }

    }
    public class Result
    {
        public string Succeed { get; set; }

        public string ResultData { get; set; }

        public string ErrorMessage { get; set; }

    }
}