[dss-commits] r8864 - in dss/trunk: core tests

dss-commits at forum.digitalstrom.org dss-commits at forum.digitalstrom.org
Tue Oct 27 09:40:35 CET 2009


Author: pstaehlin
Date: 2009-10-27 09:40:35 +0100 (Tue, 27 Oct 2009)
New Revision: 8864

Modified:
   dss/trunk/core/model.cpp
   dss/trunk/core/model.h
   dss/trunk/core/webserver.cpp
   dss/trunk/tests/modeltests.cpp
Log:
Implemented structure/removeZone

Closes #100

Modified: dss/trunk/core/model.cpp
===================================================================
--- dss/trunk/core/model.cpp	2009-10-25 10:55:16 UTC (rev 8863)
+++ dss/trunk/core/model.cpp	2009-10-27 08:40:35 UTC (rev 8864)
@@ -1480,6 +1480,42 @@
   	return *zone;
   } // allocateZone
 
+  void Apartment::removeZone(int _zoneID) {
+    for(std::vector<Zone*>::iterator ipZone = m_Zones.begin(), e = m_Zones.end();
+        ipZone != e; ++ipZone) {
+      Zone* pZone = *ipZone;
+      if(pZone->getID() == _zoneID) {
+        m_Zones.erase(ipZone);
+        delete pZone;
+        return;
+      }
+    }
+  } // removeZone
+  
+  void Apartment::removeDevice(dsid_t _device) {
+    for(std::vector<Device*>::iterator ipDevice = m_Devices.begin(), e = m_Devices.end();
+        ipDevice != e; ++ipDevice) {
+      Device* pDevice = *ipDevice;
+      if(pDevice->getDSID() == _device) {
+        m_Devices.erase(ipDevice);
+        delete pDevice;
+        return;
+      }
+    }
+  } // removeDevice
+  
+  void Apartment::removeModulator(dsid_t _modulator) {
+    for(std::vector<Modulator*>::iterator ipModulator = m_Modulators.begin(), e = m_Modulators.end();
+        ipModulator != e; ++ipModulator) {
+      Modulator* pModulator = *ipModulator;
+      if(pModulator->getDSID() == _modulator) {
+        m_Modulators.erase(ipModulator);
+        delete pModulator;
+        return;
+      }
+    }
+  } // removeModulator
+
   class SetLastCalledSceneAction : public IDeviceAction {
   protected:
     int m_SceneID;

Modified: dss/trunk/core/model.h
===================================================================
--- dss/trunk/core/model.h	2009-10-25 10:55:16 UTC (rev 8863)
+++ dss/trunk/core/model.h	2009-10-27 08:40:35 UTC (rev 8864)
@@ -792,7 +792,6 @@
     void loadDevices(XMLNode& _node);
     void loadModulators(XMLNode& _node);
     void loadZones(XMLNode& _node);
-    Modulator& allocateModulator(const dsid_t _dsid);
 
     void addDefaultGroupsToZone(Zone& _zone);
     /** Starts the event-processing */
@@ -842,8 +841,11 @@
     /** Allocates a zone and returns a reference to it. Should a zone with
       * the given _zoneID already exist, a reference to the existing zone will
       * be returned.
+      * NOTE: Outside code should never call this function
       */
     Zone& allocateZone(int _zoneID);
+    
+    Modulator& allocateModulator(const dsid_t _dsid);
 
     /** Returns a Modulator by name */
     Modulator& getModulator(const std::string& _modName);
@@ -864,6 +866,10 @@
 
     /** Returns wheter the apartment is still initializing or already running. */
     bool isInitializing() const { return m_IsInitializing; }
+    
+    void removeZone(int _zoneID);
+    void removeDevice(dsid_t _device);
+    void removeModulator(dsid_t _modulator);
 
     void initializeFromBus();
     bool scanModulator(Modulator& _modulator);

Modified: dss/trunk/core/webserver.cpp
===================================================================
--- dss/trunk/core/webserver.cpp	2009-10-25 10:55:16 UTC (rev 8863)
+++ dss/trunk/core/webserver.cpp	2009-10-27 08:40:35 UTC (rev 8864)
@@ -1404,7 +1404,6 @@
         return ResultToJSON(false, "Need parameter devid");
       }
     } else if(endsWith(_method, "structure/addZone")) {
-      bool ok = false;
       int zoneID = -1;
 
       string zoneIDStr = _parameter["zoneID"];
@@ -1413,11 +1412,33 @@
       }
       if(zoneID != -1) {
         getDSS().getApartment().allocateZone(zoneID);
-        ok = true;
       } else {
-        ResultToJSON(false, "could not find zone");
+        return ResultToJSON(false, "could not find zone");
       }
       return ResultToJSON(true, "");
+    } else if(endsWith(_method, "structure/removeZone")) {
+      int zoneID = -1;
+      
+      string zoneIDStr = _parameter["zoneID"];
+      if(!zoneIDStr.empty()) {
+        zoneID = strToIntDef(zoneIDStr, -1);
+      }
+      if(zoneID != -1) {
+        try {
+          Zone& zone = getDSS().getApartment().getZone(zoneID);
+          if(zone.getFirstZoneOnModulator() != -1) {
+            return ResultToJSON(false, "Cannot delete a primary zone");
+          }
+          if(zone.getDevices().length() > 0) {
+            return ResultToJSON(false, "Cannot delete a non-empty zone");
+          }
+          return JSONOk();
+        } catch(ItemNotFoundException&) {
+          return ResultToJSON(false, "Could not find zone");
+        }
+      } else {
+        return ResultToJSON(false, "Missing parameter zoneID");
+      }
     } else {
       _handled = false;
       return "";

Modified: dss/trunk/tests/modeltests.cpp
===================================================================
--- dss/trunk/tests/modeltests.cpp	2009-10-25 10:55:16 UTC (rev 8863)
+++ dss/trunk/tests/modeltests.cpp	2009-10-27 08:40:35 UTC (rev 8864)
@@ -291,4 +291,51 @@
   BOOST_CHECK_EQUAL(3, builderTest.length());
 } // testSetBuilder
 
+BOOST_AUTO_TEST_CASE(testRemoval) {
+  Apartment apt(NULL);
+  apt.initialize();
+
+  Device& dev1 = apt.allocateDevice(dsid_t(0,1));
+  dev1.setShortAddress(1);
+  dev1.getGroupBitmask().set(GroupIDYellow - 1);
+  
+  SetBuilder builder(apt);
+  BOOST_CHECK_EQUAL(1, builder.buildSet(".yellow", NULL).length());
+  
+  apt.removeDevice(dev1.getDSID());
+  BOOST_CHECK_EQUAL(0, builder.buildSet(".yellow", NULL).length()); 
+  
+  apt.allocateZone(1);
+  try {
+    apt.getZone(1);
+    BOOST_CHECK(true);
+  } catch(ItemNotFoundException&) {
+    BOOST_CHECK_MESSAGE(false, "Zone does not exist");
+  }
+  
+  apt.removeZone(1);
+  try {
+    apt.getZone(1);
+    BOOST_CHECK_MESSAGE(false, "Zone still exists");
+  } catch(ItemNotFoundException&) {
+    BOOST_CHECK(true);
+  }
+  
+  apt.allocateModulator(dsid_t(1,0));
+  try {
+    apt.getModulatorByDSID(dsid_t(1,0));
+    BOOST_CHECK(true);
+  } catch(ItemNotFoundException&) {
+    BOOST_CHECK_MESSAGE(false, "Modulator not found");
+  }
+  
+  apt.removeModulator(dsid_t(1,0));
+  try {
+    apt.getModulatorByDSID(dsid_t(1,0));
+    BOOST_CHECK_MESSAGE(false, "Modulator still exists");
+  } catch(ItemNotFoundException&) {
+    BOOST_CHECK(true);
+  }
+} // testRemoval
+
 BOOST_AUTO_TEST_SUITE_END()



More information about the dss-commits mailing list