Knockout/JSON coming together...

This commit is contained in:
Adrian Bowyer 2013-05-06 21:56:01 +01:00
parent 30fee16440
commit 5e82d86ca6
4 changed files with 103 additions and 22 deletions

View file

@ -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"

View file

@ -54,6 +54,7 @@
<br clear = "all">
<div data-bind="if: gotPassword().password == 'right'">
<ul class="pages" data-bind="foreach: pages">
<li data-bind="text: $data, css:
{
@ -62,6 +63,7 @@
click: $root.goToPage">
</li>
</ul>
</div>
<br>
@ -129,6 +131,12 @@
</div></table>
<br><br>
<form data-bind="submit: sendGCode">
Send a G Code: <input type="text" id="gcode" value="" data-bind="value: gcode" />
<button type="submit">Send</button>
</form>
</div>
<div data-bind="if: chosenPageId() == pages[1]">
@ -148,8 +156,11 @@ Settings
Messages
</div>
<div data-bind="if: chosenPageId() == pages[5]">
Logout
<div data-bind="if: (chosenPageId() == pages[5] && gotPassword().password != 'right')">
<form data-bind="submit: sendPassword">
Password: <input type="password" id="pwd" value="" data-bind="value: pwd" />
<button type="submit">Submit</button>
</form>
</div>
@ -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)
{
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());

View file

@ -54,6 +54,7 @@
<br clear = "all">
<div data-bind="if: gotPassword().password == 'right'">
<ul class="pages" data-bind="foreach: pages">
<li data-bind="text: $data, css:
{
@ -62,6 +63,7 @@
click: $root.goToPage">
</li>
</ul>
</div>
<br>
@ -129,6 +131,12 @@
</div></table>
<br><br>
<form data-bind="submit: sendGCode">
Send a G Code: <input type="text" id="gcode" value="" data-bind="value: gcode" />
<button type="submit">Send</button>
</form>
</div>
<div data-bind="if: chosenPageId() == pages[1]">
@ -148,8 +156,11 @@ Settings
Messages
</div>
<div data-bind="if: chosenPageId() == pages[5]">
Logout
<div data-bind="if: (chosenPageId() == pages[5] && gotPassword().password != 'right')">
<form data-bind="submit: sendPassword">
Password: <input type="password" id="pwd" value="" data-bind="value: pwd" />
<button type="submit">Submit</button>
</form>
</div>
@ -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)
{
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());

View file

@ -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 = StringEndsWith(clientQualifier, password);
gotPassword = true;
strcpy(clientRequest, INDEX_PAGE);
//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;
}