2022-11-02 17:26:18 +08:00
using Microsoft.AspNetCore.Mvc ;
using Repository.BackendRepository.Interface ;
using System.Collections.Generic ;
using System.Threading.Tasks ;
using System ;
//using Backend.Models;
using Microsoft.Extensions.Logging ;
using Backend.Services ;
using Backend.Services.Implement ;
using Repository.Models ;
2022-12-31 16:49:47 +08:00
using System.Linq ;
2023-01-13 17:28:23 +08:00
using Microsoft.Extensions.DependencyInjection ;
using NPOI.SS.Formula.Functions ;
2023-03-02 14:24:34 +08:00
using Repository.Helper ;
2023-04-25 00:48:12 +08:00
using Microsoft.AspNetCore.Routing.Matching ;
2023-10-02 09:21:31 +08:00
using Newtonsoft.Json ;
2023-11-16 11:32:32 +08:00
using System.Diagnostics ;
2022-11-02 17:26:18 +08:00
namespace Backend.Controllers
{
public class NiagaraDataSynchronizeController : MybaseController < NiagaraDataSynchronizeController >
{
private readonly IBackendRepository backendRepository ;
//private readonly IDeviceImportRepository deviceImportRepository;
private readonly INiagaraDataSynchronizeRepository niagaraDataSynchronizeRepository ;
2023-03-02 14:24:34 +08:00
private readonly IBackEndConfigHelper backEndConfig ;
2023-11-16 11:32:32 +08:00
private Stopwatch stopwatch = new Stopwatch ( ) ;
private Stopwatch stopwatchSection = new Stopwatch ( ) ;
private Stopwatch syncStopWatch = new Stopwatch ( ) ;
2023-11-16 14:47:34 +08:00
private static long syncWatchTime = 0 ;
2024-01-15 15:29:23 +08:00
public static bool isFirstAsync = false ;
2023-11-16 11:32:32 +08:00
2023-03-02 14:24:34 +08:00
public NiagaraDataSynchronizeController ( IBackendRepository backendRepository , INiagaraDataSynchronizeRepository niagaraDataSynchronizeRepository , IBackEndConfigHelper backEndConfig )
2022-11-02 17:26:18 +08:00
{
this . backendRepository = backendRepository ;
//this.deviceImportRepository = deviceImportRepository;
this . niagaraDataSynchronizeRepository = niagaraDataSynchronizeRepository ;
2023-03-02 14:24:34 +08:00
this . backEndConfig = backEndConfig ;
2022-11-02 17:26:18 +08:00
}
public IActionResult Index ( )
{
return View ( ) ;
}
/// <summary>
/// N4匯入列表
/// </summary>
/// <returns></returns>
[HttpPost]
public Task < ApiResult < List < Device_value > > > RawDataList ( )
{
2023-11-16 11:32:32 +08:00
stopwatch = new Stopwatch ( ) ;
stopwatch . Start ( ) ;
2022-11-02 17:26:18 +08:00
ApiResult < List < Device_value > > apiResult = new ApiResult < List < Device_value > > ( ) ;
try
{
2023-04-24 18:29:45 +08:00
var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'" ;
var variableObix = backendRepository . GetAllAsync < Backend . Models . KeyValue > ( sqlObix ) . Result ;
var obixApiConfig = new Backend . Models . ObixApiConfig ( ) ;
EDFunction ed = new EDFunction ( ) ;
obixApiConfig . ApiBase = variableObix . Where ( x = > x . Name = = "ApiBase" ) . Select ( x = > x . Value ) . FirstOrDefault ( ) + "obix/config/Program/ObixQuery/query/" ;
2023-05-10 15:14:46 +08:00
//obixApiConfig.UrlSlot =variableObix.Where(x => x.Name == "url_slot").Select(x => x.Value).FirstOrDefault();
2023-04-26 01:16:05 +08:00
obixApiConfig . TagQuantity = variableObix . Where ( x = > x . Name = = "tag_quantity" ) . Select ( x = > x . Value ) . FirstOrDefault ( ) ;
2023-04-26 17:43:55 +08:00
obixApiConfig . UserName = variableObix . Where ( x = > x . Name = = "UserName" ) . Select ( x = > x . Value ) . FirstOrDefault ( ) ;
obixApiConfig . Password = variableObix . Where ( x = > x . Name = = "Password" ) . Select ( x = > x . Value ) . FirstOrDefault ( ) ;
2023-04-24 18:29:45 +08:00
2023-05-10 15:14:46 +08:00
var ds = new List < Device_value > ( ) ;
2022-12-22 10:11:31 +08:00
2022-11-30 18:22:40 +08:00
string top100 = "" ; //" top 100 ";
2022-11-02 17:26:18 +08:00
webRequestService svc = new webRequestService ( ) ;
2023-05-10 15:14:46 +08:00
var urlSlots = backendRepository . GetAllAsync < string > ( "select obixSlot from building where deleted = 0" ) . Result ;
2023-11-16 11:32:32 +08:00
stopwatchSection = new Stopwatch ( ) ;
stopwatchSection . Start ( ) ;
2023-10-04 14:41:52 +08:00
foreach ( var us in urlSlots )
2023-05-10 15:14:46 +08:00
{
string bql = us + "bql:select " + top100 + " * from baja:Folder " ;
//ds = svc.obixQuery("http://192.168.0.136:8080/obix/config/Arena/Program/ObixQuery/query/", bql);
//ds = svc.obixQuery("http://localhost:8080/obix/config/Program/ObixQuery/query/", bql);
ds . AddRange ( svc . obixQuery ( obixApiConfig . ApiBase , bql , obixApiConfig . TagQuantity , obixApiConfig . UserName , obixApiConfig . Password ) ) ; //三菱
}
2023-11-16 11:32:32 +08:00
stopwatchSection . Stop ( ) ;
niagaraDataSynchronizeRepository . KeepTimeLog ( "GetDataFromNiagara" , stopwatchSection . ElapsedMilliseconds ) ;
2022-11-02 17:26:18 +08:00
apiResult . Code = "0000" ;
apiResult . Data = ds ;
}
catch ( Exception exception )
{
apiResult . Code = "9999" ;
2023-05-15 17:43:00 +08:00
apiResult . Msg = "【" + controllerName + "/" + actionName + "】" + exception . Message ;
2023-08-25 16:45:52 +08:00
Logger . LogError ( "【" + controllerName + "/" + actionName + "】" + exception . Message + Environment . NewLine + exception . StackTrace ) ;
// Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
2022-11-02 17:26:18 +08:00
}
//return apiResult;
2023-11-16 11:32:32 +08:00
stopwatchSection . Stop ( ) ;
niagaraDataSynchronizeRepository . KeepTimeLog ( "RawDataList" , stopwatch . ElapsedMilliseconds ) ;
2022-11-02 17:26:18 +08:00
return Task . FromResult ( apiResult ) ;
}
/// <summary>
/// 比對N4與資料表, 有差異在同步至資料表中
/// </summary>
/// <returns></returns>
[HttpPost]
2022-12-31 16:49:47 +08:00
public async Task < ApiResult < bool > > CompareData ( [ FromBody ] List < Device_value > ds )
2022-11-02 17:26:18 +08:00
{
2023-11-16 14:47:34 +08:00
syncStopWatch = new Stopwatch ( ) ;
2023-11-16 11:32:32 +08:00
syncStopWatch . Start ( ) ;
2023-11-16 14:47:34 +08:00
syncWatchTime = 0 ;
2023-11-16 11:32:32 +08:00
stopwatch = new Stopwatch ( ) ;
stopwatch . Start ( ) ;
2022-11-02 17:26:18 +08:00
bool result = false ;
ApiResult < bool > apiResult = new ApiResult < bool > ( ) ;
2023-05-15 17:43:00 +08:00
var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'" ;
var variableObix = await backendRepository . GetAllAsync < Backend . Models . KeyValue > ( sqlObix ) ;
string tag_quantity = variableObix . Where ( x = > x . Name = = "tag_quantity" ) . Select ( x = > x . Value ) . FirstOrDefault ( ) ;
2023-04-11 10:28:33 +08:00
2022-11-02 17:26:18 +08:00
try
2023-10-04 14:41:52 +08:00
{
2023-11-16 11:32:32 +08:00
stopwatchSection = new Stopwatch ( ) ;
stopwatchSection . Start ( ) ;
2023-04-21 02:06:32 +08:00
string ss = ds . Where ( x = > x . tag_name ! = "" ) . FirstOrDefault ( ) . tag_name ;
2023-05-10 15:14:46 +08:00
var data = ds . Where ( x = > x . tag_name ! = "" ) ;
List < string > building = null ;
2023-04-25 00:48:12 +08:00
if ( tag_quantity = = "5" )
{
2023-05-10 15:14:46 +08:00
//building.Add(ss.Split("_")[0]);
building = data . GroupBy ( x = > x . tag_name . Split ( "_" ) [ 0 ] ) . Select ( x = > x . Key ) . ToList ( ) ;
2023-04-25 00:48:12 +08:00
}
else
{
2023-05-10 15:14:46 +08:00
//building.Add(ss.Split("_")[1]);
building = data . GroupBy ( x = > x . tag_name . Split ( "_" ) [ 1 ] ) . Select ( x = > x . Key ) . ToList ( ) ;
2023-06-13 09:24:48 +08:00
}
2023-11-16 11:32:32 +08:00
stopwatchSection . Stop ( ) ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "filter building tag" , stopwatchSection . ElapsedMilliseconds ) ;
2023-06-13 09:24:48 +08:00
2023-11-16 11:32:32 +08:00
stopwatchSection = new Stopwatch ( ) ;
stopwatchSection . Start ( ) ;
2023-08-10 10:32:53 +08:00
string LightSwitchLevel = await niagaraDataSynchronizeRepository . getLightSwitchLevel ( ) ; //獲取照明開關 是否在 device or device_node
2023-11-16 11:32:32 +08:00
stopwatchSection . Stop ( ) ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "getLightSwitchLevel" , stopwatchSection . ElapsedMilliseconds ) ;
stopwatchSection = new Stopwatch ( ) ;
stopwatchSection . Start ( ) ;
2023-08-10 10:32:53 +08:00
await niagaraDataSynchronizeRepository . InsertNiagaraTagList ( ds , building , tag_quantity ) ; // 匯入 MySQL table: import_niagara_tag
2023-11-16 11:32:32 +08:00
stopwatchSection . Stop ( ) ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "InsertNiagaraTagList" , stopwatchSection . ElapsedMilliseconds ) ;
stopwatchSection = new Stopwatch ( ) ;
stopwatchSection . Start ( ) ;
2023-08-10 10:32:53 +08:00
await niagaraDataSynchronizeRepository . DeviceComparison ( LightSwitchLevel ) ;
2023-11-16 11:32:32 +08:00
stopwatchSection . Stop ( ) ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "DeviceComparison" , stopwatchSection . ElapsedMilliseconds ) ;
stopwatchSection = new Stopwatch ( ) ;
stopwatchSection . Start ( ) ;
2023-08-18 17:33:53 +08:00
await niagaraDataSynchronizeRepository . CheckDiffFullNameAndCover ( ) ; // *
2023-11-16 11:32:32 +08:00
stopwatchSection . Stop ( ) ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "CheckDiffFullNameAndCover" , stopwatchSection . ElapsedMilliseconds ) ;
stopwatchSection = new Stopwatch ( ) ;
stopwatchSection . Start ( ) ;
2023-08-10 10:32:53 +08:00
await niagaraDataSynchronizeRepository . CheckFullNameEmptyReplaceByDeviceName ( ) ;
2023-11-16 11:32:32 +08:00
stopwatchSection . Stop ( ) ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "CheckFullNameEmptyReplaceByDeviceName" , stopwatchSection . ElapsedMilliseconds ) ;
stopwatchSection = new Stopwatch ( ) ;
stopwatchSection . Start ( ) ;
2023-08-10 10:32:53 +08:00
await niagaraDataSynchronizeRepository . InsertBuildingMenu ( ) ;
2023-11-16 11:32:32 +08:00
stopwatchSection . Stop ( ) ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "InsertBuildingMenu" , stopwatchSection . ElapsedMilliseconds ) ;
stopwatchSection = new Stopwatch ( ) ;
stopwatchSection . Start ( ) ;
2023-08-10 10:32:53 +08:00
await niagaraDataSynchronizeRepository . InsertSubSystemFloor ( ) ;
2023-11-16 11:32:32 +08:00
stopwatchSection . Stop ( ) ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "InsertSubSystemFloor" , stopwatchSection . ElapsedMilliseconds ) ;
stopwatchSection = new Stopwatch ( ) ;
stopwatchSection . Start ( ) ;
2023-08-10 10:32:53 +08:00
await niagaraDataSynchronizeRepository . InsertFloor ( ) ;
2023-11-16 11:32:32 +08:00
stopwatchSection . Stop ( ) ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "InsertFloor" , stopwatchSection . ElapsedMilliseconds ) ;
stopwatchSection = new Stopwatch ( ) ;
stopwatchSection . Start ( ) ;
2023-08-10 10:32:53 +08:00
await this . DeviceDisasterAsync ( ) ;
2023-11-16 11:32:32 +08:00
stopwatchSection . Stop ( ) ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "DeviceDisasterAsync" , stopwatchSection . ElapsedMilliseconds ) ;
2022-11-02 17:26:18 +08:00
result = true ;
apiResult . Code = "0000" ;
apiResult . Data = result ;
}
catch ( Exception exception )
{
apiResult . Code = "9999" ;
2023-05-15 17:43:00 +08:00
apiResult . Msg = "【" + controllerName + "/" + actionName + "】" + exception . Message ;
2023-08-22 09:58:02 +08:00
Logger . LogError ( "【" + controllerName + "/" + actionName + "】" + exception . Message + Environment . NewLine + exception . StackTrace ) ;
2022-11-02 17:26:18 +08:00
}
2023-11-16 11:32:32 +08:00
stopwatch . Stop ( ) ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "CompareData" , stopwatch . ElapsedMilliseconds ) ;
2023-11-16 14:47:34 +08:00
syncStopWatch . Stop ( ) ;
syncWatchTime + = syncStopWatch . ElapsedMilliseconds ;
2022-11-02 17:26:18 +08:00
return apiResult ;
}
2023-01-13 17:28:23 +08:00
/// <summary>
/// N4匯入列表(device_item)
/// </summary>
/// <returns></returns>
[HttpPost]
2023-04-21 17:40:18 +08:00
public Task < ApiResult < List < ImpNiaItem > > > RawDataDevItemList ( [ FromBody ] Device_value building )
2023-01-13 17:28:23 +08:00
{
2023-11-16 14:47:34 +08:00
syncStopWatch = new Stopwatch ( ) ;
syncStopWatch . Start ( ) ;
2023-01-13 17:28:23 +08:00
ApiResult < List < ImpNiaItem > > apiResult = new ApiResult < List < ImpNiaItem > > ( ) ;
2023-11-16 11:32:32 +08:00
stopwatch = new Stopwatch ( ) ;
stopwatch . Start ( ) ;
2023-01-13 17:28:23 +08:00
try
{
2023-11-16 11:32:32 +08:00
stopwatchSection = new Stopwatch ( ) ;
stopwatchSection . Start ( ) ;
2023-05-10 15:14:46 +08:00
var ds = new List < ImpNiaItem > ( ) ;
2023-01-13 17:28:23 +08:00
webRequestService svc = new webRequestService ( ) ;
2023-04-24 18:29:45 +08:00
var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'" ;
var variableObix = backendRepository . GetAllAsync < Backend . Models . KeyValue > ( sqlObix ) . Result ;
var obixApiConfig = new Backend . Models . ObixApiConfig ( ) ;
EDFunction ed = new EDFunction ( ) ;
obixApiConfig . ApiBase = variableObix . Where ( x = > x . Name = = "ApiBase" ) . Select ( x = > x . Value ) . FirstOrDefault ( ) ;
2023-04-26 01:16:05 +08:00
obixApiConfig . UserName = variableObix . Where ( x = > x . Name = = "UserName" ) . Select ( x = > x . Value ) . FirstOrDefault ( ) ;
obixApiConfig . Password = variableObix . Where ( x = > x . Name = = "Password" ) . Select ( x = > x . Value ) . FirstOrDefault ( ) ;
2023-10-04 14:41:52 +08:00
obixApiConfig . UrlSlot = variableObix . Where ( x = > x . Name = = "url_slot" ) . Select ( x = > x . Value ) . FirstOrDefault ( ) ;
2023-08-03 16:20:59 +08:00
var urlSlots = backendRepository . GetAllAsync < BuildingDetail > ( "select obixSlot, area_tag from building where deleted = 0" ) . Result ;
2023-10-04 14:41:52 +08:00
var data2 = new List < DeviceControlPoint > ( ) ;
foreach ( var us in urlSlots )
2023-05-10 15:14:46 +08:00
{
2023-08-03 16:20:59 +08:00
var data = svc . obixHisQuery ( obixApiConfig . ApiBase + "obix/config/Program/ObixQuery/query/" , obixApiConfig . ApiBase + "obix/histories" , us . obixSlot ,
obixApiConfig . UserName , obixApiConfig . Password , us . area_tag ) ;
2023-10-04 14:41:52 +08:00
data2 . AddRange ( svc . obixAllDevicePoint ( obixApiConfig . ApiBase + "obix/config/Program/ObixQuery/query/" , obixApiConfig . ApiBase + "obix/histories" , us . obixSlot ,
obixApiConfig . UserName , obixApiConfig . Password , us . area_tag ) ) ;
2023-05-10 15:14:46 +08:00
ds . AddRange ( data ) ;
}
2023-10-04 14:41:52 +08:00
if ( data2 . Any ( ) )
{
niagaraDataSynchronizeRepository . DeviceControlPoint ( data2 ) ;
}
2023-11-16 11:32:32 +08:00
stopwatchSection . Stop ( ) ;
niagaraDataSynchronizeRepository . KeepTimeLog ( "get device_item from niagara and record all device point" , stopwatchSection . ElapsedMilliseconds ) ;
2023-05-10 15:14:46 +08:00
var buildings = backendRepository . GetAllAsync < string > ( "select building_tag from building where deleted = 0" ) . Result ;
2023-01-13 17:28:23 +08:00
apiResult . Code = "0000" ;
2023-05-10 15:14:46 +08:00
apiResult . Data = ds . Where ( x = > buildings . Contains ( x . device_building_tag ) ) . ToList ( ) ;
2023-01-13 17:28:23 +08:00
}
catch ( Exception exception )
{
apiResult . Code = "9999" ;
2023-05-15 17:43:00 +08:00
apiResult . Msg = "【" + controllerName + "/" + actionName + "】" + exception . Message ;
2023-08-22 09:58:02 +08:00
Logger . LogError ( "【" + controllerName + "/" + actionName + "】" + exception . Message + Environment . NewLine + exception . StackTrace ) ;
2023-01-13 17:28:23 +08:00
}
//return apiResult;
2023-11-16 11:32:32 +08:00
stopwatch . Stop ( ) ;
niagaraDataSynchronizeRepository . KeepTimeLog ( "RawDataDevItemList" , stopwatch . ElapsedMilliseconds ) ;
2023-11-16 14:47:34 +08:00
syncStopWatch . Stop ( ) ;
syncWatchTime + = syncStopWatch . ElapsedMilliseconds ;
2023-01-13 17:28:23 +08:00
return Task . FromResult ( apiResult ) ;
}
/// <summary>
/// 比對N4與資料表, 有差異在同步至資料表中(device_item)
/// </summary>
/// <returns></returns>
[HttpPost]
2024-01-15 15:29:23 +08:00
public async Task < ApiResult < bool > > DevIteComData ( )
2023-01-13 17:28:23 +08:00
{
2023-11-16 14:47:34 +08:00
syncStopWatch = new Stopwatch ( ) ;
syncStopWatch . Start ( ) ;
2023-11-16 11:32:32 +08:00
stopwatch = new Stopwatch ( ) ;
stopwatch . Start ( ) ;
2023-01-13 17:28:23 +08:00
bool result = false ;
2024-01-15 15:29:23 +08:00
//var oldDs = JsonConvert.DeserializeObject<List<ImpNiaItem>>(JsonConvert.SerializeObject(ds));
2023-01-13 17:28:23 +08:00
ApiResult < bool > apiResult = new ApiResult < bool > ( ) ;
try
{
2024-01-15 15:29:23 +08:00
//string ss = ds.Where(x => x.tag_name != "").FirstOrDefault().tag_name;
//var building = ss.Split("_")[0];
//var building = ds.GroupBy(x => x.device_building_tag).Select(x => x.Key).ToList();
2023-10-11 22:35:33 +08:00
2024-01-15 15:29:23 +08:00
stopwatchSection = new Stopwatch ( ) ;
stopwatchSection . Start ( ) ;
await niagaraDataSynchronizeRepository . DeviceItemComparison ( ) ; //insert device_item
stopwatchSection . Stop ( ) ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "DeviceItemComparison" , stopwatchSection . ElapsedMilliseconds ) ;
2023-11-16 11:32:32 +08:00
2024-01-15 15:29:23 +08:00
stopwatchSection = new Stopwatch ( ) ;
stopwatchSection . Start ( ) ;
await niagaraDataSynchronizeRepository . CheckItemDiffFullNameAndCover ( ) ; // update device_item.fullname
stopwatchSection . Stop ( ) ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "CheckItemDiffFullNameAndCover" , stopwatchSection . ElapsedMilliseconds ) ;
2023-11-16 11:32:32 +08:00
2024-01-15 15:29:23 +08:00
stopwatchSection = new Stopwatch ( ) ;
stopwatchSection . Start ( ) ;
await niagaraDataSynchronizeRepository . ItemCheckFullNameEmptyReplaceByDeviceName ( ) ; // 檢查device_item內FullName為空的值, 以points取代
stopwatchSection . Stop ( ) ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "ItemCheckFullNameEmptyReplaceByDeviceName" , stopwatchSection . ElapsedMilliseconds ) ;
2023-11-16 11:32:32 +08:00
2024-01-15 15:29:23 +08:00
stopwatchSection = new Stopwatch ( ) ;
stopwatchSection . Start ( ) ;
await niagaraDataSynchronizeRepository . CheckItemIsShowHistory ( ) ;
stopwatchSection . Stop ( ) ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "CheckItemIsShowHistory" , stopwatchSection . ElapsedMilliseconds ) ;
2023-10-02 09:21:31 +08:00
2024-01-15 15:29:23 +08:00
if ( isFirstAsync )
isFirstAsync = false ;
2023-04-21 02:06:32 +08:00
2024-01-15 15:29:23 +08:00
result = true ;
apiResult . Code = "0000" ;
apiResult . Data = result ;
2023-01-13 17:28:23 +08:00
}
catch ( Exception exception )
{
2024-01-15 15:29:23 +08:00
isFirstAsync = false ;
2023-01-13 17:28:23 +08:00
apiResult . Code = "9999" ;
2023-05-15 17:43:00 +08:00
apiResult . Msg = "【" + controllerName + "/" + actionName + "】" + exception . Message ;
2023-08-22 09:58:02 +08:00
Logger . LogError ( "【" + controllerName + "/" + actionName + "】" + exception . Message + Environment . NewLine + exception . StackTrace ) ;
2023-01-13 17:28:23 +08:00
}
2023-11-16 11:32:32 +08:00
stopwatch . Stop ( ) ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "DevIteComData" , stopwatch . ElapsedMilliseconds ) ;
syncStopWatch . Stop ( ) ;
2023-11-16 14:47:34 +08:00
syncWatchTime + = syncStopWatch . ElapsedMilliseconds ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "Full sync usage record" , syncWatchTime ) ;
2023-01-13 17:28:23 +08:00
return apiResult ;
}
2023-06-08 15:41:40 +08:00
private async Task DeviceDisasterAsync ( )
{
2023-11-16 11:32:32 +08:00
stopwatchSection = new Stopwatch ( ) ;
stopwatchSection . Start ( ) ;
2023-06-08 15:41:40 +08:00
var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'" ;
var variableObix = await backendRepository . GetAllAsync < Backend . Models . KeyValue > ( sqlObix ) ;
var obixApiConfig = new Models . ObixApiConfig ( ) ;
obixApiConfig . ApiBase = variableObix . Where ( x = > x . Name = = "ApiBase" ) . Select ( x = > x . Value ) . FirstOrDefault ( ) ;
obixApiConfig . UserName = variableObix . Where ( x = > x . Name = = "UserName" ) . Select ( x = > x . Value ) . FirstOrDefault ( ) ;
obixApiConfig . Password = variableObix . Where ( x = > x . Name = = "Password" ) . Select ( x = > x . Value ) . FirstOrDefault ( ) ;
obixApiConfig . TagQuantity = variableObix . Where ( x = > x . Name = = "tag_quantity" ) . Select ( x = > x . Value ) . FirstOrDefault ( ) ;
if ( obixApiConfig . TagQuantity = = "5" )
{
List < Device_value_disaster > dv = new List < Device_value_disaster > ( ) ;
List < string > urlSlots = await backendRepository . GetAllAsync < string > ( "select obixSlot from building where deleted = 0" ) ;
List < string > disasterTag = await backendRepository . GetAllAsync < string > ( "select system_value from variable where system_type = 'disaster' and deleted = 0" ) ;
webRequestService svc = new webRequestService ( ) ;
foreach ( var dt in disasterTag )
{
string bql = "neql:hs:geoAddr=\"ER" + dt + "\"" ;
dv . AddRange ( svc . obixDevDisaster ( obixApiConfig . ApiBase + "obix/config/Arena/Program/ObixQuery/query/" , bql , obixApiConfig . TagQuantity , obixApiConfig . UserName ,
obixApiConfig . Password , dt ) ) ;
}
await niagaraDataSynchronizeRepository . DeviceDisasterAysnc ( dv ) ;
}
2023-11-16 11:32:32 +08:00
stopwatchSection . Stop ( ) ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "DeviceDisasterAsync" , stopwatchSection . ElapsedMilliseconds ) ;
2023-06-08 15:41:40 +08:00
}
2024-01-15 15:29:23 +08:00
/// <summary>
/// 歷史資料處理
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task < ApiResult < bool > > GetHistoryData ( )
{
ApiResult < bool > apiResult = new ApiResult < bool > ( ) ;
apiResult . Data = false ;
apiResult . Code = "9999" ;
try
{
#region 歷 史 資 料 處 理
List < ImpNiaItem > ds = new List < ImpNiaItem > ( ) ;
var building = await backendRepository . GetAllAsync < string > ( "select building_tag from building where deleted = 0" ) ;
var isDome = await backendRepository . GetOneAsync < string > ( "select system_value from variable where deleted = 0 and system_type = 'project_name';" ) ;
var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'" ;
var variableObix = backendRepository . GetAllAsync < Backend . Models . KeyValue > ( sqlObix ) . Result ;
var obixApiConfig = new Backend . Models . ObixApiConfig ( ) ;
EDFunction ed = new EDFunction ( ) ;
obixApiConfig . ApiBase = variableObix . Where ( x = > x . Name = = "ApiBase" ) . Select ( x = > x . Value ) . FirstOrDefault ( ) ;
obixApiConfig . UserName = variableObix . Where ( x = > x . Name = = "UserName" ) . Select ( x = > x . Value ) . FirstOrDefault ( ) ;
obixApiConfig . Password = variableObix . Where ( x = > x . Name = = "Password" ) . Select ( x = > x . Value ) . FirstOrDefault ( ) ;
//obixApiConfig.UrlSlot = variableObix.Where(x => x.Name == "url_slot").Select(x => x.Value).FirstOrDefault();
webRequestService svc = new webRequestService ( ) ;
stopwatchSection = new Stopwatch ( ) ;
stopwatchSection . Start ( ) ;
var data = svc . obixHisQuery_v2 ( obixApiConfig . ApiBase + "obix/config/Program/ObixQuery/query/" , obixApiConfig . ApiBase + "obix/histories" , "" ,
obixApiConfig . UserName , obixApiConfig . Password ) ;
ds . AddRange ( data ) ;
stopwatchSection . Stop ( ) ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "Get history device point from niagara" , stopwatchSection . ElapsedMilliseconds ) ;
// save to history
stopwatchSection = new Stopwatch ( ) ;
stopwatchSection . Start ( ) ;
await niagaraDataSynchronizeRepository . InsertItemFromNiagara_History ( ds , building ) ; // insert 暫存table import_niagara_item_history
stopwatchSection . Stop ( ) ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "InsertItemFromNiagara_History" , stopwatchSection . ElapsedMilliseconds ) ;
if ( ! string . IsNullOrEmpty ( isDome ) & & isDome . Contains ( "ibms_dome" ) )
{
stopwatchSection = new Stopwatch ( ) ;
stopwatchSection . Start ( ) ;
await niagaraDataSynchronizeRepository . RecoverSysTag ( "import_niagara_item_history" ) ; // recover missing deivce_system_tag
stopwatchSection . Stop ( ) ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "RecoverSysTag import_niagara_item_history" , stopwatchSection . ElapsedMilliseconds ) ;
}
#endregion
apiResult . Code = "0000" ;
apiResult . Data = true ;
isFirstAsync = true ;
}
catch ( Exception exception )
{
apiResult . Code = "9999" ;
apiResult . Msg = "【" + controllerName + "/" + actionName + "】" + exception . Message ;
Logger . LogError ( "【" + controllerName + "/" + actionName + "】" + exception . Message + Environment . NewLine + exception . StackTrace ) ;
}
return apiResult ;
}
/// <summary>
/// 設備點位處理
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task < ApiResult < bool > > DevItemInsert ( [ FromBody ] List < ImpNiaItem > ds )
{
var oldDs = JsonConvert . DeserializeObject < List < ImpNiaItem > > ( JsonConvert . SerializeObject ( ds ) ) ;
ApiResult < bool > apiResult = new ApiResult < bool > ( ) ;
apiResult . Data = false ;
apiResult . Code = "9999" ;
try
{
if ( ds . Count > 0 )
{
var building = await backendRepository . GetAllAsync < string > ( "select building_tag from building where deleted = 0" ) ;
stopwatchSection = new Stopwatch ( ) ;
stopwatchSection . Start ( ) ;
await niagaraDataSynchronizeRepository . InsertItemFromNiagara ( oldDs , building , isFirstAsync ) ; // insert 暫存table import_niagara_item
stopwatchSection . Stop ( ) ;
await niagaraDataSynchronizeRepository . KeepTimeLog ( "InsertItemFromNiagara" , stopwatchSection . ElapsedMilliseconds ) ;
if ( isFirstAsync )
isFirstAsync = false ;
}
apiResult . Code = "0000" ;
apiResult . Data = true ;
}
catch ( Exception exception )
{
apiResult . Code = "9999" ;
apiResult . Msg = "【" + controllerName + "/" + actionName + "】" + exception . Message ;
Logger . LogError ( "【" + controllerName + "/" + actionName + "】" + exception . Message + Environment . NewLine + exception . StackTrace ) ;
}
return apiResult ;
}
2022-11-02 17:26:18 +08:00
}
2024-01-15 15:29:23 +08:00
}