113 lines
5.8 KiB
C#
113 lines
5.8 KiB
C#
|
using Backend.Models;
|
|||
|
using BackendWorkerService.Models;
|
|||
|
using BackendWorkerService.Services.Implement;
|
|||
|
using Microsoft.Extensions.Logging;
|
|||
|
using Newtonsoft.Json;
|
|||
|
using Newtonsoft.Json.Linq;
|
|||
|
using Quartz;
|
|||
|
using Repository.BackendRepository.Interface;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Diagnostics;
|
|||
|
using System.IO;
|
|||
|
using System.Linq;
|
|||
|
using System.Net;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Xml;
|
|||
|
|
|||
|
namespace BackendWorkerService.Quartz.Jobs
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 電錶歸檔,每小時執行,只執行小時歸檔
|
|||
|
/// </summary>
|
|||
|
[DisallowConcurrentExecution]
|
|||
|
class ArchiveSolarHourJob : IJob
|
|||
|
{
|
|||
|
private readonly ILogger<ArchiveSolarHourJob> logger;
|
|||
|
private readonly IBackgroundServicePostgresqlRepository backgroundServiceRepository;
|
|||
|
private readonly ILogger<Task_Detail> loggers;
|
|||
|
|
|||
|
public ArchiveSolarHourJob(
|
|||
|
ILogger<ArchiveSolarHourJob> logger,
|
|||
|
IBackgroundServicePostgresqlRepository backgroundServiceRepository, ILogger<Task_Detail> loggers)
|
|||
|
{
|
|||
|
this.logger = logger;
|
|||
|
this.backgroundServiceRepository = backgroundServiceRepository;
|
|||
|
this.loggers = loggers;
|
|||
|
}
|
|||
|
|
|||
|
public async Task Execute(IJobExecutionContext context)
|
|||
|
{
|
|||
|
Task_Detail task_Detail = new Task_Detail(loggers, backgroundServiceRepository);
|
|||
|
try
|
|||
|
{
|
|||
|
if (await task_Detail.GetNeedWorkTask("ArchiveSolarHourJob", "Hour"))
|
|||
|
{
|
|||
|
await task_Detail.InsertWorkTime("ArchiveSolarHourJob", "Hour", "太陽能時任務開始");
|
|||
|
EDFunction ed = new EDFunction();
|
|||
|
XmlDocument xmlDocument = new XmlDocument();
|
|||
|
var solarService = new SolarService();
|
|||
|
var deviceSyncData = new List<device>();
|
|||
|
var devicePointSyncData = new List<device_point>();
|
|||
|
var sites = new List<site>();
|
|||
|
string bql = string.Empty;
|
|||
|
string sql = string.Empty;
|
|||
|
|
|||
|
#region 取得obix 設定
|
|||
|
var obixApiConfig = new ObixApiConfig();
|
|||
|
var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'";
|
|||
|
var variableObix = await backgroundServiceRepository.GetAllAsync<KeyValue>(sqlObix);
|
|||
|
|
|||
|
obixApiConfig.ApiBase = variableObix.Where(x => x.Name == "ApiBase").Select(x => x.Value).FirstOrDefault();
|
|||
|
obixApiConfig.UserName = ed.AESDecrypt(variableObix.Where(x => x.Name == "UserName").Select(x => x.Value).FirstOrDefault());
|
|||
|
obixApiConfig.Password = ed.AESDecrypt(variableObix.Where(x => x.Name == "Password").Select(x => x.Value).FirstOrDefault());
|
|||
|
|
|||
|
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(obixApiConfig.UserName + ":" + obixApiConfig.Password));
|
|||
|
#endregion 取得obix 設定
|
|||
|
|
|||
|
#region sync solar data
|
|||
|
await solarService.SyncSiteInfo(logger, sites, obixApiConfig);
|
|||
|
bql = "neql:solar:archive|bql:select slotPath, name,displayName";
|
|||
|
await solarService.SyncDevice(logger, deviceSyncData, obixApiConfig, bql, sites);
|
|||
|
await solarService.ImportDevice(logger, backgroundServiceRepository, deviceSyncData);
|
|||
|
bql = "neql:solar:archive and n:history|bql:select displayName, slotPath";
|
|||
|
await solarService.SyncDevicePoint(logger, backgroundServiceRepository, devicePointSyncData, obixApiConfig, bql);
|
|||
|
await solarService.ImportDevicePoint(logger, backgroundServiceRepository, devicePointSyncData);
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 找出所有solar設備
|
|||
|
sql = "select * from device where is_link = '1'";
|
|||
|
var deviceData = await backgroundServiceRepository.GetAllAsync<device>(sql);
|
|||
|
#endregion 找出所有solar設備
|
|||
|
|
|||
|
#region 找出所有solar系統的點位
|
|||
|
sql = "select * from device_point where is_link = '1'";
|
|||
|
var devicePointData = await backgroundServiceRepository.GetAllAsync<device_point>(sql);
|
|||
|
#endregion 找出所有solar系統的點位
|
|||
|
|
|||
|
if (deviceData.Any() && devicePointData.Any())
|
|||
|
{
|
|||
|
var now = DateTime.Now;
|
|||
|
var preHour = now.AddHours(-1); //取得前一小時
|
|||
|
var startTimestamp = $"{preHour.ToString("yyyy-MM-dd")}T{preHour.ToString("HH")}:00:00.000+08:00";
|
|||
|
var endTimestamp = $"{now.ToString("yyyy-MM-dd")}T{now.ToString("HH")}:00:00.000+08:00";
|
|||
|
string intervalValue = "PT1H";
|
|||
|
var data_value = new List<data_value>();
|
|||
|
|
|||
|
await solarService.SyncDataValue(logger, data_value, obixApiConfig);
|
|||
|
await solarService.SyncData(logger, backgroundServiceRepository, obixApiConfig, deviceData, devicePointData, startTimestamp, endTimestamp, intervalValue, data_value, false);
|
|||
|
}
|
|||
|
|
|||
|
await task_Detail.InsertWorkTime_End("ArchiveSolarHourJob", "Hour", "太陽能時任務完成");
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception exception)
|
|||
|
{
|
|||
|
await task_Detail.WorkFail("ArchiveSolarHourJob", "Hour", exception.ToString());
|
|||
|
logger.LogError("【ArchiveSolarHourJob】【任務失敗】");
|
|||
|
logger.LogError("【ArchiveSolarHourJob】【任務失敗】[Exception]:{0}", exception.ToString());
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|