FIC_Solar/solarService2/Worker.cs
2022-11-15 14:08:34 +08:00

148 lines
5.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using solarApp.Service;
using System.Configuration;
using System.Xml.Linq;
using System.Runtime.InteropServices;
namespace winService
{
public class Worker : BackgroundService
{
public enum ServiceState
{
SERVICE_STOPPED = 0x00000001,
SERVICE_START_PENDING = 0x00000002,
SERVICE_STOP_PENDING = 0x00000003,
SERVICE_RUNNING = 0x00000004,
SERVICE_CONTINUE_PENDING = 0x00000005,
SERVICE_PAUSE_PENDING = 0x00000006,
SERVICE_PAUSED = 0x00000007,
}
[StructLayout(LayoutKind.Sequential)]
public struct ServiceStatus
{
public int dwServiceType;
public ServiceState dwCurrentState;
public int dwControlsAccepted;
public int dwWin32ExitCode;
public int dwServiceSpecificExitCode;
public int dwCheckPoint;
public int dwWaitHint;
};
private readonly ILogger<Worker> _logger;
XDocument xdoc;
public XDocument Xdoc { get => xdoc; set => xdoc = value; }
getStationSvc stationSvc;
System.Timers.Timer _timer;
bool autoTask = false; //測試自動跑 irrDayHour 累計日照小時差異
procSyncError svc;
DateTime doTimerTaskTime = DateTime.Now;
TimeSpan doTaskDuratin;
public Worker(ILogger<Worker> logger)
{
_logger = logger;
xdoc = XDocument.Load("../solarApp/App.config");
string conStr = Xdoc.Element("configuration").Element("connectionStrings").Element("add").Attribute("connectionString").Value;
stationSvc = new getStationSvc(conStr);
svc = new procSyncError(conStr); // 常資料同步
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
await Task.Delay(60000, stoppingToken); //每分鐘
if (DateTime.Now.Hour >= 2 && DateTime.Now.Minute == 0)
{
doTimerTaskTime = DateTime.Now;
autoTask = false;
}
// 凌晨 2點後如果間隔30分鐘 沒有跑異常同步 就啟動啟動
doTaskDuratin = DateTime.Now - doTimerTaskTime; //
if (DateTime.Now.Hour >= 2 && doTaskDuratin.TotalMinutes > 15 && (autoTask == true))
{
doTimerTaskTime = DateTime.Now;
autoTask = false;
}
//每日歸檔
if (DateTime.Now.Hour == 00 && DateTime.Now.Minute == 15)
{
//MessageBox.Show("ok");
autoTask = true;
//lbMsgTitle.Text = DateTime.Now.ToString() + " timer start";
//dtSelect1.Value = System.DateTime.Now.AddDays(-1);
//dtSelect2.Value = System.DateTime.Now.AddDays(-1);
archiveAllStation();
autoTask = false;
}
//異常處理 每 5分鐘跑一次
if ((DateTime.Now.Minute % 5) == 0 && (DateTime.Now.Second) < 30 && (autoTask == false))
{
doTimerTaskTime = DateTime.Now;
autoTask = true;
svc.syncErrData();
//lbMsgTitle.Text = "異常處理 " + DateTime.Now.ToString() + " timer start";
//btSyncErr.PerformClick();
//lbMsgTitle.Text = "異常處理 done" + DateTime.Now.ToString();
autoTask = false;
//bt_archive_Click.PerformClick();
}
}
}
// 服務停止時
public override async Task StopAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Service stopped");
//Log("Service stopped");
//cpuLogger.Dispose();
//cpuLogger = null!;
await base.StopAsync(stoppingToken);
}
void archiveAllStation()
{
var site_list = stationSvc.get_station_list();
procSensorSvc sensorSvc = new procSensorSvc();
procInvSvc invSvc = new procInvSvc();
procStationSvc siteSvc = new procStationSvc();
string date1 = System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
string date2 = System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
foreach (var item in site_list)
{
foreach (DateTime day in EachDay(DateTime.Parse(date1), DateTime.Parse(date2)))
{
sensorSvc.archiveData(item.SiteID.Substring(0, 9), day.ToString("yyyy-MM-dd"));
invSvc.archiveData(item.SiteID.Substring(0, 9), day.ToString("yyyy-MM-dd"));
siteSvc.archiveData(item.SiteID.Substring(0, 9), day.ToString("yyyy-MM-dd"));
}
}
sensorSvc.isFirst = true;
foreach (var item in site_list)
{
//for sensor_history_hour
foreach (DateTime day in EachDay(DateTime.Parse(date1), DateTime.Parse(date2)))
{
sensorSvc.archiveSensorHistoryHourData(item.SiteID.Substring(0, 9), day.ToString("yyyy-MM-dd"));
sensorSvc.isFirst = false;
invSvc.report_invDay(item.SiteID.Substring(0, 9), day.ToString("yyyy-MM-dd"));
}
}
}
public IEnumerable<DateTime> EachDay(DateTime from, DateTime thru)
{
for (var day = from.Date; day.Date <= thru.Date; day = day.AddDays(1))
yield return day;
}
}
}