]> git.basschouten.com Git - openhab-addons.git/blob
de2f974d5ce83abfa400682cd44eb9d4c602564e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.max.internal;
14
15 import static org.openhab.binding.max.internal.MaxBindingConstants.*;
16
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
20 import java.util.Set;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.max.internal.handler.MaxCubeBridgeHandler;
25 import org.openhab.core.io.console.Console;
26 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
27 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingRegistry;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.osgi.service.component.annotations.Activate;
33 import org.osgi.service.component.annotations.Component;
34 import org.osgi.service.component.annotations.Reference;
35
36 /**
37  * The {@link MaxConsoleCommandExtension} class provides additional options through the console command line.
38  *
39  * @author Marcel Verpaalen - Initial contribution
40  */
41 @Component(service = ConsoleCommandExtension.class)
42 @NonNullByDefault
43 public class MaxConsoleCommandExtension extends AbstractConsoleCommandExtension {
44
45     private static final String SUBCMD_BACKUP = "backup";
46     private static final String SUBCMD_REBOOT = "reboot";
47     private final ThingRegistry thingRegistry;
48
49     @Activate
50     public MaxConsoleCommandExtension(@Reference ThingRegistry thingRegistry) {
51         super("max", "Additional EQ3 MAX! commands.");
52         this.thingRegistry = thingRegistry;
53     }
54
55     @Override
56     public void execute(String[] args, Console console) {
57         if (args.length > 0) {
58             switch (args[0]) {
59                 case SUBCMD_BACKUP:
60                     handleBackup(console);
61                     break;
62                 case SUBCMD_REBOOT:
63                     handleReboot(args, console);
64                     break;
65                 default:
66                     console.println(String.format("Unknown MAX! sub command '%s'", args[0]));
67                     printUsage(console);
68                     break;
69             }
70         } else {
71             printUsage(console);
72             printMaxDevices(console, SUPPORTED_THING_TYPES_UIDS);
73         }
74     }
75
76     private void handleBackup(Console console) {
77         for (Thing thing : findDevices(SUPPORTED_BRIDGE_THING_TYPES_UIDS)) {
78             MaxCubeBridgeHandler handler = getHandler(thing.getUID().toString());
79             if (handler != null) {
80                 handler.backup();
81                 console.println(String.format("Creating backup for %s", thing.getUID().toString()));
82             }
83         }
84     }
85
86     private void handleReboot(String[] args, Console console) {
87         if (args.length > 1) {
88             MaxCubeBridgeHandler handler = getHandler(args[1]);
89             if (handler != null) {
90                 handler.cubeReboot();
91             } else {
92                 console.println(String.format("Could not find MAX! cube %s", args[1]));
93                 printMaxDevices(console, SUPPORTED_BRIDGE_THING_TYPES_UIDS);
94             }
95         } else {
96             console.println("Specify MAX! cube to reboot.");
97             printMaxDevices(console, SUPPORTED_BRIDGE_THING_TYPES_UIDS);
98         }
99     }
100
101     private List<Thing> findDevices(Set<ThingTypeUID> deviceTypes) {
102         List<Thing> devs = new ArrayList<>();
103         for (Thing thing : thingRegistry.getAll()) {
104             if (deviceTypes.contains(thing.getThingTypeUID())) {
105                 devs.add(thing);
106             }
107         }
108         return devs;
109     }
110
111     private @Nullable MaxCubeBridgeHandler getHandler(String thingId) {
112         MaxCubeBridgeHandler handler = null;
113         try {
114             ThingUID bridgeUID = new ThingUID(thingId);
115             Thing thing = thingRegistry.get(bridgeUID);
116             if ((thing != null) && (thing.getHandler() != null)
117                     && (thing.getHandler() instanceof MaxCubeBridgeHandler)) {
118                 handler = (MaxCubeBridgeHandler) thing.getHandler();
119             }
120         } catch (Exception e) {
121             handler = null;
122         }
123         return handler;
124     }
125
126     private void printMaxDevices(Console console, Set<ThingTypeUID> deviceTypes) {
127         console.println("Known MAX! devices: ");
128         for (Thing thing : findDevices(deviceTypes)) {
129             console.println(String.format("MAX! %s device: %s%s", thing.getThingTypeUID().getId(),
130                     thing.getUID().toString(), ((thing.getHandler() != null) ? "" : " (without handler)")));
131         }
132     }
133
134     @Override
135     public List<String> getUsages() {
136         return Arrays.asList(new String[] { buildCommandUsage(SUBCMD_BACKUP, "Backup MAX! cube data"),
137                 buildCommandUsage(SUBCMD_REBOOT + " <thingUID>", "Reset MAX! cube") });
138     }
139 }