Commits

Sebastian Bub committed 8181429

some more comments

  • Participants
  • Parent commits da90dd3

Comments (0)

Files changed (4)

src/main/java/de/derbub/rpigpio/gpio/ConfiguredGpio.java

 
 import org.apache.log4j.Logger;
 
-/**
+/** Class represents a configuration for a gpio pin. Therefore it has a reference
+ *  to GpioPin and a Direction
  *
  * @author sb
  */

src/main/java/de/derbub/rpigpio/gpio/Direction.java

 
 package de.derbub.rpigpio.gpio;
 
-/**
+/** Class represents the direction of a gpio pin
  *
  * @author sb
  */

src/main/java/de/derbub/rpigpio/gpio/GpioPin.java

  * limitations under the License.
  *
  */
-
 package de.derbub.rpigpio.gpio;
 
 /**
+ * Class represents a hardware pin (actual just the number is important and has
+ * helpers to read in configuration.
  *
  * @author sb
  */
     GPIO23(23, "GPIO.23"),
     GPIO24(24, "GPIO.24"),
     GPIO25(25, "GPIO.25");
-    private static final String CONFIG_SUFFIX_DIRECTION_KEY = ".DIRECTION";
-    private static final String CONFIG_SUFFIX_NAME_KEY = ".NAME";
-    private static final String CONFIG_SUFFIX_DEFAULT_STATE_KEY = ".OUT.DEFAULT.STATE";
-    private static final String CONFIG_SUFFIX_AUTO_TOGGLE_TIME_KEY = ".OUT.AUTO.TOGGLE.TIME";
-    private static final String CONFIG_SUFFIX_BLOCK_TIME_KEY = ".OUT.BLOCK.TIME";
     private int number;
     private String configPrefix;
 
         return Integer.toString(number) + String.format("%n");
     }
 
-    public String getConfigPrefix() {
-        return configPrefix;
-    }
-
     public String getConfigDirectionKey() {
-        return configPrefix + GpioPin.CONFIG_SUFFIX_DIRECTION_KEY;
+        return configPrefix + ".DIRECTION";
     }
 
     public String getConfigNameKey() {
-        return configPrefix + GpioPin.CONFIG_SUFFIX_NAME_KEY;
+        return configPrefix + ".NAME";
     }
 
     public String getConfigDefaultStateKey() {
-        return configPrefix + GpioPin.CONFIG_SUFFIX_DEFAULT_STATE_KEY;
+        return configPrefix + ".OUT.DEFAULT.STATE";
     }
 
     public String getConfigAutoToggleTimeKey() {
-        return configPrefix + GpioPin.CONFIG_SUFFIX_AUTO_TOGGLE_TIME_KEY;
+        return configPrefix + ".OUT.AUTO.TOGGLE.TIME";
     }
 
     public String getConfigBlockTimeKey() {
-        return configPrefix + GpioPin.CONFIG_SUFFIX_BLOCK_TIME_KEY;
+        return configPrefix + ".OUT.BLOCK.TIME";
     }
 
     @Override

src/main/java/de/derbub/rpigpio/web/GpioServlet.java

  * limitations under the License.
  *
  */
-
 package de.derbub.rpigpio.web;
 
 import de.derbub.rpigpio.gpio.ConfiguredGpio;
 import org.apache.log4j.Logger;
 
 /**
+ * Main front component servlet which reads configuration file during init(),
+ * clears pins during destroy() and handles GET and POST requests.
  *
  * @author sb
  */
     private static GpioSysFsInterface gpioInterface;
 
     /**
-     * init method.
-     *
+     * init method reads configuration and configures the gpio ports
      */
     @Override
     public void init() {
         log.debug("init()");
+        // read configuration file to configProperties
         String configFileString = System.getProperty(SYSTEM_PROPERTY_GPIO_CONFIG);
         File configFile = new File(configFileString);
         try {
             }
             log.error(e.getClass().getName() + ":" + e.getMessage());
         }
-
+        // read configuration from configProperties and initialize system
         String simulateGpiosString = configProperties.getProperty(CONFIG_SIMULATE_GPIOS_KEY);
-        Boolean SIMULATE_GPIOS = (null == simulateGpiosString ? Boolean.TRUE : Boolean.valueOf(simulateGpiosString));
+        Boolean SIMULATE_GPIOS = (null == simulateGpiosString
+                ? Boolean.TRUE
+                : Boolean.valueOf(simulateGpiosString));
         log.warn(CONFIG_SIMULATE_GPIOS_KEY + " is set to " + SIMULATE_GPIOS);
 
         Map<String, ConfiguredGpio> gpioMap = new HashMap();
         for (GpioPin gpioPin : GpioPin.values()) {
-            Direction direction = Direction.getDirectionByString(configProperties.getProperty(gpioPin.getConfigDirectionKey()));
+            Direction direction = Direction.getDirectionByString(
+                    configProperties.getProperty(gpioPin.getConfigDirectionKey()));
             String name = configProperties.getProperty(gpioPin.getConfigNameKey());
             name = (null == name ? String.valueOf(gpioPin.getNumber()) : name);
             if (null != direction) {
                 }
             }
         }
+        // setup gpio ports
         GpioSysFsInterface.init(gpioMap);
         gpioInterface = GpioSysFsInterface.getInstance();
         gpioInterface.setSimulateGpios(SIMULATE_GPIOS);
     }
 
     /**
-     * destroy method from interface. Destroy context
-     *
+     * destroy method from interface. reset all states and unexport all gpio
+     * pins
      */
     @Override
     public void destroy() {
     @Override
     public void doGet(HttpServletRequest req, HttpServletResponse resp)
             throws ServletException, IOException {
-        log.debug("Get request for " + req.getRequestURI() + "?" + req.getQueryString() + " from " + req.getRemoteAddr());
+        log.debug("Get request for " + req.getRequestURI()
+                + "?" + req.getQueryString() + " from " + req.getRemoteAddr());
         // read possible keys
         Map<String, String> nameValuesUncheckedMap = new HashMap();
         for (String name : gpioInterface.getConfiguredGpioMap().keySet()) {
             if (null != value) {
                 String oldValue = nameValuesUncheckedMap.put(name, value);
                 if (null != oldValue) {
-                    log.info("Conflicting values for " + name + ". " + value + " is used now");
+                    log.info("Conflicting values for " + name + ". "
+                            + value + " is used now");
                 }
             }
         }
-        // check validity of values for keys
+        // check validity of values (for keys)
         Map<String, String> nameCheckedValuesMap = new HashMap();
         for (String name : nameValuesUncheckedMap.keySet()) {
             String uncheckedValue = nameValuesUncheckedMap.get(name).trim();
             ConfiguredGpio gpio = gpioInterface.getConfiguredGpioMap().get(name);
-            if (gpio.getDirection().isInput() && Direction.IN.getValue().equalsIgnoreCase(uncheckedValue)) {
+            if (gpio.getDirection().isInput()
+                    && Direction.getDirectionByString(uncheckedValue).equals(Direction.IN)) {
                 nameCheckedValuesMap.put(name, Direction.IN.getValue());
             }
-            if (gpio.getDirection().isOutput() && ("0".equals(uncheckedValue) || "1".equals(uncheckedValue))) {
+            if (gpio.getDirection().isOutput()
+                    && ("0".equals(uncheckedValue) || "1".equals(uncheckedValue))) {
                 nameCheckedValuesMap.put(name, uncheckedValue);
             }
         }
         // set/get hardware values
-        Map<String, String> hwHandledMap = gpioInterface.handleGpioPorts(nameCheckedValuesMap);
-        // response
+        Map<String, String> hwHandledMap =
+                gpioInterface.handleGpioPorts(nameCheckedValuesMap);
+        // format response
         StringBuilder sb = new StringBuilder();
         sb.append("{");
         boolean isFirst = true;
         for (String key : hwHandledMap.keySet()) {
-            if (isFirst) {
-                isFirst = false;
-            } else {
-                sb.append(",");
-            }
-            sb.append("\"").append(key).append("\":").append(hwHandledMap.get(key));
+            sb.append((isFirst ? "" : ","));
+            isFirst = false;
+            sb.append("\"").append(key).append("\":");
+            sb.append(hwHandledMap.get(key));
         }
         sb.append("}");
         resp.getWriter().println(sb.toString());
     }
+
+    @Override
+    public void doPost(HttpServletRequest req, HttpServletResponse resp)
+            throws ServletException, IOException {
+        log.debug("Handle post as get");
+        this.doGet(req, resp);
+    }
 }