335 lines
15 KiB
Plaintext
335 lines
15 KiB
Plaintext
@{
|
||
ViewData["MainNum"] = "2";
|
||
ViewData["SubNum"] = "3";
|
||
ViewData["Title"] = "Niagara資料同步";
|
||
}
|
||
|
||
<ol class="breadcrumb page-breadcrumb">
|
||
<li class="breadcrumb-item"><a href="javascript:void(0);">首頁</a></li>
|
||
<li class="breadcrumb-item">設備管理</li>
|
||
<li class="breadcrumb-item active">Niagara資料同步</li>
|
||
<li class="position-absolute pos-top pos-right d-none d-sm-block"><span class="js-get-date"></span></li>
|
||
</ol>
|
||
|
||
<div class="row">
|
||
<div class="col-xl-12">
|
||
<div id="panel-5" class="panel">
|
||
<div class="panel-container show">
|
||
<div class="panel-content">
|
||
|
||
<div class="form-row align-items-center">
|
||
<div class="col-12 mb-3">
|
||
<div class="custom-control custom-radio custom-control-inline">
|
||
<input type="radio" id="Building_B" name="Building" value="B" class="custom-control-input">
|
||
<label class="custom-control-label" for="Building_B"></label>
|
||
</div>
|
||
@*<div class="custom-control custom-radio custom-control-inline">
|
||
<input type="radio" id="Building_O" name="Building" value="O" class="custom-control-input">
|
||
<label class="custom-control-label" for="Building_O">辦公棟</label>
|
||
</div>*@
|
||
</div>
|
||
</div>
|
||
|
||
<div class="form-row align-items-center">
|
||
<button id="loadDataBtn" type="button" class="btn btn-primary" onclick="LoadData()">載入資料</button>
|
||
<button id="synchornizeDataBtn" type="button" class="btn btn-dark" onclick="SynchronizeData()">同步資料</button>
|
||
</div>
|
||
|
||
<br />
|
||
<br />
|
||
|
||
<div class="form-row align-items-center">
|
||
<p id="loadDataText"></p>
|
||
</div>
|
||
|
||
<div class="mb-5">
|
||
<div class="card p-3 w-100">
|
||
<div class="card-header d-flex align-items-center flex-wrap">
|
||
<div class="card-title">
|
||
Niagara資料
|
||
</div>
|
||
</div>
|
||
<div class="card-body row">
|
||
<div class="col-12">
|
||
<!-- datatable start -->
|
||
<table id="niagara_data_table" class="table table-bordered table-hover m-0 text-center">
|
||
<thead class="thead-themed">
|
||
<tr>
|
||
<th>value</th>
|
||
<th>tag_name</th>
|
||
<th>point_name</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
@section Scripts {
|
||
<script>
|
||
var buildingId, building;
|
||
var rawDataImportTable;
|
||
var ds;
|
||
|
||
var today = new Date();
|
||
var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
|
||
|
||
$(function () {
|
||
ds = null;
|
||
document.getElementById("Building_B").checked = true;
|
||
|
||
projectName();
|
||
rawDataImportTable = $("#niagara_data_table").DataTable({
|
||
"columns": [
|
||
{
|
||
"data": "value"
|
||
},
|
||
{
|
||
"data": "tag_name"
|
||
},
|
||
{
|
||
"data": "point_name"
|
||
},
|
||
],
|
||
"ajax": {
|
||
"url": "/NiagaraDataSynchronize/RawDataList",
|
||
"type": "POST",
|
||
"dataSrc": function (rel) {
|
||
if (rel.code == "9999") {
|
||
toast_error(rel.msg);
|
||
document.getElementById('loadDataText').innerText = "載入資料出錯了!";
|
||
return;
|
||
}
|
||
|
||
var data = rel.data;
|
||
ds = data;
|
||
console.log(ds)
|
||
if (data == null || data.length == 0) {
|
||
this.data = [];
|
||
document.getElementById('loadDataText').innerText = "";
|
||
}
|
||
else if(data.length > 0) {
|
||
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
|
||
var dateTime = date + ' ' + time;
|
||
document.getElementById('loadDataText').innerText = "共 " + rel.data.length + " 筆資料 \n" + dateTime;
|
||
}
|
||
return data;
|
||
}
|
||
}
|
||
});
|
||
});
|
||
|
||
//#region 載入Niagara資料
|
||
function LoadData() {
|
||
document.getElementById('loadDataText').innerText = "Loading...";
|
||
buildingId = document.querySelector('input[name="Building"]:checked').value;
|
||
if (buildingId == "B") {
|
||
//building = "B1";
|
||
rawDataImportTable.ajax.reload();
|
||
}
|
||
}
|
||
//#endregion
|
||
|
||
function SynchronizeData() {
|
||
if (ds.length > 0) {
|
||
document.getElementById('loadDataText').innerText = "同步中...";
|
||
showSpinner();
|
||
//比對資料,有差異的話,再同步到device等資料表
|
||
var url_synchronize_data = "/NiagaraDataSynchronize/CompareData/";
|
||
|
||
$.each(ds, function (i, v) {
|
||
v.building = building;
|
||
})
|
||
|
||
$.ajax({
|
||
method: "POST",
|
||
url: url_synchronize_data,
|
||
data: JSON.stringify(ds),
|
||
cache: false,
|
||
async: true,
|
||
contentType: "application/json; charset=UTF-8",
|
||
dataType: 'json',
|
||
success: function (rel) {
|
||
if (rel.code != "0000") {
|
||
hideSpinner();
|
||
toast_error(rel.msg);
|
||
document.getElementById('loadDataText').innerText = "比對資料出錯了!";
|
||
return;
|
||
}
|
||
|
||
SyncDevItem();
|
||
//var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
|
||
//var dateTime = date + ' ' + time;
|
||
//document.getElementById('loadDataText').innerText = "比對完成 \n" + dateTime;
|
||
},
|
||
error: function () {
|
||
hideSpinner();
|
||
}
|
||
});
|
||
}
|
||
else {
|
||
document.getElementById('loadDataText').innerText = "查無資料 無法比對!";
|
||
}
|
||
}
|
||
|
||
function SyncDevItem()
|
||
{
|
||
//比對資料,有差異的話,再同步到device等資料表
|
||
var url_synchronize_data = "/NiagaraDataSynchronize/RawDataDevItemList/";
|
||
var url_synchronize_data_device_item = "/NiagaraDataSynchronize/DevItemInsert/";
|
||
|
||
let object = {};
|
||
let dataLength = 0;
|
||
let curData = {};
|
||
let asyncSendData = [];
|
||
let contentLength = 5000;
|
||
object.building = building;
|
||
|
||
//device async
|
||
$.ajax({
|
||
method: "POST",
|
||
url: url_synchronize_data,
|
||
cache: false,
|
||
async: false,
|
||
contentType: "application/json; charset=UTF-8",
|
||
data: JSON.stringify(object),
|
||
dataType: 'json',
|
||
success: function (rel) {
|
||
if (rel.code != "0000") {
|
||
hideSpinner();
|
||
toast_error(rel.msg);
|
||
document.getElementById('loadDataText').innerText = "比對資料出錯了!";
|
||
return;
|
||
}
|
||
|
||
//history async
|
||
$.ajax({
|
||
method: "POST",
|
||
url: '/NiagaraDataSynchronize/GetHistoryData/',
|
||
cache: false,
|
||
async: false,
|
||
contentType: "application/json; charset=UTF-8",
|
||
success: function (res) {
|
||
if (res.code != "0000") {
|
||
hideSpinner();
|
||
toast_error(res.msg);
|
||
document.getElementById('loadDataText').innerText = "比對資料出錯了!";
|
||
return;
|
||
}
|
||
|
||
curData = rel.data;
|
||
let relDataLength = curData.length;
|
||
$.each(rel.data, function (index, val) {
|
||
if (relDataLength > 0) {
|
||
//push data, counting
|
||
asyncSendData.push(val);
|
||
dataLength++;
|
||
relDataLength--;
|
||
|
||
if (dataLength >= contentLength || relDataLength <= 0) {
|
||
//device_item async-insert
|
||
$.ajax({
|
||
method: "POST",
|
||
url: url_synchronize_data_device_item,
|
||
data: JSON.stringify(asyncSendData),
|
||
cache: false,
|
||
async: false,
|
||
contentType: "application/json; charset=UTF-8",
|
||
dataType: 'json',
|
||
success: function (rel) {
|
||
if (rel.code != "0000") {
|
||
toast_error(rel.msg);
|
||
hideSpinner();
|
||
document.getElementById('loadDataText').innerText = "比對資料出錯了!";
|
||
return;
|
||
}
|
||
|
||
//reset variable
|
||
dataLength = 0;
|
||
asyncSendData = [];
|
||
|
||
//device_item async-compare
|
||
if (relDataLength <= 0) {
|
||
$.ajax({
|
||
method: "POST",
|
||
url: '/NiagaraDataSynchronize/DevIteComData/',
|
||
cache: false,
|
||
async: false,
|
||
contentType: "application/json; charset=UTF-8",
|
||
success: function (rel) {
|
||
if (rel.code != "0000") {
|
||
toast_error(rel.msg);
|
||
hideSpinner();
|
||
document.getElementById('loadDataText').innerText = "比對資料出錯了!";
|
||
return;
|
||
}
|
||
|
||
hideSpinner();
|
||
console.log(rel);
|
||
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
|
||
var dateTime = date + ' ' + time;
|
||
document.getElementById('loadDataText').innerText = "比對完成 \n" + dateTime;
|
||
},
|
||
error: function () {
|
||
hideSpinner();
|
||
document.getElementById('loadDataText').innerText = "比對資料出錯了!";
|
||
return;
|
||
}
|
||
})
|
||
}
|
||
},
|
||
error: function () {
|
||
hideSpinner();
|
||
document.getElementById('loadDataText').innerText = "比對資料出錯了!";
|
||
return;
|
||
}
|
||
})
|
||
}
|
||
}
|
||
});
|
||
|
||
},
|
||
error: function () {
|
||
hideSpinner();
|
||
document.getElementById('loadDataText').innerText = "比對資料出錯了!";
|
||
return;
|
||
}
|
||
})
|
||
|
||
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
|
||
var dateTime = date + ' ' + time;
|
||
document.getElementById('loadDataText').innerText = "比對完成 \n" + dateTime;
|
||
},
|
||
});
|
||
}
|
||
|
||
function projectName()
|
||
{
|
||
$.post("/Variable/ProjectName", null, function (rel) {
|
||
building = "B1";
|
||
if (rel.code != "0000") {
|
||
toast_error(rel.msg);
|
||
return;
|
||
}
|
||
|
||
if (rel.data != null) {
|
||
building = rel.data.system_value.split('/')[1];
|
||
$(`label[for='Building_B']`).html(rel.data.system_key + building);
|
||
}
|
||
else
|
||
{
|
||
$(`label[for='Building_B']`).html('三菱B1');
|
||
}
|
||
}, 'json');
|
||
}
|
||
</script>
|
||
} |