]> git.basschouten.com Git - openhab-addons.git/commitdiff
[zoneminder] Add support for setting the server run state (#14906)
authorMark Hilbush <mark@hilbush.com>
Sun, 30 Apr 2023 13:10:54 +0000 (09:10 -0400)
committerGitHub <noreply@github.com>
Sun, 30 Apr 2023 13:10:54 +0000 (15:10 +0200)
* Add support for server run states

Signed-off-by: Mark Hilbush <mark@hilbush.com>
bundles/org.openhab.binding.zoneminder/README.md
bundles/org.openhab.binding.zoneminder/src/main/java/org/openhab/binding/zoneminder/internal/ZmBindingConstants.java
bundles/org.openhab.binding.zoneminder/src/main/java/org/openhab/binding/zoneminder/internal/dto/RunStateDTO.java [new file with mode: 0644]
bundles/org.openhab.binding.zoneminder/src/main/java/org/openhab/binding/zoneminder/internal/dto/RunStatesDTO.java [new file with mode: 0644]
bundles/org.openhab.binding.zoneminder/src/main/java/org/openhab/binding/zoneminder/internal/handler/ZmBridgeHandler.java
bundles/org.openhab.binding.zoneminder/src/main/resources/OH-INF/i18n/zoneminder.properties
bundles/org.openhab.binding.zoneminder/src/main/resources/OH-INF/thing/thing-types.xml
bundles/org.openhab.binding.zoneminder/src/main/resources/OH-INF/update/instructions.xml [new file with mode: 0644]

index 7f3b56af5e7a29905362aa82b75126d29e4a7094..225a81087185888134d38bf764544848144a7fc4 100644 (file)
@@ -77,6 +77,7 @@ The following configuration parameters are available on the Monitor thing:
 | imageUrl       | String      | Image URL for monitor id specified by imageMonitorId. Channel is UNDEF if the monitor id is not set, or if an OFF command is sent to the imageMonitorId channel. |
 | videoMonitorId | String      | Monitor ID to use for selecting a video URL. Also, sending an OFF command to this channel will reset the monitor id and url to UNDEF.  |
 | videoUrl       | String      | Video URL for monitor id specified by videoMonitorId. Channel is UNDEF if the monitor id is not set, or if an OFF command is sent to the videoMonitorId channel. |
+| runState       | String      | Set the run state for the ZoneMinder server |
 
 ### Monitor Thing
 
@@ -171,6 +172,7 @@ String ZmServer_ImageMonitorId "Image Monitor Id [%s]" { channel="zoneminder:ser
 String ZmServer_ImageUrl "Image Url [%s]" { channel="zoneminder:server:server:imageUrl" }
 String ZmServer_VideoMonitorId "Video Monitor Id [%s]" { channel="zm:server:server:videoMonitorId" }
 String ZmServer_VideoUrl "Video Url [%s]" { channel="zoneminder:server:server:videoUrl" }
+String ZmServer_RunState "Run State [%s]" { channel="zoneminder:server:server:runState" }
 
 // Monitor
 String      ZM_Monitor1_Id           "Monitor Id [%s]"              { channel="zoneminder:monitor:1:id" }
index 665ad7c92cbab915fb9506431cda354a9bfccb33..1d78f7c5b782e31d3aae3644f74343fa7591e4b4 100644 (file)
@@ -67,6 +67,7 @@ public class ZmBindingConstants {
     public static final String DEFAULT_URL_PATH = "/zm";
 
     // List of all channel ids
+    public static final String CHANNEL_RUN_STATE = "runState";
     public static final String CHANNEL_IMAGE_MONITOR_ID = "imageMonitorId";
     public static final String CHANNEL_VIDEO_MONITOR_ID = "videoMonitorId";
     public static final String CHANNEL_ID = "id";
diff --git a/bundles/org.openhab.binding.zoneminder/src/main/java/org/openhab/binding/zoneminder/internal/dto/RunStateDTO.java b/bundles/org.openhab.binding.zoneminder/src/main/java/org/openhab/binding/zoneminder/internal/dto/RunStateDTO.java
new file mode 100644 (file)
index 0000000..b6186f3
--- /dev/null
@@ -0,0 +1,55 @@
+/**
+ * Copyright (c) 2010-2023 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.zoneminder.internal.dto;
+
+import com.google.gson.annotations.SerializedName;
+
+/**
+ * The {@link RunStateDTO} contains a run state.
+ *
+ * @author Mark Hilbush - Initial contribution
+ */
+public class RunStateDTO extends AbstractResponseDTO {
+
+    /**
+     * A run state
+     */
+    @SerializedName("State")
+    public RunState runState;
+
+    public class RunState {
+        /**
+         * ID of run state, typically "1", "2", etc.
+         */
+        @SerializedName("Id")
+        public String id;
+
+        /**
+         * Name of run state
+         */
+        @SerializedName("Name")
+        public String name;
+
+        /**
+         * Definition of the run state
+         */
+        @SerializedName("Definition")
+        public String definition;
+
+        /**
+         * "1" if run state is active; "0" if not active
+         */
+        @SerializedName("IsActive")
+        public String isActive;
+    }
+}
diff --git a/bundles/org.openhab.binding.zoneminder/src/main/java/org/openhab/binding/zoneminder/internal/dto/RunStatesDTO.java b/bundles/org.openhab.binding.zoneminder/src/main/java/org/openhab/binding/zoneminder/internal/dto/RunStatesDTO.java
new file mode 100644 (file)
index 0000000..2f62388
--- /dev/null
@@ -0,0 +1,31 @@
+/**
+ * Copyright (c) 2010-2023 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.zoneminder.internal.dto;
+
+import java.util.List;
+
+import com.google.gson.annotations.SerializedName;
+
+/**
+ * The {@link RunStatesDTO} contains the list of run states.
+ *
+ * @author Mark Hilbush - Initial contribution
+ */
+public class RunStatesDTO extends AbstractResponseDTO {
+
+    /**
+     * List of run states
+     */
+    @SerializedName("states")
+    public List<RunStateDTO> runStatesList;
+}
index 61b645db289e1c17dca68fe1a3260294f484ac66..5f6b8e88625c8ab182c1938d04b133d9d80a1180 100644 (file)
@@ -16,6 +16,8 @@ import static org.openhab.binding.zoneminder.internal.ZmBindingConstants.*;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.net.URLEncoder;
+import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -48,6 +50,9 @@ import org.openhab.binding.zoneminder.internal.dto.MonitorItemDTO;
 import org.openhab.binding.zoneminder.internal.dto.MonitorStateDTO;
 import org.openhab.binding.zoneminder.internal.dto.MonitorStatusDTO;
 import org.openhab.binding.zoneminder.internal.dto.MonitorsDTO;
+import org.openhab.binding.zoneminder.internal.dto.RunStateDTO;
+import org.openhab.binding.zoneminder.internal.dto.RunStateDTO.RunState;
+import org.openhab.binding.zoneminder.internal.dto.RunStatesDTO;
 import org.openhab.binding.zoneminder.internal.dto.VersionDTO;
 import org.openhab.core.io.net.http.HttpUtil;
 import org.openhab.core.library.types.OnOffType;
@@ -186,6 +191,11 @@ public class ZmBridgeHandler extends BaseBridgeHandler {
             case CHANNEL_VIDEO_MONITOR_ID:
                 handleMonitorIdCommand(command, CHANNEL_VIDEO_MONITOR_ID, CHANNEL_VIDEO_URL, STREAM_VIDEO);
                 break;
+            case CHANNEL_RUN_STATE:
+                if (command instanceof StringType) {
+                    changeRunState(command);
+                }
+                break;
         }
     }
 
@@ -204,6 +214,12 @@ public class ZmBridgeHandler extends BaseBridgeHandler {
         }
     }
 
+    private void changeRunState(Command command) {
+        logger.debug("Bridge: Change run state to {}", command);
+        executeGet(buildUrl(String.format("/api/states/change/%s.json",
+                URLEncoder.encode(command.toString(), Charset.defaultCharset()))));
+    }
+
     @Override
     public Collection<Class<? extends ThingHandlerService>> getServices() {
         return Collections.singleton(MonitorDiscoveryService.class);
@@ -332,6 +348,7 @@ public class ZmBridgeHandler extends BaseBridgeHandler {
                         m.setLastEvent(getLastEvent(m.getId()));
                     }
                 }
+                updateRunStates();
             }
         } catch (JsonSyntaxException e) {
             logger.debug("Bridge: JsonSyntaxException: {}", e.getMessage(), e);
@@ -390,6 +407,32 @@ public class ZmBridgeHandler extends BaseBridgeHandler {
         return null;
     }
 
+    private void updateRunStates() {
+        if (!zmAuth.isAuthorized() || !isLinked(CHANNEL_RUN_STATE)) {
+            return;
+        }
+        try {
+            String response = executeGet(buildUrl("/api/states.json"));
+            RunStatesDTO runStates = GSON.fromJson(response, RunStatesDTO.class);
+            if (runStates != null) {
+                List<StateOption> options = new ArrayList<>();
+                for (RunStateDTO runState : runStates.runStatesList) {
+                    RunState state = runState.runState;
+                    logger.debug("Found runstate: id={}, name={}, desc={}, isActive={}", state.id, state.name,
+                            state.definition, state.isActive);
+                    options.add(new StateOption(state.name, state.name));
+                    if ("1".equals(state.isActive)) {
+                        updateState(CHANNEL_RUN_STATE, new StringType(state.name));
+                    }
+                }
+                stateDescriptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), CHANNEL_RUN_STATE),
+                        options);
+            }
+        } catch (JsonSyntaxException e) {
+            logger.debug("Bridge: JsonSyntaxException: {}", e.getMessage(), e);
+        }
+    }
+
     private @Nullable VersionDTO getVersion() {
         if (!zmAuth.isAuthorized()) {
             return null;
index 2b97d89abb9dc363e13afeb3cc227d82276d6cdd..dabe6b6d11d38e356bcb9b17d5771e3b1cd3a36e 100644 (file)
@@ -99,6 +99,8 @@ channel-type.zoneminder.image.label = Image
 channel-type.zoneminder.image.description = A single snapshot image
 channel-type.zoneminder.name.label = Name
 channel-type.zoneminder.name.description = Monitor name
+channel-type.zoneminder.runState.label = Run State
+channel-type.zoneminder.runState.description = The currently executing run state
 channel-type.zoneminder.state.label = State
 channel-type.zoneminder.state.description = Current monitor state
 channel-type.zoneminder.state.state.option.UNKNOWN = UNKNOWN
index a9e12479e863c2338012c7b2f249c95c95d32fea..3efd9d05728cdc8848479bf57c763e1015f949b5 100644 (file)
@@ -7,6 +7,7 @@
        <bridge-type id="server">
                <label>ZoneMinder Server</label>
                <description>Represents a ZoneMinder server</description>
+
                <channels>
                        <channel id="imageMonitorId" typeId="id">
                                <label>Image Monitor Id</label>
                        <channel id="videoUrl" typeId="url">
                                <label>Video URL</label>
                        </channel>
+                       <channel id="runState" typeId="runState"/>
                </channels>
+
+               <properties>
+                       <property name="thingTypeVersion">1</property>
+               </properties>
+
                <config-description-ref uri="thing-type:zoneminder:server"/>
        </bridge-type>
 
                <description>Length of the event in seconds</description>
                <state readOnly="true" pattern="%.2f %unit%"></state>
        </channel-type>
+       <channel-type id="runState">
+               <item-type>String</item-type>
+               <label>Run State</label>
+               <description>The currently executing run state</description>
+               <state readOnly="false" pattern="%s"></state>
+       </channel-type>
 
 </thing:thing-descriptions>
diff --git a/bundles/org.openhab.binding.zoneminder/src/main/resources/OH-INF/update/instructions.xml b/bundles/org.openhab.binding.zoneminder/src/main/resources/OH-INF/update/instructions.xml
new file mode 100644 (file)
index 0000000..36327cb
--- /dev/null
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
+<update:update-descriptions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:update="https://openhab.org/schemas/update-description/v1.0.0"
+       xsi:schemaLocation="https://openhab.org/schemas/update-description/v1.0.0 https://openhab.org/schemas/update-description-1.0.0.xsd">
+
+       <thing-type uid="zoneminder:server">
+
+               <instruction-set targetVersion="1">
+                       <add-channel id="runState">
+                               <type>zoneminder:runState</type>
+                       </add-channel>
+               </instruction-set>
+
+       </thing-type>
+
+</update:update-descriptions>