using COMMON;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Models;
using Models.Models.DEBUGGER_DB;
using Models.Models.LOGDB;
using Newtonsoft.Json;
using Quartz;
using SERVER;
using SERVER.DataServer;
using SERVER.LIB;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using TcpServer;
using WebUI.LIB;
using static Models.TCPDATA;
namespace WebUI.Controllers
{
///
/// api控制器
///
public class ApiController : BaseController
{
private IScheduler _scheduler;
private readonly LOG_DBContext _logdb;
private readonly HotelServiceContext _db;
private readonly DEBUGGER_DB _DEBUGGER_DB;
private readonly ISchedulerFactory _schedulerFactory;
///
/// 注入定时器等服务
///
///
///
///
///
public ApiController(HotelServiceContext db, LOG_DBContext logdb, ISchedulerFactory schedulerFactory, DEBUGGER_DB _DB)
{
_db = db;
_schedulerFactory = schedulerFactory;
_logdb = logdb;
_DEBUGGER_DB = _DB;
}
///
/// 下发固件、配置
///
///
[HttpPost]
public IActionResult SendCmd(int type = 1,params string[] mac)
{
try
{
foreach (var item in mac)
{
XC_Redis.Redis.Remove(item);
}
return Json(Send_Cmd.Send_File(type,mac));
}
catch (Exception EX)
{
LogHelp.Error(EX.ToString());
return null;
}
}
///
/// 下发 固件、配置 进度条
///
///
[HttpPost]
public IActionResult Send_Schedule(int type = 1, params string[] mac)
{
try
{
Dictionary macData = new Dictionary();
foreach (var item in mac)
{
var rd = XC_Redis.Redis.GetKey(item);
macData.Add(item, rd+"%");
}
return Json(macData);
}
catch (Exception EX)
{
LogHelp.Error(EX.ToString());
return Content(EX.Message); ;
}
}
///
/// 酒店信息
///
///
[HttpPost]
public IActionResult HOTELINFO()
{
try
{
IList RESHotels = new List();
foreach (var item in CacheData.TBL_HOTEL_BASIC_INFO.GroupBy(X=>X.HOTEL_OLD_GROUP))
{
var GROUP = CacheData.TBL_HOTEL_GROUP_INFO.FirstOrDefault(X=>X.HOTEL_GROUP_OLD_ID == item.First().HOTEL_OLD_GROUP);
if (GROUP == null)
{
LogHelp.Error(item.First().HOTEL_OLD_GROUP + "酒店组信息为null~");
continue;
}
RESHotels.Add(new HotelDataItem() {HotelGroupsName = GROUP.HOTEL_GROUP_NAME,HotelGroupsId = GROUP.HOTEL_GROUP_OLD_ID,Hotels = item.Select(X => new HotelsItem() { Auth = null, Code = X.PROJECT_NUMBER, HotelId = X.IDOLD, HotelName = X.HOTEL_NAME_CN }).ToList() });
RESHotels.Add(new HotelDataItem() {HotelGroupsName = GROUP.HOTEL_GROUP_NAME,HotelGroupsId = GROUP.HOTEL_GROUP_OLD_ID,Hotels = item.Select(X => new HotelsItem() { Auth = null, Code = X.PROJECT_NUMBER, HotelId = X.IDOLD, HotelName = X.HOTEL_NAME_CN }).ToList() });
}
// var RES = Hotels.ToArray();
//for(int i = 0; i < RES.Count();i++)
//{
// for (int J = 0; J < RES[i].Hotels.Count; J++)
// {
// RES[i].Hotels[J].Auth = null;
// }
//}
return Json(RESHotels);
}
catch (Exception EX)
{
LogHelp.Error(EX.ToString());
return null;
}
}
///
/// 房间信息
///
/// 酒店id
/// 酒店组id
///
[HttpPost]
public IActionResult ROOMINFO(int HotelId = 0,int GroupId = 0)
{
try
{
// 这里是进行了权限判断 以及数据的简单校验
//if ((HotelId <= 0 && GroupId<=0) || (HotelId > 0 && Hoteldata.FirstOrDefault(X=>X.HotelId == HotelId)==null ))
// return Json(null);
List HotelDATA = HotelDATA = _db.TBL_HOTEL_BASIC_INFOS.Where(X => (GroupId ==0 || X.HOTEL_OLD_GROUP == GroupId) && (HotelId==0 || X.IDOLD == HotelId)).Select(X=>X.IDOLD).ToList();
return Json(_db.TBL_ROOM_BASIC_INFOS.Where(x => HotelDATA.Contains(x.HOTEL_OLD_ID)).Select(x => new { text = x.ROOM_NUMBER, val = x.ROOM_OLD_ID,mac=x.MAC }));
}
catch (Exception EX)
{
LogHelp.Error(EX.ToString());
return Json(null);
}
}
///
/// 获取文件数据列表
///
///
[HttpPost]
public IActionResult SelModelFile(int start = 0, int length = 15, int draw = 0, string type = "")
{
try
{
int count = 0;
int count_ = 0;
var data = ModelFile.GetFtp(out count, out count_, start, length, type);
return Json(new
{
draw,
recordsTotal = count,
recordsFiltered = count_,
data = data
});
}
catch (Exception ex)
{
LogHelp.Error(ex.ToString());
return null;
}
}
///
/// 添加文件数据
///
///
[HttpPost]
public IActionResult AddModelFile(List files, int type = -1)
{
try
{
return Json(ModelFile.AddModelFile(type, files, TOken, _db).Result);
}
catch (Exception ex)
{
LogHelp.Error(ex.ToString());
return Json(false);
}
}
///
/// 删除文件数据
///
///
[HttpPost]
public IActionResult DelModelFile(int id)
{
try
{
return Json(ModelFile.DelModelFile(id, _db));
}
catch (Exception ex)
{
LogHelp.Error(ex.ToString());
return Json(false);
}
}
///
/// 刷新token 同时也返回服务器服务状态
///
///
[HttpPost]
public IActionResult Token()
{
try
{
Get();
BaseController.SXTOKEN(this, HttpContext);
return Json(new ReturnData { IsSuccess = true, Result = new int[] { XC_Redis.Redis.GetKey("udpserver"), TestingServices.ISok("BLV RUCS") } });
}
catch (Exception ex)
{
LogHelp.Error(ex.ToString());
return Json(new ReturnData { IsSuccess = false, Result = ex.Message });
}
}
///
/// 刷新udp log 数
///
///
[HttpPost]
public IActionResult Getudplogcount()
{
try
{
return Json(new
{
udp = CacheData.UDPcount,
// ruc 是没有直接跟房间绑定的 只能酒店房间 反推 ruc rcu数量 等于房间绑定的rcu 数量
rcu = SelHotel == null ? 0 : _db.TBL_ROOM_BASIC_INFOS.Where(x => x.HOTEL_OLD_ID == SelHotel.HotelId && x.MAC.Length > 0).Count()
});
}
catch (Exception ex)
{
LogHelp.Error(ex.ToString());
return Json(new
{
udp = 0,
// ruc 是没有直接跟房间绑定的 只能酒店房间 反推 ruc
rcu = 0
});
}
}
///
/// 获取rcu列表 分页
///
///
[HttpPost]
public IActionResult GetRCU(int start,int draw, int length, int room_online_satatus, string roomnumber,int room_type)
{
try
{
if (SelHotel != null)
{
int COUNT, COUNT_ = 0;
var macdata = RcuServer.SetRucServer(out COUNT, out COUNT_,SelHotel.HotelId, start, length,room_online_satatus, roomnumber, room_type, _db);
return Json(new
{
draw,
recordsTotal = COUNT,
recordsFiltered = COUNT_,
data = macdata
});
}
else
return null;
}
catch (Exception EX)
{
LogHelp.Error(EX.ToString());
return null;
}
}
///
/// 切换酒店
///
///
[HttpPost]
public IActionResult Sel(int id)
{
HotelsItem hotels = Hoteldata.FirstOrDefault(x => x.HotelId == id);
if (hotels != null)
{
Response.Cookies.Append("old_host_" + TOken, id.ToString());
COMMON.XC_Redis.Redis.SetKey(TOken_rom + "_sel", hotels);
}
return Json(new ReturnData { IsSuccess = hotels != null, Result = null });
}
///
/// 退出登录
///
///
[HttpGet]
public IActionResult signout()
{
Response.Cookies.Delete("CurrentUser");
// 删除会话用户标识
XC_Redis.Redis.Remove(TOken + Cookie.Substring(Cookie.Length - 8) + "_rom");
// 删除会话用户选择的 酒店
XC_Redis.Redis.Remove(TOken_rom + "_sel");
// 删除 随机截取的token 下标数组
XC_Redis.Redis.Remove(TOken_rom);
if (ConfigEntity.Instance.IsMore == false)
{
XC_Redis.Redis.Remove(TOken);
XC_Redis.Redis.Remove(TOken + "__hoteldata");
}
return RedirectToAction("Index", "Login");
}
///
/// RUCS_CMD
///
///
[HttpGet]
public IActionResult RUCS_CMD()
{
return Json(new { Send_RUCS_RCU_LIST = SendHelp.Send_RUCS_RCU_LIST().ToString() });
}
///
/// 获取酒店组视图
///
///
[HttpPost]
public IActionResult GetGroup(int group = 0, string search = "")
{
for (int i = 0; i < Hotels.Count; i++)
{
if (Hotels[i].HotelGroupsId == group)
{
ViewData["HotelGroup"] = Hotels[i];
}
}
ViewData["Hotel"] = SelHotel.HotelId;
ViewData["search"] = string.IsNullOrEmpty(search) ? "" : search;
return View(Hotels);
}
///
/// 获取酒店视图 未用到
///
/// 未用到
[HttpPost]
public IActionResult GetHotels(int GroupId)
{
return View();
}
///
/// 获取UDPINFO 通讯包数统计
///
///
[HttpPost]
public IActionResult GetCHARTS(int[] TX, int[] RX, string startDate, string endDate)
{
ECHARTS res = CHARTSSERVER.GetCHARTS(_logdb, TX, RX, startDate, endDate);
return Json(res);
}
///
/// 获取UDPINFO 统计 通讯字节流量统计
///
///
[HttpPost]
public IActionResult GetErrorSucess(int[] TX, int[] RX, string startDate, string endDate)
{
ECHARTS res = CHARTSSERVER.GetErrorSucess(_logdb, startDate, endDate);
return Json(res);
}
///
/// 获取UDPINFO 端口数据使用情况
///
///
[HttpPost]
public IActionResult GetProt(string startDate, string endDate)
{
ECHARTS res = CHARTSSERVER.GetProt(_logdb, startDate, endDate);
return Json(res);
}
///
/// 获取UDPINFO 处理耗时
///
///
[HttpPost]
public IActionResult GetPROCESSINGTIME(string startDate, string endDate)
{
ECHARTS res = CHARTSSERVER.GetPROCESSINGTIME(_logdb, startDate, endDate);
return Json(res);
}
///
/// 获取UDPINFO 处理耗时
///
///
[HttpPost]
public IActionResult GetRevDelayDateTime(string startDate, string endDate)
{
ECHARTS res = CHARTSSERVER.GetRevDelayDateTime(_logdb, startDate, endDate);
return Json(res);
}
///
/// 获取UDPINFO 命令饼图 tx rx
///
///
[HttpPost]
public IActionResult GetCMDpie(string startDate, string endDate, string type = "TX")
{
ECHARTS res = CHARTSSERVER.GetCMDpie_(_logdb, startDate, endDate, type);
return Json(res);
}
///
/// 获取UDPINFO 重发饼图 tx rx
///
///
[HttpPost]
public IActionResult GetRETRY_COUNT(string startDate, string endDate, string type = "TX")
{
ECHARTS res = CHARTSSERVER.GetRETRY_COUNT(_logdb, startDate, endDate, type);
return Json(res);
}
///
/// 更改服务状态
///
///
[HttpPost]
public IActionResult Changerserver(int type)
{
return Json(TestingServices.ISok("BLV RUCS", type));
}
///
/// 同步数据
///
public void Get()
{
if (Client.dateTime == string.Empty) { return; }
lock (Client.dateTimelocks)
{
if (Client.dateTime == null)
{
Client.dateTime = "000000000";
Thread thread = new Thread(async () =>
{
//1、通过调度工厂获得调度器
_scheduler = await _schedulerFactory.GetScheduler();
//2、开启调度器
await _scheduler.Start();
//3、创建一个触发器
var trigger = TriggerBuilder.Create()
.WithCronSchedule("0/3 * * * * ?")//间隔3秒 检测 tcp 是否在运行
.Build();
//3、创建一个触发器
var trigger1 = TriggerBuilder.Create()
.WithCronSchedule("0 00 00 * * ? *")// 每日 0 点触发 同步数据
.Build();
//3、创建一个触发器
var trigger2 = TriggerBuilder.Create()
.WithCronSchedule("4 20 * * * ?")//每个小时 10min 统计字节流量等
.Build();
//4、创建任务
// 检测tcp 是否在运行
var jobDetail = JobBuilder.Create()
.Build();
//4、创建任务
// 同步数据
var jobDetail1 = JobBuilder.Create()
.Build();
//4、创建任务
// 统计字数数量等
var jobDetai2 = JobBuilder.Create()
.Build();
//5、将触发器和任务器绑定到调度器中
await _scheduler.ScheduleJob(jobDetail, trigger);
await _scheduler.ScheduleJob(jobDetail1, trigger1);
await _scheduler.ScheduleJob(jobDetai2, trigger2);
});
thread.Start();
}
}
}
#region BLV_UdpDebug 数据统计图
///
/// 数据统计图
///
///
///
///
public IActionResult GetBLV_UdpDebugImg(string startDate, string endDate)
{
ECHARTS res = CHARTSSERVER.GetBLV_UdpDebugImg(_DEBUGGER_DB, startDate, endDate);
return Json(res);
}
#endregion
#region BLV_UdpDebug 数据统计饼图
///
/// 数据统计饼图 通讯命令 数据统计接收耗时条形统计图
///
///
///
///
public IActionResult GetBLV_UdpDebugImg_PIE(string startDate, string endDate)
{
ECHARTS res = CHARTSSERVER.GetBLV_UdpDebugImg_RevDelayDateTime(_DEBUGGER_DB, startDate, endDate);
return Json(res);
}
#endregion
}
}