CM69 - Bugfix graph cartest, CMXX - Healthindex update
This commit is contained in:
@@ -2591,47 +2591,16 @@ function storeMeasurementEquipment($serialnumber){
|
||||
}
|
||||
}
|
||||
//STORE RESULTS IN EQUIPMENT DATA
|
||||
foreach ($equipment_watchlist as $equipment => $data){
|
||||
foreach ($equipment_watchlist as $equipment => $data){
|
||||
|
||||
//GET the equipmentid and historyid from watchlist
|
||||
if (str_contains($equipment,'||')){
|
||||
$identification = explode('||',$equipment);
|
||||
//json_encode array
|
||||
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
|
||||
|
||||
//Calculate healthindex
|
||||
$healthindex = 100;
|
||||
|
||||
//Check if record exists
|
||||
$sql = 'SELECT * FROM equipment_data WHERE equipmentid = ? and historyid =?';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$identification[2],$identification[3]]);
|
||||
$equipment_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!empty($equipment_data)){
|
||||
//EXIST UPDATE
|
||||
$sql = 'UPDATE equipment_data SET measurement = ? WHERE equipmentid = ? and historyid =?';
|
||||
}
|
||||
else {
|
||||
//EXIST INSERT
|
||||
$sql = 'INSERT INTO equipment_data (measurement, equipmentid, historyid) VALUES (?,?,?)';
|
||||
}
|
||||
|
||||
//EXECUTE QUERY
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$data, $identification[2],$identification[3]]);
|
||||
$equipment_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// calculatehealthindex of asset ++++++++++
|
||||
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
function assetHealthIndex($prof,$pem,$healthdata){
|
||||
$healthindex = 100;
|
||||
//Allowed check
|
||||
if (isAllowed('equipment_data',$prof,$pem,'R') === 1 && !empty($healthdata)){
|
||||
//GET last data
|
||||
$last_data_measurement = end($healthdata);
|
||||
foreach (json_decode($last_data_measurement->measurement,true) as $data_measure){
|
||||
|
||||
foreach ($data as $data_measure){
|
||||
switch ($data_measure['deviation']) {
|
||||
case 1:
|
||||
$healthindex = $healthindex - $data_measure['deviation'];
|
||||
@@ -2643,7 +2612,72 @@ function assetHealthIndex($prof,$pem,$healthdata){
|
||||
$healthindex = $healthindex - ($data_measure['deviation']*3);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
//json_encode array
|
||||
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
|
||||
|
||||
//Check if record exists
|
||||
$sql = 'SELECT * FROM equipment_data WHERE equipmentid = ? and historyid =?';
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$identification[2],$identification[3]]);
|
||||
$equipment_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!empty($equipment_data)){
|
||||
//EXIST UPDATE
|
||||
$sql = 'UPDATE equipment_data SET measurement = ?, healthindex = ? WHERE equipmentid = ? and historyid = ?';
|
||||
}
|
||||
else {
|
||||
//EXIST INSERT
|
||||
$sql = 'INSERT INTO equipment_data (measurement, , healthindex, equipmentid, historyid) VALUES (?,?,?,?)';
|
||||
}
|
||||
|
||||
//EXECUTE QUERY
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute([$data, $healthindex, $identification[2],$identification[3]]);
|
||||
$equipment_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// calculatehealthindex of asset ++++++++++
|
||||
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
function assetHealthIndex($prof,$pem,$healthdata,$type){
|
||||
|
||||
$healthindex = 100;
|
||||
//Allowed check
|
||||
if (isAllowed('equipment_data',$prof,$pem,'R') === 1 && !empty($healthdata)){
|
||||
|
||||
//GET last data based on type returned
|
||||
//Type
|
||||
// 0 = $healthdata per equipmentid
|
||||
// 1 = $healthdata per rowID of equipment_data
|
||||
switch ($type) {
|
||||
case 0:
|
||||
$last_data_measurement = end($healthdata);
|
||||
break;
|
||||
default:
|
||||
$last_data_measurement = $healthdata;
|
||||
|
||||
break;
|
||||
}
|
||||
//Ensure $last_data_measurement is an array
|
||||
$last_data_measurement = (is_object($last_data_measurement))? (array) $last_data_measurement : $last_data_measurement;
|
||||
|
||||
foreach (json_decode($last_data_measurement['measurement'],true) as $data_measure){
|
||||
|
||||
switch ($data_measure['deviation']) {
|
||||
case 1:
|
||||
$healthindex = $healthindex - $data_measure['deviation'];
|
||||
break;
|
||||
case 2:
|
||||
$healthindex = $healthindex - ($data_measure['deviation']*2);
|
||||
break;
|
||||
case 3:
|
||||
$healthindex = $healthindex - ($data_measure['deviation']*3);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
//Not allowed or no data return 100% health
|
||||
|
||||
@@ -2,15 +2,6 @@ var port, textEncoder, writableStreamClosed, writer, historyIndex = -1;
|
||||
const lineHistory = [];
|
||||
maintenanceRun = 0;
|
||||
|
||||
// To read and clean device data
|
||||
const VCP = []; //for VCP values
|
||||
const VPP1 = []; //for VPP1 values
|
||||
const VPP2 = []; //for VPP2 values
|
||||
const VBAT = []; //for VBAT values
|
||||
const PWM = []; //for PWM values
|
||||
const STATE = []; //for STATE values
|
||||
const xChart = []; //x-axis chart values
|
||||
|
||||
function progressBar(percentage, message, color){
|
||||
var readbar = document.getElementById("readBar");
|
||||
readBar.style.background = color;
|
||||
@@ -239,26 +230,92 @@ const plug_data = document.getElementById("plug_data");
|
||||
|
||||
async function appendToTerminal(newStuff) {
|
||||
serialResultsDiv.innerHTML += newStuff;
|
||||
|
||||
const keyword = '$STATUS;STATE';
|
||||
const keyword2 = 'PWM';
|
||||
|
||||
// To read and clean device data
|
||||
VCP = []; //for VCP values
|
||||
VPP1 = []; //for VPP1 values
|
||||
VPP2 = []; //for VPP2 values
|
||||
VBAT = []; //for VBAT values
|
||||
PWM = []; //for PWM values
|
||||
STATE = []; //for STATE values
|
||||
xChart = []; //x-axis chart values
|
||||
|
||||
// Input string with multiple status reports
|
||||
const statusString = serialResultsDiv.innerHTML;
|
||||
let lineCounter = 0;
|
||||
|
||||
// Split the input by newline to get each status report
|
||||
let statusLines = statusString.split(/\r?\n/);
|
||||
|
||||
// Process each status line and populate the parameter arrays and counters
|
||||
statusLines.forEach((line) => {
|
||||
|
||||
if (line.includes(keyword) && line.includes(keyword2) && /\d$/.test(line.trim())) {
|
||||
|
||||
const pairs = line.split(';'); // Split by ';'
|
||||
|
||||
// Loop through each pair and split by '='
|
||||
pairs.forEach(pair => {
|
||||
const [key, value] = pair.split('=');
|
||||
if (key && value !== undefined && key !== "$STATUS") { // Skip '$STATUS'
|
||||
// Push the value and counter to the corresponding parameter object
|
||||
if (key === "STATE") {
|
||||
let state
|
||||
switch (value) {
|
||||
case 'FULL_CONNECT':
|
||||
state = 5;
|
||||
break;
|
||||
case 'REACTIVATE':
|
||||
state = 3;
|
||||
break;
|
||||
case 'WAIT_CONNECT':
|
||||
state = -5;
|
||||
break;
|
||||
case 'DISCONNECTED':
|
||||
state = 0;
|
||||
break;
|
||||
default:
|
||||
state = 0;
|
||||
break;
|
||||
}
|
||||
STATE.push(state);
|
||||
} else if (key === "VCP") {
|
||||
VCP.push(value / 1000000);
|
||||
} else if (key === "VPP1") {
|
||||
VPP1.push(value / 1000000);
|
||||
} else if (key === "VPP2") {
|
||||
VPP2.push(value / 1000000);
|
||||
} else if (key === "VBAT") {
|
||||
VBAT.push(value / 1000000);
|
||||
} else if (key === "PWM") {
|
||||
PWM.push(value /10);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
xChart.push(lineCounter);
|
||||
lineCounter = lineCounter + 1;
|
||||
}
|
||||
});
|
||||
|
||||
//vcp_data.value +=newStuff
|
||||
if (typeof plug_data !== "undefined"){
|
||||
if (typeof plug_data !== "undefined"){
|
||||
var plugdata = {"VCP":{VCP}, "VPP1":{VPP1},"VPP2":{VPP2},"VBAT":{VBAT}, "PWM":{PWM},"STATE":{STATE}, "xChart":{xChart}}
|
||||
plug_data.value = JSON.stringify(plugdata);
|
||||
}
|
||||
|
||||
serialOutput +=newStuff;
|
||||
//serialOutput +=newStuff;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function renderChart(){
|
||||
var test = serialResultsDiv.innerHTML.split(";");
|
||||
test.forEach(getData);
|
||||
const xValues = xChart;
|
||||
new Chart("chart_VCP", {
|
||||
type: "line",
|
||||
data: {
|
||||
labels: xValues,
|
||||
labels: xChart,
|
||||
datasets: [{
|
||||
label: "VCP",
|
||||
data: VCP,
|
||||
@@ -300,54 +357,76 @@ function renderChart(){
|
||||
}
|
||||
|
||||
//Get the RAW data and turn into object
|
||||
function getData(item) {
|
||||
function getData() {
|
||||
const keyword = '$STATUS;STATE';
|
||||
const keyword2 = 'PWM';
|
||||
|
||||
let item_array = item.split("=");
|
||||
let xaxis = xChart.length + 1;
|
||||
// To read and clean device data
|
||||
const VCP = []; //for VCP values
|
||||
const VPP1 = []; //for VPP1 values
|
||||
const VPP2 = []; //for VPP2 values
|
||||
const VBAT = []; //for VBAT values
|
||||
const PWM = []; //for PWM values
|
||||
const STATE = []; //for STATE values
|
||||
const xChart = []; //x-axis chart values
|
||||
|
||||
// Input string with multiple status reports
|
||||
const statusString = serialResultsDiv.innerHTML;
|
||||
let lineCounter = 0;
|
||||
|
||||
// Split the input by newline to get each status report
|
||||
let statusLines = statusString.split(/\r?\n/);
|
||||
|
||||
// Process each status line and populate the parameter arrays and counters
|
||||
statusLines.forEach((line) => {
|
||||
|
||||
if (line.includes(keyword) && line.includes(keyword2) && /\d$/.test(line.trim())) {
|
||||
|
||||
const pairs = line.split(';'); // Split by ';'
|
||||
|
||||
if (item_array[0] == "VCP"){
|
||||
let vcp = item_array[1] / 1000000;
|
||||
VCP.push(vcp)
|
||||
xChart.push(xaxis);
|
||||
}
|
||||
if (item_array[0] == "VPP1"){
|
||||
let vpp1 = item_array[1] / 1000000;
|
||||
VPP1.push(vpp1)
|
||||
}
|
||||
if (item_array[0] == "VPP2"){
|
||||
let vpp2 = item_array[1] / 1000000;
|
||||
VPP2.push(vpp2)
|
||||
}
|
||||
if (item_array[0] == "VBAT"){
|
||||
let vbat = item_array[1] / 1000000;
|
||||
VBAT.push(vbat)
|
||||
}
|
||||
if (item_array[0] == "STATE"){
|
||||
let state
|
||||
switch (item_array[1]) {
|
||||
case 'FULL_CONNECT':
|
||||
state = 5;
|
||||
break;
|
||||
case 'REACTIVATE':
|
||||
state = 3;
|
||||
break;
|
||||
case 'WAIT_CONNECT':
|
||||
state = -5;
|
||||
break;
|
||||
case 'DISCONNECTED':
|
||||
state = 0;
|
||||
break;
|
||||
}
|
||||
STATE.push(state)
|
||||
}
|
||||
if (item_array[0] == "PWM")
|
||||
{
|
||||
var check_return = item_array[1].match(/[^\r\n]+/g);
|
||||
if (check_return.length == 2){
|
||||
let pwm = check_return[0] / 10;
|
||||
PWM.push(pwm)
|
||||
}
|
||||
// Loop through each pair and split by '='
|
||||
pairs.forEach(pair => {
|
||||
const [key, value] = pair.split('=');
|
||||
if (key && value !== undefined && key !== "$STATUS") { // Skip '$STATUS'
|
||||
// Push the value and counter to the corresponding parameter object
|
||||
if (key === "STATE") {
|
||||
let state
|
||||
switch (value) {
|
||||
case 'FULL_CONNECT':
|
||||
state = 5;
|
||||
break;
|
||||
case 'REACTIVATE':
|
||||
state = 3;
|
||||
break;
|
||||
case 'WAIT_CONNECT':
|
||||
state = -5;
|
||||
break;
|
||||
case 'DISCONNECTED':
|
||||
state = 0;
|
||||
break;
|
||||
default:
|
||||
state = 0;
|
||||
break;
|
||||
}
|
||||
STATE.push(state);
|
||||
} else if (key === "VCP") {
|
||||
VCP.push(value / 1000000);
|
||||
} else if (key === "VPP1") {
|
||||
VPP1.push(value / 1000000);
|
||||
} else if (key === "VPP2") {
|
||||
VPP2.push(value / 1000000);
|
||||
} else if (key === "VBAT") {
|
||||
VBAT.push(value / 1000000);
|
||||
} else if (key === "PWM") {
|
||||
PWM.push(value /10);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
xChart.push(lineCounter);
|
||||
lineCounter = lineCounter + 1;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function closePort(){
|
||||
|
||||
Reference in New Issue
Block a user