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

HTTP PUT for bulbs responds with new bulb state

Chris Mullins 8 роки тому
батько
коміт
c4fb11aefe
2 змінених файлів з 22 додано та 9 видалено
  1. 21 9
      lib/WebServer/MiLightHttpServer.cpp
  2. 1 0
      lib/WebServer/MiLightHttpServer.h

+ 21 - 9
lib/WebServer/MiLightHttpServer.cpp

@@ -292,6 +292,16 @@ void MiLightHttpServer::handleListenGateway(const UrlTokenBindings* bindings) {
   server.send(200, "text/plain", response);
   server.send(200, "text/plain", response);
 }
 }
 
 
+void MiLightHttpServer::sendGroupState(GroupState &state) {
+  String body;
+  StaticJsonBuffer<200> jsonBuffer;
+  JsonObject& obj = jsonBuffer.createObject();
+  state.applyState(obj);
+  obj.printTo(body);
+
+  server.send(200, APPLICATION_JSON, body);
+}
+
 void MiLightHttpServer::handleGetGroup(const UrlTokenBindings* urlBindings) {
 void MiLightHttpServer::handleGetGroup(const UrlTokenBindings* urlBindings) {
   const String _deviceId = urlBindings->get("device_id");
   const String _deviceId = urlBindings->get("device_id");
   uint8_t _groupId = atoi(urlBindings->get("group_id"));
   uint8_t _groupId = atoi(urlBindings->get("group_id"));
@@ -306,14 +316,7 @@ void MiLightHttpServer::handleGetGroup(const UrlTokenBindings* urlBindings) {
 
 
   GroupId groupId(parseInt<uint16_t>(_deviceId), _groupId, _remoteType->type);
   GroupId groupId(parseInt<uint16_t>(_deviceId), _groupId, _remoteType->type);
   GroupState& state = stateStore.get(groupId);
   GroupState& state = stateStore.get(groupId);
-
-  String body;
-  StaticJsonBuffer<200> jsonBuffer;
-  JsonObject& obj = jsonBuffer.createObject();
-  state.applyState(obj);
-  obj.printTo(body);
-
-  server.send(200, APPLICATION_JSON, body);
+  sendGroupState(stateStore.get(groupId));
 }
 }
 
 
 void MiLightHttpServer::handleUpdateGroup(const UrlTokenBindings* urlBindings) {
 void MiLightHttpServer::handleUpdateGroup(const UrlTokenBindings* urlBindings) {
@@ -343,6 +346,9 @@ void MiLightHttpServer::handleUpdateGroup(const UrlTokenBindings* urlBindings) {
   TokenIterator groupIdItr(groupIds, _groupIds.length());
   TokenIterator groupIdItr(groupIds, _groupIds.length());
   TokenIterator remoteTypesItr(remoteTypes, _remoteTypes.length());
   TokenIterator remoteTypesItr(remoteTypes, _remoteTypes.length());
 
 
+  GroupId foundGroupId;
+  size_t groupCount = 0;
+
   while (remoteTypesItr.hasNext()) {
   while (remoteTypesItr.hasNext()) {
     const char* _remoteType = remoteTypesItr.nextToken();
     const char* _remoteType = remoteTypesItr.nextToken();
     const MiLightRemoteConfig* config = MiLightRemoteConfig::fromType(_remoteType);
     const MiLightRemoteConfig* config = MiLightRemoteConfig::fromType(_remoteType);
@@ -364,11 +370,17 @@ void MiLightHttpServer::handleUpdateGroup(const UrlTokenBindings* urlBindings) {
 
 
         milightClient->prepare(config, deviceId, groupId);
         milightClient->prepare(config, deviceId, groupId);
         handleRequest(request);
         handleRequest(request);
+        foundGroupId = GroupId(deviceId, groupId, config->type);
+        groupCount++;
       }
       }
     }
     }
   }
   }
 
 
-  server.send(200, APPLICATION_JSON, "true");
+  if (groupCount == 1) {
+    sendGroupState(stateStore.get(foundGroupId));
+  } else {
+    server.send(200, APPLICATION_JSON, "true");
+  }
 }
 }
 
 
 void MiLightHttpServer::handleRequest(const JsonObject& request) {
 void MiLightHttpServer::handleRequest(const JsonObject& request) {

+ 1 - 0
lib/WebServer/MiLightHttpServer.h

@@ -44,6 +44,7 @@ protected:
   ESP8266WebServer::THandlerFunction handleUpdateFile(const char* filename);
   ESP8266WebServer::THandlerFunction handleUpdateFile(const char* filename);
   ESP8266WebServer::THandlerFunction handleServe_P(const char* data, size_t length);
   ESP8266WebServer::THandlerFunction handleServe_P(const char* data, size_t length);
   void applySettings(Settings& settings);
   void applySettings(Settings& settings);
+  void sendGroupState(GroupState& state);
 
 
   void handleUpdateSettings();
   void handleUpdateSettings();
   void handleGetRadioConfigs();
   void handleGetRadioConfigs();