first commit
This commit is contained in:
178
ESP8266_DOMOTICZ_OCTOPRINT_WEB_VOLETS/Octoprint.cpp
Normal file
178
ESP8266_DOMOTICZ_OCTOPRINT_WEB_VOLETS/Octoprint.cpp
Normal file
@@ -0,0 +1,178 @@
|
||||
#include "Octoprint.h"
|
||||
|
||||
Octoprint::Octoprint(String octoprint, String port, String API)
|
||||
{
|
||||
_octoprint = octoprint;
|
||||
_port = port;
|
||||
_API = API;
|
||||
}
|
||||
|
||||
boolean Octoprint::connect()
|
||||
{
|
||||
|
||||
// Domo
|
||||
char _octo[_octoprint.length() + 1];
|
||||
_octoprint.toCharArray(_octo, _octoprint.length() + 1);
|
||||
|
||||
// Port
|
||||
char portc[_port.length() + 1];
|
||||
_port.toCharArray(portc, _port.length() + 1);
|
||||
int _iport = atoi(portc);
|
||||
|
||||
boolean connected = _client.connect(_octo, _iport);
|
||||
Serial.print(_octo);
|
||||
Serial.print(" ");
|
||||
Serial.print(_iport);
|
||||
Serial.print(" ");
|
||||
Serial.print(" connected ");
|
||||
Serial.print(connected);
|
||||
Serial.print(" ");
|
||||
Serial.println(WiFi.localIP());
|
||||
return connected;
|
||||
}
|
||||
|
||||
void Octoprint::close()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Octoprint::executeJson(String json, String svalue, String nvalue)
|
||||
{
|
||||
// Domo
|
||||
char _octo[_octoprint.length() + 1];
|
||||
_octoprint.toCharArray(_octo, _octoprint.length() + 1);
|
||||
|
||||
// Port
|
||||
char portc[_port.length() + 1];
|
||||
_port.toCharArray(portc, _port.length() + 1);
|
||||
int _iport = atoi(portc);
|
||||
|
||||
_client.print("GET " + json); //"GET /json.htm?type=command¶m=getuservariables");
|
||||
|
||||
if (svalue != "") {
|
||||
_client.print("&svalue=" + svalue);
|
||||
}
|
||||
if (nvalue != "") {
|
||||
_client.print("&nvalue=" + nvalue);
|
||||
}
|
||||
|
||||
_client.println(" HTTP/1.1");
|
||||
_client.print("Host: ");
|
||||
_client.print(_octo);
|
||||
_client.print(":");
|
||||
_client.println(_iport);
|
||||
_client.println("User-Agent: Arduino-ethernet");
|
||||
_client.println("X-Api-Key:" + _API);
|
||||
_client.println("Connection: close");
|
||||
_client.println();
|
||||
|
||||
Serial.println("json=" + json);
|
||||
|
||||
long interval = 2000;
|
||||
unsigned long currentMillis = millis(), previousMillis = millis();
|
||||
|
||||
while(!_client.available()){
|
||||
if( (currentMillis - previousMillis) > interval ){
|
||||
Serial.println("Timeout");
|
||||
_client.stop();
|
||||
return;
|
||||
}
|
||||
currentMillis = millis();
|
||||
}
|
||||
|
||||
// while (_client.connected())
|
||||
// {
|
||||
// if ( _client.available() )
|
||||
// {
|
||||
// char str=_client.read();
|
||||
// Serial.print(str);
|
||||
// }
|
||||
// }
|
||||
|
||||
while (1)
|
||||
{
|
||||
// Avoid WDT reset during long process
|
||||
yield();
|
||||
String line = readLine();
|
||||
|
||||
if (line == "") {
|
||||
break;
|
||||
}
|
||||
Serial.println(line);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Octoprint::executeJsonPost(String json, String postData)
|
||||
{
|
||||
if (connect()) {
|
||||
|
||||
_client.println("POST " +json + " HTTP/1.1");
|
||||
_client.println("Host: " + _octoprint + ":" + _port);
|
||||
_client.println("Cache-Control: no-cache");
|
||||
_client.println("X-Api-Key:" + _API);
|
||||
_client.println("Content-Type: application/json");
|
||||
_client.print("Content-Length: ");
|
||||
_client.println(postData.length());
|
||||
_client.println();
|
||||
_client.println(postData);
|
||||
|
||||
long interval = 2000;
|
||||
unsigned long currentMillis = millis(), previousMillis = millis();
|
||||
|
||||
while(!_client.available()){
|
||||
if( (currentMillis - previousMillis) > interval ){
|
||||
Serial.println("Timeout");
|
||||
_client.stop();
|
||||
return;
|
||||
}
|
||||
currentMillis = millis();
|
||||
}
|
||||
|
||||
// while (_client.connected())
|
||||
// {
|
||||
// if ( _client.available() )
|
||||
// {
|
||||
// char str=_client.read();
|
||||
// Serial.print(str);
|
||||
// }
|
||||
// }
|
||||
while (1)
|
||||
{
|
||||
// Avoid WDT reset during long process
|
||||
yield();
|
||||
String line = readLine();
|
||||
|
||||
if (line == "") {
|
||||
break;
|
||||
}
|
||||
Serial.println(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
String Octoprint::getIndexOfString(String data, String separator, int index)
|
||||
{
|
||||
int found = 0;
|
||||
int strIndex[] = { 0, -1 };
|
||||
int maxIndex = data.length() - 1;
|
||||
|
||||
for (int i = 0; i <= maxIndex && found <= index; i++) {
|
||||
if (data.charAt(i) == separator.charAt(0) || i == maxIndex) {
|
||||
found++;
|
||||
strIndex[0] = strIndex[1] + 1;
|
||||
strIndex[1] = (i == maxIndex) ? i + 1 : i;
|
||||
}
|
||||
}
|
||||
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
|
||||
}
|
||||
|
||||
String Octoprint::readLine()
|
||||
{
|
||||
return _client.readStringUntil('\n');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user