Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[updateopenhab] [experimental] initial contribution #10622

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@
/bundles/org.openhab.binding.unifi/ @mgbowman
/bundles/org.openhab.binding.unifiedremote/ @GiviMAD
/bundles/org.openhab.binding.upb/ @marcusb
/bundles/org.openhab.binding.updateopenhab/ @andrewfg
/bundles/org.openhab.binding.upnpcontrol/ @mherwege
/bundles/org.openhab.binding.urtsi/ @OLibutzki
/bundles/org.openhab.binding.valloxmv/ @bjoernbrings
Expand Down
5 changes: 5 additions & 0 deletions bom/openhab-addons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,11 @@
<artifactId>org.openhab.binding.upb</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.binding.updateopenhab</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.binding.upnpcontrol</artifactId>
Expand Down
13 changes: 13 additions & 0 deletions bundles/org.openhab.binding.updateopenhab/NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
This content is produced and maintained by the openHAB project.

* Project home: https://www.openhab.org

== Declared Project Licenses

This program and the accompanying materials are made available under the terms
of the Eclipse Public License 2.0 which is available at
https://www.eclipse.org/legal/epl-2.0/.

== Source Code

https://github.com/openhab/openhab-addons
32 changes: 32 additions & 0 deletions bundles/org.openhab.binding.updateopenhab/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# UpdateOpenHab Binding

This binding supports automated updating of OpenHAB.

## Supported Things

This binding supports one type of Thing - namely `updater`.

## Thing Configuration for `updater`

The `updater` thing requires one configuration parameter - namely `targetVersion`.
This defines which version/type of OpenHAB it should install during updates.
There are three possible values..

- `STABLE` the updater installs the latest stable release of OpenHAB. This is the default.
- `MILESTONE` the updater installs the latest Milestone release of OpenHAB.
- `SNAPSHOT` the updater installs the latest daily SNAPSHOT release of OpenHAB. This is NOT recommended.

## Channels for `updater`

| Channel | Type | Description |
|-----------------|--------|-------------------------------------------------------------|
| actualVersion | String | The version of the running OpenHab instance. |
| latestVersion | String | The latest released version of OpenHab. |
| updateAvailable | Switch | State is ON if an OpenHab update is available. |
| updateCommand | Switch | Switch ON to start updating OpenHab to the 'latestVersion'. |

## Full Example

```
TBD
```
31 changes: 31 additions & 0 deletions bundles/org.openhab.binding.updateopenhab/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.addons.reactor.bundles</artifactId>
<version>3.1.0-SNAPSHOT</version>
</parent>

<artifactId>org.openhab.binding.updateopenhab</artifactId>

<name>openHAB Add-ons :: Bundles :: Update OpenHab Binding</name>
<dependencies>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jzlib</artifactId>
<version>1.1.3</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<features name="org.openhab.binding.updateopenhab-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.4.0">
<repository>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${ohc.version}/xml/features
</repository>

<feature name="openhab-binding-updateopenhab" description="updateOpenHab Binding" version="${project.version}">
<feature>openhab-runtime-base</feature>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.updateopenhab/${project.version}
</bundle>
</feature>
</features>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.updateopenhab.binding;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.thing.ThingTypeUID;

/**
* The {@link BindingConstants} class defines common constants, which are
* used across the whole binding.
*
* @author Andrew Fiddian-Green - Initial contribution
*/
@NonNullByDefault
public class BindingConstants {

private static final String BINDING_ID = "updateopenhab";

// List of all Thing Type UIDs
public static final ThingTypeUID THING_TYPE_OH_UPDATER = new ThingTypeUID(BINDING_ID, "updater");

// List of all Channel ids
public static final String CHANNEL_UPDATE_COMMAND = "updateCommand";
public static final String CHANNEL_ACTUAL_OH_VERSION = "actualVersion";
public static final String CHANNEL_LATEST_OH_VERSION = "latestVersion";
public static final String CHANNEL_UPDATE_AVAILABLE = "updateAvailable";

// List of all property names
public static final String PROPERTY_OPERATING_SYSTEM = "operatingSystem";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.updateopenhab.binding;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The {@link Configuration} class contains fields mapping thing configuration parameters.
*
* @author Andrew Fiddian-Green - Initial contribution
*/
@NonNullByDefault
public class Configuration {

public String targetVersion = "STABLE";
public String password = "";
public Integer sleepTime = 20;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.updateopenhab.binding;

import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.updateopenhab.updaters.BaseUpdater;
import org.openhab.binding.updateopenhab.updaters.OperatingSystem;
import org.openhab.binding.updateopenhab.updaters.TargetVersionType;
import org.openhab.binding.updateopenhab.updaters.UpdaterFactory;
import org.openhab.binding.updateopenhab.updaters.UpdaterStates;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.types.Command;
import org.openhab.core.types.RefreshType;
import org.openhab.core.types.UnDefType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The {@link UpdaterHandler} is responsible for handling commands, which are sent to one of the channels.
*
* @author Andrew Fiddian-Green - Initial contribution
*/
@NonNullByDefault
public class UpdaterHandler extends BaseThingHandler {

protected final Logger logger = LoggerFactory.getLogger(UpdaterHandler.class);

private TargetVersionType targetVersion;
private @Nullable BaseUpdater updater;
private @Nullable ScheduledFuture<?> refreshTask;

private boolean initialized;

/**
* Constructor
*/
public UpdaterHandler(Thing thing) {
super(thing);
targetVersion = TargetVersionType.STABLE;
}

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
if (command instanceof RefreshType) {
updateChanels();
return;
}
if (BindingConstants.CHANNEL_UPDATE_COMMAND.equals(channelUID.getId()) && (OnOffType.ON.equals(command))) {
scheduler.schedule(() -> {
updateState(channelUID, OnOffType.OFF);
}, 1, TimeUnit.SECONDS);
if (updater != null) {
scheduler.submit(updater);
}
}
}

@Override
public void initialize() {
BaseUpdater updater = this.updater = UpdaterFactory.newUpdater();
if (updater == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
return;
}

Configuration config = getConfigAs(Configuration.class);
try {
updater.setTargetVersion(config.targetVersion);
} catch (IllegalArgumentException e) {
logger.debug("Bad targetVersion {}.", targetVersion);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
return;
}
try {
updater.setPassword(config.password);
} catch (IllegalArgumentException e) {
logger.debug("Bad password {}.", config.password);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
return;
}
try {
updater.setSleepTime(config.sleepTime.toString());
} catch (IllegalArgumentException e) {
logger.debug("Bad sleepTime {}.", config.sleepTime);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
return;
}

updateProperty(BindingConstants.PROPERTY_OPERATING_SYSTEM,
OperatingSystem.getOperatingSystemVersion().toString());

updateStatus(ThingStatus.ONLINE);
initialized = true;

ScheduledFuture<?> refreshTask = this.refreshTask;
if (refreshTask == null || refreshTask.isCancelled()) {
this.refreshTask = scheduler.scheduleWithFixedDelay(() -> {
updateChanels();
}, 5, 3600, TimeUnit.SECONDS);
}
}

@Override
public void dispose() {
initialized = false;
ScheduledFuture<?> refreshTask = this.refreshTask;
if (refreshTask != null) {
refreshTask.cancel(false);
}
}

private synchronized void updateChanels() {
if (!initialized) {
return;
}

updateState(BindingConstants.CHANNEL_UPDATE_COMMAND, OnOffType.OFF);
updateState(BindingConstants.CHANNEL_ACTUAL_OH_VERSION, UpdaterStates.getActualVersion());

BaseUpdater updater = this.updater;
if (updater != null) {
updateState(BindingConstants.CHANNEL_LATEST_OH_VERSION, UpdaterStates.getRemoteVersion(updater));
updateState(BindingConstants.CHANNEL_UPDATE_AVAILABLE, UpdaterStates.getRemoteVersionHigher(updater));
} else {
updateState(BindingConstants.CHANNEL_LATEST_OH_VERSION, UnDefType.UNDEF);
updateState(BindingConstants.CHANNEL_UPDATE_AVAILABLE, UnDefType.UNDEF);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.updateopenhab.binding;

import static org.openhab.binding.updateopenhab.binding.BindingConstants.THING_TYPE_OH_UPDATER;

import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.binding.BaseThingHandlerFactory;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerFactory;
import org.osgi.service.component.annotations.Component;

/**
* The {@link UpdaterHandlerFactory} is responsible for creating things and thing
* handlers.
*
* @author Andrew Fiddian-Green - Initial contribution
*/
@NonNullByDefault
@Component(configurationPid = "binding.updateopenhab", service = ThingHandlerFactory.class)
public class UpdaterHandlerFactory extends BaseThingHandlerFactory {

private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_OH_UPDATER);

@Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
}

@Override
protected @Nullable ThingHandler createHandler(Thing thing) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (THING_TYPE_OH_UPDATER.equals(thingTypeUID)) {
return new UpdaterHandler(thing);
}
return null;
}
}
Loading