mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-16 18:24:36 +00:00
Populate git repository
First Update
This commit is contained in:
17
.gitattributes
vendored
Normal file
17
.gitattributes
vendored
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# Auto detect text files and perform LF normalization
|
||||||
|
* text=auto
|
||||||
|
|
||||||
|
# Custom for Visual Studio
|
||||||
|
*.cs diff=csharp
|
||||||
|
|
||||||
|
# Standard to msysgit
|
||||||
|
*.doc diff=astextplain
|
||||||
|
*.DOC diff=astextplain
|
||||||
|
*.docx diff=astextplain
|
||||||
|
*.DOCX diff=astextplain
|
||||||
|
*.dot diff=astextplain
|
||||||
|
*.DOT diff=astextplain
|
||||||
|
*.pdf diff=astextplain
|
||||||
|
*.PDF diff=astextplain
|
||||||
|
*.rtf diff=astextplain
|
||||||
|
*.RTF diff=astextplain
|
||||||
11
.gitignore
vendored
Normal file
11
.gitignore
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
target/
|
||||||
|
pom.xml.tag
|
||||||
|
pom.xml.releaseBackup
|
||||||
|
pom.xml.versionsBackup
|
||||||
|
pom.xml.next
|
||||||
|
release.properties
|
||||||
|
dependency-reduced-pom.xml
|
||||||
|
buildNumber.properties
|
||||||
|
*.iml
|
||||||
|
data
|
||||||
|
.idea
|
||||||
5
.settings/org.eclipse.jdt.core.prefs
Normal file
5
.settings/org.eclipse.jdt.core.prefs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||||
|
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||||
|
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||||
|
org.eclipse.jdt.core.compiler.source=1.8
|
||||||
4
.settings/org.eclipse.m2e.core.prefs
Normal file
4
.settings/org.eclipse.m2e.core.prefs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
activeProfiles=
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
resolveWorkspaceProjects=true
|
||||||
|
version=1
|
||||||
202
LICENSE
Normal file
202
LICENSE
Normal file
@@ -0,0 +1,202 @@
|
|||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
|
To apply the Apache License to your work, attach the following
|
||||||
|
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||||
|
replaced with your own identifying information. (Don't include
|
||||||
|
the brackets!) The text should be enclosed in the appropriate
|
||||||
|
comment syntax for the file format. We also recommend that a
|
||||||
|
file or class name and description of purpose be included on the
|
||||||
|
same "printed page" as the copyright notice for easier
|
||||||
|
identification within third-party archives.
|
||||||
|
|
||||||
|
Copyright {yyyy} {name of copyright owner}
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
|
||||||
38
README.md
Normal file
38
README.md
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
# amazon-echo-ha-bridge-compact
|
||||||
|
emulates philips hue api to other home automation gateways. The Amazon echo now supports wemo and philip hue... great news if you own any of those devices!
|
||||||
|
My house is pretty heavily invested in the z-wave using the Vera as the gateway and thought it would be nice bridge the Amazon Echo to it.
|
||||||
|
|
||||||
|
Build
|
||||||
|
-----
|
||||||
|
The server defaults to running on port 8080. If you're already running a server (like openHAB) on 8080, edit ```server.port``` in ```src/main/resources/application.properties``` to your desired port before building the jar. Alternately you can pass in a command line argument to override ```server.port```.
|
||||||
|
|
||||||
|
To customize and build it yourself, build a new jar with maven:
|
||||||
|
```
|
||||||
|
mvn install
|
||||||
|
```
|
||||||
|
Then locate the jar and start the server with:
|
||||||
|
```
|
||||||
|
java -jar -Dupnp.config.address=192.168.1.Z target/amazon-echo-bridge-compact0.X.Y.jar
|
||||||
|
```
|
||||||
|
replace the --upnp.config.address value with the server ipv4 address.
|
||||||
|
|
||||||
|
Then configure by going to the /configurator.html url
|
||||||
|
```
|
||||||
|
http://192.168.1.240:8080
|
||||||
|
```
|
||||||
|
or Register a device, via REST by binding some sort of on/off (vera style) url
|
||||||
|
```
|
||||||
|
POST http://host:8080/api/devices
|
||||||
|
{
|
||||||
|
"name" : "bedroom light",
|
||||||
|
"deviceType" : "switch",
|
||||||
|
"onUrl" : "http://192.168.1.201:3480/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1&DeviceNum=41",
|
||||||
|
"offUrl" : "http://192.168.1.201:3480/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0&DeviceNum=41"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
After this Tell Alexa: "Alexa, discover my devices"
|
||||||
|
|
||||||
|
Then you can say "Alexa, Turn on the office light" or whatever name you have given your configured devices.
|
||||||
|
|
||||||
|
To view or remove devices that Alexa knows about, you can use the mobile app Menu / Settings / Connected Home
|
||||||
86
pom.xml
Normal file
86
pom.xml
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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>
|
||||||
|
|
||||||
|
<groupId>com.bwssytems.HABridge</groupId>
|
||||||
|
<artifactId>amazon-echo-bridge-compact</artifactId>
|
||||||
|
<version>0.1.0</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>Amazon Echo Bridge Compact</name>
|
||||||
|
<description>Emulates a Philips Hue bridge to allow the Amazon Echo to hook up to other HA using lightweight frameworks</description>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.sparkjava</groupId>
|
||||||
|
<artifactId>spark-core</artifactId>
|
||||||
|
<version>2.2</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpclient</artifactId>
|
||||||
|
<version>4.3.6</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-simple</artifactId>
|
||||||
|
<version>1.7.5</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>2.2.4</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>2.6.0</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<version>2.3</version>
|
||||||
|
<configuration>
|
||||||
|
<createDependencyReducedPom>true</createDependencyReducedPom>
|
||||||
|
<filters>
|
||||||
|
<filter>
|
||||||
|
<artifact>*:*</artifact>
|
||||||
|
<excludes>
|
||||||
|
<exclude>META-INF/*.SF</exclude>
|
||||||
|
<exclude>META-INF/*.DSA</exclude>
|
||||||
|
<exclude>META-INF/*.RSA</exclude>
|
||||||
|
</excludes>
|
||||||
|
</filter>
|
||||||
|
</filters>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<transformers>
|
||||||
|
<transformer
|
||||||
|
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||||
|
<mainClass>com.bwssytems.HABridge.AmazonEchoBridge</mainClass>
|
||||||
|
</transformer>
|
||||||
|
</transformers>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
59
src/main/java/com/bwssytems/HABridge/AmazonEchoBridge.java
Normal file
59
src/main/java/com/bwssytems/HABridge/AmazonEchoBridge.java
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
package com.bwssytems.HABridge;
|
||||||
|
|
||||||
|
import static spark.Spark.*;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.bwssytems.HABridge.devicemanagmeent.*;
|
||||||
|
import com.bwssytems.HABridge.hue.HueMulator;
|
||||||
|
import com.bwssytems.HABridge.upnp.UpnpListener;
|
||||||
|
import com.bwssytems.HABridge.upnp.UpnpSettingsResource;
|
||||||
|
|
||||||
|
public class AmazonEchoBridge {
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This program is based on the work of armzilla from this github repository:
|
||||||
|
* https://github.com/armzilla/amazon-echo-ha-bridge
|
||||||
|
*
|
||||||
|
* This is the main entry point to start the amazon echo bridge.
|
||||||
|
*
|
||||||
|
* This program is using sparkjava rest server to build all the http calls.
|
||||||
|
* Sparkjava is a microframework that uses Jetty webserver module to host
|
||||||
|
* its' calls. This is a very compact system than using the spring frameworks
|
||||||
|
* that was previously used.
|
||||||
|
*
|
||||||
|
* There is a custom upnp listener that is started to handle discovery.
|
||||||
|
*
|
||||||
|
* This application does not store the lights configuration persistently.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Logger log = LoggerFactory.getLogger(AmazonEchoBridge.class);
|
||||||
|
DeviceResource theResources;
|
||||||
|
HueMulator theHueMulator;
|
||||||
|
UpnpSettingsResource theSettingResponder;
|
||||||
|
UpnpListener theUpnpListener;
|
||||||
|
|
||||||
|
// sparkjava config directive to set ip address for the web server to listen on
|
||||||
|
ipAddress(System.getProperty("upnp.config.address", "0.0.0.0"));
|
||||||
|
// sparkjava config directive to set port for the web server to listen on
|
||||||
|
port(Integer.valueOf(System.getProperty("server.port", "8080")));
|
||||||
|
// sparkjava config directive to set html static file location for Jetty
|
||||||
|
staticFileLocation("/public");
|
||||||
|
log.debug("Starting setup....");
|
||||||
|
// setup the class to handle the resource setup rest api
|
||||||
|
theResources = new DeviceResource();
|
||||||
|
// setup the class to handle the hue emulator rest api
|
||||||
|
theHueMulator = new HueMulator(theResources.getDeviceRepository());
|
||||||
|
// setup the class to handle the upnp response rest api
|
||||||
|
theSettingResponder = new UpnpSettingsResource();
|
||||||
|
// wait for the sparkjava initialization of the rest api classes to be complete
|
||||||
|
awaitInitialization();
|
||||||
|
// start the upnp ssdp discovery listener
|
||||||
|
theUpnpListener = new UpnpListener();
|
||||||
|
log.debug("Done setup, application to run....");
|
||||||
|
theUpnpListener.startListening();
|
||||||
|
}
|
||||||
|
}
|
||||||
17
src/main/java/com/bwssytems/HABridge/JsonTransformer.java
Normal file
17
src/main/java/com/bwssytems/HABridge/JsonTransformer.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package com.bwssytems.HABridge;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import spark.ResponseTransformer;
|
||||||
|
/*
|
||||||
|
* Implementation of a Json renderer through google GSON utility.
|
||||||
|
*/
|
||||||
|
public class JsonTransformer implements ResponseTransformer {
|
||||||
|
|
||||||
|
private Gson gson = new Gson();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String render(Object model) {
|
||||||
|
return gson.toJson(model);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
43
src/main/java/com/bwssytems/HABridge/api/Device.java
Normal file
43
src/main/java/com/bwssytems/HABridge/api/Device.java
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package com.bwssytems.HABridge.api;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by arm on 4/13/15.
|
||||||
|
*/
|
||||||
|
public class Device {
|
||||||
|
private String name;
|
||||||
|
private String deviceType;
|
||||||
|
private String offUrl;
|
||||||
|
private String onUrl;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDeviceType() {
|
||||||
|
return deviceType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceType(String deviceType) {
|
||||||
|
this.deviceType = deviceType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getOffUrl() {
|
||||||
|
return offUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOffUrl(String offUrl) {
|
||||||
|
this.offUrl = offUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getOnUrl() {
|
||||||
|
return onUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOnUrl(String onUrl) {
|
||||||
|
this.onUrl = onUrl;
|
||||||
|
}
|
||||||
|
}
|
||||||
122
src/main/java/com/bwssytems/HABridge/api/hue/DeviceResponse.java
Normal file
122
src/main/java/com/bwssytems/HABridge/api/hue/DeviceResponse.java
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
package com.bwssytems.HABridge.api.hue;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by arm on 4/14/15.
|
||||||
|
*/
|
||||||
|
public class DeviceResponse {
|
||||||
|
private DeviceState state;
|
||||||
|
private String type;
|
||||||
|
private String name;
|
||||||
|
private String modelid;
|
||||||
|
private String manufacturername;
|
||||||
|
private String uniqueid;
|
||||||
|
private String swversion;
|
||||||
|
private Map<String, String> pointsymbol;
|
||||||
|
|
||||||
|
public DeviceState getState() {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setState(DeviceState state) {
|
||||||
|
this.state = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getModelid() {
|
||||||
|
return modelid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setModelid(String modelid) {
|
||||||
|
this.modelid = modelid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getManufacturername() {
|
||||||
|
return manufacturername;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setManufacturername(String manufacturername) {
|
||||||
|
this.manufacturername = manufacturername;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUniqueid() {
|
||||||
|
return uniqueid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUniqueid(String uniqueid) {
|
||||||
|
this.uniqueid = uniqueid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSwversion() {
|
||||||
|
return swversion;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSwversion(String swversion) {
|
||||||
|
this.swversion = swversion;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getPointsymbol() {
|
||||||
|
Map<String, String> dummyValue = new HashMap<>();
|
||||||
|
dummyValue.put("1", "none");
|
||||||
|
dummyValue.put("2", "none");
|
||||||
|
dummyValue.put("3", "none");
|
||||||
|
dummyValue.put("4", "none");
|
||||||
|
dummyValue.put("5", "none");
|
||||||
|
dummyValue.put("6", "none");
|
||||||
|
dummyValue.put("7", "none");
|
||||||
|
dummyValue.put("8", "none");
|
||||||
|
|
||||||
|
return dummyValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPointsymbol(Map<String, String> pointsymbol) {
|
||||||
|
this.pointsymbol = pointsymbol;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DeviceResponse createResponse(String name, String id){
|
||||||
|
DeviceState deviceState = new DeviceState();
|
||||||
|
DeviceResponse response = new DeviceResponse();
|
||||||
|
response.setState(deviceState);
|
||||||
|
deviceState.setOn(false);
|
||||||
|
deviceState.setReachable(true);
|
||||||
|
deviceState.setEffect("none");
|
||||||
|
deviceState.setAlert("none");
|
||||||
|
deviceState.setBri(254);
|
||||||
|
deviceState.setHue(15823);
|
||||||
|
deviceState.setSat(88);
|
||||||
|
deviceState.setCt(313);
|
||||||
|
|
||||||
|
List<Double> xv = new LinkedList<>();
|
||||||
|
xv.add(0.4255);
|
||||||
|
xv.add(0.3998);
|
||||||
|
deviceState.setXy(xv);
|
||||||
|
deviceState.setColormode("ct");
|
||||||
|
response.setName(name);
|
||||||
|
response.setUniqueid(id);
|
||||||
|
response.setManufacturername("Philips");
|
||||||
|
response.setType("Extended color light");
|
||||||
|
response.setModelid("LCT001");
|
||||||
|
response.setSwversion("65003148");
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
||||||
107
src/main/java/com/bwssytems/HABridge/api/hue/DeviceState.java
Normal file
107
src/main/java/com/bwssytems/HABridge/api/hue/DeviceState.java
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
package com.bwssytems.HABridge.api.hue;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by arm on 4/14/15.
|
||||||
|
*/
|
||||||
|
public class DeviceState {
|
||||||
|
private boolean on;
|
||||||
|
private int bri = 255;
|
||||||
|
private int hue;
|
||||||
|
private int sat;
|
||||||
|
private String effect;
|
||||||
|
private int ct;
|
||||||
|
private String alert;
|
||||||
|
private String colormode;
|
||||||
|
private boolean reachable;
|
||||||
|
private List<Double> xy;
|
||||||
|
|
||||||
|
public boolean isOn() {
|
||||||
|
return on;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOn(boolean on) {
|
||||||
|
this.on = on;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getBri() {
|
||||||
|
return bri;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBri(int bri) {
|
||||||
|
this.bri = bri;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getHue() {
|
||||||
|
return hue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHue(int hue) {
|
||||||
|
this.hue = hue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSat() {
|
||||||
|
return sat;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSat(int sat) {
|
||||||
|
this.sat = sat;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEffect() {
|
||||||
|
return effect;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEffect(String effect) {
|
||||||
|
this.effect = effect;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCt() {
|
||||||
|
return ct;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCt(int ct) {
|
||||||
|
this.ct = ct;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAlert() {
|
||||||
|
return alert;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAlert(String alert) {
|
||||||
|
this.alert = alert;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getColormode() {
|
||||||
|
return colormode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setColormode(String colormode) {
|
||||||
|
this.colormode = colormode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isReachable() {
|
||||||
|
return reachable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setReachable(boolean reachable) {
|
||||||
|
this.reachable = reachable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Double> getXy() {
|
||||||
|
return xy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setXy(List<Double> xy) {
|
||||||
|
this.xy = xy;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "DeviceState{" +
|
||||||
|
"on=" + on +
|
||||||
|
", bri=" + bri +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.bwssytems.HABridge.api.hue;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.bwssytems.HABridge.api.hue.DeviceResponse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by arm on 4/14/15.
|
||||||
|
*/
|
||||||
|
public class HueApiResponse {
|
||||||
|
private Map<String, DeviceResponse> lights;
|
||||||
|
|
||||||
|
public Map<String, DeviceResponse> getLights() {
|
||||||
|
return lights;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLights(Map<String, DeviceResponse> lights) {
|
||||||
|
this.lights = lights;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package com.bwssytems.HABridge.dao;
|
||||||
|
/*
|
||||||
|
* Object to handle the device configuration
|
||||||
|
*/
|
||||||
|
public class DeviceDescriptor{
|
||||||
|
private String id;
|
||||||
|
private String name;
|
||||||
|
private String deviceType;
|
||||||
|
private String offUrl;
|
||||||
|
private String onUrl;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDeviceType() {
|
||||||
|
return deviceType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceType(String deviceType) {
|
||||||
|
this.deviceType = deviceType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getOffUrl() {
|
||||||
|
return offUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOffUrl(String offUrl) {
|
||||||
|
this.offUrl = offUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getOnUrl() {
|
||||||
|
return onUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOnUrl(String onUrl) {
|
||||||
|
this.onUrl = onUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package com.bwssytems.HABridge.dao;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import com.bwssytems.HABridge.dao.DeviceDescriptor;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
/*
|
||||||
|
* This is an in memory list to manage the configured devices.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class DeviceRepository {
|
||||||
|
Map<String, DeviceDescriptor> devices;
|
||||||
|
final Random random = new Random();
|
||||||
|
|
||||||
|
public DeviceRepository() {
|
||||||
|
super();
|
||||||
|
devices = new HashMap<String, DeviceDescriptor>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<DeviceDescriptor> findAll() {
|
||||||
|
List<DeviceDescriptor> list = new ArrayList<DeviceDescriptor>(devices.values());
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<DeviceDescriptor> findByDeviceType(String aType) {
|
||||||
|
List<DeviceDescriptor> list = new ArrayList<DeviceDescriptor>(devices.values());
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceDescriptor findOne(String id) {
|
||||||
|
return devices.get(id);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save(DeviceDescriptor aDescriptor) {
|
||||||
|
int id = random.nextInt(Integer.MAX_VALUE);
|
||||||
|
aDescriptor.setId(String.valueOf(id));
|
||||||
|
devices.put(String.valueOf(id),aDescriptor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String delete(DeviceDescriptor aDescriptor) {
|
||||||
|
if (aDescriptor != null) {
|
||||||
|
devices.remove(aDescriptor.getId());
|
||||||
|
return "Device with id '" + aDescriptor.getId() + "' deleted";
|
||||||
|
} else {
|
||||||
|
return "Device not found";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
package com.bwssytems.HABridge.devicemanagmeent;
|
||||||
|
|
||||||
|
import com.bwssytems.HABridge.JsonTransformer;
|
||||||
|
import com.bwssytems.HABridge.dao.DeviceDescriptor;
|
||||||
|
import com.bwssytems.HABridge.dao.DeviceRepository;
|
||||||
|
|
||||||
|
import static spark.Spark.get;
|
||||||
|
import static spark.Spark.post;
|
||||||
|
import static spark.Spark.put;
|
||||||
|
import static spark.Spark.delete;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
|
/**
|
||||||
|
spark core server for bridge configuration
|
||||||
|
*/
|
||||||
|
public class DeviceResource {
|
||||||
|
private static final String API_CONTEXT = "/api/devices";
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(DeviceResource.class);
|
||||||
|
|
||||||
|
private DeviceRepository deviceRepository;
|
||||||
|
|
||||||
|
|
||||||
|
public DeviceResource() {
|
||||||
|
super();
|
||||||
|
deviceRepository = new DeviceRepository();
|
||||||
|
setupEndpoints();
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceRepository getDeviceRepository() {
|
||||||
|
return deviceRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupEndpoints() {
|
||||||
|
log.debug("Setting up endpoints");
|
||||||
|
post(API_CONTEXT + "/", "application/json", (request, response) -> {
|
||||||
|
log.debug("Create a Device - request body: " + request.body());
|
||||||
|
DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);
|
||||||
|
DeviceDescriptor deviceEntry = new DeviceDescriptor();
|
||||||
|
deviceEntry.setName(device.getName());
|
||||||
|
log.debug("Create a Device - device json name: " + deviceEntry.getName());
|
||||||
|
deviceEntry.setDeviceType(device.getDeviceType());
|
||||||
|
log.debug("Create a Device - device json type:" + deviceEntry.getDeviceType());
|
||||||
|
deviceEntry.setOnUrl(device.getOnUrl());
|
||||||
|
log.debug("Create a Device - device json on URL:" + deviceEntry.getOnUrl());
|
||||||
|
deviceEntry.setOffUrl(device.getOffUrl());
|
||||||
|
log.debug("Create a Device - device json off URL:" + deviceEntry.getOffUrl());
|
||||||
|
|
||||||
|
deviceRepository.save(deviceEntry);
|
||||||
|
log.debug("Created a Device");
|
||||||
|
|
||||||
|
response.status(201);
|
||||||
|
return deviceEntry;
|
||||||
|
}, new JsonTransformer());
|
||||||
|
|
||||||
|
put (API_CONTEXT + "/:id", "application/json", (request, response) -> {
|
||||||
|
log.debug("Saved a Device");
|
||||||
|
DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);
|
||||||
|
DeviceDescriptor deviceEntry = deviceRepository.findOne(request.params(":id"));
|
||||||
|
if(deviceEntry == null){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
deviceEntry.setName(device.getName());
|
||||||
|
deviceEntry.setDeviceType(device.getDeviceType());
|
||||||
|
deviceEntry.setOnUrl(device.getOnUrl());
|
||||||
|
deviceEntry.setOffUrl(device.getOffUrl());
|
||||||
|
|
||||||
|
deviceRepository.save(deviceEntry);
|
||||||
|
return deviceEntry;
|
||||||
|
}, new JsonTransformer());
|
||||||
|
|
||||||
|
get (API_CONTEXT + "/", "application/json", (request, response) -> {
|
||||||
|
List<DeviceDescriptor> deviceList = deviceRepository.findAll();
|
||||||
|
log.debug("Get all devices");
|
||||||
|
JsonTransformer aRenderer = new JsonTransformer();
|
||||||
|
String theStream = aRenderer.render(deviceList);
|
||||||
|
log.debug("The Device List: " + theStream);
|
||||||
|
return deviceList;
|
||||||
|
}, new JsonTransformer());
|
||||||
|
|
||||||
|
get (API_CONTEXT + "/:id", "application/json", (request, response) -> {
|
||||||
|
log.debug("Get a device");
|
||||||
|
DeviceDescriptor descriptor = deviceRepository.findOne(request.params(":id"));
|
||||||
|
if(descriptor == null){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return descriptor;
|
||||||
|
}, new JsonTransformer());
|
||||||
|
|
||||||
|
delete (API_CONTEXT + "/:id", "application/json", (request, response) -> {
|
||||||
|
log.debug("Delete a device");
|
||||||
|
DeviceDescriptor deleted = deviceRepository.findOne(request.params(":id"));
|
||||||
|
if(deleted == null){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
deviceRepository.delete(deleted);
|
||||||
|
return null;
|
||||||
|
}, new JsonTransformer());
|
||||||
|
}
|
||||||
|
}
|
||||||
197
src/main/java/com/bwssytems/HABridge/hue/HueMulator.java
Normal file
197
src/main/java/com/bwssytems/HABridge/hue/HueMulator.java
Normal file
@@ -0,0 +1,197 @@
|
|||||||
|
package com.bwssytems.HABridge.hue;
|
||||||
|
|
||||||
|
import com.bwssytems.HABridge.api.hue.DeviceResponse;
|
||||||
|
import com.bwssytems.HABridge.api.hue.DeviceState;
|
||||||
|
import com.bwssytems.HABridge.api.hue.HueApiResponse;
|
||||||
|
import com.bwssytems.HABridge.dao.*;
|
||||||
|
import com.bwssytems.HABridge.JsonTransformer;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
import static spark.Spark.get;
|
||||||
|
import static spark.Spark.post;
|
||||||
|
import static spark.Spark.put;
|
||||||
|
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.client.HttpClient;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.impl.client.HttpClients;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Based on Armzilla's HueMulator - a Philips Hue emulator using sparkjava rest server
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class HueMulator {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(HueMulator.class);
|
||||||
|
private static final String INTENSITY_PERCENT = "${intensity.percent}";
|
||||||
|
private static final String INTENSITY_BYTE = "${intensity.byte}";
|
||||||
|
private static final String HUE_CONTEXT = "/api";
|
||||||
|
|
||||||
|
private DeviceRepository repository;
|
||||||
|
private HttpClient httpClient;
|
||||||
|
private ObjectMapper mapper;
|
||||||
|
|
||||||
|
|
||||||
|
public HueMulator(DeviceRepository aDeviceRepository){
|
||||||
|
httpClient = HttpClients.createMinimal();
|
||||||
|
mapper = new ObjectMapper(); //armzilla: work around Echo incorrect content type and breaking mapping. Map manually
|
||||||
|
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
|
repository = aDeviceRepository;
|
||||||
|
setupEndpoints();
|
||||||
|
}
|
||||||
|
|
||||||
|
// This function sets up the sparkjava rest calls for the hue api
|
||||||
|
private void setupEndpoints() {
|
||||||
|
// http://ip_address:port/api/{userId}/lights returns json objects of all lights configured
|
||||||
|
get(HUE_CONTEXT + "/:userid/lights", "application/json", (request, response) -> {
|
||||||
|
String userId = request.params(":userid");
|
||||||
|
log.info("hue lights list requested: " + userId + " from " + request.ip());
|
||||||
|
List<DeviceDescriptor> deviceList = repository.findByDeviceType("switch");
|
||||||
|
JsonTransformer aRenderer = new JsonTransformer();
|
||||||
|
String theStream = aRenderer.render(deviceList);
|
||||||
|
log.debug("The Device List: " + theStream);
|
||||||
|
Map<String, String> deviceResponseMap = new HashMap<>();
|
||||||
|
for (DeviceDescriptor device : deviceList) {
|
||||||
|
deviceResponseMap.put(device.getId(), device.getName());
|
||||||
|
}
|
||||||
|
response.status(200);
|
||||||
|
return deviceResponseMap;
|
||||||
|
} , new JsonTransformer());
|
||||||
|
|
||||||
|
// http://ip_address:port/api/* returns json object for a test call
|
||||||
|
post(HUE_CONTEXT + "/*", "application/json", (request, response) -> {
|
||||||
|
response.status(200);
|
||||||
|
return "[{\"success\":{\"username\":\"lights\"}}]";
|
||||||
|
} );
|
||||||
|
|
||||||
|
// http://ip_address:port/api/{userId} returns json objects for the list of names of lights
|
||||||
|
get(HUE_CONTEXT + "/:userid", "application/json", (request, response) -> {
|
||||||
|
String userId = request.params(":userid");
|
||||||
|
log.info("hue api root requested: " + userId + " from " + request.ip());
|
||||||
|
List<DeviceDescriptor> descriptorList = repository.findByDeviceType("switch");
|
||||||
|
if (descriptorList == null) {
|
||||||
|
response.status(404);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Map<String, DeviceResponse> deviceList = new HashMap<>();
|
||||||
|
|
||||||
|
descriptorList.forEach(descriptor -> {
|
||||||
|
DeviceResponse deviceResponse = DeviceResponse.createResponse(descriptor.getName(), descriptor.getId());
|
||||||
|
deviceList.put(descriptor.getId(), deviceResponse);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
HueApiResponse apiResponse = new HueApiResponse();
|
||||||
|
apiResponse.setLights(deviceList);
|
||||||
|
|
||||||
|
response.status(200);
|
||||||
|
return apiResponse;
|
||||||
|
}, new JsonTransformer());
|
||||||
|
|
||||||
|
// http://ip_address:port/api/{userId}/lights/{lightId} returns json object for a given light
|
||||||
|
get(HUE_CONTEXT + "/:userid/lights/:id", "application/json", (request, response) -> {
|
||||||
|
String userId = request.params(":userid");
|
||||||
|
String lightId = request.params(":id");
|
||||||
|
log.info("hue light requested: " + lightId + "for user: " + userId + " from " + request.ip());
|
||||||
|
DeviceDescriptor device = repository.findOne(lightId);
|
||||||
|
if (device == null) {
|
||||||
|
response.status(404);
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
log.info("found device named: " + device.getName());
|
||||||
|
}
|
||||||
|
DeviceResponse lightResponse = DeviceResponse.createResponse(device.getName(), device.getId());
|
||||||
|
|
||||||
|
response.status(200);
|
||||||
|
return lightResponse;
|
||||||
|
}, new JsonTransformer());
|
||||||
|
|
||||||
|
// http://ip_address:port/api/{userId}/lights/{lightId}/state uses json object to set the lights state
|
||||||
|
put(HUE_CONTEXT + "/:userid/lights/:id/state", "application/json", (request, response) -> {
|
||||||
|
/**
|
||||||
|
* strangely enough the Echo sends a content type of application/x-www-form-urlencoded even though
|
||||||
|
* it sends a json object
|
||||||
|
*/
|
||||||
|
String userId = request.params(":userid");
|
||||||
|
String lightId = request.params(":id");
|
||||||
|
log.info("hue state change requested: " + userId + " from " + request.ip());
|
||||||
|
log.info("hue stage change body: " + request.body() );
|
||||||
|
|
||||||
|
DeviceState state = null;
|
||||||
|
try {
|
||||||
|
state = mapper.readValue(request.body(), DeviceState.class);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.info("object mapper barfed on input", e);
|
||||||
|
response.status(400);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
DeviceDescriptor device = repository.findOne(lightId);
|
||||||
|
if (device == null) {
|
||||||
|
response.status(404);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
String responseString;
|
||||||
|
String url;
|
||||||
|
if (state.isOn()) {
|
||||||
|
responseString = "[{\"success\":{\"/lights/" + lightId + "/state/on\":true}}]";
|
||||||
|
url = device.getOnUrl();
|
||||||
|
} else {
|
||||||
|
responseString = "[{\"success\":{\"/lights/" + lightId + "/state/on\":false}}]";
|
||||||
|
url = device.getOffUrl();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* light weight templating here, was going to use free marker but it was a bit too
|
||||||
|
* heavy for what we were trying to do.
|
||||||
|
*
|
||||||
|
* currently provides only two variables:
|
||||||
|
* intensity.byte : 0-255 brightness. this is raw from the echo
|
||||||
|
* intensity.percent : 0-100, adjusted for the vera
|
||||||
|
*/
|
||||||
|
if(url.contains(INTENSITY_BYTE)){
|
||||||
|
String intensityByte = String.valueOf(state.getBri());
|
||||||
|
url = url.replace(INTENSITY_BYTE, intensityByte);
|
||||||
|
}else if(url.contains(INTENSITY_PERCENT)){
|
||||||
|
int percentBrightness = (int) Math.round(state.getBri()/255.0*100);
|
||||||
|
String intensityPercent = String.valueOf(percentBrightness);
|
||||||
|
url = url.replace(INTENSITY_PERCENT, intensityPercent);
|
||||||
|
}
|
||||||
|
|
||||||
|
//make call
|
||||||
|
if(!doHttpGETRequest(url)){
|
||||||
|
response.status(503);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
response.status(200);
|
||||||
|
return responseString;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// This function executes the url from the device repository against the vera
|
||||||
|
protected boolean doHttpGETRequest(String url) {
|
||||||
|
log.info("calling GET on URL: " + url);
|
||||||
|
HttpGet httpGet = new HttpGet(url);
|
||||||
|
try {
|
||||||
|
HttpResponse response = httpClient.execute(httpGet);
|
||||||
|
EntityUtils.consume(response.getEntity()); //close out inputstream ignore content
|
||||||
|
log.info("GET on URL responded: " + response.getStatusLine().getStatusCode());
|
||||||
|
if(response.getStatusLine().getStatusCode() == 200){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("Error calling out to HA gateway", e);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
108
src/main/java/com/bwssytems/HABridge/upnp/UpnpListener.java
Normal file
108
src/main/java/com/bwssytems/HABridge/upnp/UpnpListener.java
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
package com.bwssytems.HABridge.upnp;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.*;
|
||||||
|
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import org.apache.http.conn.util.*;
|
||||||
|
|
||||||
|
|
||||||
|
public class UpnpListener {
|
||||||
|
private Logger log = LoggerFactory.getLogger(UpnpListener.class);
|
||||||
|
private static final int UPNP_DISCOVERY_PORT = 1900;
|
||||||
|
private static final String UPNP_MULTICAST_ADDRESS = "239.255.255.250";
|
||||||
|
|
||||||
|
private int upnpResponsePort;
|
||||||
|
|
||||||
|
private int httpServerPort;
|
||||||
|
|
||||||
|
private String responseAddress;
|
||||||
|
|
||||||
|
public UpnpListener() {
|
||||||
|
super();
|
||||||
|
upnpResponsePort = Integer.valueOf(System.getProperty("upnp.response.port", "50000"));
|
||||||
|
httpServerPort = Integer.valueOf(System.getProperty("server.port", "8080"));
|
||||||
|
responseAddress = System.getProperty("upnp.config.address", "192.168.14.136");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void startListening(){
|
||||||
|
log.info("Starting UPNP Discovery Listener");
|
||||||
|
|
||||||
|
try (DatagramSocket responseSocket = new DatagramSocket(upnpResponsePort);
|
||||||
|
MulticastSocket upnpMulticastSocket = new MulticastSocket(UPNP_DISCOVERY_PORT);) {
|
||||||
|
InetSocketAddress socketAddress = new InetSocketAddress(UPNP_MULTICAST_ADDRESS, UPNP_DISCOVERY_PORT);
|
||||||
|
Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();
|
||||||
|
|
||||||
|
while (ifs.hasMoreElements()) {
|
||||||
|
NetworkInterface xface = ifs.nextElement();
|
||||||
|
Enumeration<InetAddress> addrs = xface.getInetAddresses();
|
||||||
|
String name = xface.getName();
|
||||||
|
int IPsPerNic = 0;
|
||||||
|
|
||||||
|
while (addrs.hasMoreElements()) {
|
||||||
|
InetAddress addr = addrs.nextElement();
|
||||||
|
log.debug(name + " ... has addr " + addr);
|
||||||
|
if (InetAddressUtils.isIPv4Address(addr.getHostAddress())) {
|
||||||
|
IPsPerNic++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.debug("Checking " + name + " to our interface set");
|
||||||
|
if (IPsPerNic > 0) {
|
||||||
|
upnpMulticastSocket.joinGroup(socketAddress, xface);
|
||||||
|
log.debug("Adding " + name + " to our interface set");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while(true){ //trigger shutdown here
|
||||||
|
byte[] buf = new byte[1024];
|
||||||
|
DatagramPacket packet = new DatagramPacket(buf, buf.length);
|
||||||
|
upnpMulticastSocket.receive(packet);
|
||||||
|
String packetString = new String(packet.getData());
|
||||||
|
if(isSSDPDiscovery(packetString)){
|
||||||
|
log.debug("Got SSDP Discovery packet from " + packet.getAddress().getHostAddress() + ":" + packet.getPort());
|
||||||
|
sendUpnpResponse(responseSocket, packet.getAddress(), packet.getPort());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("UpnpListener encountered an error. Shutting down", e);
|
||||||
|
|
||||||
|
}
|
||||||
|
log.info("UPNP Discovery Listener Stopped");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* very naive ssdp discovery packet detection
|
||||||
|
* @param body
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
protected boolean isSSDPDiscovery(String body){
|
||||||
|
if(body != null && body.startsWith("M-SEARCH * HTTP/1.1") && body.contains("MAN: \"ssdp:discover\"")){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
String discoveryTemplate = "HTTP/1.1 200 OK\r\n" +
|
||||||
|
"CACHE-CONTROL: max-age=86400\r\n" +
|
||||||
|
"EXT:\r\n" +
|
||||||
|
"LOCATION: http://%s:%s/upnp/amazon-ha-bridge/setup.xml\r\n" +
|
||||||
|
"OPT: \"http://schemas.upnp.org/upnp/1/0/\"; ns=01\r\n" +
|
||||||
|
"01-NLS: %s\r\n" +
|
||||||
|
"ST: urn:schemas-upnp-org:device:basic:1\r\n" +
|
||||||
|
"USN: uuid:Socket-1_0-221438K0100073::urn:Belkin:device:**\r\n\r\n";
|
||||||
|
protected void sendUpnpResponse(DatagramSocket socket, InetAddress requester, int sourcePort) throws IOException {
|
||||||
|
String discoveryResponse = String.format(discoveryTemplate, responseAddress, httpServerPort, getRandomUUIDString());
|
||||||
|
log.debug("sndUpnpResponse: " + discoveryResponse);
|
||||||
|
DatagramPacket response = new DatagramPacket(discoveryResponse.getBytes(), discoveryResponse.length(), requester, sourcePort);
|
||||||
|
socket.send(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String getRandomUUIDString(){
|
||||||
|
return "88f6698f-2c83-4393-bd03-cd54a9f8595"; // https://xkcd.com/221/
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package com.bwssytems.HABridge.upnp;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import static spark.Spark.get;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class UpnpSettingsResource {
|
||||||
|
private static final String UPNP_CONTEXT = "/upnp";
|
||||||
|
|
||||||
|
private Logger log = LoggerFactory.getLogger(UpnpSettingsResource.class);
|
||||||
|
|
||||||
|
private String hueTemplate = "<?xml version=\"1.0\"?>\n" + "<root xmlns=\"urn:schemas-upnp-org:device-1-0\">\n"
|
||||||
|
+ "<specVersion>\n" + "<major>1</major>\n" + "<minor>0</minor>\n" + "</specVersion>\n"
|
||||||
|
+ "<URLBase>http://%s:%s/</URLBase>\n" + // hostname string
|
||||||
|
"<device>\n" + "<deviceType>urn:schemas-upnp-org:device:Basic:1</deviceType>\n"
|
||||||
|
+ "<friendlyName>Amazon-Echo-HA-Bridge (%s)</friendlyName>\n"
|
||||||
|
+ "<manufacturer>Royal Philips Electronics</manufacturer>\n"
|
||||||
|
+ "<manufacturerURL>http://www.armzilla..com</manufacturerURL>\n"
|
||||||
|
+ "<modelDescription>Hue Emulator for Amazon Echo bridge</modelDescription>\n"
|
||||||
|
+ "<modelName>Philips hue bridge 2012</modelName>\n" + "<modelNumber>929000226503</modelNumber>\n"
|
||||||
|
+ "<modelURL>http://www.armzilla.com/amazon-echo-ha-bridge</modelURL>\n"
|
||||||
|
+ "<serialNumber>01189998819991197253</serialNumber>\n"
|
||||||
|
+ "<UDN>uuid:88f6698f-2c83-4393-bd03-cd54a9f8595</UDN>\n" + "<serviceList>\n" + "<service>\n"
|
||||||
|
+ "<serviceType>(null)</serviceType>\n" + "<serviceId>(null)</serviceId>\n"
|
||||||
|
+ "<controlURL>(null)</controlURL>\n" + "<eventSubURL>(null)</eventSubURL>\n"
|
||||||
|
+ "<SCPDURL>(null)</SCPDURL>\n" + "</service>\n" + "</serviceList>\n"
|
||||||
|
+ "<presentationURL>index.html</presentationURL>\n" + "<iconList>\n" + "<icon>\n"
|
||||||
|
+ "<mimetype>image/png</mimetype>\n" + "<height>48</height>\n" + "<width>48</width>\n"
|
||||||
|
+ "<depth>24</depth>\n" + "<url>hue_logo_0.png</url>\n" + "</icon>\n" + "<icon>\n"
|
||||||
|
+ "<mimetype>image/png</mimetype>\n" + "<height>120</height>\n" + "<width>120</width>\n"
|
||||||
|
+ "<depth>24</depth>\n" + "<url>hue_logo_3.png</url>\n" + "</icon>\n" + "</iconList>\n" + "</device>\n"
|
||||||
|
+ "</root>\n";
|
||||||
|
|
||||||
|
public UpnpSettingsResource() {
|
||||||
|
super();
|
||||||
|
setupListener();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupListener () {
|
||||||
|
// http://ip_address:port/upnp/:id/setup.xml which returns the xml configuration for the location of the hue emulator
|
||||||
|
get(UPNP_CONTEXT + "/:id/setup.xml", "application/xml", (request, response) -> {
|
||||||
|
log.info("upnp device settings requested: " + request.params(":id") + " from " + request.ip());
|
||||||
|
String hostName = System.getProperty("upnp.config.address", "192.168.1.1");
|
||||||
|
String portNumber = Integer.toString(request.port());
|
||||||
|
String filledTemplate = String.format(hueTemplate, hostName, portNumber, hostName);
|
||||||
|
log.debug("upnp device settings response: " + filledTemplate);
|
||||||
|
response.status(201);
|
||||||
|
|
||||||
|
return filledTemplate;
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
}
|
||||||
3
src/main/resources/application.properties
Normal file
3
src/main/resources/application.properties
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
upnp.response.port=50000
|
||||||
|
server.port=8080
|
||||||
|
upnp.config.address=192.168.4.136
|
||||||
179
src/main/resources/public/index.html
Normal file
179
src/main/resources/public/index.html
Normal file
@@ -0,0 +1,179 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title>Amazon Echo Bridge Configuration</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
padding-top: 60px;
|
||||||
|
padding-bottom: 20px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
|
||||||
|
<!--[if lt IE 9]>
|
||||||
|
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
|
||||||
|
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
|
||||||
|
<![endif]-->
|
||||||
|
|
||||||
|
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
|
||||||
|
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js"></script>
|
||||||
|
<script type="text/javascript" src="scripts/app.js"></script>
|
||||||
|
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
|
||||||
|
</head>
|
||||||
|
<body ng-app="amazonechobridge">
|
||||||
|
|
||||||
|
<nav class="navbar navbar-inverse navbar-fixed-top">
|
||||||
|
<div class="container">
|
||||||
|
<div class="navbar-header">
|
||||||
|
<a class="navbar-brand" href="#">Amazon Echo Bridge Configuration</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
|
||||||
|
<div class="container">
|
||||||
|
<div ng-controller="ViewingController">
|
||||||
|
|
||||||
|
<div class="panel panel-default bridgeServer">
|
||||||
|
<div class="panel-heading"><h1 class="panel-title">Bridge settings</h1></div>
|
||||||
|
<div class="panel-body">
|
||||||
|
|
||||||
|
<form class="form-horizontal">
|
||||||
|
<label class="col-xs-12 col-sm-3 control-label" for="bridge-base">Bridge server</label>
|
||||||
|
|
||||||
|
<div class="col-xs-8 col-sm-7">
|
||||||
|
<input id="bridge-base" class="form-control" type="text" ng-model="bridge.base"
|
||||||
|
placeholder="URL to bridge">
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="col-xs-2 col-sm-1 btn btn-primary"
|
||||||
|
ng-click="setBridgeUrl(bridge.base)">Load
|
||||||
|
</button>
|
||||||
|
<button type="submit" class="col-xs-2 col-sm-1 btn btn-primary" ng-click="testUrl(bridge.base)">Go
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div ng-controller="ErrorsController">
|
||||||
|
<div ng-if="bridge.error" class="alert alert-warning alert-dismissible" role="alert">
|
||||||
|
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span
|
||||||
|
aria-hidden="true">×</span></button>
|
||||||
|
|
||||||
|
<h2 ng-show='bridge.error != ""'>ERROR</h2>
|
||||||
|
|
||||||
|
<div ng-show='bridge.error != ""'>
|
||||||
|
{{bridge.error}}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="panel panel-default">
|
||||||
|
<div class="panel-heading"><h2 class="panel-title">Current devices</h2></div>
|
||||||
|
<table class="table table-bordered table-striped table-hover">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>ID</th>
|
||||||
|
<th>Name</th>
|
||||||
|
<th>Type</th>
|
||||||
|
<th>Actions</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tr ng-repeat="device in bridge.devices">
|
||||||
|
<td>{{device.id}}</td>
|
||||||
|
<td>{{device.name}}</td>
|
||||||
|
<td>{{device.deviceType}}</td>
|
||||||
|
<td>
|
||||||
|
<button class="btn btn-info" type="submit" ng-click="testUrl(device.onUrl)">Test ON</button>
|
||||||
|
<button class="btn btn-info" type="submit" ng-click="testUrl(device.offUrl)">Test OFF</button>
|
||||||
|
<button class="btn btn-danger" type="submit" ng-click="editDevice(device)">Edit</button>
|
||||||
|
<button class="btn btn-danger" type="submit" ng-click="deleteDevice(device)">Delete</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div ng-controller="AddingController">
|
||||||
|
<div class="panel panel-default bridgeServer" ng-if="!bridge.error">
|
||||||
|
<div class="panel-heading"><h2 class="panel-title">Add a new device</h2></div>
|
||||||
|
<ul class="list-group">
|
||||||
|
<li class="list-group-item">
|
||||||
|
<p class="text-muted">You can generate on/off URLs by filling in the Vera server URL, port, and
|
||||||
|
device ID; or you can fill them out manually in the lower section.</p>
|
||||||
|
|
||||||
|
<form class="form-horizontal">
|
||||||
|
<div class="form-group">
|
||||||
|
<label class="col-xs-12 col-sm-2 control-label" for="vera-base">Vera Server URL </label>
|
||||||
|
|
||||||
|
<div class="col-xs-12 col-sm-10">
|
||||||
|
<input type="text" class="form-control" id="vera-base" ng-model="vera.base"
|
||||||
|
placeholder="Vera URL (e.g. http://192.168.1.100)">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label class="col-xs-2 col-sm-2 control-label" for="vera-port">Vera Request Port </label>
|
||||||
|
|
||||||
|
<div class="col-xs-10 col-sm-2">
|
||||||
|
<input type="text" class="form-control" id="vera-port" ng-model="vera.port"
|
||||||
|
placeholder="Vera Port (typically 3480)">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<label class="col-xs-2 col-sm-2 control-label" for="vera-id">Device ID </label>
|
||||||
|
|
||||||
|
<div class="col-xs-10 col-sm-2">
|
||||||
|
<input type="text" class="form-control" id="vera-id" ng-model="vera.id"
|
||||||
|
placeholder="ID">
|
||||||
|
</div>
|
||||||
|
<button type="submit" ng-click="buildUrls()"
|
||||||
|
class="col-xs-offset-2 col-sm-offset-1 col-xs-4 col-sm-2 btn btn-success">
|
||||||
|
Generate URLs
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</li>
|
||||||
|
<li class="list-group-item">
|
||||||
|
<form class="form-horizontal" ng-submit="addDevice()">
|
||||||
|
<div class="form-group">
|
||||||
|
<label class="col-xs-12 col-sm-2 control-label" for="device-name">Name </label>
|
||||||
|
|
||||||
|
<div class="col-xs-8 col-sm-7">
|
||||||
|
<input type="text" class="form-control" id="device-name" ng-model="device.name"
|
||||||
|
placeholder="Device Name">
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="col-xs-4 col-sm-2 btn btn-primary">
|
||||||
|
{{device.id ? 'Update' : 'Add' }} Device
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label class="col-xs-12 col-sm-2 control-label" for="device-on-url">On URL </label>
|
||||||
|
|
||||||
|
<div class="col-xs-8 col-sm-7">
|
||||||
|
<input type="text" class="form-control" id="device-on-url" ng-model="device.onUrl"
|
||||||
|
placeholder="URL to turn device on">
|
||||||
|
</div>
|
||||||
|
<button class="col-xs-4 col-sm-2 btn btn-success" type="button"
|
||||||
|
ng-click="testUrl(device.onUrl)">Test
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label class="col-xs-12 col-sm-2 control-label" for="device-off-url">Off URL </label>
|
||||||
|
|
||||||
|
<div class="col-xs-8 col-sm-7">
|
||||||
|
<input type="text" class="form-control" id="device-off-url" ng-model="device.offUrl"
|
||||||
|
placeholder="URL to turn device off">
|
||||||
|
</div>
|
||||||
|
<button class="col-xs-4 col-sm-2 btn btn-success" type="button"
|
||||||
|
ng-click="testUrl(device.offUrl)">Test
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
145
src/main/resources/public/scripts/app.js
Normal file
145
src/main/resources/public/scripts/app.js
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
angular.module('amazonechobridge', [])
|
||||||
|
.service('bridgeService', ["$http", function ($http) {
|
||||||
|
var self = this;
|
||||||
|
this.state = {base: window.location.origin + "/api/devices/", devices: [], error: ""};
|
||||||
|
|
||||||
|
this.viewDevices = function () {
|
||||||
|
this.state.error = "";
|
||||||
|
return $http.get(this.state.base).then(
|
||||||
|
function (response) {
|
||||||
|
self.state.devices = response.data;
|
||||||
|
},
|
||||||
|
function (error) {
|
||||||
|
if (error.data) {
|
||||||
|
self.state.error = error.data.message;
|
||||||
|
} else {
|
||||||
|
self.state.error = "If you're not seeing any devices, you may be running into problems with CORS. " +
|
||||||
|
"You can work around this by running a fresh launch of Chrome with the --disable-web-security flag.";
|
||||||
|
}
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
this.addDevice = function (id, name, type, onUrl, offUrl) {
|
||||||
|
this.state.error = "";
|
||||||
|
if (id) {
|
||||||
|
var putUrl = this.state.base + "/" + id;
|
||||||
|
return $http.put(putUrl, {
|
||||||
|
id: id,
|
||||||
|
name: name,
|
||||||
|
deviceType: type,
|
||||||
|
onUrl: onUrl,
|
||||||
|
offUrl: offUrl
|
||||||
|
}).then(
|
||||||
|
function (response) {
|
||||||
|
self.viewDevices();
|
||||||
|
},
|
||||||
|
function (error) {
|
||||||
|
if (error.data) {
|
||||||
|
self.state.error = error.data.message;
|
||||||
|
}
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return $http.post(this.state.base, {
|
||||||
|
name: name,
|
||||||
|
deviceType: type,
|
||||||
|
onUrl: onUrl,
|
||||||
|
offUrl: offUrl
|
||||||
|
}).then(
|
||||||
|
function (response) {
|
||||||
|
self.viewDevices();
|
||||||
|
},
|
||||||
|
function (error) {
|
||||||
|
if (error.data) {
|
||||||
|
self.state.error = error.data.message;
|
||||||
|
}
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
this.deleteDevice = function (id) {
|
||||||
|
this.state.error = "";
|
||||||
|
return $http.delete(this.state.base + "/" + id).then(
|
||||||
|
function (response) {
|
||||||
|
self.viewDevices();
|
||||||
|
},
|
||||||
|
function (error) {
|
||||||
|
if (error.data) {
|
||||||
|
self.state.error = error.data.message;
|
||||||
|
}
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
this.editDevice = function (id, name, type, onUrl, offUrl) {
|
||||||
|
this.device.id = id;
|
||||||
|
this.device.name = name;
|
||||||
|
this.device.onUrl = onUrl;
|
||||||
|
this.device.offUrl = offUrl;
|
||||||
|
};
|
||||||
|
}])
|
||||||
|
|
||||||
|
.controller('ViewingController', ["$scope", "bridgeService", function ($scope, bridgeService) {
|
||||||
|
bridgeService.viewDevices();
|
||||||
|
$scope.bridge = bridgeService.state;
|
||||||
|
$scope.deleteDevice = function (device) {
|
||||||
|
bridgeService.deleteDevice(device.id);
|
||||||
|
};
|
||||||
|
$scope.testUrl = function (url) {
|
||||||
|
window.open(url, "_blank");
|
||||||
|
};
|
||||||
|
$scope.setBridgeUrl = function (url) {
|
||||||
|
bridgeService.state.base = url;
|
||||||
|
bridgeService.viewDevices();
|
||||||
|
};
|
||||||
|
$scope.editDevice = function (device) {
|
||||||
|
bridgeService.editDevice(device.id, device.name, device.type, device.onUrl, device.offUrl);
|
||||||
|
};
|
||||||
|
}])
|
||||||
|
|
||||||
|
.controller('AddingController', ["$scope", "bridgeService", function ($scope, bridgeService) {
|
||||||
|
|
||||||
|
$scope.bridge = bridgeService.state;
|
||||||
|
$scope.device = {id: "", name: "", type: "switch", onUrl: "", offUrl: ""};
|
||||||
|
$scope.vera = {base: "", port: "3480", id: ""};
|
||||||
|
bridgeService.device = $scope.device;
|
||||||
|
|
||||||
|
$scope.buildUrls = function () {
|
||||||
|
if ($scope.vera.base.indexOf("http") < 0) {
|
||||||
|
$scope.vera.base = "http://" + $scope.vera.base;
|
||||||
|
}
|
||||||
|
$scope.device.onUrl = $scope.vera.base + ":" + $scope.vera.port
|
||||||
|
+ "/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1&DeviceNum="
|
||||||
|
+ $scope.vera.id;
|
||||||
|
$scope.device.offUrl = $scope.vera.base + ":" + $scope.vera.port
|
||||||
|
+ "/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0&DeviceNum="
|
||||||
|
+ $scope.vera.id;
|
||||||
|
};
|
||||||
|
|
||||||
|
$scope.testUrl = function (url) {
|
||||||
|
window.open(url, "_blank");
|
||||||
|
};
|
||||||
|
|
||||||
|
$scope.addDevice = function () {
|
||||||
|
bridgeService.addDevice($scope.device.id, $scope.device.name, $scope.device.type, $scope.device.onUrl, $scope.device.offUrl).then(
|
||||||
|
function () {
|
||||||
|
$scope.device.id = "";
|
||||||
|
$scope.device.name = "";
|
||||||
|
$scope.device.onUrl = "";
|
||||||
|
$scope.device.offUrl = "";
|
||||||
|
},
|
||||||
|
function (error) {
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}])
|
||||||
|
|
||||||
|
.controller('ErrorsController', ["$scope", "bridgeService", function ($scope, bridgeService) {
|
||||||
|
$scope.bridge = bridgeService.state;
|
||||||
|
}]);
|
||||||
14
src/test/java/demo/DemoApplicationTests.java
Normal file
14
src/test/java/demo/DemoApplicationTests.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package demo;
|
||||||
|
|
||||||
|
import com.bwssytems.HABridge.AmazonEchoBridge;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Dummy test holder
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class DemoApplicationTests {
|
||||||
|
|
||||||
|
public void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user