]> git.basschouten.com Git - openhab-addons.git/blob
601c481330bb22f5e0bb2698eddc45df88059ea0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.solaredge.internal.handler;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.solaredge.internal.command.AggregateDataUpdatePrivateApi;
20 import org.openhab.binding.solaredge.internal.command.AggregateDataUpdatePublicApi;
21 import org.openhab.binding.solaredge.internal.command.SolarEdgeCommand;
22 import org.openhab.binding.solaredge.internal.model.AggregatePeriod;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Polling worker class. This is responsible for periodic polling of sensor data.
28  *
29  * @author Alexander Friese - initial contribution
30  */
31 @NonNullByDefault
32 public class SolarEdgeAggregateDataPolling implements Runnable {
33     /**
34      * Logger
35      */
36     private final Logger logger = LoggerFactory.getLogger(getClass());
37
38     /**
39      * Handler for delegation to callbacks.
40      */
41     private final SolarEdgeHandler handler;
42
43     /**
44      * Constructor.
45      *
46      * @param handler handler which handles results of polling
47      */
48     public SolarEdgeAggregateDataPolling(SolarEdgeHandler handler) {
49         this.handler = handler;
50     }
51
52     /**
53      * Poll the SolarEdge Webservice one time per call.
54      */
55     @Override
56     public void run() {
57         // if no meter is present all data will be fetched by the 'LiveDataUpdateMeterless'
58         if (handler.getConfiguration().isMeterInstalled()) {
59             logger.debug("polling SolarEdge aggregate data {}", handler.getConfiguration());
60
61             List<SolarEdgeCommand> commands = new ArrayList<>();
62
63             if (handler.getConfiguration().isUsePrivateApi()) {
64                 commands.add(new AggregateDataUpdatePrivateApi(handler, AggregatePeriod.DAY));
65                 commands.add(new AggregateDataUpdatePrivateApi(handler, AggregatePeriod.WEEK));
66                 commands.add(new AggregateDataUpdatePrivateApi(handler, AggregatePeriod.MONTH));
67                 commands.add(new AggregateDataUpdatePrivateApi(handler, AggregatePeriod.YEAR));
68             } else {
69                 commands.add(new AggregateDataUpdatePublicApi(handler, AggregatePeriod.DAY));
70                 commands.add(new AggregateDataUpdatePublicApi(handler, AggregatePeriod.WEEK));
71                 commands.add(new AggregateDataUpdatePublicApi(handler, AggregatePeriod.MONTH));
72                 commands.add(new AggregateDataUpdatePublicApi(handler, AggregatePeriod.YEAR));
73             }
74
75             for (SolarEdgeCommand command : commands) {
76                 handler.getWebInterface().enqueueCommand(command);
77             }
78         }
79     }
80 }