2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.max.internal;
15 import static org.openhab.binding.max.internal.MaxBindingConstants.*;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
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;
37 * The {@link MaxConsoleCommandExtension} class provides additional options through the console command line.
39 * @author Marcel Verpaalen - Initial contribution
41 @Component(service = ConsoleCommandExtension.class)
43 public class MaxConsoleCommandExtension extends AbstractConsoleCommandExtension {
45 private static final String SUBCMD_BACKUP = "backup";
46 private static final String SUBCMD_REBOOT = "reboot";
47 private final ThingRegistry thingRegistry;
50 public MaxConsoleCommandExtension(@Reference ThingRegistry thingRegistry) {
51 super("max", "Additional EQ3 MAX! commands.");
52 this.thingRegistry = thingRegistry;
56 public void execute(String[] args, Console console) {
57 if (args.length > 0) {
60 handleBackup(console);
63 handleReboot(args, console);
66 console.println(String.format("Unknown MAX! sub command '%s'", args[0]));
72 printMaxDevices(console, SUPPORTED_THING_TYPES_UIDS);
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) {
81 console.println(String.format("Creating backup for %s", thing.getUID().toString()));
86 private void handleReboot(String[] args, Console console) {
87 if (args.length > 1) {
88 MaxCubeBridgeHandler handler = getHandler(args[1]);
89 if (handler != null) {
92 console.println(String.format("Could not find MAX! cube %s", args[1]));
93 printMaxDevices(console, SUPPORTED_BRIDGE_THING_TYPES_UIDS);
96 console.println("Specify MAX! cube to reboot.");
97 printMaxDevices(console, SUPPORTED_BRIDGE_THING_TYPES_UIDS);
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())) {
111 private @Nullable MaxCubeBridgeHandler getHandler(String thingId) {
112 MaxCubeBridgeHandler handler = null;
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();
120 } catch (Exception e) {
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)")));
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") });