Skip to content

Commit

Permalink
Add ZigBee firmware provider backed by GitHub repository
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Jackson <[email protected]>
  • Loading branch information
cdjackson committed Jun 19, 2022
1 parent d4dbb4d commit 7b3c600
Show file tree
Hide file tree
Showing 12 changed files with 971 additions and 87 deletions.
15 changes: 10 additions & 5 deletions feature/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,16 @@
<artifactId>org.openhab.binding.zigbee.telegesis</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.binding.zigbee.xbee</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.binding.zigbee.xbee</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.binding.zigbee.firmware</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.binding.zigbee.serial</artifactId>
Expand Down
1 change: 1 addition & 0 deletions org.openhab.binding.zigbee.firmware/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
14 changes: 14 additions & 0 deletions org.openhab.binding.zigbee.firmware/NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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/org.openhab.binding.zigbee

29 changes: 29 additions & 0 deletions org.openhab.binding.zigbee.firmware/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.zigbee.reactor</artifactId>
<version>3.3.0-SNAPSHOT</version>
</parent>

<artifactId>org.openhab.binding.zigbee.firmware</artifactId>

<name>openHAB Add-ons :: Bundles :: ZigBee Firmware Provider</name>

<dependencies>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.binding.zigbee</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.zsmartsystems.zigbee</groupId>
<artifactId>com.zsmartsystems.zigbee</artifactId>
<version>${zsmartsystems.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/**
* 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.zigbee.firmware;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.zigbee.firmware.internal.DirectoryFileEntry;
import org.openhab.binding.zigbee.firmware.internal.GithubLibraryReader;
import org.openhab.binding.zigbee.handler.ZigBeeFirmwareVersion;
import org.openhab.binding.zigbee.handler.ZigBeeThingHandler;
import org.openhab.core.OpenHAB;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.binding.firmware.Firmware;
import org.openhab.core.thing.binding.firmware.FirmwareBuilder;
import org.openhab.core.thing.firmware.FirmwareProvider;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The firmware provider using a github based repository
*
* @author Chris Jackson
*/
@Component(service = FirmwareProvider.class, immediate = true, configurationPid = "org.openhab.binding.zigbee.firmware")
public class ZigBeeFirmwareProvider implements FirmwareProvider {
private Logger logger = LoggerFactory.getLogger(ZigBeeFirmwareProvider.class);

private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

private GithubLibraryReader directoryReader;

@Activate
protected void activate() {
logger.debug("ZigBee Firmware Provider: Activated");
String folder = OpenHAB.getUserDataFolder() + File.separator + "firmware" + File.separator;
directoryReader = new GithubLibraryReader(folder);
try {
directoryReader.create("https://raw.githubusercontent.com/Koenkk/zigbee-OTA/master");
directoryReader.updateRemoteDirectory();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Deactivate
protected void deactivate() {
logger.debug("ZigBee Firmware Provider: Deactivated");
}

@Override
public @Nullable Firmware getFirmware(@NonNull Thing thing, @NonNull String version) {
return getFirmware(thing, version, null);
}

@Override
public @Nullable Firmware getFirmware(@NonNull Thing thing, @NonNull String version, @Nullable Locale locale) {
ZigBeeFirmwareVersion requestedVersion = getRequestedVersionFromThing(thing);
if (requestedVersion == null) {
return null;
}

List<DirectoryFileEntry> directory = directoryReader.getDirectory();
for (DirectoryFileEntry firmware : directory) {
if (firmware.getFirmwareVersion().equals(requestedVersion)) {
return getZigBeeFirmware(thing.getThingTypeUID(), firmware);
}
}

logger.debug("Unable to find firmware version {}", version);
return null;
}

@Override
public @Nullable Set<@NonNull Firmware> getFirmwares(@NonNull Thing thing) {
return getFirmwares(thing, null);
}

@Override
public @Nullable Set<@NonNull Firmware> getFirmwares(@NonNull Thing thing, @Nullable Locale locale) {
final Set<Firmware> firmwareSet = new HashSet<>();

ZigBeeFirmwareVersion requestedVersion = getRequestedVersionFromThing(thing);
if (requestedVersion == null) {
return firmwareSet;
}

for (DirectoryFileEntry firmware : directoryReader.getDirectory()) {
if (firmware.getFirmwareVersion().equals(requestedVersion)) {
firmwareSet.add(getZigBeeFirmware(thing.getThingTypeUID(), firmware));
}
}
return firmwareSet;
}

private ZigBeeFirmwareVersion getRequestedVersionFromThing(@NonNull Thing thing) {
// We only deal in ZigBee devices here
if (!(thing.getHandler() instanceof ZigBeeThingHandler)) {
return null;
}
ZigBeeThingHandler zigbeeHandler = (ZigBeeThingHandler) thing.getHandler();
if (zigbeeHandler == null) {
return null;
}
return zigbeeHandler.getRequestedFirmwareVersion();
}

private Firmware getZigBeeFirmware(@NonNull ThingTypeUID thingTypeUID, DirectoryFileEntry directoryEntry) {
FirmwareBuilder builder = FirmwareBuilder.create(thingTypeUID, directoryEntry.getVersion().toString());

if (!directoryEntry.getModel().isEmpty()) {
builder.withModel(directoryEntry.getModel());
}
if (!directoryEntry.getVendor().isEmpty()) {
builder.withVendor(directoryEntry.getVendor());
}
if (!directoryEntry.getDescription().isEmpty()) {
builder.withDescription(directoryEntry.getDescription());
}
if (!directoryEntry.getMd5().isEmpty()) {
builder.withMd5Hash(directoryEntry.getMd5());
}
if (!directoryEntry.getPrerequisiteVersion().isEmpty()) {
builder.withPrerequisiteVersion(directoryEntry.getPrerequisiteVersion());
}

// BufferedInputStream inputStream = new BufferedInputStream(new URL("").openStream());

// builder.withInputStream(inputStream);

return builder.build();
}

}
Loading

0 comments on commit 7b3c600

Please sign in to comment.