]> git.basschouten.com Git - openhab-addons.git/blob
4751fa21668cbaef3f132c35322e40064ef890c3
[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.linky.internal.console;
14
15 import java.time.LocalDate;
16 import java.time.format.DateTimeFormatter;
17 import java.time.format.DateTimeParseException;
18 import java.util.Arrays;
19 import java.util.List;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.linky.internal.handler.LinkyHandler;
23 import org.openhab.core.io.console.Console;
24 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
25 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingRegistry;
28 import org.openhab.core.thing.ThingUID;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Reference;
33
34 /**
35  * The {@link LinkyCommandExtension} is responsible for handling console commands
36  *
37  * @author Laurent Garnier - Initial contribution
38  */
39
40 @NonNullByDefault
41 @Component(service = ConsoleCommandExtension.class)
42 public class LinkyCommandExtension extends AbstractConsoleCommandExtension {
43
44     private static final String REPORT = "report";
45
46     private final ThingRegistry thingRegistry;
47
48     @Activate
49     public LinkyCommandExtension(final @Reference ThingRegistry thingRegistry) {
50         super("linky", "Interact with the Linky binding.");
51         this.thingRegistry = thingRegistry;
52     }
53
54     @Override
55     public void execute(String[] args, Console console) {
56         if (args.length >= 2) {
57             Thing thing = null;
58             try {
59                 ThingUID thingUID = new ThingUID(args[0]);
60                 thing = thingRegistry.get(thingUID);
61             } catch (IllegalArgumentException e) {
62                 thing = null;
63             }
64             ThingHandler thingHandler = null;
65             LinkyHandler handler = null;
66             if (thing != null) {
67                 thingHandler = thing.getHandler();
68                 if (thingHandler instanceof LinkyHandler) {
69                     handler = (LinkyHandler) thingHandler;
70                 }
71             }
72             if (thing == null) {
73                 console.println("Bad thing id '" + args[0] + "'");
74                 printUsage(console);
75             } else if (thingHandler == null) {
76                 console.println("No handler initialized for the thing id '" + args[0] + "'");
77                 printUsage(console);
78             } else if (handler == null) {
79                 console.println("'" + args[0] + "' is not a Linky thing id");
80                 printUsage(console);
81             } else if (REPORT.equals(args[1])) {
82                 LocalDate now = LocalDate.now();
83                 LocalDate start = now.minusDays(7);
84                 LocalDate end = now.minusDays(1);
85                 String separator = " ";
86                 if (args.length >= 3) {
87                     try {
88                         start = LocalDate.parse(args[2], DateTimeFormatter.ISO_LOCAL_DATE);
89                     } catch (DateTimeParseException e) {
90                         console.println(
91                                 "Invalid format for start day '" + args[2] + "'; expected format is YYYY-MM-DD");
92                         printUsage(console);
93                         return;
94                     }
95                 }
96                 if (args.length >= 4) {
97                     try {
98                         end = LocalDate.parse(args[3], DateTimeFormatter.ISO_LOCAL_DATE);
99                     } catch (DateTimeParseException e) {
100                         console.println("Invalid format for end day '" + args[3] + "'; expected format is YYYY-MM-DD");
101                         printUsage(console);
102                         return;
103                     }
104                 }
105                 if (!start.isBefore(now) || start.isAfter(end)) {
106                     console.println("Start day must be in the past and before the end day");
107                     printUsage(console);
108                     return;
109                 }
110                 if (end.isAfter(now.minusDays(1))) {
111                     end = now.minusDays(1);
112                 }
113                 if (args.length >= 5) {
114                     separator = args[4];
115                 }
116                 handler.reportValues(start, end, separator).forEach(console::println);
117             } else {
118                 printUsage(console);
119             }
120         } else {
121             printUsage(console);
122         }
123     }
124
125     @Override
126     public List<String> getUsages() {
127         return Arrays.asList(buildCommandUsage("<thingUID> " + REPORT + " <start day> <end day> [<separator>]",
128                 "report daily consumptions between two dates"));
129     }
130 }