FIC_Solar/solarWinService/Service1.cs

88 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Xml.Linq;
// ref: https://dotblogs.com.tw/BigCow/2020/09/17/104221
namespace solarWinService
{
public partial class Service1 : ServiceBase
{
// private readonly ILogger<Worker> _logger;
System.Xml.Linq.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 Service1()
{
InitializeComponent();
// _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 void OnStart(string[] args)
{
}
protected override void OnStop()
{
}
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;
}
}
}