]> git.basschouten.com Git - openhab-addons.git/blob
78336bde5d7ef9509a66014eb11a6209bb26fe61
[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.astro.internal.job;
14
15 import static org.openhab.binding.astro.internal.AstroBindingConstants.CHANNEL_ID_SUN_PHASE_NAME;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.astro.internal.AstroHandlerFactory;
19 import org.openhab.binding.astro.internal.handler.AstroThingHandler;
20 import org.openhab.binding.astro.internal.model.Planet;
21 import org.openhab.binding.astro.internal.model.Sun;
22 import org.openhab.binding.astro.internal.model.SunPhaseName;
23 import org.openhab.core.thing.Channel;
24
25 /**
26  * Scheduled job for Sun phase change
27  *
28  * @author Gerhard Riegler - Initial contribution
29  * @author Amit Kumar Mondal - Implementation to be compliant with ESH Scheduler
30  */
31 @NonNullByDefault
32 public final class SunPhaseJob extends AbstractJob {
33
34     private final SunPhaseName sunPhaseName;
35
36     /**
37      * Constructor
38      *
39      * @param thingUID thing UID
40      * @param sunPhaseName {@link SunPhaseName} name
41      * @throws IllegalArgumentException
42      *             if any of the arguments is {@code null}
43      */
44     public SunPhaseJob(String thingUID, SunPhaseName sunPhaseName) {
45         super(thingUID);
46         this.sunPhaseName = sunPhaseName;
47     }
48
49     @Override
50     public void run() {
51         AstroThingHandler astroHandler = AstroHandlerFactory.getHandler(getThingUID());
52         if (astroHandler != null) {
53             Channel phaseNameChannel = astroHandler.getThing().getChannel(CHANNEL_ID_SUN_PHASE_NAME);
54             if (phaseNameChannel != null) {
55                 Planet planet = astroHandler.getPlanet();
56                 if (planet != null && planet instanceof Sun) {
57                     final Sun typedSun = (Sun) planet;
58                     typedSun.getPhase().setName(sunPhaseName);
59                     astroHandler.publishChannelIfLinked(phaseNameChannel.getUID());
60                 }
61             } else {
62                 LOGGER.trace("{}", "Phase Name Channel is null");
63             }
64         } else {
65             LOGGER.trace("AstroThingHandler is null");
66         }
67     }
68
69     @Override
70     public String toString() {
71         return "Sun phase job " + getThingUID() + "/" + sunPhaseName;
72     }
73 }