Commits

Sebastian Bub committed d141f36

integrated external processes into the calling routine

  • Participants
  • Parent commits 0dfb135

Comments (0)

Files changed (2)

cmd.conf.MAY_BE_CHANGED

 #
 ################################################################################
 #
-# simple configuration file to define external commands as if it would be a gpio port
+# simple configuration file to define external commands to be called within normal processing
+# of GPIO ports
 #
 # In order to integrate anything you want to your GPIO server, you can define external
 # processes, e.g. you may send a notification on special conditions or you may integrate
-# a script which handles other hardware like the SPI port.
+# a commands/scripts which handles other hardware like the SPI port.
 #
-# The script is only called when not used in a condition.
+# The external command is only called when it is given a value, e.g. CMDsample=1. It is not called,
+# when used in a condition, e.g. CMDsample==1 or when only read, e.g. CMDsample=IN (old values are
+# evaluated/read here, or 0 if it has not run since server startup).
 # The value for the GET-Parameter is send as first parameter, e.g. CMDsample=1 would call /xxx/sample.sh 1
-# The STDOUT and STDERR is only logged on debug level. The return value of the script can be used in coditions.
-# The script is only called when not used in a condition. You must call it before, e.g. CMDsample=1&CMDsample==1&...
+# The STDOUT and STDERR is only logged on debug level and can not be processed by the server. The return
+# value of the script can be used in coditions.
+# The script is only called when not used in a condition. You must call it before,
+# e.g. CMDsample=1&CMDsample==0&...
 #
 #
 
-# you may define multiple commands, just count up the suffix and specify the maximum to check for
+# you may define multiple commands, just count up the suffix (starting with 1) and specify the maximum 
+# (0 means nothing is read)
 EXTCMD.NO=0
 
 # a name to address, e.g. the name 'date' must be addressed like CMDdate
 
 #EXTCMD.NAME.2=sample
 #EXTCMD.COMMANDNAME.2=/xxx/sample.sh
-#EXTCMD.TIMEOUT.2=2000
+#EXTCMD.TIMEOUT.2=2000
+
+
+

src/main/java/de/derbub/rpigpio/SingleContext.java

     private static GpioSysFsInterface gpioInterface;
     private static SingleContext INSTANCE;
     private static Map<String, String> virtualVariables = new HashMap<String, String>();
+    private static Map<String, String> cmdReturnCodes = new HashMap<String, String>();
     public static ExternalProcessesManager extProcessManager;
     public static final String CONFIG_SIMULATE_GPIOS_KEY = "simulate.gpios";
     public static final String CONFIG_TI_MAX_COUNTER_KEY = "ti.max.counter";
         for (int i = 0; i < commandArray.length; i++) {
             cmd = commandArray[i];
             if (cmd.isCondition()) {
-                // if it is a condition on a virtual key
-                if (isVirtualKey(cmd.getKey())) {
-                    String oldValue = virtualVariables.get(cmd.getKey());
+                // if it is a condition on a virtual key or a command
+                if (isVirtualKey(cmd.getKey()) || isCommandKey(cmd.getKey())) {
+                    String oldValue = isVirtualKey(cmd.getKey()) ? virtualVariables.get(cmd.getKey()) : cmdReturnCodes.get(cmd.getKey());
                     oldValue = (null == oldValue ? "0" : oldValue);
                     if (cmd.checkCondition(oldValue)) {
                         log.debug("condition '" + cmd.getKey() + " as expected");
                             log.info("Setting value " + cmd.getValue() + " for " + cmd.getKey());
                         }
                     } else {
-                        map.put(cmd.getKey(), cmd.getValue());
+                        if (isCommandKey(cmd.getKey())) {
+                            if (Direction.IN.equals(Direction.getDirectionByString(cmd.getValue()))) {
+                                returnMap.put(cmd.getKey(), (null == cmdReturnCodes.get(cmd.getKey()) ? "0" : cmdReturnCodes.get(cmd.getKey())));
+                                log.debug("Read " + returnMap.get(cmd.getKey()) + " for " + cmd.getKey());
+                            } else {
+                                String retCode = extProcessManager.process(cmd.getKey(), cmd.getValue());
+                                cmdReturnCodes.put(cmd.getKey(), retCode);
+                                returnMap.put(cmd.getKey(), retCode);
+                                log.info("Setting return code value " + retCode + " for " + cmd.getKey());
+                            }
+                        } else {
+                            map.put(cmd.getKey(), cmd.getValue());
+                        }
                     }
                 }
                 returnMap.putAll(gpioInterface.handleGpioPorts(checkGpioMapValidity(map)));
      * @return true if key starts with CMD_KEY_PREFIX
      */
     private boolean isCommandKey(String key) {
-        return key.startsWith(CMD_KEY_PREFIX);
+        return key.startsWith(ExternalProcessesManager.CMD_KEY_PREFIX);
     }
 
     /**