修改自動運行

This commit is contained in:
cesar liu 2022-11-15 14:08:34 +08:00
parent fa6b26df81
commit 637409f817
19 changed files with 167 additions and 90 deletions

View File

@ -46,8 +46,10 @@
"SMTPConfig": { "SMTPConfig": {
"Host": "smtp.gmail.com", "Host": "smtp.gmail.com",
"Port": 25, "Port": 25,
"UserName": "ficgreen01@gmail.com", "UserName": "shanghohui@gmail.com",
"Password": "opuisjyxgyjbjtha", //2022-09-02 update by jiahao "Password": "wswgnluvoodfexrb",
//"UserName": "ficgreen01@gmail.com",
//"Password": "opuisjyxgyjbjtha", //2022-09-02 update by jiahao
"EnableSsl": true "EnableSsl": true
}, },
"ExceptionTimes": 240, //<EFBFBD><EFBFBD><EFBFBD>`<EFBFBD>q<EFBFBD><EFBFBD><EFBFBD>ɶ<EFBFBD><EFBFBD>A<EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD>) "ExceptionTimes": 240, //<EFBFBD><EFBFBD><EFBFBD>`<EFBFBD>q<EFBFBD><EFBFBD><EFBFBD>ɶ<EFBFBD><EFBFBD>A<EFBFBD><EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD>)

View File

@ -7,7 +7,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SolarPower", "SolarPower\So
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "solarApp", "solarApp\solarApp.csproj", "{3D54E7DA-4EF8-42A3-85D3-39DD08AC9A0C}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "solarApp", "solarApp\solarApp.csproj", "{3D54E7DA-4EF8-42A3-85D3-39DD08AC9A0C}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "winService", "winService\winService.csproj", "{132A1CF9-2C64-493D-8F8E-1F86AA595D14}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "solarService", "solarService\solarService.csproj", "{CBD09B63-979E-4723-BC88-035DAEC6B0E0}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -23,10 +23,10 @@ Global
{3D54E7DA-4EF8-42A3-85D3-39DD08AC9A0C}.Debug|Any CPU.Build.0 = Debug|Any CPU {3D54E7DA-4EF8-42A3-85D3-39DD08AC9A0C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3D54E7DA-4EF8-42A3-85D3-39DD08AC9A0C}.Release|Any CPU.ActiveCfg = Release|Any CPU {3D54E7DA-4EF8-42A3-85D3-39DD08AC9A0C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3D54E7DA-4EF8-42A3-85D3-39DD08AC9A0C}.Release|Any CPU.Build.0 = Release|Any CPU {3D54E7DA-4EF8-42A3-85D3-39DD08AC9A0C}.Release|Any CPU.Build.0 = Release|Any CPU
{132A1CF9-2C64-493D-8F8E-1F86AA595D14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {CBD09B63-979E-4723-BC88-035DAEC6B0E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{132A1CF9-2C64-493D-8F8E-1F86AA595D14}.Debug|Any CPU.Build.0 = Debug|Any CPU {CBD09B63-979E-4723-BC88-035DAEC6B0E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{132A1CF9-2C64-493D-8F8E-1F86AA595D14}.Release|Any CPU.ActiveCfg = Release|Any CPU {CBD09B63-979E-4723-BC88-035DAEC6B0E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{132A1CF9-2C64-493D-8F8E-1F86AA595D14}.Release|Any CPU.Build.0 = Release|Any CPU {CBD09B63-979E-4723-BC88-035DAEC6B0E0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -759,7 +759,7 @@ namespace solarApp
this.ClientSize = new System.Drawing.Size(1782, 953); this.ClientSize = new System.Drawing.Size(1782, 953);
this.Controls.Add(this.tabControl1); this.Controls.Add(this.tabControl1);
this.Name = "fmArchive"; this.Name = "fmArchive";
this.Text = "fmArchive 1020"; this.Text = "fmArchive 11-15";
this.Load += new System.EventHandler(this.fmArchive_Load); this.Load += new System.EventHandler(this.fmArchive_Load);
this.tabControl1.ResumeLayout(false); this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false); this.tabPage1.ResumeLayout(false);

View File

@ -84,11 +84,11 @@ namespace solarApp
#endregion #endregion
#region #region
//_timer = new System.Timers.Timer(); _timer = new System.Timers.Timer();
//_timer.Interval = 10000; _timer.Interval = 10000;
//_timer.SynchronizingObject = this; _timer.SynchronizingObject = this;
//_timer.Elapsed += _timer_Elapsed; _timer.Elapsed += _timer_Elapsed;
//_timer.Start(); _timer.Start();
#endregion #endregion
// dtselect_station1.Value = DateTime.Today.AddDays(-1); // dtselect_station1.Value = DateTime.Today.AddDays(-1);
@ -107,46 +107,46 @@ namespace solarApp
private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{ {
//lbMsgStatus.Text = (autoTask) ? "autoTask = true" : "autoTask = false"; lbMsgStatus.Text = (autoTask) ? "autoTask = true" : "autoTask = false";
//lbMsgStatus.Text += " timespan = " + (DateTime.Now - doTimerTaskTime).TotalMinutes.ToString(); lbMsgStatus.Text += " timespan = " + (DateTime.Now - doTimerTaskTime).TotalMinutes.ToString();
//if (DateTime.Now.Hour >= 2 && DateTime.Now.Minute == 0) if (DateTime.Now.Hour >= 2 && DateTime.Now.Minute == 0)
//{ {
// doTimerTaskTime = DateTime.Now; doTimerTaskTime = DateTime.Now;
// autoTask = false; autoTask = false;
//} }
//// 凌晨 2點後如果間隔30分鐘 沒有跑異常同步 就啟動啟動 // 凌晨 2點後如果間隔30分鐘 沒有跑異常同步 就啟動啟動
//doTaskDuratin = DateTime.Now - doTimerTaskTime; // doTaskDuratin = DateTime.Now - doTimerTaskTime; //
//if (DateTime.Now.Hour >= 2 && doTaskDuratin.TotalMinutes > 15 && (autoTask == true)) if (DateTime.Now.Hour >= 2 && doTaskDuratin.TotalMinutes > 15 && (autoTask == true))
//{ {
// doTimerTaskTime = DateTime.Now; doTimerTaskTime = DateTime.Now;
// autoTask = false; autoTask = false;
//} }
////每日歸檔 //每日歸檔
//if (DateTime.Now.Hour == 00 && DateTime.Now.Minute == 15 ) if (DateTime.Now.Hour == 00 && DateTime.Now.Minute == 15)
//{ {
// //MessageBox.Show("ok"); //MessageBox.Show("ok");
// autoTask = true; autoTask = true;
// lbMsgTitle.Text = DateTime.Now.ToString() + " timer start"; lbMsgTitle.Text = DateTime.Now.ToString() + " timer start";
// dtSelect1.Value = System.DateTime.Now.AddDays(-1); dtSelect1.Value = System.DateTime.Now.AddDays(-1);
// dtSelect2.Value = System.DateTime.Now.AddDays(-1); dtSelect2.Value = System.DateTime.Now.AddDays(-1);
// bt_archive.PerformClick(); bt_archive.PerformClick();
// autoTask = false; autoTask = false;
// //bt_archive_Click.PerformClick(); //bt_archive_Click.PerformClick();
//} }
////異常處理 每 5分鐘跑一次 //異常處理 每 5分鐘跑一次
//if ((DateTime.Now.Minute % 5) == 0 && (DateTime.Now.Second) < 30 && (autoTask == false)) if ((DateTime.Now.Minute % 5) == 0 && (DateTime.Now.Second) < 30 && (autoTask == false))
//{ {
// doTimerTaskTime = DateTime.Now; doTimerTaskTime = DateTime.Now;
// autoTask = true; autoTask = true;
// lbMsgTitle.Text = "異常處理 " + DateTime.Now.ToString() + " timer start"; lbMsgTitle.Text = "異常處理 " + DateTime.Now.ToString() + " timer start";
// btSyncErr.PerformClick(); btSyncErr.PerformClick();
// lbMsgTitle.Text = "異常處理 done" + DateTime.Now.ToString() ; lbMsgTitle.Text = "異常處理 done" + DateTime.Now.ToString();
// autoTask = false; autoTask = false;
// //bt_archive_Click.PerformClick(); //bt_archive_Click.PerformClick();
//} }
//臨時的 累計日照 //臨時的 累計日照
@ -644,7 +644,7 @@ namespace solarApp
if (CanDoSend) if (CanDoSend)
{ {
SMTPConfig smtp = new SMTPConfig(); ; SMTPConfig smtp = new SMTPConfig(); ;
smtp.Host = "smtp.gmail.com"; smtp.Host = "msr.hinet.net";
//smtp.UserName = "ficgreen01@gmail.com"; //smtp.UserName = "ficgreen01@gmail.com";
//smtp.Password = "qwe2015qwe"; //smtp.Password = "qwe2015qwe";
//smtp.UserName = "ficgreen02@gmail.com"; //smtp.UserName = "ficgreen02@gmail.com";

View File

@ -1711,7 +1711,6 @@ namespace solarApp
// //
// button1 // button1
// //
this.button1.Cursor = System.Windows.Forms.Cursors.Arrow;
this.button1.Location = new System.Drawing.Point(1603, 51); this.button1.Location = new System.Drawing.Point(1603, 51);
this.button1.Name = "button1"; this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(94, 29); this.button1.Size = new System.Drawing.Size(94, 29);
@ -1740,7 +1739,6 @@ namespace solarApp
// //
// btLoadData // btLoadData
// //
this.btLoadData.Cursor = System.Windows.Forms.Cursors.Arrow;
this.btLoadData.Location = new System.Drawing.Point(34, 21); this.btLoadData.Location = new System.Drawing.Point(34, 21);
this.btLoadData.Name = "btLoadData"; this.btLoadData.Name = "btLoadData";
this.btLoadData.Size = new System.Drawing.Size(94, 29); this.btLoadData.Size = new System.Drawing.Size(94, 29);
@ -1785,7 +1783,6 @@ namespace solarApp
dataGridViewCellStyle54.WrapMode = System.Windows.Forms.DataGridViewTriState.True; dataGridViewCellStyle54.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.gv_all_data.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle54; this.gv_all_data.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle54;
this.gv_all_data.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.gv_all_data.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.gv_all_data.Cursor = System.Windows.Forms.Cursors.Arrow;
dataGridViewCellStyle55.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; dataGridViewCellStyle55.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle55.BackColor = System.Drawing.SystemColors.Window; dataGridViewCellStyle55.BackColor = System.Drawing.SystemColors.Window;
dataGridViewCellStyle55.Font = new System.Drawing.Font("Microsoft JhengHei UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); dataGridViewCellStyle55.Font = new System.Drawing.Font("Microsoft JhengHei UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
@ -1819,7 +1816,6 @@ namespace solarApp
dataGridViewCellStyle57.WrapMode = System.Windows.Forms.DataGridViewTriState.True; dataGridViewCellStyle57.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.gv_notice_data.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle57; this.gv_notice_data.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle57;
this.gv_notice_data.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.gv_notice_data.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.gv_notice_data.Cursor = System.Windows.Forms.Cursors.Arrow;
dataGridViewCellStyle58.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; dataGridViewCellStyle58.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle58.BackColor = System.Drawing.SystemColors.Window; dataGridViewCellStyle58.BackColor = System.Drawing.SystemColors.Window;
dataGridViewCellStyle58.Font = new System.Drawing.Font("Microsoft JhengHei UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); dataGridViewCellStyle58.Font = new System.Drawing.Font("Microsoft JhengHei UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);

View File

@ -1,7 +1,6 @@
using winService; using solarService;
IHost host = Host.CreateDefaultBuilder(args) IHost host = Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureServices(services => .ConfigureServices(services =>
{ {
services.AddHostedService<Worker>(); services.AddHostedService<Worker>();

View File

@ -1,6 +1,6 @@
{ {
"profiles": { "profiles": {
"winService": { "solarService": {
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": true, "dotnetRunMessages": true,
"environmentVariables": { "environmentVariables": {

21
solarService/Worker.cs Normal file
View File

@ -0,0 +1,21 @@
namespace solarService
{
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
public Worker(ILogger<Worker> logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
await Task.Delay(1000, stoppingToken);
}
}
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

View File

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-solarService-2179A249-99D7-4FA2-AFD2-32FA4EA04A9E</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
</ItemGroup>
</Project>

14
solarService2/Program.cs Normal file
View File

@ -0,0 +1,14 @@
using solarService;
IHost host = Host.CreateDefaultBuilder(args)
.UseWindowsService(options =>
{
options.ServiceName = "solar_archive_service";
})
.ConfigureServices(services =>
{
services.AddHostedService<Worker>();
})
.Build();
await host.RunAsync();

View File

@ -0,0 +1,11 @@
{
"profiles": {
"solarService": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -32,7 +32,7 @@ namespace winService
private readonly ILogger<Worker> _logger; private readonly ILogger<Worker> _logger;
XDocument xdoc; XDocument xdoc;
public XDocument Xdoc { get => xdoc; set => xdoc = value; } public XDocument Xdoc { get => xdoc; set => xdoc = value; }
getStationSvc stationSvc; getStationSvc stationSvc;
System.Timers.Timer _timer; System.Timers.Timer _timer;
bool autoTask = false; //測試自動跑 irrDayHour 累計日照小時差異 bool autoTask = false; //測試自動跑 irrDayHour 累計日照小時差異
@ -40,15 +40,15 @@ namespace winService
DateTime doTimerTaskTime = DateTime.Now; DateTime doTimerTaskTime = DateTime.Now;
TimeSpan doTaskDuratin; TimeSpan doTaskDuratin;
public Worker(ILogger<Worker> logger) public Worker(ILogger<Worker> logger)
{ {
_logger = logger; _logger = logger;
xdoc = XDocument.Load("../solarApp/App.config"); xdoc = XDocument.Load("../solarApp/App.config");
string conStr = Xdoc.Element("configuration").Element("connectionStrings").Element("add").Attribute("connectionString").Value; string conStr = Xdoc.Element("configuration").Element("connectionStrings").Element("add").Attribute("connectionString").Value;
stationSvc = new getStationSvc(conStr); stationSvc = new getStationSvc(conStr);
svc = new procSyncError(conStr); // ±`¸ê®Æ¦P¨B svc = new procSyncError(conStr); // ±`¸ê®Æ¦P¨B
} }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
@ -98,7 +98,18 @@ namespace winService
} }
} }
void archiveAllStation() { // ªA°È°±¤î®É
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(); var site_list = stationSvc.get_station_list();
procSensorSvc sensorSvc = new procSensorSvc(); procSensorSvc sensorSvc = new procSensorSvc();
procInvSvc invSvc = new procInvSvc(); procInvSvc invSvc = new procInvSvc();
@ -107,7 +118,7 @@ namespace winService
string date2 = 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 (var item in site_list)
{ {
foreach (DateTime day in EachDay(DateTime.Parse(date1), DateTime.Parse(date2))) foreach (DateTime day in EachDay(DateTime.Parse(date1), DateTime.Parse(date2)))
{ {
sensorSvc.archiveData(item.SiteID.Substring(0, 9), day.ToString("yyyy-MM-dd")); sensorSvc.archiveData(item.SiteID.Substring(0, 9), day.ToString("yyyy-MM-dd"));

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

View File

@ -1,18 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Worker"> <Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0-windows10.0.17763.0</TargetFramework> <TargetFramework>net6.0-windows10.0.22000.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-winService-78282A39-DE2D-4E5B-806B-31D6FE6D57E7</UserSecretsId> <UserSecretsId>dotnet-solarService-DE237DB4-CE53-4A5A-A2CA-F28052220202</UserSecretsId>
<StartupObject></StartupObject> <OutputType>exe</OutputType>
<OutputType>WinExe</OutputType>
<UseWindowsForms>False</UseWindowsForms>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" /> <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,9 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"mySql": "server=60.251.164.103;user=webuser;Database=solar_master;Port=11306;password=FICadmin99;charset='utf8';pooling=true;sslmode=none;"
}

View File

@ -1,10 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"mySql": "server=60.251.164.103;user=webuser;Database=solar_master;Port=11306;password=FICadmin99;charset='utf8';pooling=true;sslmode=none;"
}