Skip to content

Commit

Permalink
[CoronaStats] Initial contribution (openhab#7358)
Browse files Browse the repository at this point in the history
* [CoronaStats] Initial contribution bridge is running

Signed-off-by: Johannes DerOetzi Ott <info@johannes-ott.net>
  • Loading branch information
DerOetzi authored and andrewfg committed Aug 31, 2020
1 parent 0f66631 commit 31a3e8b
Show file tree
Hide file tree
Showing 25 changed files with 1,210 additions and 0 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
/bundles/org.openhab.binding.chromecast/ @kaikreuzer
/bundles/org.openhab.binding.cm11a/ @BobRak
/bundles/org.openhab.binding.coolmasternet/ @projectgus
/bundles/org.openhab.binding.coronastats/ @DerOetzi
/bundles/org.openhab.binding.daikin/ @caffineehacker
/bundles/org.openhab.binding.darksky/ @cweitkamp
/bundles/org.openhab.binding.deconz/ @davidgraeff
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 @@ -144,6 +144,11 @@
<artifactId>org.openhab.binding.coolmasternet</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.binding.coronastats</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.binding.daikin</artifactId>
Expand Down
49 changes: 49 additions & 0 deletions bundles/org.openhab.binding.coronastats/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions bundles/org.openhab.binding.coronastats/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.openhab.binding.coronastats</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
13 changes: 13 additions & 0 deletions bundles/org.openhab.binding.coronastats/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
127 changes: 127 additions & 0 deletions bundles/org.openhab.binding.coronastats/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# CoronaStats Binding

This binding provides the statistic about cases of COVID-19 from the website https://corona-stats.online/.


## Supported Things

This binding supports a `world` thing, which polls the dataset in an adjustable interval as a bridge and provides the statistics for the whole world.
The `country` thing, representing the statistics for a specified country.

## Discovery

This binding adds a default `world` thing to the Inbox.
This can be used as bridge for country things you may add manually.

## Thing Configuration

### World

| Parameter | Default | Required | Description |
| --------- | :-----: | :------: | ---------------------------------------------------------------------------------------- |
| `refresh` | 30 | no | Define the interval for polling the data from website in minutes. Minimum is 15 minutes. |

### Country

| Parameter | Default | Required | Description |
| ------------- | :-----: | :------: | ------------------------------------------------- |
| `countryCode` | - | yes | 2-letter code for the country you want to display |

For the correct 2-letter country code have a look at the website https://corona-stats.online/

## Channels

### World and Country

| channels | type | description |
| -------------- | -------------------- | ---------------------------------------------- |
| `cases` | Number:Dimensionless | Total cases |
| `today_cases` | Number:Dimensionless | Increase of total cases today |
| `deaths` | Number:Dimensionless | Deaths |
| `today_deaths` | Number:Dimensionless | Increase of deaths |
| `recovered` | Number:Dimensionless | Recovered cases |
| `active` | Number:Dimensionless | Active cases |
| `critical` | Number:Dimensionless | Critical cases |
| `tests` | Number:Dimensionless | Count of reported tests (`country` thing only) |
| `updated` | Number:Dimensionless | Data last update time (`country` thing only) |

## Full Example

### Things

```
Bridge coronastats:world:stats "Corona Stats World" @ "Corona" [refresh=15] {
Thing country usa "Corona Stats USA" @ "Corona" [countryCode="US"]
Thing country germany "Corona Stats Germany" @ "Corona" [countryCode="DE"]
Thing country austria "Corona Stats Austria" @ "Corona" [countryCode="AT"]
Thing country italy "Corona Stats Italy" @ "Corona" [countryCode="IT"]
Thing country spain "Corona Stats Spain" @ "Corona" [countryCode="ES"]
Thing country uk "Corona Stats United Kingdom" @ "Corona" [countryCode="GB"]
}
```

### Items

```
Number:Dimensionless coronaCasesWorld "Total Cases World [%,d]"
{channel="coronastats:world:stats:cases"}
Number:Dimensionless coronaDeathsWorld "Deaths World [%,d]"
{channel="coronastats:world:stats:deaths"}
Number:Dimensionless coronaRecoveredWorld "Recovered Cases World [%,d]"
{channel="coronastats:world:stats:recovered"}
Number:Dimensionless coronaActiveWorld "Active Cases World [%,d]"
{channel="coronastats:world:stats:active"}
Number:Dimensionless coronaCriticalWorld "Critical Cases World [%,d]"
{channel="coronastats:world:stats:critical"}
Number:Dimensionless coronaCasesUSA "Total Cases USA [%,d]"
{channel="coronastats:country:stats:usa:cases"}
Number:Dimensionless coronaDeathsUSA "Deaths USA [%,d]"
{channel="coronastats:country:stats:usa:deaths"}
Number:Dimensionless coronaRecoveredUSA "Recovered Cases USA [%,d]"
{channel="coronastats:country:stats:usa:recovered"}
Number:Dimensionless coronaActiveUSA "Active Cases USA [%,d]"
{channel="coronastats:country:stats:usa:active"}
Number:Dimensionless coronaCriticalUSA "Critical Cases USA [%,d]"
{channel="coronastats:country:stats:usa:critical"}
Number:Dimensionless coronaTestsUSA "Tests USA [%d]"
{channel="coronastats:country:stats:usa:cases"}
DateTime coronaUpdatedUSA "Updated USA [%1$tA, %1$td.%1$tm.%1$tY %1$tH:%1$tM]"
{channel="coronastats:country:stats:usa:updated"}
```

### Sitemap

```
Text label="Corona" {
Frame label="World" {
Text item=coronaCasesWorld
Text item=coronaActiveWorld
Text item=coronaRecoveredWorld
Text item=coronaDeathsWorld
Text item=coronaCriticalWorld
Text item=coronaTestUSA
}
Frame label="USA" {
Text item=coronaCasesUSA
Text item=coronaActiveUSA
Text item=coronaRecoveredUSA
Text item=coronaDeathsUSA
Text item=coronaCriticalUSA
Text item=coronaTestsUSA
Text item=coronaUpdatedUSA
}
}
```
16 changes: 16 additions & 0 deletions bundles/org.openhab.binding.coronastats/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?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>2.5.4-SNAPSHOT</version>
</parent>

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

<name>openHAB Add-ons :: Bundles :: CoronaStats Binding</name>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<features name="org.openhab.binding.coronastats-${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-coronastats" description="CoronaStats Binding" version="${project.version}">
<feature>openhab-runtime-base</feature>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.coronastats/${project.version}</bundle>
</feature>
</features>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright (c) 2010-2020 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.coronastats.internal;

import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

/**
* The {@link CoronaStatsBindingConstants} class defines common constants, which are
* used across the whole binding.
*
* @author Johannes Ott - Initial contribution
*/
@NonNullByDefault
public class CoronaStatsBindingConstants {
public static final String BINDING_ID = "coronastats";

// World
public static final ThingTypeUID THING_TYPE_WORLD = new ThingTypeUID(BINDING_ID, "world");
public static final String STATS = "stats";
public static final String WORLD_LABEL = "Corona Statistics (World)";

// Country
public static final ThingTypeUID THING_TYPE_COUNTRY = new ThingTypeUID(BINDING_ID, "country");

// @formatter:off
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS =
Collections.unmodifiableSet(Stream
.of(THING_TYPE_WORLD, THING_TYPE_COUNTRY)
.collect(Collectors.toSet())
);
// @formatter:on

// Properties country
public static final String PROPERTY_COUNTRY = "country";

// Channels world/country
public static final String CHANNEL_CASES = "cases";
public static final String CHANNEL_NEW_CASES = "today_cases";
public static final String CHANNEL_DEATHS = "deaths";
public static final String CHANNEL_NEW_DEATHS = "today_deaths";
public static final String CHANNEL_RECOVERED = "recovered";
public static final String CHANNEL_ACTIVE = "active";
public static final String CHANNEL_CRITICAL = "critical";
public static final String CHANNEL_TESTS = "tests";
public static final String CHANNEL_UPDATED = "updated";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright (c) 2010-2020 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.coronastats.internal;

import static org.openhab.binding.coronastats.internal.CoronaStatsBindingConstants.*;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory;
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory;
import org.eclipse.smarthome.io.net.http.HttpClientFactory;
import org.openhab.binding.coronastats.internal.handler.CoronaStatsWorldHandler;
import org.openhab.binding.coronastats.internal.handler.CoronaStatsCountryHandler;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
* The {@link CoronaStatsHandlerFactory} is responsible for creating things and thing
* handlers.
*
* @author Johannes Ott - Initial contribution
*/
@NonNullByDefault
@Component(configurationPid = "binding.coronastats", service = ThingHandlerFactory.class)
public class CoronaStatsHandlerFactory extends BaseThingHandlerFactory {
private final HttpClient httpClient;

@Activate
public CoronaStatsHandlerFactory(final @Reference HttpClientFactory httpClientFactory) {
this.httpClient = httpClientFactory.getCommonHttpClient();
}

@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_WORLD.equals(thingTypeUID)) {
return new CoronaStatsWorldHandler((Bridge) thing, httpClient);
} else if (THING_TYPE_COUNTRY.equals(thingTypeUID)) {
return new CoronaStatsCountryHandler(thing);
}

return null;
}
}
Loading

0 comments on commit 31a3e8b

Please sign in to comment.