diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index 32bb7da..c64c5da 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -3181,6 +3181,34 @@ namespace SolarPower.Repository.Implement { var stratDateTime = Convert.ToDateTime(dateTime + ":00").AddMinutes(-15).ToString("yyyy-MM-dd HH:mm"); + //var sql = $@"SELECT + // s.TIMESTAMP, + // s.INVERTERID, + // s.KWH, + // s.TODAYKWH, + // i.Capacity, + // s.KWH/(i.Capacity/4) AS KWHKWP + // FROM (SELECT + // MAX(FROM_UNIXTIME(sub_inv.TIMESTAMP/1000, '%Y-%m-%d %H:%i')) AS TIMESTAMP, + // sub_inv.INVERTERID, + // SUM(sub_inv.WH)/1000 AS KWH, + // MAX(sub_inv.TODAYKWH) AS TODAYKWH + // FROM {table_name} sub_inv + // WHERE FROM_UNIXTIME(sub_inv.TIMESTAMP/1000, '%Y-%m-%d %H:%i') BETWEEN @StartDateTime AND @EndDateTime + // AND sub_inv.INVERTERID IN @InverterIds + // GROUP BY sub_inv.INVERTERID) s + // /*LEFT JOIN ( + // SELECT MAX(FROM_UNIXTIME(sub_inv.TIMESTAMP/1000, '%Y-%m-%d %H:%i')) AS TIMESTAMP, + // sub_inv.INVERTERID, + // SUM(sub_inv.WH)/1000 AS KWH + // FROM {table_name} sub_inv + // WHERE FROM_UNIXTIME(sub_inv.TIMESTAMP/1000, '%Y-%m-%d %H:%i') BETWEEN @StartDateTime AND @EndDateTime + // AND sub_inv.INVERTERID IN @InverterIds + // GROUP BY sub_inv.INVERTERID) a + // ON s.TIMESTAMP = a.TIMESTAMP AND s.INVERTERID = a. INVERTERID*/ + // LEFT JOIN {db_name}.inverter i ON s.INVERTERID = REPLACE(i.InverterId, 's', '');"; + //result = (await conn.QueryAsync(sql, new { StartDateTime = stratDateTime, EndDateTime = dateTime, InverterIds = inverterIds }, commandTimeout: 300)).ToList(); + var sql = $@"SELECT s.TIMESTAMP, s.INVERTERID, @@ -3194,21 +3222,11 @@ namespace SolarPower.Repository.Implement SUM(sub_inv.WH)/1000 AS KWH, MAX(sub_inv.TODAYKWH) AS TODAYKWH FROM {table_name} sub_inv - WHERE FROM_UNIXTIME(sub_inv.TIMESTAMP/1000, '%Y-%m-%d %H:%i') BETWEEN @StartDateTime AND @EndDateTime - AND sub_inv.INVERTERID IN @InverterIds - GROUP BY sub_inv.INVERTERID) s - /*LEFT JOIN ( - SELECT MAX(FROM_UNIXTIME(sub_inv.TIMESTAMP/1000, '%Y-%m-%d %H:%i')) AS TIMESTAMP, - sub_inv.INVERTERID, - SUM(sub_inv.WH)/1000 AS KWH - FROM {table_name} sub_inv - WHERE FROM_UNIXTIME(sub_inv.TIMESTAMP/1000, '%Y-%m-%d %H:%i') BETWEEN @StartDateTime AND @EndDateTime - AND sub_inv.INVERTERID IN @InverterIds - GROUP BY sub_inv.INVERTERID) a - ON s.TIMESTAMP = a.TIMESTAMP AND s.INVERTERID = a. INVERTERID*/ - LEFT JOIN {db_name}.inverter i ON s.INVERTERID = REPLACE(i.InverterId, 's', '');"; - - result = (await conn.QueryAsync(sql, new { StartDateTime = stratDateTime, EndDateTime = dateTime, InverterIds = inverterIds }, commandTimeout: 300)).ToList(); + WHERE CrdTime BETWEEN '{stratDateTime}' AND '{inverterIds}' + GROUP BY sub_inv.INVERTERID) s + LEFT JOIN {db_name}.inverter i ON s.INVERTERID = REPLACE(i.InverterId, 's', '') + where Deleted = 0 and enabled = 1 ;"; + result = (await conn.QueryAsync(sql, commandTimeout: 300)).ToList(); } catch (Exception exception) { diff --git a/SolarPower/appsettings.json b/SolarPower/appsettings.json index 94acfda..4949fa1 100644 --- a/SolarPower/appsettings.json +++ b/SolarPower/appsettings.json @@ -7,8 +7,8 @@ } }, "AllowedHosts": "*", - "LoginExpireMinute": 60, //登入到期時間,單位(分) - "GoBackDay": 1, //補償機制(背景執行(每天)),回推天數 + "LoginExpireMinute": 60, //嚙緯嚙皚嚙踝蕭嚙踝蕭伅嚙踝蕭A嚙踝蕭嚙(嚙踝蕭) + "GoBackDay": 1, //嚙踝蕭嚙緞嚙踝蕭嚙踝蕭(嚙瘢嚙踝蕭嚙踝蕭嚙踝蕭(嚙瘠嚙踝蕭))嚙璀嚙稷嚙踝蕭嚙諸潘蕭 "mySql": "server=60.251.164.103;user=webuser;Database=solar_master;Port=11306;password=FICadmin99;charset='utf8';pooling=true;sslmode=none;", //"DBConfig": { @@ -18,7 +18,7 @@ // "Root": "mWlR2HshQNhRRE34jg4kdg==", // "Password": "y4uPqlH9ncTgR/I07qpwaA==" //}, - // FIC 預發佈環境 + // FIC 嚙緩嚙緻嚙瘦嚙踝蕭嚙 //"DBConfig": { // "Server": "MWAxcj1mgmbZ8tB6NgApnQ==", // "port": "CY1x+1WYXRCBab3wKnBCOQ==", @@ -26,7 +26,7 @@ // "Root": "pBX64+ALGFnLiHGRFXNh7w==", // "Password": "bOlWkSFsV3qNLkZTJ3UPog==" //}, - // 正式 + // 嚙踝蕭嚙踝蕭 "DBConfig": { "Server": "AVXfxd+IRlLtJ0MCi9HU1g==", "port": "CrEmevYrUsSo7Mkb7Gxn8A==", @@ -50,7 +50,7 @@ "Password": "opuisjyxgyjbjtha", //2022-09-02 update by jiahao "EnableSsl": true }, - "ExceptionTimes": 240, //異常通知時間,單位(分鐘) + "ExceptionTimes": 240, //嚙踝蕭嚙窯嚙緬嚙踝蕭嚙褕塚蕭嚙璀嚙踝蕭嚙(嚙踝蕭嚙踝蕭) "ExceptionTimes_Priority1": 0, "ExceptionTimes_Priority2": 720, "ExceptionTimes_Priority3": 1440 diff --git a/slnSolarPower.sln b/slnSolarPower.sln index 731c6d0..2e666c6 100644 --- a/slnSolarPower.sln +++ b/slnSolarPower.sln @@ -1,12 +1,14 @@ 嚜 Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.31005.135 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32901.215 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SolarPower", "SolarPower\SolarPower.csproj", "{6C88BBD7-5A1F-47B8-BF46-9CBDC73EF2E9}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "solarApp", "solarApp\solarApp.csproj", "{3D54E7DA-4EF8-42A3-85D3-39DD08AC9A0C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "winService", "winService\winService.csproj", "{132A1CF9-2C64-493D-8F8E-1F86AA595D14}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +23,10 @@ Global {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.Build.0 = Release|Any CPU + {132A1CF9-2C64-493D-8F8E-1F86AA595D14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {132A1CF9-2C64-493D-8F8E-1F86AA595D14}.Debug|Any CPU.Build.0 = Debug|Any CPU + {132A1CF9-2C64-493D-8F8E-1F86AA595D14}.Release|Any CPU.ActiveCfg = Release|Any CPU + {132A1CF9-2C64-493D-8F8E-1F86AA595D14}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/solarApp/Properties/launchSettings.json b/solarApp/Properties/launchSettings.json new file mode 100644 index 0000000..b26925d --- /dev/null +++ b/solarApp/Properties/launchSettings.json @@ -0,0 +1,12 @@ +{ + "profiles": { + "solarApp": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "https://localhost:2904;http://localhost:2905" + } + } +} \ No newline at end of file diff --git a/solarApp/Service/getStationSvc.cs b/solarApp/Service/getStationSvc.cs index 7c71f36..a1dcae4 100644 --- a/solarApp/Service/getStationSvc.cs +++ b/solarApp/Service/getStationSvc.cs @@ -13,7 +13,7 @@ namespace solarApp.Service /// public class getStationSvc { - string Connection1 = ""; + public string Connection1 { get; set; } public getStationSvc(string Connection_parame = null) { if (!string.IsNullOrEmpty(Connection_parame)) diff --git a/solarApp/fmArchive.cs b/solarApp/fmArchive.cs index 71528d8..78cbe2b 100644 --- a/solarApp/fmArchive.cs +++ b/solarApp/fmArchive.cs @@ -84,11 +84,11 @@ namespace solarApp #endregion #region 芸瑁 - _timer = new System.Timers.Timer(); - _timer.Interval = 10000; - _timer.SynchronizingObject = this; - _timer.Elapsed += _timer_Elapsed; - _timer.Start(); + //_timer = new System.Timers.Timer(); + //_timer.Interval = 10000; + //_timer.SynchronizingObject = this; + //_timer.Elapsed += _timer_Elapsed; + //_timer.Start(); #endregion // dtselect_station1.Value = DateTime.Today.AddDays(-1); @@ -107,46 +107,46 @@ namespace solarApp private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { - lbMsgStatus.Text = (autoTask) ? "autoTask = true" : "autoTask = false"; - lbMsgStatus.Text += " timespan = " + (DateTime.Now - doTimerTaskTime).TotalMinutes.ToString(); + //lbMsgStatus.Text = (autoTask) ? "autoTask = true" : "autoTask = false"; + //lbMsgStatus.Text += " timespan = " + (DateTime.Now - doTimerTaskTime).TotalMinutes.ToString(); - if (DateTime.Now.Hour >= 2 && DateTime.Now.Minute == 0) - { - doTimerTaskTime = DateTime.Now; - autoTask = false; - } + //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); - bt_archive.PerformClick(); - autoTask = false; - //bt_archive_Click.PerformClick(); - } + //// 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); + // bt_archive.PerformClick(); + // autoTask = false; + // //bt_archive_Click.PerformClick(); + //} - //啣虜 瘥 5頝銝甈 - if ((DateTime.Now.Minute % 5) == 0 && (DateTime.Now.Second) < 30 && (autoTask == false)) - { - doTimerTaskTime = DateTime.Now; - autoTask = true; - lbMsgTitle.Text = "啣虜 " + DateTime.Now.ToString() + " timer start"; - btSyncErr.PerformClick(); - lbMsgTitle.Text = "啣虜 done" + DateTime.Now.ToString() ; - autoTask = false; - //bt_archive_Click.PerformClick(); - } + ////啣虜 瘥 5頝銝甈 + //if ((DateTime.Now.Minute % 5) == 0 && (DateTime.Now.Second) < 30 && (autoTask == false)) + //{ + // doTimerTaskTime = DateTime.Now; + // autoTask = true; + // lbMsgTitle.Text = "啣虜 " + DateTime.Now.ToString() + " timer start"; + // btSyncErr.PerformClick(); + // lbMsgTitle.Text = "啣虜 done" + DateTime.Now.ToString() ; + // autoTask = false; + // //bt_archive_Click.PerformClick(); + //} //冽 蝝航亦 @@ -305,11 +305,7 @@ namespace solarApp private void bt_archive_Click(object sender, EventArgs e) { var site_list = stationSvc.get_station_list(); - - //bt_Sensor.PerformClick(); - //bt_Inv.PerformClick(); - //bt_site.PerformClick(); - //bt_rpt_invDay.PerformClick(); + string date1 = dtSelect1.Value.ToString("yyyy-MM-dd"); string date2 = dtSelect2.Value.ToString("yyyy-MM-dd"); procSensorSvc sensorSvc = new procSensorSvc(); diff --git a/winService/Program.cs b/winService/Program.cs new file mode 100644 index 0000000..202bc9d --- /dev/null +++ b/winService/Program.cs @@ -0,0 +1,11 @@ +using winService; + +IHost host = Host.CreateDefaultBuilder(args) + .UseWindowsService() + .ConfigureServices(services => + { + services.AddHostedService(); + }) + .Build(); + +await host.RunAsync(); diff --git a/winService/Properties/launchSettings.json b/winService/Properties/launchSettings.json new file mode 100644 index 0000000..7aaa119 --- /dev/null +++ b/winService/Properties/launchSettings.json @@ -0,0 +1,11 @@ +嚜篦 + "profiles": { + "winService": { + "commandName": "Project", + "dotnetRunMessages": true, + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Development" + } + } + } +} diff --git a/winService/Worker.cs b/winService/Worker.cs new file mode 100644 index 0000000..75465e8 --- /dev/null +++ b/winService/Worker.cs @@ -0,0 +1,114 @@ +using solarApp.Service; +using System.Configuration; +using System.Xml.Linq; + +namespace winService +{ + public class Worker : BackgroundService + { + private readonly ILogger _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 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(); + } + } + } + + 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 EachDay(DateTime from, DateTime thru) + { + for (var day = from.Date; day.Date <= thru.Date; day = day.AddDays(1)) + yield return day; + } + } +} \ No newline at end of file diff --git a/winService/appsettings.Development.json b/winService/appsettings.Development.json new file mode 100644 index 0000000..92d108d --- /dev/null +++ b/winService/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "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;" +} diff --git a/winService/appsettings.json b/winService/appsettings.json new file mode 100644 index 0000000..93ff8ee --- /dev/null +++ b/winService/appsettings.json @@ -0,0 +1,10 @@ +{ + "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;" + +} diff --git a/winService/winService.csproj b/winService/winService.csproj new file mode 100644 index 0000000..b30ddec --- /dev/null +++ b/winService/winService.csproj @@ -0,0 +1,20 @@ + + + + net6.0 + enable + enable + dotnet-winService-78282A39-DE2D-4E5B-806B-31D6FE6D57E7 + SolarPower.Program + + + + + + + + + + + +