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
{
///
/// 停止联网
///
///
///
///
///
public int StopMachine(IDatabase db, Machine mac, ref string errorinfo)
{
try
{
EapAppserver eapapp = db.FindListForCondition($@" 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(result);
if (int.Parse(obj["resultcode"].ToString()) <= 0)
{
errorinfo = obj["errorinfo"].ToString();
}
return 1;
}
catch (Exception ex)
{
errorinfo = $"从服务器停止机台联网发生异常:{ex.ToString()}";
return -1;
}
}
///
/// 启动联网
///
///
///
///
///
public int StartMachine(IDatabase db, string maccode, ref string errorinfo)
{
try
{
EapAppserver eapapp = db.FindListForCondition($@" 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(result);
if (int.Parse(obj["resultcode"].ToString()) <= 0)
{
errorinfo = obj["errorinfo"].ToString();
}
return 1;
}
catch (Exception ex)
{
errorinfo = $"从服务器启动机台联网发生异常:{ex.ToString()}";
return -1;
}
}
///
/// 刷新RMS配置数据
///
///
///
///
///
public int RmsRefreshOrders(IDatabase db, string maccode, ref string errorinfo)
{
try
{
EapAppserver eapapp = db.FindListForCondition($@" 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($" 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(result);
if (int.Parse(obj["resultcode"].ToString()) <= 0)
{
errorinfo = obj["errorinfo"].ToString();
}
return 1;
}
catch (Exception ex)
{
errorinfo = $"从服务器刷新机台指令发生异常:{ex.ToString()}";
return -1;
}
}
}
}