using CsharpHttpHelper;
using DeviceRepair.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DeviceRepair.Utils;
namespace DeviceRepairAndOptimization
{
///
/// 后台任务调度
///
public class Scheduler
{
public delegate void CallBackEventHandler(string args, object value);
public event CallBackEventHandler CallBack;
HttpHelper http = new HttpHelper();
private static Scheduler scheduler;
public static Scheduler Instance
{
get
{
if (scheduler == null)
scheduler = new Scheduler();
return scheduler;
}
}
Dictionary httpItems = new Dictionary
{
{
"SchedulerPlanTips",new HttpItem { URL = $"http://{DeviceRepair.Utils.Config.Configurations.Properties.ServiceIP}/{DeviceRepair.Utils.Config.Configurations.Properties.ServiceName}/Api/Plan/SchedulerPlanTips"}
},
{
"CurrentYearPlanSchedule",new HttpItem { URL = $"http://{DeviceRepair.Utils.Config.Configurations.Properties.ServiceIP}/{DeviceRepair.Utils.Config.Configurations.Properties.ServiceName}/Api/Plan/GetCurrentYearPlanSchedule", ContentType = "application/json;charset=utf-8"}
}
};
///
/// 启动
///
public void Run()
{
foreach (HttpItem item in httpItems.Values)
{
item.Header.Add("auth", GlobalInfo.token);
}
Task.Run(() =>
{
while (true)
{
Console.WriteLine(DateTime.Now.ToLongTimeString());
GetSchedulerPlanTips();
GetCurrentYearPlanSchedule();
Task.Delay(3000).Wait();
}
});
}
///
/// 获取当年度 - 当月待保养计划 - 数量
///
protected void GetSchedulerPlanTips()
{
HttpResult Rtn = http.GetHtml(httpItems["SchedulerPlanTips"]);
try
{
APIResponseData apiResponseData = Rtn.GetApiResponseData();
if (apiResponseData.IsSuccess)
{
CallBack?.Invoke("SchedulerPlanTips", apiResponseData.Data);
}
else
{
CallBack?.Invoke("SchedulerPlanTips", apiResponseData.Message);
}
}
catch (Exception)
{
CallBack?.Invoke("SchedulerPlanTips", HttpHelper.StripHTML(Rtn.Html));
}
}
protected void GetCurrentYearPlanSchedule()
{
HttpResult Rtn = http.GetHtml(httpItems["CurrentYearPlanSchedule"]);
try
{
APIResponseData apiResponseData = Rtn.GetApiResponseData();
if (apiResponseData.IsSuccess)
{
object Value = apiResponseData.ToDeserializeObject();
CallBack?.Invoke("CurrentYearPlanSchedule", Value);
}
else
{
CallBack?.Invoke("CurrentYearPlanSchedule", apiResponseData.Message);
}
}
catch (Exception)
{
CallBack?.Invoke("CurrentYearPlanSchedule", HttpHelper.StripHTML(Rtn.Html));
}
}
}
}