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