Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for messages from the server with ohSnap #57

Open
wants to merge 12 commits into
base: feature/notifications
Choose a base branch
from
Open
3 changes: 1 addition & 2 deletions css/ohsnap.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

.alert {
padding: 15px;
margin-bottom: 20px;
margin-bottom: 10px;
border: 1px solid #eed3d7;
border-radius: 4px;
position: absolute;
bottom: 0px;
right: 21px;
/* Each alert has its own width */
Expand Down
6 changes: 6 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ body {
font-size: 12px;
}

#ohsnap
{
position: absolute;
bottom: 0;
right: 0;
}
#top-bar button.script-status{
margin:10px;
}
Expand Down
4 changes: 2 additions & 2 deletions js/control-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ function showProfileEditDialog(editableName, dialogTitle, isSaveAs) {
url: "save_beer_profile.php",
dataType: "json",
data: { name: profName, profile: profData },
success: function(response) {
success: ajaxSuccessHandler( function(response) {
if ( response.status !== 'error' ) {
loadProfile(profName, renderProfile);
$('#profileSaveError').hide();
Expand All @@ -431,7 +431,7 @@ function showProfileEditDialog(editableName, dialogTitle, isSaveAs) {
console.log("profile save error: " + decodeURIComponent(response.message));
$('#profileSaveError').show();
}
},
}),
error: function(xhr, ajaxOptions, thrownError) {
console.log("profile save HTTP error - request status: " + xhr.status + " - error: " + thrownError);
$('#profileSaveError').show();
Expand Down
5 changes: 3 additions & 2 deletions js/device-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function getDeviceList(){
type: 'POST',
url: 'socketmessage.php',
data: {messageType: "getDeviceList", message: ""},
success: function(response){
success: ajaxSuccessHandler( function(response){
if(showErrorsInNotification(response)){
return;
}
Expand All @@ -34,6 +34,7 @@ function getDeviceList(){
try
{
deviceAndPinList = JSON.parse(response);
deviceAndPinList = deviceAndPinList.response;
deviceList = deviceAndPinList.deviceList;
pinList = deviceAndPinList.pinList;
jsonParsed = true;
Expand Down Expand Up @@ -90,7 +91,7 @@ function getDeviceList(){
// scroll box down
var deviceConsole = document.getElementById('device-console');
deviceConsole.scrollTop = deviceConsole.scrollHeight;
},
}),
async:true
});
}
Expand Down
42 changes: 34 additions & 8 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,30 @@ var controlConstants = {};
var controlSettings = {};
var controlVariables = {};


function ajaxSuccessHandler(func){
return function(data) {
if (data !== null && typeof data === 'object')
{
jQuery.each(data.messages, function(i, val) {
switch (val.messageType) {
case 'error': var m_color = "red"; break;
case 'info': var m_color = "green"; break;
case 'warning': var m_color = "orange"; break;
}
ohSnap(val.message, {color: m_color, duration: 4000});
console.log(val);
});
// Send the 'response' part on to the original handler
func(data.response);
} else {
// Send the response to the handler
func(data);
}

};
}

function showErrorsInNotification(socketResponse){
if(socketResponse.indexOf("ERROR: ") == 0){
var errorMessage = socketResponse.replace("ERROR: ", "");
Expand All @@ -32,6 +56,7 @@ function showErrorsInNotification(socketResponse){
return false;
}


function receiveControlConstants(){
"use strict";
$.ajax({
Expand All @@ -41,7 +66,7 @@ function receiveControlConstants(){
contentType:"application/x-www-form-urlencoded; charset=utf-8",
data: {messageType: "getControlConstants", message: ""},
url: 'socketmessage.php',
success: function(controlConstantsJSON){
success: ajaxSuccessHandler (function(controlConstantsJSON){
window.controlConstants = controlConstantsJSON;
for (var i in window.controlConstants){
if(window.controlConstants.hasOwnProperty(i)){
Expand All @@ -54,7 +79,7 @@ function receiveControlConstants(){
$('.cc.'+i+' .val').text(window.controlConstants[i]);
}
}
}
})
});
}

Expand All @@ -67,7 +92,7 @@ function receiveControlSettings(callback){
contentType:"application/x-www-form-urlencoded; charset=utf-8",
url: 'socketmessage.php',
data: {messageType: "getControlSettings", message: ""},
success: function(controlSettingsJSON){
success: ajaxSuccessHandler( function(controlSettingsJSON){
window.controlSettings = controlSettingsJSON;
for (var i in controlSettings) {
if(controlSettings.hasOwnProperty(i)){
Expand Down Expand Up @@ -98,7 +123,7 @@ function receiveControlSettings(callback){
if (callback && typeof(callback) === "function") {
callback();
}
}
})
});
}

Expand Down Expand Up @@ -130,13 +155,13 @@ function receiveControlVariables(){
contentType:"application/x-www-form-urlencoded; charset=utf-8",
url: 'socketmessage.php',
data: {messageType: "getControlVariables", message: ""},
success: function(controlVariablesJSON){
success: ajaxSuccessHandler( function(controlVariablesJSON){
if(showErrorsInNotification(controlVariablesJSON)){
return;
}
var jsonPretty = JSON.stringify(JSON.parse(controlVariablesJSON),null,2);
$('#algorithm-json').html(syntaxHighlight(jsonPretty));
}
})
});
}

Expand Down Expand Up @@ -194,13 +219,13 @@ function refreshLcd(){
url: 'socketmessage.php',
data: {messageType: "lcd", message: ""}
})
.done( function(lcdText){
.done( ajaxSuccessHandler (function(lcdText){
var $lcdText = $('#lcd .lcd-text');
for (var i = lcdText.length - 1; i >= 0; i--) {
$lcdText.find('#lcd-line-' + i).html(lcdText[i]);
}
updateScriptStatus(true);
})
}))
.fail(function() {
var $lcdText = $('#lcd .lcd-text');
$lcdText.find('#lcd-line-0').html("Cannot receive");
Expand Down Expand Up @@ -435,5 +460,6 @@ $(document).ready(function(){
receiveControlSettings();
receiveControlVariables();
refreshLcd(); //will call refreshLcd and alternate between the two

});

4 changes: 2 additions & 2 deletions js/maintenance-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ function updateControllerVersion() {
contentType:"application/x-www-form-urlencoded; charset=utf-8",
url: 'socketmessage.php',
data: {messageType: "getVersion", message: ""},
success: function(controllerVersionJSON){
success: ajaxSuccessHandler( function(controllerVersionJSON){
setControllerVersion(controllerVersionJSON);
}
})
});
}

Expand Down
3 changes: 2 additions & 1 deletion maintenance-panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ function echoRotarySelect($optionName){
<option value="diecimila">Atmega168 based</option>
<option value="mega2560">Mega 2560</option>
<option value="mega">Mega 1280</option>
<option value="spark-core">Spark Core</option>
<option value="core">Spark Core</option>
<option value="photon">Particle Photon</option>
</select>
</div>
<div class="program-option">
Expand Down