Quellcode durchsuchen

return reference instead of pointer

Chris Mullins vor 8 Jahren
Ursprung
Commit
cd3110d16c

+ 4 - 8
lib/MiLight/FUT089PacketFormatter.cpp

@@ -60,13 +60,6 @@ void FUT089PacketFormatter::parsePacket(const uint8_t *packet, JsonObject& resul
   uint8_t command = (packetCopy[V2_COMMAND_INDEX] & 0x7F);
   uint8_t arg = packetCopy[V2_ARGUMENT_INDEX];
 
-  // only need state for saturation and kelvin (they have the same command ID)
-  GroupState* state = NULL;
-  if (command == FUT089_SATURATION) {
-    GroupId group(deviceId, groupId, REMOTE_TYPE_FUT089);
-    state = stateStore->get(group);
-  }
-
   if (command == FUT089_ON) {
     if (arg == FUT089_MODE_SPEED_DOWN) {
       result["command"] = "mode_speed_down";
@@ -91,7 +84,10 @@ void FUT089PacketFormatter::parsePacket(const uint8_t *packet, JsonObject& resul
   // saturation == kelvin. arg ranges are the same, so can't distinguish
   // without using state
   } else if (command == FUT089_SATURATION) {
-    if (state->getBulbMode() == BULB_MODE_COLOR) {
+    GroupId group(deviceId, groupId, REMOTE_TYPE_FUT089);
+    GroupState& state = stateStore->get(group);
+
+    if (state.getBulbMode() == BULB_MODE_COLOR) {
       result["saturation"] = 100 - constrain(arg, 0, 100);
     } else {
       result["color_temp"] = Units::whiteValToMireds(100 - arg, 100);

+ 1 - 4
lib/MiLight/MiLightClient.cpp

@@ -10,8 +10,7 @@ MiLightClient::MiLightClient(MiLightRadioFactory* radioFactory, GroupStateStore&
     currentRemote(NULL),
     numRadios(MiLightRadioConfig::NUM_CONFIGS),
     packetSentHandler(NULL),
-    stateStore(stateStore),
-    currentState(NULL)
+    stateStore(stateStore)
 {
   radios = new MiLightRadio*[numRadios];
 
@@ -72,8 +71,6 @@ void MiLightClient::prepare(const MiLightRemoteConfig* config,
   if (deviceId >= 0 && groupId >= 0) {
     currentRemote->packetFormatter->prepare(deviceId, groupId);
   }
-
-  currentState = stateStore.get(GroupId(deviceId, groupId, config->type));
 }
 
 void MiLightClient::prepare(const MiLightRemoteType type,

+ 0 - 1
lib/MiLight/MiLightClient.h

@@ -84,7 +84,6 @@ protected:
   unsigned int resendCount;
   PacketSentHandler packetSentHandler;
   GroupStateStore& stateStore;
-  const GroupState* currentState;
 
   MiLightRadio* switchRadio(const MiLightRemoteConfig* remoteConfig);
   uint8_t parseStatus(const JsonObject& object);

+ 6 - 4
lib/MiLightState/GroupStateStore.cpp

@@ -4,7 +4,7 @@ GroupStateStore::GroupStateStore(const size_t maxSize)
   : cache(GroupStateCache(maxSize))
 { }
 
-GroupState* GroupStateStore::get(const GroupId& id) {
+GroupState& GroupStateStore::get(const GroupId& id) {
   GroupState* state = cache.get(id);
 
   if (state == NULL) {
@@ -15,11 +15,13 @@ GroupState* GroupStateStore::get(const GroupId& id) {
     state = cache.set(id, loadedState);
   }
 
-  return state;
+  return *state;
 }
 
-GroupState* GroupStateStore::set(const GroupId &id, const GroupState& state) {
-  *(get(id)) = state;
+GroupState& GroupStateStore::set(const GroupId &id, const GroupState& state) {
+  GroupState& storedState = get(id);
+  storedState = state;
+  return storedState;
 }
 
 void GroupStateStore::trackEviction() {

+ 2 - 2
lib/MiLightState/GroupStateStore.h

@@ -9,8 +9,8 @@ class GroupStateStore {
 public:
   GroupStateStore(const size_t maxSize);
 
-  GroupState* get(const GroupId& id);
-  GroupState* set(const GroupId& id, const GroupState& state);
+  GroupState& get(const GroupId& id);
+  GroupState& set(const GroupId& id, const GroupState& state);
 
   void flush();
 

+ 3 - 5
lib/WebServer/MiLightHttpServer.cpp

@@ -7,7 +7,6 @@
 #include <string.h>
 #include <TokenIterator.h>
 #include <index.html.gz.h>
-#include <WiFiManager.h>
 
 void MiLightHttpServer::begin() {
   applySettings(settings);
@@ -116,8 +115,7 @@ void MiLightHttpServer::handleSystemPost() {
         server.send_P(200, TEXT_PLAIN, PSTR("true"));
 
         delay(100);
-        WiFiManager wifiManager;
-        wifiManager.resetSettings();
+        ESP.eraseConfig();
         delay(100);
         ESP.restart();
 
@@ -307,12 +305,12 @@ void MiLightHttpServer::handleGetGroup(const UrlTokenBindings* urlBindings) {
   }
 
   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);
+  state.applyState(obj);
   obj.printTo(body);
 
   server.send(200, APPLICATION_JSON, body);

+ 3 - 3
src/main.cpp

@@ -87,14 +87,14 @@ void onPacketSentHandler(uint8_t* packet, const MiLightRemoteConfig& config) {
     const MiLightRemoteConfig* remoteConfig = MiLightRemoteConfig::fromType(result.get<String>("device_type"));
 
     GroupId bulbId(deviceId, groupId, remoteConfig->type);
-    GroupState* groupState = stateStore.get(bulbId);
-    bool changes = groupState->patch(result);
+    GroupState& groupState = stateStore.get(bulbId);
+    bool changes = groupState.patch(result);
 
     char output[200];
     result.printTo(output);
 
     mqttClient->sendUpdate(config, deviceId, groupId, output);
-    groupState->applyState(result);
+    groupState.applyState(result);
     result.printTo(output);
     mqttClient->sendState(config, deviceId, groupId, output);
   }