123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- using Cksoft.Data;
- using DllEapEntity;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Linq;
- using System.Net.Http;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System.Net;
- using System.IO;
- namespace DllEapCommon
- {
- public class AppserverHelper
- {
- /// <summary>
- /// 停止联网
- /// </summary>
- /// <param name="db"></param>
- /// <param name="maccode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public int StopMachine(IDatabase db, Machine mac, ref string errorinfo)
- {
- try
- {
- EapAppserver eapapp = db.FindListForCondition<EapAppserver>($@" and a.id=(SELECT aa.EapAppserverID FROM eap.eapappservermac as aa
- inner join machine as bb on aa.macid = bb.id
- where bb.id = '{mac.ID}')", ref errorinfo).FirstOrDefault();
- if (eapapp == null)
- {
- errorinfo = $"未找到机台编号={mac.FCode}对应的服务器信息。";
- return 1;
- }
- //调用服务器停止联网接口
- // HttpClient client = HttpClientFactory.Create();
- string url = $"http://{eapapp.FIp}:{eapapp.FPort}/home/StopMachineById?macId={mac.ID}";
- var request = (HttpWebRequest)WebRequest.Create(url);
- request.Method = "GET";
- var response = (HttpWebResponse)request.GetResponse();
- var stream = response.GetResponseStream();
- var sr = new StreamReader(stream);
- var result = sr.ReadToEnd();
- //HttpResponseMessage response = client.GetAsync(url).Result;
- //if (!response.IsSuccessStatusCode)
- //{
- // errorinfo = $"访问API失败:{url},{response.Content}";
- // return -1;
- //}
- //string result = response.Content.ReadAsStringAsync().Result;
- JObject obj = JsonConvert.DeserializeObject<JObject>(result);
- if (int.Parse(obj["resultcode"].ToString()) <= 0)
- {
- errorinfo = obj["errorinfo"].ToString();
- }
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = $"从服务器停止机台联网发生异常:{ex.ToString()}";
- return -1;
- }
- }
- /// <summary>
- /// 启动联网
- /// </summary>
- /// <param name="db"></param>
- /// <param name="maccode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public int StartMachine(IDatabase db, string maccode, ref string errorinfo)
- {
- try
- {
- EapAppserver eapapp = db.FindListForCondition<EapAppserver>($@" and a.id=(SELECT aa.EapAppserverID FROM eap.eapappservermac as aa
- inner join machine as bb on aa.macid = bb.id
- where bb.FCode = '{maccode}')", ref errorinfo).FirstOrDefault();
- if (eapapp == null)
- {
- errorinfo = $"未找到机台编号={maccode}对应的服务器信息。";
- return -1;
- }
- //调用服务器停止联网接口
- HttpClient client = HttpClientFactory.Create();
- string url = $"http://{eapapp.FIp}:{eapapp.FPort}/home/StartMachine?maccode={maccode}";
- HttpResponseMessage response = client.GetAsync(url).Result;
- if (!response.IsSuccessStatusCode)
- {
- errorinfo = $"访问API失败:{url},{response.Content}";
- return -1;
- }
- string result = response.Content.ReadAsStringAsync().Result;
- JObject obj = JsonConvert.DeserializeObject<JObject>(result);
- if (int.Parse(obj["resultcode"].ToString()) <= 0)
- {
- errorinfo = obj["errorinfo"].ToString();
- }
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = $"从服务器启动机台联网发生异常:{ex.ToString()}";
- return -1;
- }
- }
- /// <summary>
- /// 刷新RMS配置数据
- /// </summary>
- /// <param name="db"></param>
- /// <param name="maccode"></param>
- /// <param name="errorinfo"></param>
- /// <returns></returns>
- public int RmsRefreshOrders(IDatabase db, string maccode, ref string errorinfo)
- {
- try
- {
- EapAppserver eapapp = db.FindListForCondition<EapAppserver>($@" and a.id=(SELECT aa.EapAppserverID FROM eap.eapappservermac as aa
- inner join machine as bb on aa.macid = bb.id
- where bb.FCode = '{maccode}')", ref errorinfo).FirstOrDefault();
- if (eapapp == null)
- {
- errorinfo = $"未找到机台编号={maccode}对应的服务器信息。";
- return -1;
- }
- MQServer mq = db.FindListForCondition<MQServer>($" and a.id=(SELECT z.MstID FROM eap.mqserverdetail z where z.APServerID={eapapp.ID})", ref errorinfo).FirstOrDefault();
- if (mq == null)
- {
- errorinfo = $"未找到机台编号={maccode}对应的MQ信息。";
- return -1;
- }
- //RMS服务放在MQ服务器上,此为约定,如果RMS不是放在MQ服务器上,则不能这么调用
- //调用服务器停止联网接口
- HttpClient client = HttpClientFactory.Create();
- string url = $"http://{mq.IpAddress}:9602/home/RefreshOrders?maccode={maccode}";
- HttpResponseMessage response = client.GetAsync(url).Result;
- if (!response.IsSuccessStatusCode)
- {
- errorinfo = $"访问API失败:{url},{response.Content}";
- return -1;
- }
- string result = response.Content.ReadAsStringAsync().Result;
- JObject obj = JsonConvert.DeserializeObject<JObject>(result);
- if (int.Parse(obj["resultcode"].ToString()) <= 0)
- {
- errorinfo = obj["errorinfo"].ToString();
- }
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = $"从服务器刷新机台指令发生异常:{ex.ToString()}";
- return -1;
- }
- }
- }
- }
|