kiln-controller-max31856/public/assets/js/picoreflow.js

576 wiersze
17 KiB
JavaScript
Czysty Zwykły widok Historia

2013-11-28 22:21:00 +00:00
var state = "IDLE";
2013-11-29 23:08:04 +00:00
var state_last = "";
2013-11-29 21:46:00 +00:00
var graph = [ 'profile', 'live'];
var points = [];
2013-11-28 22:21:00 +00:00
var profiles = [];
var selected_profile = 0;
var selected_profile_name = "leadfree";
2013-11-29 21:46:00 +00:00
2013-11-28 22:21:00 +00:00
var host = "ws://" + window.location.hostname + ":8080";
var ws_status = new WebSocket(host+"/status");
var ws_control = new WebSocket(host+"/control");
var ws_storage = new WebSocket(host+"/storage");
if(window.webkitRequestAnimationFrame) window.requestAnimationFrame = window.webkitRequestAnimationFrame;
2013-11-28 22:21:00 +00:00
2013-11-29 21:46:00 +00:00
graph.profile =
{
label: "Profile",
data: [],
points: { show: false },
color: "#75890c",
draggable: false
};
graph.live =
{
label: "Live",
data: [],
points: { show: false },
color: "#d8d3c5",
draggable: false
};
2013-12-09 11:51:08 +00:00
function updateProfile(id)
2013-11-29 21:46:00 +00:00
{
selected_profile = id;
2013-11-30 13:02:46 +00:00
job_time = parseInt(profiles[id].data[profiles[id].data.length-1][0]);
var kwh = (3850*job_time/3600/1000).toFixed(2);
var cost = (kwh*0.26).toFixed(2);
var minutes = Math.floor(job_time/60), seconds = job_time-minutes*60;
job_time = minutes+':'+ (seconds < 10 ? "0" : "") + seconds;
2013-11-29 21:46:00 +00:00
$('#sel_prof').html(profiles[id].name);
2013-11-30 13:02:46 +00:00
$('#sel_prof_eta').html(job_time);
$('#sel_prof_cost').html(kwh + ' kWh (EUR: '+ cost +')');
2013-11-29 21:46:00 +00:00
graph.profile.data = profiles[id].data;
graph.plot = $.plot("#graph_container", [ graph.profile, graph.live ] , getOptions());
2013-11-28 16:17:15 +00:00
}
2013-11-29 21:46:00 +00:00
2013-12-09 11:51:08 +00:00
function deleteProfile()
{
console.log("Delete profile:" + selected_profile_name);
// FIXME: Add cmd for socket communication to delete stored profile
2013-12-09 14:10:22 +00:00
leaveEditMode();
2013-12-09 11:51:08 +00:00
}
2013-11-29 21:46:00 +00:00
function updateProgress(percentage)
2013-11-29 21:46:00 +00:00
{
if(state=="RUNNING")
{
if(percentage > 100) percentage = 100;
$('#progressBar').css('width', percentage+'%');
2013-12-09 14:10:22 +00:00
if(percentage>5) $('#progressBar').html(parseInt(percentage)+'%');
2013-11-29 21:46:00 +00:00
}
else
{
$('#progressBar').css('width', 0+'%');
$('#progressBar').html('');
}
2013-11-28 16:17:15 +00:00
}
2013-12-01 00:51:45 +00:00
function updateProfileTable()
{
var dps = 0;
var slope = "";
2013-12-09 11:51:08 +00:00
var color = "";
var html = '<h3>Profile Points</h3><div class="table-responsive" style="scroll: none"><table class="table table-striped">';
html += '<tr><th style="width: 50px">#</th><th>Target Time</th><th>Target Temperature</th><th>Slope in &deg;C/s</th><th></th></tr>';
2013-12-01 00:51:45 +00:00
2013-12-01 01:05:24 +00:00
for(var i=0; i<graph.profile.data.length;i++)
2013-12-01 00:51:45 +00:00
{
if (i>=1) dps = Math.round( (graph.profile.data[i][1]-graph.profile.data[i-1][1])/(graph.profile.data[i][0]-graph.profile.data[i-1][0]) * 10) / 10;
2013-12-09 11:51:08 +00:00
if (dps > 0) { slope = "up"; color="rgba(206, 5, 5, 1)"; } else
if (dps < 0) { slope = "down"; color="rgba(23, 108, 204, 1)"; dps *= -1; } else
if (dps == 0) { slope = "right"; color="grey"; }
2013-12-09 14:10:22 +00:00
html += '<tr><td><h4>' + (i+1) + '</h4></td>';
html += '<td><input type="text" class="form-control" id="profiletable-0-'+i+'" value="'+ graph.profile.data[i][0] + '" style="width: 60px" /></td>';
html += '<td><input type="text" class="form-control" id="profiletable-1-'+i+'" value="'+ graph.profile.data[i][1] + '" style="width: 60px" /></td>';
2013-12-09 11:51:08 +00:00
html += '<td><div class="input-group"><span class="glyphicon glyphicon-circle-arrow-' + slope +
' input-group-addon ds-trend" style="background: '+color+'"></span><input type="text" class="form-control ds-input" readonly value="' + dps + '" style="width: 50px" /></div></td>';
html += '<td>&nbsp;</td></tr>';
2013-12-01 00:51:45 +00:00
}
html += '</table></div>';
2013-12-05 18:53:18 +00:00
2013-12-01 00:51:45 +00:00
$('#profile_table').html(html);
2013-12-09 11:51:08 +00:00
//Link table to graph
$(".form-control").change(function(e)
{
var id = $(this)[0].id//e.currentTarget.attributes.id;
var value = parseInt($(this)[0].value);
var fields = id.split("-");
var col = parseInt(fields[1]);
var row = parseInt(fields[2]);
2013-12-09 11:51:08 +00:00
graph.profile.data[row][col] = value;
graph.plot = $.plot("#graph_container", [ graph.profile, graph.live ], getOptions());
updateProfileTable();
});
2013-12-01 00:51:45 +00:00
}
2013-11-28 16:17:15 +00:00
2013-11-29 21:46:00 +00:00
function runTask()
{
2013-12-01 00:51:45 +00:00
var cmd =
2013-11-29 21:46:00 +00:00
{
"cmd": "RUN",
"profile": profiles[selected_profile]
}
graph.live.data = [];
graph.plot = $.plot("#graph_container", [ graph.profile, graph.live ] , getOptions());
2013-11-28 16:17:15 +00:00
2013-12-01 00:51:45 +00:00
ws_control.send(JSON.stringify(cmd));
2013-11-28 16:17:15 +00:00
}
2013-12-06 22:02:07 +00:00
function runTaskSimulation()
{
var cmd =
{
"cmd": "SIMULATE",
"profile": profiles[selected_profile]
}
graph.live.data = [];
graph.plot = $.plot("#graph_container", [ graph.profile, graph.live ] , getOptions());
ws_control.send(JSON.stringify(cmd));
}
2013-11-28 16:17:15 +00:00
2013-11-29 21:46:00 +00:00
function abortTask()
{
2013-12-01 00:51:45 +00:00
var cmd = {"cmd": "STOP"};
ws_control.send(JSON.stringify(cmd));
2013-11-28 16:17:15 +00:00
}
2013-12-01 00:51:45 +00:00
function enterNewMode()
{
state="EDIT"
2013-12-05 23:47:43 +00:00
$('#status').slideUp();
2013-12-01 00:51:45 +00:00
$('#edit').show();
2013-12-05 18:53:18 +00:00
$('#profile_selector').hide();
2013-12-01 00:51:45 +00:00
$('#btn_controls').hide();
$('#form_profile_name').attr('value', '');
$('#form_profile_name').attr('placeholder', 'Please enter a name');
graph.profile.points.show = true;
graph.profile.draggable = true;
2013-12-01 01:05:24 +00:00
graph.profile.data = [];
2013-12-01 00:51:45 +00:00
graph.plot = $.plot("#graph_container", [ graph.profile, graph.live ], getOptions());
updateProfileTable();
}
2013-11-28 16:17:15 +00:00
2013-12-01 00:51:45 +00:00
function enterEditMode()
{
2013-11-28 16:17:15 +00:00
state="EDIT"
2013-12-05 23:47:43 +00:00
$('#status').slideUp();
2013-12-01 00:51:45 +00:00
$('#edit').show();
2013-12-05 18:53:18 +00:00
$('#profile_selector').hide();
2013-12-01 00:51:45 +00:00
$('#btn_controls').hide();
2013-11-28 16:17:15 +00:00
$('#form_profile_name').attr('value', profiles[selected_profile].name);
2013-11-29 21:46:00 +00:00
graph.profile.points.show = true;
graph.profile.draggable = true;
graph.plot = $.plot("#graph_container", [ graph.profile, graph.live ], getOptions());
2013-12-01 00:51:45 +00:00
updateProfileTable();
2013-11-28 16:17:15 +00:00
}
2013-11-29 21:46:00 +00:00
2013-12-01 00:51:45 +00:00
function leaveEditMode()
{
selected_profile_name = $('#form_profile_name').val();
ws_storage.send('GET');
2013-11-28 16:17:15 +00:00
state="IDLE";
2013-12-01 00:51:45 +00:00
$('#edit').hide();
2013-12-05 18:53:18 +00:00
$('#profile_selector').show();
2013-12-01 00:51:45 +00:00
$('#btn_controls').show();
2013-12-05 23:47:43 +00:00
$('#status').slideDown();
2013-12-01 00:51:45 +00:00
$('#profile_table').slideUp();
2013-11-29 21:46:00 +00:00
graph.profile.points.show = false;
graph.profile.draggable = false;
graph.plot = $.plot("#graph_container", [ graph.profile, graph.live ], getOptions());
2013-11-28 16:17:15 +00:00
}
2013-12-01 00:51:45 +00:00
function newPoint()
{
2013-12-01 01:05:24 +00:00
if(graph.profile.data.length > 0)
{
var pointx = parseInt(graph.profile.data[graph.profile.data.length-1][0])+15;
}
else
{
var pointx = 0;
}
graph.profile.data.push([pointx, Math.floor((Math.random()*230)+25)]);
2013-12-01 00:51:45 +00:00
graph.plot = $.plot("#graph_container", [ graph.profile, graph.live ], getOptions());
updateProfileTable();
}
2013-11-29 21:46:00 +00:00
2013-12-01 00:51:45 +00:00
function delPoint()
{
graph.profile.data.splice(-1,1)
graph.plot = $.plot("#graph_container", [ graph.profile, graph.live ], getOptions());
2013-12-01 00:51:45 +00:00
updateProfileTable();
}
2013-11-29 21:46:00 +00:00
2013-12-01 00:51:45 +00:00
function toggleTable()
{
if($('#profile_table').css('display') == 'none')
{
$('#profile_table').slideDown();
}
else
{
$('#profile_table').slideUp();
}
}
2013-11-29 21:46:00 +00:00
function saveProfile()
{
2013-11-28 16:17:15 +00:00
name = $('#form_profile_name').val();
2013-11-29 21:46:00 +00:00
var rawdata = graph.plot.getData()[0].data
2013-11-28 16:17:15 +00:00
var data = [];
2013-11-28 22:21:00 +00:00
var last = -1;
2013-11-28 16:17:15 +00:00
for(var i=0; i<rawdata.length;i++)
{
2013-11-29 21:46:00 +00:00
if(rawdata[i][0] > last)
2013-11-28 22:21:00 +00:00
{
2013-11-29 21:46:00 +00:00
data.push([rawdata[i][0], rawdata[i][1]]);
2013-11-28 22:21:00 +00:00
}
else
{
$.bootstrapGrowl("<span class=\"glyphicon glyphicon-exclamation-sign\"></span> <b>ERROR 88:</b><br/>An oven is not a time-machine", {
ele: 'body', // which element to append to
type: 'alert', // (null, 'info', 'error', 'success')
offset: {from: 'top', amount: 250}, // 'top', or 'bottom'
align: 'center', // ('left', 'right', or 'center')
width: 385, // (integer, or 'auto')
delay: 5000,
allow_dismiss: true,
stackup_spacing: 10 // spacing between consecutively stacked growls.
});
return false;
}
2013-11-29 21:46:00 +00:00
last = rawdata[i][0];
2013-11-28 16:17:15 +00:00
}
var profile = { "type": "profile", "data": data, "name": name }
var put = { "cmd": "PUT", "profile": profile }
var put_cmd = JSON.stringify(put);
ws_storage.send(put_cmd);
leaveEditMode();
}
2013-11-29 21:46:00 +00:00
function getOptions()
{
2013-11-29 21:46:00 +00:00
var options =
{
2013-11-29 21:46:00 +00:00
series:
{
2013-12-09 22:52:17 +00:00
lines:
{
show: true
},
points:
{
show: true,
radius: 5,
symbol: "circle"
},
shadowSize: 3
2013-11-29 21:46:00 +00:00
},
2013-11-29 21:46:00 +00:00
xaxis:
{
2013-12-09 22:52:17 +00:00
//tickSize: 30,
2013-11-29 21:46:00 +00:00
min: 0,
tickColor: 'rgba(216, 211, 197, 0.2)',
font:
{
2013-12-09 11:51:08 +00:00
size: 14,
2013-11-30 00:42:35 +00:00
lineHeight: 14, weight: "normal",
2013-12-09 11:51:08 +00:00
family: "Digi",
2013-11-29 21:46:00 +00:00
variant: "small-caps",
color: "rgba(216, 211, 197, 0.85)"
}
},
2013-11-29 21:46:00 +00:00
yaxis:
{
tickSize: 25,
min: 0,
max: 250,
tickDecimals: 0,
draggable: false,
tickColor: 'rgba(216, 211, 197, 0.2)',
font:
{
2013-12-09 11:51:08 +00:00
size: 14,
2013-11-29 21:46:00 +00:00
lineHeight: 14,
weight: "normal",
2013-12-09 11:51:08 +00:00
family: "Digi",
2013-11-29 21:46:00 +00:00
variant: "small-caps",
color: "rgba(216, 211, 197, 0.85)"
}
},
2013-11-28 22:21:00 +00:00
2013-11-29 21:46:00 +00:00
grid:
{
color: 'rgba(216, 211, 197, 0.55)',
borderWidth: 1,
labelMargin: 10,
mouseActiveRadius: 50
},
2013-11-28 22:21:00 +00:00
2013-11-29 21:46:00 +00:00
legend:
{
show: false
}
}
2013-11-28 22:21:00 +00:00
2013-11-29 21:46:00 +00:00
return options;
2013-11-28 22:21:00 +00:00
2013-11-29 21:46:00 +00:00
}
2013-11-28 22:21:00 +00:00
2013-11-29 21:46:00 +00:00
$(document).ready(function()
2013-11-28 22:21:00 +00:00
{
2013-11-29 21:46:00 +00:00
if(!("WebSocket" in window))
{
$('#chatLog, input, button, #examples').fadeOut("fast");
$('<p>Oh no, you need a browser that supports WebSockets. How about <a href="http://www.google.com/chrome">Google Chrome</a>?</p>').appendTo('#container');
}
else
{
// Status Socket ////////////////////////////////
ws_status.onopen = function()
{
console.log("Status Socket has been opened");
$.bootstrapGrowl("<span class=\"glyphicon glyphicon-exclamation-sign\"></span> <b>Yay</b><br/>I'm alive",
{
2013-11-29 18:39:11 +00:00
ele: 'body', // which element to append to
type: 'success', // (null, 'info', 'error', 'success')
offset: {from: 'top', amount: 250}, // 'top', or 'bottom'
align: 'center', // ('left', 'right', or 'center')
width: 385, // (integer, or 'auto')
delay: 2500,
allow_dismiss: true,
stackup_spacing: 10 // spacing between consecutively stacked growls.
2013-11-29 21:46:00 +00:00
});
2013-11-29 23:08:04 +00:00
};
2013-11-28 22:21:00 +00:00
2013-11-29 23:08:04 +00:00
ws_status.onclose = function()
{
$.bootstrapGrowl("<span class=\"glyphicon glyphicon-exclamation-sign\"></span> <b>ERROR 1:</b><br/>Status Websocket not available", {
2013-11-28 22:21:00 +00:00
ele: 'body', // which element to append to
2013-11-29 18:39:11 +00:00
type: 'error', // (null, 'info', 'error', 'success')
2013-11-28 22:21:00 +00:00
offset: {from: 'top', amount: 250}, // 'top', or 'bottom'
align: 'center', // ('left', 'right', or 'center')
width: 385, // (integer, or 'auto')
delay: 5000,
allow_dismiss: true,
stackup_spacing: 10 // spacing between consecutively stacked growls.
});
2013-11-29 23:08:04 +00:00
};
2013-11-29 21:46:00 +00:00
2013-11-29 23:08:04 +00:00
ws_status.onmessage = function(e)
{
x = JSON.parse(e.data);
2013-11-29 21:46:00 +00:00
2013-11-30 00:42:35 +00:00
if (x.type == "backlog")
{
if (x.profile)
{
selected_profile_name = x.profile.name;
}
2013-11-30 00:42:35 +00:00
$.each(x.log, function(i,v) {
graph.live.data.push([v.runtime, v.temperature]);
graph.plot = $.plot("#graph_container", [ graph.profile, graph.live ] , getOptions());
});
}
2013-11-29 23:08:04 +00:00
if(state!="EDIT")
{
state = x.state;
2013-11-29 21:46:00 +00:00
2013-11-30 13:05:42 +00:00
if (x.door == "OPEN")
{
2013-11-30 13:12:39 +00:00
2013-11-30 13:05:42 +00:00
}
if (state!=state_last)
{
2013-11-30 13:02:46 +00:00
if(state_last == "RUNNING")
{
$('#target_temp').html('---');
updateProgress(0);
$.bootstrapGrowl("<span class=\"glyphicon glyphicon-exclamation-sign\"></span> <b>Run completed</b>", {
ele: 'body', // which element to append to
type: 'success', // (null, 'info', 'error', 'success')
offset: {from: 'top', amount: 250}, // 'top', or 'bottom'
align: 'center', // ('left', 'right', or 'center')
width: 385, // (integer, or 'auto')
delay: 0,
allow_dismiss: true,
stackup_spacing: 10 // spacing between consecutively stacked growls.
});
}
}
2013-11-29 23:08:04 +00:00
if(state=="RUNNING")
{
$("#nav_start").hide();
$("#nav_stop").show();
2013-11-29 21:46:00 +00:00
2013-11-29 23:08:04 +00:00
graph.live.data.push([x.runtime, x.temperature]);
graph.plot = $.plot("#graph_container", [ graph.profile, graph.live ] , getOptions());
2013-11-29 21:46:00 +00:00
2013-11-29 23:08:04 +00:00
left = parseInt(x.totaltime-x.runtime);
var minutes = Math.floor(left / 60);
var seconds = left - minutes * 60;
eta = minutes+':'+ (seconds < 10 ? "0" : "") + seconds;
2013-11-29 21:46:00 +00:00
2013-11-29 23:08:04 +00:00
updateProgress(parseFloat(x.runtime)/parseFloat(x.totaltime)*100);
2013-12-09 11:51:08 +00:00
$('#state').html(eta);
$('#target_temp').html(parseInt(x.target));
2013-11-29 21:46:00 +00:00
2013-11-29 23:08:04 +00:00
}
else
{
$("#nav_start").show();
$("#nav_stop").hide();
2013-12-09 14:10:22 +00:00
$('#state').html('<p class="ds-text">'+state+'</p>');
2013-11-29 23:08:04 +00:00
}
$('#act_temp').html(parseInt(x.temperature));
2013-12-09 11:51:08 +00:00
$('#hazard').css("color", (x.temperature > 45 ? "rgb(255, 204, 0)" : "rgba(12, 12, 12, 0.62") );
$('#heat').css("color", (x.heat > 0.5 ? "rgba(233, 28, 0, 0.84)" : "rgba(46, 12, 12, 0.62") );
//$('#heat').css("text-shadow", (x.heat > 0.5 ? "0 0 5px rgba(233, 28, 0, 0.84), inset 0 0 5px 2px rgba(255,255,255,0.25)" : "0 0 1.1em rgba(0,0,0,0.75)") );
2013-12-09 14:10:22 +00:00
$('#air').css("color", (x.air > 0.5 ? "rgba(216, 211, 197, 1)" : "rgba(55, 55, 55, 0.62)") );
2013-12-09 11:51:08 +00:00
//$('#air').css("text-shadow", (x.air > 0.5 ? "0 0 5px rgba(240, 199, 67, 0.84), inset 0 0 5px 2px rgba(255,255,255,0.25)" : "0 0 1.1em rgba(0,0,0,0.75)") );
$('#cool').css("color", (x.cool > 0.5 ? "rgba(74, 159, 255, 0.84)" : "rgba(12, 28, 46, 0.62)") );
//$('#cool').css("text-shadow", (x.cool > 0.5 ? "0 0 5px rgba(74, 159, 255, 0.84), inset 0 0 5px 2px rgba(255,255,255,0.25)" : "0 0 1.1em rgba(0,0,0,0.75)") );
2013-12-05 21:11:43 +00:00
2013-11-29 21:46:00 +00:00
state_last = state;
2013-11-29 23:08:04 +00:00
}
};
2013-11-29 21:46:00 +00:00
2013-11-29 23:08:04 +00:00
// Control Socket ////////////////////////////////
2013-11-29 21:46:00 +00:00
2013-11-29 23:08:04 +00:00
ws_control.onopen = function()
{
2013-11-29 23:08:04 +00:00
};
2013-11-29 21:46:00 +00:00
2013-12-06 22:02:07 +00:00
ws_control.onmessage = function(e)
{
//Data from Simulation
console.log (e.data);
x = JSON.parse(e.data);
graph.live.data.push([x.runtime, x.temperature]);
graph.plot = $.plot("#graph_container", [ graph.profile, graph.live ] , getOptions());
}
2013-11-29 21:46:00 +00:00
2013-11-29 23:08:04 +00:00
// Storage Socket ///////////////////////////////
2013-11-28 22:21:00 +00:00
2013-11-29 23:08:04 +00:00
ws_storage.onopen = function()
{
ws_storage.send('GET');
};
2013-11-28 22:21:00 +00:00
2013-11-29 23:08:04 +00:00
ws_storage.onmessage = function(e)
{
message = JSON.parse(e.data);
2013-11-28 22:21:00 +00:00
2013-11-29 23:08:04 +00:00
if(message.resp)
{
if(message.resp == "FAIL")
{
if (confirm('Overwrite?'))
{
message.force=true;
console.log("Sending: " + JSON.stringify(message));
ws_storage.send(JSON.stringify(message));
}
else
{
//do nothing
}
}
2013-11-28 22:21:00 +00:00
2013-11-29 23:08:04 +00:00
return;
}
2013-11-28 22:21:00 +00:00
2013-11-29 23:08:04 +00:00
//the message is an array of profiles
//FIXME: this should be better, maybe a {"profiles": ...} container?
profiles = message;
//delete old options in select
$('#e2').find('option').remove().end();
2013-11-28 22:21:00 +00:00
2013-11-29 23:08:04 +00:00
// fill select with new options from websocket
for (var i=0; i<profiles.length; i++)
{
2013-11-28 22:21:00 +00:00
var profile = profiles[i];
2013-11-29 21:46:00 +00:00
//console.log(profile.name);
2013-11-28 22:21:00 +00:00
$('#e2').append('<option value="'+i+'">'+profile.name+'</option>');
if (profile.name == selected_profile_name)
{
selected_profile = i;
$('#e2').select2('val', i);
2013-12-09 11:51:08 +00:00
updateProfile(i);
2013-11-28 22:21:00 +00:00
}
2013-11-29 23:08:04 +00:00
}
2013-11-28 22:21:00 +00:00
2013-11-29 23:08:04 +00:00
};
2013-11-28 22:21:00 +00:00
2013-11-29 23:08:04 +00:00
$("#e2").select2(
{
placeholder: "Select Profile",
2013-12-05 18:53:18 +00:00
allowClear: false,
minimumResultsForSearch: -1
2013-11-29 23:08:04 +00:00
});
2013-11-28 22:21:00 +00:00
2013-11-29 23:08:04 +00:00
$("#e2").on("change", function(e)
{
2013-12-09 11:51:08 +00:00
updateProfile(e.val);
2013-11-29 23:08:04 +00:00
});
2013-11-29 23:08:04 +00:00
}
2013-11-28 22:21:00 +00:00
});