]> git.basschouten.com Git - openhab-addons.git/blob
2072becffc9dcfcb706740250d67928213ee54ee
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.ipcamera.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.ipcamera.internal.handler.IpCameraHandler;
18 import org.openhab.core.automation.annotation.ActionInput;
19 import org.openhab.core.automation.annotation.RuleAction;
20 import org.openhab.core.thing.binding.ThingActions;
21 import org.openhab.core.thing.binding.ThingActionsScope;
22 import org.openhab.core.thing.binding.ThingHandler;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * The {@link IpCameraActions} is responsible for Actions.
28  *
29  * @author Matthew Skinner - Initial contribution
30  */
31
32 @ThingActionsScope(name = "ipcamera")
33 @NonNullByDefault
34 public class IpCameraActions implements ThingActions {
35     public final Logger logger = LoggerFactory.getLogger(getClass());
36     private @Nullable IpCameraHandler handler;
37
38     @Override
39     public void setThingHandler(@Nullable ThingHandler handler) {
40         this.handler = (IpCameraHandler) handler;
41     }
42
43     @Override
44     public @Nullable ThingHandler getThingHandler() {
45         return handler;
46     }
47
48     @RuleAction(label = "Record MP4", description = "Record MP4 to a set filename if given, or if filename is null to ipcamera.mp4")
49     public void recordMP4(
50             @ActionInput(name = "filename", label = "Filename", description = "Name that the recording will have once created, don't include the .mp4.") @Nullable String filename,
51             @ActionInput(name = "secondsToRecord", label = "Seconds to Record", description = "Enter a number of how many seconds to record.") int secondsToRecord) {
52         logger.debug("Recording {}.mp4 for {} seconds.", filename, secondsToRecord);
53         if (filename == null && handler != null) {
54             handler.recordMp4("ipcamera", secondsToRecord);
55         } else if (handler != null && filename != null) {
56             handler.recordMp4(filename, secondsToRecord);
57         }
58     }
59
60     public static void recordMP4(@Nullable ThingActions actions, @Nullable String filename, int secondsToRecord) {
61         if (actions instanceof IpCameraActions) {
62             ((IpCameraActions) actions).recordMP4(filename, secondsToRecord);
63         } else {
64             throw new IllegalArgumentException("Instance is not a IpCamera class.");
65         }
66     }
67
68     @RuleAction(label = "Record GIF", description = "Record GIF to a set filename if given, or if filename is null to ipcamera.gif")
69     public void recordGIF(
70             @ActionInput(name = "filename", label = "Filename", description = "Name that the recording will have once created, don't include the .mp4.") @Nullable String filename,
71             @ActionInput(name = "secondsToRecord", label = "Seconds to Record", description = "Enter a number of how many seconds to record.") int secondsToRecord) {
72         logger.debug("Recording {}.gif for {} seconds.", filename, secondsToRecord);
73         if (filename == null && handler != null) {
74             handler.recordGif("ipcamera", secondsToRecord);
75         } else if (handler != null && filename != null) {
76             handler.recordGif(filename, secondsToRecord);
77         }
78     }
79
80     public static void recordGIF(@Nullable ThingActions actions, @Nullable String filename, int secondsToRecord) {
81         if (actions instanceof IpCameraActions) {
82             ((IpCameraActions) actions).recordGIF(filename, secondsToRecord);
83         } else {
84             throw new IllegalArgumentException("Instance is not a IpCamera class.");
85         }
86     }
87 }