Переглянути джерело

remove unneeded github client; fix json bug

Chris Mullins 8 роки тому
батько
коміт
a5aeed9913

Різницю між файлами не показано, бо вона завелика
+ 2 - 2
dist/index.html.gz.h


+ 0 - 116
lib/GithubClient/GithubClient.cpp

@@ -1,116 +0,0 @@
-#include <GithubClient.h>
-#include <FS.h>
-#include <Size.h>
-
-static const char HTTP_REQUEST_FORMAT[] PROGMEM =
-  "GET %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: esp8266_milight_hub\r\nConnection: close\r\n\r\n";
-
-Stream& GithubClient::stream(const String& path) {
-  if (!client.connect(domain.c_str(), 443)) {
-    Serial.println(F("Failed to connect to github over HTTPS."));
-    return client;
-  }
-
-  char buffer[strlen_P(HTTP_REQUEST_FORMAT)+1];
-  strcpy_P(buffer, HTTP_REQUEST_FORMAT);
-
-  client.printf(
-    buffer,
-    path.c_str(),
-    domain.c_str()
-  );
-
-  return client;
-}
-
-bool GithubClient::download(const String& path, Stream& dest) {
-  Stream& client = stream(path);
-
-  if (client.available()) {
-    if (!client.find("\r\n\r\n")) {
-      Serial.println(F("Error seeking to body"));
-      return false;
-    }
-  } else {
-    Serial.println(F("Failed to open stream to Github"));
-    return false;
-  }
-
-  Serial.println(F("Downloading..."));
-
-  size_t bytes = 0;
-  size_t nextCheckpoint = 4096;
-
-  while (client.available()) {
-    size_t l = client.readBytes(buffer, GITHUB_CLIENT_BUFFER_SIZE);
-    size_t w = dest.write(buffer, l);
-
-    dest.flush();
-
-    if (w != l) {
-      printf_P(PSTR("Error writing to stream. Expected to write %d bytes, but only wrote %d\n"), l, w);
-      return false;
-    }
-
-    bytes += w;
-
-    if (bytes % 10 == 0) {
-      printf_P(".");
-    }
-
-    if (bytes >= nextCheckpoint) {
-      printf("[%d KB]\n", bytes/1024);
-      nextCheckpoint += 4096;
-    }
-
-    yield();
-  }
-
-  Serial.println(F("\n"));
-
-  return true;
-}
-
-bool GithubClient::download(const String& path, const String& fsPath) {
-  String tmpFile = fsPath + ".download_tmp";
-  File f = SPIFFS.open(tmpFile.c_str(), "w");
-
-  if (!f) {
-    Serial.print(F("ERROR - could not open file for downloading: "));
-    Serial.println(fsPath);
-    return false;
-  }
-  printf(".");
-
-  if (!download(path, f)) {
-    f.close();
-    return false;
-  }
-
-  f.flush();
-  f.close();
-
-  SPIFFS.remove(fsPath);
-  SPIFFS.rename(tmpFile, fsPath);
-
-  printf_P(PSTR("Finished downloading file: %s\n"), fsPath.c_str());
-
-  return true;
-}
-
-String GithubClient::buildRepoPath(const String& username, const String& repo, const String& repoPath) {
-  String path = String("/") + username + "/" + repo + "/master/" + repoPath;
-  return path;
-}
-
-String GithubClient::buildApiRequest(const String &username, const String &repo, const String &path) {
-  return String("/repos/") + username + "/" + repo + path;
-}
-
-GithubClient GithubClient::rawDownloader() {
-  return GithubClient(GITHUB_RAW_DOMAIN);
-}
-
-GithubClient GithubClient::apiClient() {
-  return GithubClient(GITHUB_API_DOMAIN);
-}

+ 0 - 38
lib/GithubClient/GithubClient.h

@@ -1,38 +0,0 @@
-#include <Arduino.h>
-#include <WiFiClientSecure.h>
-
-#ifndef _GITHUB_CLIENT
-#define _GITHUB_CLIENT
-
-#define GITHUB_CLIENT_BUFFER_SIZE 32
-
-// #define GITHUB_RAW_FINGERPRINT "CC AA 48 48 66 46 0E 91 53 2C 9C 7C 23 2A B1 74 4D 29 9D 33"
-#define GITHUB_RAW_DOMAIN "raw.githubusercontent.com"
-
-// #define GITHUB_API_FINGERPRINT "35 85 74 EF 67 35 A7 CE 40 69 50 F3 C0 F6 80 CF 80 3B 2E 19"
-#define GITHUB_API_DOMAIN "api.github.com"
-
-class GithubClient {
-public:
-  GithubClient(const char* domain)
-    : domain(String(domain))
-  { }
-
-  Stream& stream(const String& path);
-  bool download(const String& path, Stream& dest);
-  bool download(const String& path, const String& fsPath);
-
-  static GithubClient rawDownloader();
-  static GithubClient apiClient();
-
-  static String buildRepoPath(const String& username, const String& repo, const String& path);
-  static String buildApiRequest(const String& username, const String& repo, const String& path);
-
-  uint8_t buffer[GITHUB_CLIENT_BUFFER_SIZE];
-
-private:
-  WiFiClientSecure client;
-  const String domain;
-};
-
-#endif

+ 6 - 77
lib/WebServer/MiLightHttpServer.cpp

@@ -4,7 +4,6 @@
 #include <Settings.h>
 #include <MiLightHttpServer.h>
 #include <MiLightRadioConfig.h>
-#include <GithubClient.h>
 #include <string.h>
 #include <TokenIterator.h>
 #include <index.html.gz.h>
@@ -23,10 +22,8 @@ void MiLightHttpServer::begin() {
 
   server.onPattern("/gateways/:device_id/:type/:group_id", HTTP_ANY, [this](const UrlTokenBindings* b) { handleUpdateGroup(b); });
   server.onPattern("/raw_commands/:type", HTTP_ANY, [this](const UrlTokenBindings* b) { handleSendRaw(b); });
-  server.onPattern("/download_update/:component", HTTP_GET, [this](const UrlTokenBindings* b) { handleDownloadUpdate(b); });
   server.on("/web", HTTP_POST, [this]() { server.send_P(200, TEXT_PLAIN, PSTR("success")); }, handleUpdateFile(WEB_INDEX_FILENAME));
   server.on("/about", HTTP_GET, [this]() { handleAbout(); });
-  server.on("/latest_release", HTTP_GET, [this]() { handleGetLatestRelease(); });
   server.on("/system", HTTP_POST, [this]() { handleSystemPost(); });
   server.on("/firmware", HTTP_POST,
     [this](){
@@ -74,36 +71,6 @@ void MiLightHttpServer::begin() {
   server.begin();
 }
 
-void MiLightHttpServer::handleGetLatestRelease() {
-  GithubClient client = GithubClient::apiClient();
-  String path = GithubClient::buildApiRequest(
-    MILIGHT_GITHUB_USER,
-    MILIGHT_GITHUB_REPO,
-    "/releases/latest"
-  );
-
-  // This is an ugly hack, but probably not worth optimizing. The nice way
-  // to do this would be to extract the content len from GitHub's response
-  // and stream the body to the server directly. But this would require parsing
-  // headers in the response from GitHub, which seems like more trouble than
-  // it's worth.
-  const String& fsPath = "/_cv.json";
-  size_t tries = 0;
-
-  while (tries++ < MAX_DOWNLOAD_ATTEMPTS && !client.download(path, fsPath)) {
-    Serial.println(F("Failed download attempt."));
-  }
-
-  if (!SPIFFS.exists(fsPath)) {
-    server.send_P(500, TEXT_PLAIN, PSTR("Failed to stream API request from GitHub. Check Serial logs for more information."));
-    return;
-  }
-
-  File file = SPIFFS.open(fsPath, "r");
-  server.streamFile(file, APPLICATION_JSON);
-  SPIFFS.remove(fsPath);
-}
-
 void MiLightHttpServer::handleClient() {
   server.handleClient();
 }
@@ -152,43 +119,6 @@ void MiLightHttpServer::handleSystemPost() {
   }
 }
 
-void MiLightHttpServer::handleDownloadUpdate(const UrlTokenBindings* bindings) {
-  GithubClient downloader = GithubClient::rawDownloader();
-  const String& component = bindings->get("component");
-
-  if (component.equalsIgnoreCase("web")) {
-    Serial.println(F("Attempting to update web UI..."));
-
-    bool result = false;
-    size_t tries = 0;
-
-    while (!result && tries++ <= MAX_DOWNLOAD_ATTEMPTS) {
-      Serial.println(F("building url\n"));
-      String urlPath = GithubClient::buildRepoPath(
-        MILIGHT_GITHUB_USER,
-        MILIGHT_GITHUB_REPO,
-        MILIGHT_REPO_WEB_PATH
-      );
-
-      printf_P(PSTR("URL: %s\n"), urlPath.c_str());
-
-      result = downloader.download(urlPath, WEB_INDEX_FILENAME);
-    }
-
-    Serial.println(F("Download complete!"));
-
-    if (result) {
-      server.sendHeader("Location", "/");
-      server.send(302);
-    } else {
-      server.send_P(500, TEXT_PLAIN, PSTR("Failed to download update from Github. Check serial logs for more information."));
-    }
-  } else {
-    String body = String("Unknown component: ") + component;
-    server.send(400, "text/plain", body);
-  }
-}
-
 void MiLightHttpServer::applySettings(Settings& settings) {
   if (settings.hasAuthSettings()) {
     server.requireAuthentication(settings.adminUsername, settings.adminPassword);
@@ -380,9 +310,9 @@ void MiLightHttpServer::handleUpdateGroup(const UrlTokenBindings* urlBindings) {
     MiLightRadioConfig* config = MiLightRadioConfig::fromString(_radioType);
 
     if (config == NULL) {
-      String body = "Unknown device type: ";
-      body += String(_radioType);
-      server.send(400, "text/plain", body);
+      char buffer[40];
+      sprintf_P(buffer, PSTR("Unknown device type: %s"), _radioType);
+      server.send(400, "text/plain", buffer);
       return;
     }
 
@@ -413,10 +343,9 @@ void MiLightHttpServer::handleSendRaw(const UrlTokenBindings* bindings) {
   MiLightRadioConfig* config = MiLightRadioConfig::fromString(bindings->get("type"));
 
   if (config == NULL) {
-    String body = "Unknown device type: ";
-    body += bindings->get("type");
-
-    server.send(400, "text/plain", body);
+    char buffer[50];
+    sprintf_P(buffer, PSTR("Unknown device type: %s"), bindings->get("type"));
+    server.send(400, "text/plain", buffer);
     return;
   }
 

+ 0 - 2
lib/WebServer/MiLightHttpServer.h

@@ -42,12 +42,10 @@ protected:
   void handleUpdateSettings();
   void handleGetRadioConfigs();
   void handleAbout();
-  void handleGetLatestRelease();
   void handleSystemPost();
   void handleListenGateway(const UrlTokenBindings* urlBindings);
   void handleSendRaw(const UrlTokenBindings* urlBindings);
   void handleUpdateGroup(const UrlTokenBindings* urlBindings);
-  void handleDownloadUpdate(const UrlTokenBindings* urlBindings);
 
   void handleRequest(const JsonObject& request);
 

+ 0 - 2
src/main.cpp

@@ -3,7 +3,6 @@
 #include <ArduinoJson.h>
 #include <stdlib.h>
 #include <FS.h>
-#include <GithubClient.h>
 #include <IntParsing.h>
 #include <Size.h>
 #include <MiLightRadioConfig.h>
@@ -16,7 +15,6 @@
 #include <RGBConverter.h>
 #include <MiLightDiscoveryServer.h>
 #include <MiLightClient.h>
-#include <index.html.gz.h>
 
 WiFiManager wifiManager;
 

+ 1 - 1
web/src/js/script.js

@@ -305,7 +305,7 @@ var handleCheckForUpdates = function() {
     '/about',
     {
       success: function(data) {
-        currentVersion = JSON.parse(data);
+        currentVersion = data;
         handleReceiveData();
       },
       failure: handleError