From 5e82d86ca65f1dfa97bf58dbe518ad6fa7d5d148 Mon Sep 17 00:00:00 2001 From: Adrian Bowyer Date: Mon, 6 May 2013 21:56:01 +0100 Subject: [PATCH] Knockout/JSON coming together... --- Configuration.h | 3 ++- SD-image/www/reprap.htm | 45 +++++++++++++++++++++++++++++++++++----- SD-image/www/reprap.htm~ | 45 +++++++++++++++++++++++++++++++++++----- Webserver.ino | 32 ++++++++++++++++++---------- 4 files changed, 103 insertions(+), 22 deletions(-) diff --git a/Configuration.h b/Configuration.h index 0efc1fa..f26dfe3 100644 --- a/Configuration.h +++ b/Configuration.h @@ -49,7 +49,8 @@ class Webserver; #define CLIENT_CLOSE_DELAY 1000 // Microseconds to wait after serving a page -#define PASSWORD_PAGE "passwd.php" +//#define PASSWORD_PAGE "passwd.php" +#define PASSWORD_PAGE "reprap.htm" #define INDEX_PAGE "reprap.htm" #define PRINT_PAGE "print.php" #define MESSAGE_FILE "messages.php" diff --git a/SD-image/www/reprap.htm b/SD-image/www/reprap.htm index e946180..6c2aac6 100644 --- a/SD-image/www/reprap.htm +++ b/SD-image/www/reprap.htm @@ -54,6 +54,7 @@
+
+

@@ -129,6 +131,12 @@ +

+
+ Send a G Code: + +
+
@@ -148,8 +156,11 @@ Settings Messages
-
-Logout +
+
+ Password: + +
@@ -167,6 +178,10 @@ function viewModel() self.pages = ['Control', 'Print', 'Help', 'Settings', 'Messages', 'Logout']; self.chosenPageId = ko.observable(); self.machineName = ko.observable(); + self.gotPassword = ko.observable(); + self.pwd = ko.observable(); + self.gcode = ko.observable(); + self.dummy = ko.observable(); // Behaviours self.getName = function() @@ -174,14 +189,34 @@ function viewModel() $.get('/rr_name', {}, self.machineName); }; + self.sendPassword = function() + { + $.get('/rr_password', {pwd: self.pwd()}, self.gotPassword); + //self.chosenPageId(self.pages[0]); + }; + + self.sendGCode = function() + { + $.get('/rr_gcode', {gcode: self.gcode()}, self.dummy); + }; + self.goToPage = function(page) { - self.chosenPageId(page); + if(page == self.pages[5]) + self.gotPassword(JSON.parse('{"password":"wrong"}')); + + if(self.gotPassword().password != "right") + { + self.chosenPageId(self.pages[5]); + return; + } + + self.chosenPageId(page); }; self.getName(); - self.goToPage(self.pages[0]); - + self.goToPage(self.pages[5]); + self.gotPassword(JSON.parse('{"password":"wrong"}')); }; ko.applyBindings(new viewModel()); diff --git a/SD-image/www/reprap.htm~ b/SD-image/www/reprap.htm~ index e946180..6c2aac6 100644 --- a/SD-image/www/reprap.htm~ +++ b/SD-image/www/reprap.htm~ @@ -54,6 +54,7 @@
+
+

@@ -129,6 +131,12 @@
+

+
+ Send a G Code: + +
+
@@ -148,8 +156,11 @@ Settings Messages
-
-Logout +
+
+ Password: + +
@@ -167,6 +178,10 @@ function viewModel() self.pages = ['Control', 'Print', 'Help', 'Settings', 'Messages', 'Logout']; self.chosenPageId = ko.observable(); self.machineName = ko.observable(); + self.gotPassword = ko.observable(); + self.pwd = ko.observable(); + self.gcode = ko.observable(); + self.dummy = ko.observable(); // Behaviours self.getName = function() @@ -174,14 +189,34 @@ function viewModel() $.get('/rr_name', {}, self.machineName); }; + self.sendPassword = function() + { + $.get('/rr_password', {pwd: self.pwd()}, self.gotPassword); + //self.chosenPageId(self.pages[0]); + }; + + self.sendGCode = function() + { + $.get('/rr_gcode', {gcode: self.gcode()}, self.dummy); + }; + self.goToPage = function(page) { - self.chosenPageId(page); + if(page == self.pages[5]) + self.gotPassword(JSON.parse('{"password":"wrong"}')); + + if(self.gotPassword().password != "right") + { + self.chosenPageId(self.pages[5]); + return; + } + + self.chosenPageId(page); }; self.getName(); - self.goToPage(self.pages[0]); - + self.goToPage(self.pages[5]); + self.gotPassword(JSON.parse('{"password":"wrong"}')); }; ko.applyBindings(new viewModel()); diff --git a/Webserver.ino b/Webserver.ino index 788ba0f..738901c 100644 --- a/Webserver.ino +++ b/Webserver.ino @@ -172,11 +172,11 @@ void Webserver::SendFile(char* nameOfFileToSend) char sLen[POST_LENGTH]; int len = -1; - if(!gotPassword) - { - sendTable = false; - nameOfFileToSend = PASSWORD_PAGE; - } else +// if(!gotPassword) +// { +// sendTable = false; +// nameOfFileToSend = PASSWORD_PAGE; +// } else sendTable = true; /* if(StringEndsWith(nameOfFileToSend, ".js")) @@ -279,11 +279,9 @@ void Webserver::WriteByte() void Webserver::CheckPassword() { - if(!StringEndsWith(clientQualifier, password)) - return; - - gotPassword = true; - strcpy(clientRequest, INDEX_PAGE); + gotPassword = StringEndsWith(clientQualifier, password); + + //strcpy(clientRequest, INDEX_PAGE); } @@ -312,9 +310,21 @@ void Webserver::GetKOString(char* request) ok = true; } + if(StringStartsWith(request, "password")) + { + CheckPassword(); + strcpy(jsonResponse, "{\"password\":\""); + if(gotPassword) + strcat(jsonResponse, "right"); + else + strcat(jsonResponse, "wrong"); + strcat(jsonResponse, "\"}"); + ok = true; + } + if(StringStartsWith(request, "gcode")) { - // TODO put something here + // TODO interpret GCode here, not in parse qual. strcpy(jsonResponse, "{}"); ok = true; }