You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
4.2 KiB

using Nancy;
using Nancy.Bootstrapper;
using Nancy.Hosting.Self;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DBFactory;
using ICommLayer;
using System.Data;
using System.Runtime.Caching;
namespace WcfControlMonitorLib
{
public class APIChannel : NancyModule
{
private static NancyHost _server;
private Object thisLock = new Object();
public static void Start()
{
_server = new NancyHost(new Bootstrapper(), new Uri("http://127.0.0.1:8686"));
//_server = new NancyHost(new Bootstrapper(), new Uri("http://192.168.43.193:8686"));
_server.Start();
}
public class WMS
{
public string statusMessage { get; set; }
public int result { get; set; }
}
public APIChannel()
{
StringBuilder sql = new StringBuilder();
//DBOperator dbo = CStaticClass.dbo;
static DBOperator dbo = new DBOperator();
//get 方法沒用
Get["/"] = _ => "RCS to WCS APIServer";
// 查询设备状态
Post["/jingbao/oapi/Demo/event/fixed-position", true] = async (x, ct) =>
{
try
{
// 创建一个MemoryCache对象
ObjectCache cache = MemoryCache.Default;
InAffimBack inb = new InAffimBack();
T_BASE_DEVICE TestDevice = new T_BASE_DEVICE();
T_BASE_DEVICE_STATE TestDeviceState = new T_BASE_DEVICE_STATE();
// 查询设备状态
DataView dv = new DataView();
sql.Remove(0, sql.Length);
sql.Append('select B.F_ERRORNAME as "问题描述", A.F_DEVICENAME as "设备编码", A.F_ERRORCODE as "故障现象Code", A.F_ErrorStartTime as "报错时间" from T_BASE_DEVICE A, T_BASE_DEVICE_STATE B where A.f_errorcode != 0 and(B.F_DEVICEERRORINDEX = A.f_errorcode and A.F_DEVICEKINDINDEX = B.F_DEVICEKINDINDEX) ');
dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
if (dv.Count > 0 )
{
// 从缓存中读取结果
var cachedResult = cache.Get("cacheKey");
if (cachedResult != null)
{
for (int i = 0; i < dv.Count; i++)
{
dv.isKeyProces = "false";
dv.isStopLine = "false";
dv.submitSource = "系统";
if (cachedResult.Exists(dv[i].F_DEVICENAME) {
cachedResult.push(dv[i]);
}
}
for (int j = 0; j < cachedResult.Count; j++)
{
if (dv.Exists(cachedResult[j].F_DEVICENAME) {
continue
}
else
{
cachedResult.Remove(cachedResult[j]);
}
}
}
else
{
cachedResult = dv;
}
// var diffElements = dv.Except( cachedResult ).Union( cachedResult.Except( dv ));
inb.result = dv.ToString();
inb.code = "200";
return JsonConvert.SerializeObject(inb);
}
else
{
inb.result = "false";
inb.code = "1";
return JsonConvert.SerializeObject(inb);
}
}
catch (Exception ex)
{
CommonClassLib.CCarryConvert.WriteDarkCasket("WEBAPI,WCS查询设备状态,异常:", ex.ToString(), "", "");
throw;
}
};
}
}
}