ASCU_ALL/Site/js/analog.js
2024-09-22 04:27:05 +05:00

323 lines
8.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

var analog_diag_body = $('#analog_diag_body');
var analog_diag = $('#analog_diag');
var filter_table = $('#filter_table');
var axis_table = $('#axis_table');
var aply_filter = $('#aply_filter');
var change_filter = $('#change_filter');
var aply_axis = $('#aply_axis');
var data_analog = null;
var data_analog_params = null;
var data_metrics = null;
function ValueDateFormatter(e) {
var hours = 1000 * 60 * 60;
if (e.trigger === "reset") {
e.chart.options.axisX.valueFormatString = "MM.DD HH";
}
else if (e.trigger === "zoom") {
//Hour (Comparing Hours)
if ((((e.axisX[0].viewportMaximum - e.axisX[0].viewportMinimum) / (hours)) < 24)) {
e.chart.options.axisX.valueFormatString = "HH:mm:ss";
}
//Day (Comparing Days)
else if (((e.axisX.viewportMaximum - e.axisX.viewportMinimum) / (hours)) < 48) {
e.chart.options.axisX.valueFormatString = "HH:mm";
}
//Year (Comparing Years)
else if (((e.axisX[0].viewportMaximum - e.axisX[0].viewportMinimum) / (hours * 24)) < 2) {
e.chart.options.axisX.valueFormatString = "MM.DD HH";
}
else {
e.chart.options.axisX.valueFormatString = "MM.DD";
}
}
}
var a_chart = new CanvasJS.Chart("analog_diag", {
toolTip: {
fontSize: 12,
shared: true,
contentFormatter: function (e) {
var str = "";
if (e.entries.length < 1)
return str;
var lastdate = e.entries[0].dataPoint.x;
var data = e.chart._dataInRenderedOrder;
for (var i = 0; i < data.length; i++) {
var y = null;
for (var j = 0; j < data[i].dataPoints.length; j++) {
var t = data[i].dataPoints[j].x - lastdate;
if (t <= 0)
y = data[i].dataPoints[j].y;
else
break;
t = data[i].dataPoints[(data[i].dataPoints.length - 1) - j].x - lastdate;
if (t <= 0) {
y = data[i].dataPoints[(data[i].dataPoints.length - 1) - j].y;
break;
}
}
var temp =
data[i].name + ': <strong style="color:' +
data[i].color + ';">' +
y + ' </strong>' + data[i].axisY.suffix + "<br/>";
str = str.concat(temp);
}
return (str);
}
},
animationEnabled: true,
animationDuration: 1000,
exportFileName: "Analog Signals",
exportEnabled: true,
zoomEnabled: true,
theme: "light1",
backgroundColor: "#FFFFFF",
colorSet: "colorSet1",
culture: "ru",
title: {},
legend: {},
rangeChanging: ValueDateFormatter,
axisX: {
labelFontSize: 12,
margin: 1,
tickLength: 3,
tickThickness: 1,
lineThickness: 1,
gridThickness: 0.3,
valueFormatString: "MM.DD HH"
},
axisY: [],
data: []
});
//Первая загрузка страницы
$(document).ready(
async function () {
show_load();
var flag_pasport_load = await get_url_params();
pasport_fill_label();
pasport_show_lable();
if (!flag_pasport_load) {
hide_load();
return;
}
if (!pasport_fill_date()) {
hide_load();
return;
}
show_menu();
pasport_show_date();
var ap = load_data_analog_params();
var m = load_data_metrics();
await Promise.all([ap]);
var ad = load_data_analog();
if (ap)
fill_filter_analog();
await Promise.all([ad, m]);
if (ap) {
if (ad) {
fill_analog_char_data();
}
}
a_chart.render();
hide_load();
}
);
aply_filter.click(async function () {
read_filter();
await load_data_analog();
fill_analog_char_data();
a_chart.render();
});
var change_filter_flag = true;
change_filter.click(function () {
change_filter.empty();
if (change_filter_flag)
change_filter.append("Установить все");
else
change_filter.append("Снять все");
for (var i = 0; i < data_analog_params.length; i++) {
var check_label = filter_table.find('#analog_filter_id_' + data_analog_params[i].index);
if (change_filter_flag)
check_label.removeAttr('checked');
else
check_label.attr('checked', true);
}
change_filter_flag = !change_filter_flag;
});
function fill_filter_analog() {
filter_table.empty();
if (data_analog_params == undefined ||
data_analog_params == null)
return;
for (var i = 0; i < data_analog_params.length; i++) {
filter_table.append(
'<div class= "form-check">' +
'<input class="form-check-input" type="checkbox" value="' + data_analog_params[i].index +
'" id="analog_filter_id_' + data_analog_params[i].index + '" ' +
(data_analog_params[i].showDefault ? 'checked' : '') + '>' +
'<label class="form-check-label" for="analog_filter_id_' + data_analog_params[i].index + '">' +
data_analog_params[i].index + ' ' + data_analog_params[i].name +
'</label></div>'
);
}
}
function read_filter() {
for (var i = 0; i < data_analog_params.length; i++) {
if (filter_table.find('#analog_filter_id_' + data_analog_params[i].index).is(':checked'))
data_analog_params[i].showDefault = true;
else
data_analog_params[i].showDefault = false;
}
}
async function load_data_analog_params() {
try {
var name_vdp = "0" + pasport_data.numVDP;
name_vdp = name_vdp.substr(name_vdp.length - 2);
var res = await $.ajax({
type: "POST",
url: 'https://vsmpo.mbucb.ru/api/params/getanalogs',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({ "name": name_vdp })
});
data_analog_params = res.analog_params;
return true;
}
catch (err) {
data_protect = null;
alert("Ошибка загрузки структуры аналогов");
return false;
}
}
async function load_data_metrics() {
try {
var res = await $.ajax({
type: "POST",
url: 'https://vsmpo.mbucb.ru/api/params/getmetrics',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({ })
});
data_metrics = res.metrics;
for (var i = 0; i < data_metrics; i++)
data_metrics[i].show = false;
return true;
}
catch (err) {
data_protect = null;
alert("Ошибка загрузки метрик осей");
return false;
}
}
async function load_data_analog() {
try {
data_analog = null;
if (data_analog_params == undefined ||
data_analog_params == null)
return false;
var selected_analog = select_analogs = data_analog_params.filter(item => item.showDefault == true);
var ask_array = [];
for (var i = 0; i < selected_analog.length; i++)
ask_array.push(selected_analog[i].index)
var res = await $.ajax({
type: "POST",
url: 'https://vsmpo.mbucb.ru/api/date/getanalogs',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({
"vdp": pasport_data.numVDP,
"index": ask_array,
"start": pasport_data.dStart,
"end": pasport_data.dEnd
})
});
data_analog = res.analogs;
return true;
}
catch (err) {
data_analog = null;
alert("Ошибка загрузки дискретов.");
return false;
}
}
function fill_analog_char_data() {
var d = [];
a_chart.options.axisY = [];
a_chart.options.data = [];
for (var i = 0; i < data_analog.length; i++) {
var a_p = data_analog_params.find(item => item.index == data_analog[i].index);
var a_p_exist = a_p != undefined && a_p != null;
var axis = {
labelFontSize: 12,
labelAngle: -90,
margin: 0,
tickLength: 3,
tickThickness: 1,
lineThickness: 1,
gridThickness: 0.1,
suffix: "УЕ",
logarithmic: false
}
if (a_p_exist) {
var m = data_metrics.find(item => item.name = a_p.metric)
if (m != undefined && m != null) {
axis.suffix = m.name;
axis.logarithmic = m.log;
}
}
var axis_y_idx = a_chart.options.axisY.findIndex(item => item.suffix == axis.suffix);
if (axis_y_idx == undefined || axis_y_idx == null || axis_y_idx == -1) {
a_chart.options.axisY.push(axis);
axis_y_idx = a_chart.options.axisY.length - 1;
}
var d_a_line = {
type: "stepLine",
markerType: "none",
name: a_p_exist ? a_p.sname : "Сигнал №" + data_analog[i].index,
suffix: axis.suffix,
axisYIndex: axis_y_idx,
dataPoints: []
};
var l_d_a = data_analog[i].analogs;
for (var j = 0; j < l_d_a.length; j++) {
var rdt = null
if (l_d_a[j].start != undefined && l_d_a[j].start != null) {
var dta = l_d_a[j].start.split(' ');
if (dta.length == 2) {
dtd = dta[0].split('.');
dtt = dta[1].split(':');
if (dtd.length == 3 && dtt.length === 3)
rdt = new Date(dtd[0], dtd[1], dtd[2], dtt[0], dtt[1], dtt[2]);
}
}
d_a_line.dataPoints.push({
x: rdt,
y: l_d_a[j].value
});
}
a_chart.options.data.push(d_a_line);
}
};