JSON responses half working.
This commit is contained in:
parent
731af3a04d
commit
522ca8a891
3 changed files with 67 additions and 37 deletions
39
Platform.ino
39
Platform.ino
|
@ -70,6 +70,13 @@ inline void Platform::SendToClient(unsigned char b)
|
||||||
if(client)
|
if(client)
|
||||||
{
|
{
|
||||||
client.write(b);
|
client.write(b);
|
||||||
|
if(reprap->GetWebserver()->EchoOutput())
|
||||||
|
{
|
||||||
|
Serial.write(b);
|
||||||
|
//Message(HOST_MESSAGE, message);
|
||||||
|
//if(StringEndsWith(message, "\n"))
|
||||||
|
// Message(HOST_MESSAGE, "o: ");
|
||||||
|
}
|
||||||
//Serial.write(b);
|
//Serial.write(b);
|
||||||
} else
|
} else
|
||||||
Message(HOST_MESSAGE, "Attempt to send byte to disconnected client.");
|
Message(HOST_MESSAGE, "Attempt to send byte to disconnected client.");
|
||||||
|
@ -521,6 +528,25 @@ void Platform::WriteString(int file, char* b)
|
||||||
//files[file].print(b);
|
//files[file].print(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Send something to the network client
|
||||||
|
|
||||||
|
void Platform::SendToClient(char* message)
|
||||||
|
{
|
||||||
|
if(client)
|
||||||
|
{
|
||||||
|
client.print(message);
|
||||||
|
if(reprap->GetWebserver()->EchoOutput())
|
||||||
|
{
|
||||||
|
Serial.print(message);
|
||||||
|
//Message(HOST_MESSAGE, message);
|
||||||
|
//if(StringEndsWith(message, "\n"))
|
||||||
|
// Message(HOST_MESSAGE, "o: ");
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
Message(HOST_MESSAGE, "Attempt to send string to disconnected client.<br>\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void Platform::Message(char type, char* message)
|
void Platform::Message(char type, char* message)
|
||||||
{
|
{
|
||||||
|
@ -547,19 +573,6 @@ void Platform::Message(char type, char* message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send something to the network client
|
|
||||||
|
|
||||||
void Platform::SendToClient(char* message)
|
|
||||||
{
|
|
||||||
if(client)
|
|
||||||
{
|
|
||||||
client.print(message);
|
|
||||||
//Serial.print("Sent: ");
|
|
||||||
//Serial.print(message);
|
|
||||||
} else
|
|
||||||
Message(HOST_MESSAGE, "Attempt to send string to disconnected client.<br>\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Where the php/htm etc files are
|
// Where the php/htm etc files are
|
||||||
|
|
||||||
char* Platform::GetWebDir()
|
char* Platform::GetWebDir()
|
||||||
|
|
|
@ -40,6 +40,7 @@ class Webserver
|
||||||
|
|
||||||
Webserver(Platform* p);
|
Webserver(Platform* p);
|
||||||
boolean Available();
|
boolean Available();
|
||||||
|
boolean EchoOutput();
|
||||||
byte Read();
|
byte Read();
|
||||||
void Init();
|
void Init();
|
||||||
void Spin();
|
void Spin();
|
||||||
|
@ -91,8 +92,9 @@ class Webserver
|
||||||
char clientLine[STRING_LENGTH];
|
char clientLine[STRING_LENGTH];
|
||||||
char clientRequest[STRING_LENGTH];
|
char clientRequest[STRING_LENGTH];
|
||||||
char clientQualifier[STRING_LENGTH];
|
char clientQualifier[STRING_LENGTH];
|
||||||
|
char jsonResponse[STRING_LENGTH];
|
||||||
char gcodeBuffer[GCODE_LENGTH];
|
char gcodeBuffer[GCODE_LENGTH];
|
||||||
int koPointer;
|
int jsonPointer;
|
||||||
boolean gcodeAvailable;
|
boolean gcodeAvailable;
|
||||||
int gcodePointer;
|
int gcodePointer;
|
||||||
int clientLinePointer;
|
int clientLinePointer;
|
||||||
|
@ -112,7 +114,11 @@ class Webserver
|
||||||
boolean sendTable;
|
boolean sendTable;
|
||||||
char eatInputChar;
|
char eatInputChar;
|
||||||
int phpRecordPointer;
|
int phpRecordPointer;
|
||||||
|
boolean echoInput;
|
||||||
|
boolean echoOutput;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline boolean Webserver::EchoOutput() { return echoOutput; }
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -191,7 +191,7 @@ void Webserver::SendFile(char* nameOfFileToSend)
|
||||||
if(StringStartsWith(nameOfFileToSend, KO_START))
|
if(StringStartsWith(nameOfFileToSend, KO_START))
|
||||||
GetKOString(&nameOfFileToSend[KO_FIRST]);
|
GetKOString(&nameOfFileToSend[KO_FIRST]);
|
||||||
|
|
||||||
if(koPointer < 0)
|
if(jsonPointer < 0)
|
||||||
{
|
{
|
||||||
fileBeingSent = platform->OpenFile(platform->PrependRoot(platform->GetWebDir(), nameOfFileToSend), false);
|
fileBeingSent = platform->OpenFile(platform->PrependRoot(platform->GetWebDir(), nameOfFileToSend), false);
|
||||||
if(fileBeingSent < 0)
|
if(fileBeingSent < 0)
|
||||||
|
@ -207,7 +207,7 @@ void Webserver::SendFile(char* nameOfFileToSend)
|
||||||
writing = true;
|
writing = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(koPointer >=0)
|
if(jsonPointer >=0)
|
||||||
platform->SendToClient("HTTP/1.1 201 OK\n");
|
platform->SendToClient("HTTP/1.1 201 OK\n");
|
||||||
else
|
else
|
||||||
platform->SendToClient("HTTP/1.1 200 OK\n");
|
platform->SendToClient("HTTP/1.1 200 OK\n");
|
||||||
|
@ -216,17 +216,17 @@ void Webserver::SendFile(char* nameOfFileToSend)
|
||||||
|
|
||||||
if(StringEndsWith(nameOfFileToSend, ".png"))
|
if(StringEndsWith(nameOfFileToSend, ".png"))
|
||||||
platform->SendToClient("image/png\n");
|
platform->SendToClient("image/png\n");
|
||||||
else if (koPointer >=0)
|
else if (jsonPointer >=0)
|
||||||
platform->SendToClient("application/json\n");
|
platform->SendToClient("application/json\n");
|
||||||
else if(StringEndsWith(nameOfFileToSend, ".js"))
|
else if(StringEndsWith(nameOfFileToSend, ".js"))
|
||||||
platform->SendToClient("application/javascript\n");
|
platform->SendToClient("application/javascript\n");
|
||||||
else
|
else
|
||||||
platform->SendToClient("text/html\n");
|
platform->SendToClient("text/html\n");
|
||||||
|
|
||||||
if (koPointer >=0)
|
if (jsonPointer >=0)
|
||||||
{
|
{
|
||||||
platform->SendToClient("Content-Length: ");
|
platform->SendToClient("Content-Length: ");
|
||||||
sprintf(sLen, "%d", strlen(clientRequest));
|
sprintf(sLen, "%d", strlen(jsonResponse));
|
||||||
platform->SendToClient(sLen);
|
platform->SendToClient(sLen);
|
||||||
platform->SendToClient("\n");
|
platform->SendToClient("\n");
|
||||||
}
|
}
|
||||||
|
@ -251,14 +251,14 @@ void Webserver::WriteByte()
|
||||||
{
|
{
|
||||||
unsigned char b;
|
unsigned char b;
|
||||||
|
|
||||||
if(koPointer >= 0)
|
if(jsonPointer >= 0)
|
||||||
{
|
{
|
||||||
if(clientRequest[koPointer])
|
if(jsonResponse[jsonPointer])
|
||||||
platform->SendToClient(clientRequest[koPointer++]);
|
platform->SendToClient(jsonResponse[jsonPointer++]);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
koPointer = -1;
|
jsonPointer = -1;
|
||||||
clientRequest[0] = 0;
|
jsonResponse[0] = 0;
|
||||||
CloseClient();
|
CloseClient();
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
|
@ -291,31 +291,31 @@ void Webserver::CheckPassword()
|
||||||
|
|
||||||
void Webserver::GetKOString(char* request)
|
void Webserver::GetKOString(char* request)
|
||||||
{
|
{
|
||||||
koPointer = 0;
|
jsonPointer = 0;
|
||||||
writing = true;
|
writing = true;
|
||||||
boolean ok = false;
|
boolean ok = false;
|
||||||
|
|
||||||
if(StringStartsWith(request, "name"))
|
if(StringStartsWith(request, "name"))
|
||||||
{
|
{
|
||||||
strcpy(clientRequest, "{\"myName\":\"");
|
strcpy(jsonResponse, "{\"myName\":\"");
|
||||||
//strcpy(clientRequest, "{\"");
|
//strcpy(clientRequest, "{\"");
|
||||||
strcat(clientRequest, myName);
|
strcat(jsonResponse, myName);
|
||||||
strcat(clientRequest, "\"}");
|
strcat(jsonResponse, "\"}");
|
||||||
ok = true;
|
ok = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(StringStartsWith(request, "page"))
|
if(StringStartsWith(request, "page"))
|
||||||
{
|
{
|
||||||
strcpy(clientRequest, "{\"page\":\"");
|
strcpy(jsonResponse, "{\"page\":\"");
|
||||||
strcat(clientRequest, myName); //FIXME
|
strcat(jsonResponse, myName); //FIXME
|
||||||
strcat(clientRequest, "\"}");
|
strcat(jsonResponse, "\"}");
|
||||||
ok = true;
|
ok = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ok)
|
if(ok)
|
||||||
{
|
{
|
||||||
platform->Message(HOST_MESSAGE, "KnockOut response: ");
|
platform->Message(HOST_MESSAGE, "KnockOut response: ");
|
||||||
platform->Message(HOST_MESSAGE, clientRequest);
|
platform->Message(HOST_MESSAGE, jsonResponse);
|
||||||
platform->Message(HOST_MESSAGE, " queued<br>\n");
|
platform->Message(HOST_MESSAGE, " queued<br>\n");
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
|
@ -347,9 +347,9 @@ void Webserver::ParseGetPost()
|
||||||
// Serial.print("HTTP request: ");
|
// Serial.print("HTTP request: ");
|
||||||
// Serial.println(clientLine);
|
// Serial.println(clientLine);
|
||||||
|
|
||||||
platform->Message(HOST_MESSAGE, "HTTP request: ");
|
//platform->Message(HOST_MESSAGE, "HTTP request: ");
|
||||||
platform->Message(HOST_MESSAGE, clientLine);
|
//platform->Message(HOST_MESSAGE, clientLine);
|
||||||
platform->Message(HOST_MESSAGE, "<br>\n");
|
//platform->Message(HOST_MESSAGE, "<br>\n");
|
||||||
|
|
||||||
int i = 5;
|
int i = 5;
|
||||||
int j = 0;
|
int j = 0;
|
||||||
|
@ -545,6 +545,7 @@ void Webserver::CharFromClient(char c)
|
||||||
|
|
||||||
void Webserver::Spin()
|
void Webserver::Spin()
|
||||||
{
|
{
|
||||||
|
char sw[2];
|
||||||
if(!active)
|
if(!active)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -562,7 +563,15 @@ void Webserver::Spin()
|
||||||
if(platform->ClientStatus() & AVAILABLE)
|
if(platform->ClientStatus() & AVAILABLE)
|
||||||
{
|
{
|
||||||
char c = platform->ClientRead();
|
char c = platform->ClientRead();
|
||||||
Serial.write(c);
|
if(echoInput)
|
||||||
|
{
|
||||||
|
Serial.print(c);
|
||||||
|
//sw[0] = c;
|
||||||
|
// sw[1] = 0;
|
||||||
|
//platform->Message(HOST_MESSAGE, sw);
|
||||||
|
//if(c == '\n')
|
||||||
|
// platform->Message(HOST_MESSAGE, "i: ");
|
||||||
|
}
|
||||||
|
|
||||||
if(receivingPost && postFile >= 0)
|
if(receivingPost && postFile >= 0)
|
||||||
{
|
{
|
||||||
|
@ -899,7 +908,7 @@ void Webserver::Init()
|
||||||
receivingPost = false;
|
receivingPost = false;
|
||||||
postSeen = false;
|
postSeen = false;
|
||||||
getSeen = false;
|
getSeen = false;
|
||||||
koPointer = -1;
|
jsonPointer = -1;
|
||||||
//postLength = 0L;
|
//postLength = 0L;
|
||||||
inPHPFile = false;
|
inPHPFile = false;
|
||||||
InitialisePHP();
|
InitialisePHP();
|
||||||
|
@ -915,6 +924,8 @@ void Webserver::Init()
|
||||||
gcodePointer = 0;
|
gcodePointer = 0;
|
||||||
sendTable = true;
|
sendTable = true;
|
||||||
phpRecordPointer = 0;
|
phpRecordPointer = 0;
|
||||||
|
echoInput = false;
|
||||||
|
echoOutput = false;
|
||||||
InitialisePost();
|
InitialisePost();
|
||||||
active = true;
|
active = true;
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue