TheLine.cs 7.5 KB
using ConfigHelper;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
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 
        }
        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));
        }
        /// <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));
            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);
            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, ResultData>(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, ResultData>(url, ngMsgData,5000,true);
        }
        /// <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);
        }
        /// <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);
        }
        /// <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);
            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)
        {
            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--;

                    //将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);

                    // 请求失败,返回false
                    return "";
                }
            }
        }       
    }
}