TheLine.cs 14.3 KB
using ConfigHelper;
using Newtonsoft.Json;
using OnlineStore;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace RemoteSheardObject
{
    public class TheLine
    {
        public enum LineStatusE {
            INLINE,
            INROBOT,
            BOXDOOR,
            FINISHED,
            BOXDOOR_NOREEL
        }
        public static bool UpdateLocInfo(string taskId, string barcode, LineStatusE status, string locInfo)
        {
            if (barcode.Count(c => c == '#') == 3)
            {
                barcode = barcode.Split('#')[1].Substring(1);
            }
            var postData = new Dictionary<string, string>()
            {
                {"taskId", taskId},
                {"barcode", barcode},
                {"status", status.ToString()},
                {"locInfo", locInfo}
            };

            return !string.IsNullOrEmpty(SubmitPostData("/service/store/robotBox/updateLocInfo", postData, out _));
        }
        public static bool UpdateReelQty(string barcode, int qty)
        {
            if (barcode.Count(c => c == '#') == 3)
            {
                barcode = barcode.Split('#')[1].Substring(1);
            }
            var postData = new Dictionary<string, string>()
            {
                {"barcode", barcode},
                {"qty", qty.ToString()}
            };
            var data = SubmitPostData("/rest/micron/device/updateReelQty", postData, out string msg);
            LogUtil.info("UpdateReelQty:"+data);

            if (string.IsNullOrEmpty(data))
                LogUtil.info("UpdateReelQty:" + msg);
            return !string.IsNullOrEmpty(data);
        }
        public static bool emptyOut(string barcode)
        {
            //料盘空出后直接结束任务
            return true;
            if (barcode.Count(c => c == '#') == 3)
            {
                barcode = barcode.Split('#')[1].Substring(1);
            }
            var postData = new Dictionary<string, string>()
            {
                {"barcode", barcode}
            };
            return !string.IsNullOrEmpty(SubmitPostData("/service/store/robotBox/emptyOut", postData, out _));
        }
        /// <summary>
        /// 获取正在进行的任务数量
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string,int> GetTaskCount()
        {
            //pizzaBox,pcb,tray,reel
            var postData = new Dictionary<string, string>()
            {
            };
            Dictionary<string, int> taskdata = new Dictionary<string, int>();
            taskdata["pizzaBox"] = 0;
            taskdata["pcb"] = 0;
            taskdata["tray"] = 0;
            taskdata["reel"] = 0;

            var result=  JsonConvert.DeserializeObject<ResultData>(SubmitPostData("/rest/micron/device/getTaskCount", postData, out _));
            if (result==null || result.code != 0)
                return taskdata;

            foreach (var k in taskdata.Keys.ToArray())
            {
                if (result.data.ContainsKey(k+"_out"))
                    taskdata[k] = int.Parse(result.data[k + "_out"].ToString());
            }
            return taskdata;
        }

        public static string CombineUrl(string baseUrl, string relativeUrl)
        {
            if (string.IsNullOrEmpty(baseUrl))
            {
                return relativeUrl;
            }

            if (string.IsNullOrEmpty(relativeUrl))
            {
                return baseUrl;
            }

            baseUrl = baseUrl.TrimEnd('/');
            relativeUrl = relativeUrl.TrimStart('/');

            return string.Format("{0}/{1}", baseUrl, relativeUrl);
        }

        public static T GetReelSize<T>(string barcode)
        {
            if (barcode.Count(c => c == '#') == 3)
            {
                barcode = barcode.Split('#')[1].Substring(1);
            }
            var postData = new Dictionary<string, string>()
            {
                {"barcode", barcode},
            };
            var result = SubmitPostData( "/service/store/robotBox/getSize", postData, out _);
            if (result != null)
                return JsonConvert.DeserializeObject<T>(result);
            else
                return default;
        }


        public static void UploadStatus(EquipMsgData equipMsgData)
        {
            string url = CombineUrl(Config.Get("Device_Server_Address"), "/rest/micron/device/updateStatus");
            var resultStr = HttpHelper.Post<EquipMsgData, ResultData2>(url, equipMsgData);
        }

        public static void uploadNgReel(NgMsgData ngMsgData)
        {
            string url = CombineUrl(Config.Get("Device_Server_Address"), "/rest/micron/device/uploadNgReel");
            var resultStr = HttpHelper.Post<NgMsgData, ResultData2>(url, ngMsgData,5000,true);
        }
        /// <summary>
        /// 上传mycronic库存信息
        /// </summary>
        /// <param name="equipMsgData"></param>
        public static void UploadCarrierInformation(List<CarrierInformationData> equipMsgData)
        {
            string url = CombineUrl(Config.Get("Device_Server_Address"), "/rest/micron/device/updateMInventory");
            var resultStr = HttpHelper.Post<List<CarrierInformationData>, ResultData2>(url, equipMsgData);
        }

        /// <summary>
        /// NG口位置,1=左侧,2=右侧
        /// </summary>
        /// <param name="ngPos"></param>
        public static void ClearNgPos(int ngPos)
        {
            var postData = new Dictionary<string, string>()
            {
                {"ngPos",ngPos.ToString() }
            };
            string url = "/rest/micron/device/clearNgPos";
            var resultStr = SubmitPostData(url, postData, out _);
        }
        /// <summary>
        /// 上传自定义数据
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void UploadCustData(string key,string value)
        {
            var postData = new Dictionary<string, string>()
            {
                {"key",key  },
                {"value",value  }
            };
            string url = "/rest/micron/device/updateData";
            var resultStr = SubmitPostData(url, postData, out _);
        }
        /// <summary>
        /// 上传自定义数据
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static string GetCustData(string key)
        {
            var postData = new Dictionary<string, string>()
            {
                {"key",key}
            };
            string url = "/rest/micron/device/getData";
            var resultStr = SubmitPostData(url, postData, out _);
            var r= JsonConvert.DeserializeObject<ResultData2>(resultStr);
            if (!string.IsNullOrWhiteSpace(r.data)) { 
            return r.data;
            }
            return null;
        }
        public class ResultData2
        {
            //{"code":0,"msg":"ok","data":"7"} 
            public int code { get; set; }

            public string msg { get; set; }

            public string data { get; set; }
        }
        public class ResultData
        {
            //{"code":0,"msg":"ok","data":"7"} 
            public int code { get; set; }

            public string msg { get; set; }

            public Dictionary<string, object> data { get; set; }
        }
        public static string SubmitPostData(string url, Dictionary<string, string> postData,out string msg)
        {
            msg = "";
            url = CombineUrl(Config.Get("Device_Server_Address"), url);
            //创建WebClient对象
            using (var client = new WebClient())
            {
                //设置提交数据的方式为"application/x-www-form-urlencoded"
                client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";

                try
                {
                    //将POST请求参数字典转换为参数字符串
                    var postDataString = new StringBuilder();
                    foreach (var item in postData)
                    {
                        if (!string.IsNullOrEmpty(item.Value))
                        {
                            postDataString.AppendFormat("{0}={1}&", item.Key, Uri.EscapeDataString(item.Value));
                        }
                    }

                    //去掉最后一个"&"符号
                    if (postDataString.Length>0)
                        postDataString.Length--;

                    msg += postDataString;
                    //将POST请求参数转换为字节数组
                    byte[] bytes = Encoding.UTF8.GetBytes(postDataString.ToString());

                    //提交POST请求并获取响应
                    byte[] response = client.UploadData(url, "POST", bytes);

                    //将响应转换为字符串并输出
                    return Encoding.UTF8.GetString(response);

                }
                catch (Exception ex)
                {
                    // 捕获任何网络异常,并输出错误信息
                    //Console.WriteLine(crc.GetString("Res0071","提交POST请求时发生错误:") + ex.Message);
                    msg += "\r\n提交POST请求时发生错误:"+ex.Message;
                    // 请求失败,返回false
                    Debug.WriteLine(url);
                    Debug.WriteLine(ex);
                    return "";
                }
            }
        }
        /// <summary>
        /// 料仓离线后二次重新获取物料库位
        /// </summary>
        /// <param name="reelParam"></param>
        /// <param name="traytype"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static ReelParam Regetposid(ReelParam reelParam,string traytype, out string msg)
        {
            msg = "";
            var barcode = reelParam.WareCode;
            if (barcode.Count(c => c == '#') == 3)
            {
                var bs = barcode.Split('#');
                reelParam.RI = bs[1].Substring(1);
                reelParam.PN = bs[0].Substring(1);
                reelParam.DC = bs[2];
                barcode = bs[1].Substring(1);
            }
            try
            {
                Dictionary<string, string> nameValue = new Dictionary<string, string>();
                nameValue.Add("code", barcode);
                nameValue.Add("preCid", reelParam.cid);
                if (reelParam.PlateW == 13 && reelParam.PlateH < 44)
                    nameValue.Add("cids", "AMH-SBDH3-2,AMH-SBSH1,AMH-SBSH2,AMH-SBDH2-1,AMH-SBDH2-2,AMH-SBDH1-1,AMH-SBDH1-2,003040,003048,003039,003043,003035,003036,003041,003042");
                else //不包含 AMH-SBDH3-1
                    nameValue.Add("cids", "AMH-SBDH3-1,AMH-SBDH3-2,AMH-SBSH1,AMH-SBSH2,AMH-SBDH2-1,AMH-SBDH2-2,AMH-SBDH1-1,AMH-SBDH1-2,003040,003048,003039,003043,003035,003036,003041,003042");

                var data = SubmitPostData("/service/store/robotBox/renewPosForPutin", nameValue, out _);
                msg += "Regetposid code: " + reelParam.WareCode + ",preCid: " + reelParam.cid + ",cids:" + nameValue["cids"] + ",Result: " + data + "\r\n";

                emptyPosForPutin result = JsonConvert.DeserializeObject<emptyPosForPutin>(data);
                if (result != null && (result.result == "0" || result.result == "98"))
                {
                    if (string.IsNullOrEmpty(result.cid))
                    {
                        msg += "no cid\r\n";
                        reelParam.cid = traytype == "MTP1" ? "AMH-ML5-2" : "AMH-ML5-1";
                        reelParam.IsNg = true;
                        reelParam.NgMsg = result.msg;
                        return reelParam;
                    }
                    var mid = "";
                    if ("003040,003048,003039,003043".IndexOf(result.cid) >= 0)
                    {
                        mid = "AMH-MI1";
                    }
                    else if ("003035,003036,003041,003042".IndexOf(result.cid) >= 0)
                    {
                        mid = "AMH-MI2";
                    }

                    //if (reel.PlateW == 7 || reel.PlateW == 13 && reel.PlateH < 44 || reel.PlateW == 15)
                    if (mid == "AMH-MI1" || mid == "AMH-MI2")
                    {
                        var micode = $"P{reelParam.PN}#S{reelParam.RI}#{reelParam.QTY}#{reelParam.DC}";
                        msg += $"送往MI物料重组2dcode:" + micode;

                        reelParam.SubCID = result.cid;
                        reelParam.WareCode = micode;
                        reelParam.PosID = result.cid;
                        reelParam.cid = mid;
                        //RobotManage.SendTrayRequest("003039", micode, "AMH-MI1");
                        //RobotManage.SendTrayRequest(result.cid, micode, mid);
                    }
                    else
                    {
                        reelParam.cid = result.cid;
                        reelParam.PosID = result.pos;
                    }
                    return reelParam;
                    //RobotManage.SendTrayRequest(result.pos, result.barcode, result.cid);
                }
                else
                {
                    if (result != null && result.msg != null)
                        msg = result.msg;
                    reelParam.cid = traytype == "MTP1" ? "AMH-ML5-2" : "AMH-ML5-1"; ;
                    reelParam.IsNg = true;
                    reelParam.NgMsg = msg;
                    return reelParam;
                }
            }
            catch (Exception ex) {
                msg += ex.ToString();
                reelParam.cid = traytype == "MTP1" ? "AMH-ML5-2" : "AMH-ML5-1"; ;
                reelParam.IsNg = true;
                reelParam.NgMsg = msg;
                return reelParam;
            }
        }
        public class emptyPosForPutin
        {
            public string result;
            public string msg;
            public string pos;
            public string barcode;
            public string cid;
        }
        [Serializable]
        public class CarrierInformationData
        {
            public string carrier;
            public string partnumber;
            public string depot;
            public int diameter;
            public int height;

        }
    }
}