CService.cs 6.44 KB
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using XingYe_ACS.BaseStruct;

namespace XingYe_ACS.WebApi
{
    public class CService
    {
        /// <summary>
        /// 任务回传
        /// </summary>
        /// <param name="task"></param>
        /// <returns></returns>
        public static ReMessage TaskRefresh(WebApiTask task,int falg)
        {
            string url = "";
            if (falg == 1)
            {
                url = ConfigurationManager.AppSettings["ToWebApiIp"].ToString() + "/TaskStateReturn";
            }
            else
            {
                url = ConfigurationManager.AppSettings["ToWebApiIp"].ToString() + "/TaskStateReturn";
            }
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";
            request.KeepAlive = false;
            request.AllowAutoRedirect = true;
            request.ContentType = "application/json";
            string json = JsonConvert.SerializeObject(task);
            byte[] postdatabtyes = Encoding.UTF8.GetBytes(json);
            request.ContentLength = postdatabtyes.Length;
            try
            {
                Stream requeststream = request.GetRequestStream();
                requeststream.Write(postdatabtyes, 0, postdatabtyes.Length);
                requeststream.Close();
                
                string resp;
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    resp = sr.ReadToEnd();
                }
                return JsonConvert.DeserializeObject<ReMessage>(resp);
            }
            catch (WebException ex)
            {
                HttpWebResponse res;
                res = (HttpWebResponse)ex.Response;
                StreamReader sr1 = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
                string sErrMsg = sr1.ReadToEnd().Replace(Environment.NewLine, "");
                App.ExFile.MessageError("WEB错误:TaskRefresh", sErrMsg);
                return null;
            }
            catch (Exception ex)
            {
                App.ExFile.MessageError("WEB接口:TaskRefresh", ex.Message.Replace(Environment.NewLine, ""));
                return null;
            }

        }

        /// <summary>
        /// 连接AGV是否成功回传
        /// </summary>
        /// <param name="task"></param>
        /// <returns></returns>
        public static ReMessage LinkAgvRefresh(LinkAgv linkAgv)
        {
            string url = ConfigurationManager.AppSettings["ToWebApiIp"].ToString() + "/LinkAgvRefresh";
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";
            request.KeepAlive = false;
            request.AllowAutoRedirect = true;
            request.ContentType = "application/json";
            string json = JsonConvert.SerializeObject(linkAgv);
            byte[] postdatabtyes = Encoding.UTF8.GetBytes(json);
            request.ContentLength = postdatabtyes.Length;
            try
            {
                Stream requeststream = request.GetRequestStream();
                requeststream.Write(postdatabtyes, 0, postdatabtyes.Length);
                requeststream.Close();

                string resp;
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    resp = sr.ReadToEnd();
                }
                return JsonConvert.DeserializeObject<ReMessage>(resp);
            }
            catch (WebException ex)
            {
                HttpWebResponse res;
                res = (HttpWebResponse)ex.Response;
                StreamReader sr1 = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
                string sErrMsg = sr1.ReadToEnd().Replace(Environment.NewLine, "");
                App.ExFile.MessageError("WEB错误:LinkAgvRefresh", sErrMsg);
                return null;
            }
            catch (Exception ex)
            {
                App.ExFile.MessageError("WEB接口:LinkAgvRefresh", ex.Message.Replace(Environment.NewLine, ""));
                return null;
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="task"></param>
        /// <returns></returns>
        public static ReMessage HoisterTask(FTask task)
        {
            string url = ConfigurationManager.AppSettings["ToWebApiIp"].ToString() + "/LinkAgvRefresh";
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";
            request.KeepAlive = false;
            request.AllowAutoRedirect = true;
            request.ContentType = "application/json";
            string json = JsonConvert.SerializeObject(task);
            byte[] postdatabtyes = Encoding.UTF8.GetBytes(json);
            request.ContentLength = postdatabtyes.Length;
            try
            {
                Stream requeststream = request.GetRequestStream();
                requeststream.Write(postdatabtyes, 0, postdatabtyes.Length);
                requeststream.Close();

                string resp;
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    resp = sr.ReadToEnd();
                }
                return JsonConvert.DeserializeObject<ReMessage>(resp);
            }
            catch (WebException ex)
            {
                HttpWebResponse res;
                res = (HttpWebResponse)ex.Response;
                StreamReader sr1 = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
                string sErrMsg = sr1.ReadToEnd().Replace(Environment.NewLine, "");
                App.ExFile.MessageError("WEB错误:LinkAgvRefresh", sErrMsg);
                return null;
            }
            catch (Exception ex)
            {
                App.ExFile.MessageError("WEB接口:LinkAgvRefresh", ex.Message.Replace(Environment.NewLine, ""));
                return null;
            }

        }
    }
}