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; using System.Linq; using Microsoft.Extensions.DependencyInjection; using NPOI.SS.Formula.Functions; using Repository.Helper; using Microsoft.AspNetCore.Routing.Matching; namespace Backend.Controllers { public class NiagaraDataSynchronizeController : MybaseController { private readonly IBackendRepository backendRepository; //private readonly IDeviceImportRepository deviceImportRepository; private readonly INiagaraDataSynchronizeRepository niagaraDataSynchronizeRepository; private readonly IBackEndConfigHelper backEndConfig; public NiagaraDataSynchronizeController(IBackendRepository backendRepository, INiagaraDataSynchronizeRepository niagaraDataSynchronizeRepository,IBackEndConfigHelper backEndConfig) { this.backendRepository = backendRepository; //this.deviceImportRepository = deviceImportRepository; this.niagaraDataSynchronizeRepository = niagaraDataSynchronizeRepository; this.backEndConfig = backEndConfig; } public IActionResult Index() { return View(); } /// /// N4匯入列表 /// /// [HttpPost] public Task>> RawDataList() { ApiResult> apiResult = new ApiResult>(); try { var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'"; var variableObix = backendRepository.GetAllAsync(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/"; obixApiConfig.UrlSlot =variableObix.Where(x => x.Name == "url_slot").Select(x => x.Value).FirstOrDefault(); obixApiConfig.TagQuantity = variableObix.Where(x => x.Name == "tag_quantity").Select(x => x.Value).FirstOrDefault(); List ds; string top100 = "";//" top 100 "; webRequestService svc = new webRequestService(); string bql = obixApiConfig.UrlSlot + "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 = svc.obixQuery(obixApiConfig.ApiBase, bql, obixApiConfig.TagQuantity);//三菱 apiResult.Code = "0000"; apiResult.Data = ds; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } //return apiResult; return Task.FromResult(apiResult); } /// /// 比對N4與資料表,有差異在同步至資料表中 /// /// [HttpPost] public async Task> CompareData([FromBody] List ds) { bool result = false; ApiResult apiResult = new ApiResult(); string tag_quantity = backEndConfig.Getobixtag_quantity(); try { string ss = ds.Where(x => x.tag_name != "").FirstOrDefault().tag_name; var building = ""; if (tag_quantity == "5") { building = ss.Split("_")[0]; } else { building = ss.Split("_")[1]; } string LightSwitchLevel = await niagaraDataSynchronizeRepository.getLightSwitchLevel(); //獲取照明開關 是否在 device or device_node await niagaraDataSynchronizeRepository.InsertNiagaraTagList(ds, building, tag_quantity); // 匯入 MySQL table: import_niagara_tag await niagaraDataSynchronizeRepository.DeviceComparison(LightSwitchLevel); await niagaraDataSynchronizeRepository.CheckDiffFullNameAndCover(); await niagaraDataSynchronizeRepository.CheckFullNameEmptyReplaceByDeviceName(); await niagaraDataSynchronizeRepository.InsertBuildingMenu(); await niagaraDataSynchronizeRepository.InsertSubSystemFloor(); result = true; apiResult.Code = "0000"; apiResult.Data = result; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } /// /// N4匯入列表(device_item) /// /// [HttpPost] public Task>> RawDataDevItemList([FromBody] Device_value building) { ApiResult> apiResult = new ApiResult>(); try { List ds; webRequestService svc = new webRequestService(); var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'"; var variableObix = backendRepository.GetAllAsync(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(); ds = svc.obixHisQuery(obixApiConfig.ApiBase + "obix/config/Program/ObixQuery/query/", obixApiConfig.ApiBase + "obix/histories",obixApiConfig.UrlSlot, obixApiConfig.UserName, obixApiConfig.Password); apiResult.Code = "0000"; apiResult.Data = ds.Where(x => x.device_building_tag == building.building).ToList(); ; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } //return apiResult; return Task.FromResult(apiResult); } /// /// 比對N4與資料表,有差異在同步至資料表中(device_item) /// /// [HttpPost] public async Task> DevIteComData([FromBody] List ds) { bool result = false; ApiResult apiResult = new ApiResult(); try { if (ds.Count > 0) { //string ss = ds.Where(x => x.tag_name != "").FirstOrDefault().tag_name; //var building = ss.Split("_")[0]; var building = ds.FirstOrDefault().device_building_tag; await niagaraDataSynchronizeRepository.InsertItemFromNiagara(ds, building); // insert 暫存table import_niagara_item await niagaraDataSynchronizeRepository.DeviceItemComparison(); //insert device_item await niagaraDataSynchronizeRepository.CheckItemDiffFullNameAndCover(); // update device_item.fullname await niagaraDataSynchronizeRepository.ItemCheckFullNameEmptyReplaceByDeviceName(); // 檢查device_item內FullName為空的值,以points取代 await niagaraDataSynchronizeRepository.CheckItemIsShowHistory(); result = true; apiResult.Code = "0000"; apiResult.Data = result; } } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } } }