Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

1376 - Initial contribution of ZoneMinder Binding #1376

Closed
wants to merge 109 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
109 commits
Select commit Hold shift + click to select a range
92db05e
Initial version of ZoneMinder Binding.
Oct 21, 2016
9687223
Every source file has been cleaned.
Oct 21, 2016
e04ba0c
Fixed missing libraries (jackson-*.jar)
Oct 21, 2016
12a38e4
Fixed mixing of new high priority and low priority parameter in Server
Oct 21, 2016
aac44e7
Improvements regarding Initialisation of Server Thing and Discovery of
Oct 21, 2016
04ded82
Fixed misconfiguration of Monitor from discovery, which resulted in
Oct 22, 2016
b5cfa3b
Removed local variables representing OH config, instead the config class
Oct 22, 2016
fcbe950
Added handlers for all channels to avoid log entries in OpenHAB
Oct 22, 2016
01dedd8
Added missing handler for SourceTupe channel in Monitor
Oct 22, 2016
cd627e6
First attempt to update channels from server.
Oct 22, 2016
1fff812
Initial handling of DiskUsage and CPULoad
Oct 23, 2016
905b48f
Disk Usage fetched in low priority loop
Oct 23, 2016
a5ecd00
Changed TimeUnit in low priority job to Minutes
Oct 23, 2016
21c61b9
Basic handling of initialisation problems. For all objects
Oct 23, 2016
52cec07
Fixed problem with Bridge staying offline with errornous config.
Oct 23, 2016
c17d063
Fixed missing break in switch, cause a lot of entries in the OH error
Oct 23, 2016
13960cd
Before rework of API access
Oct 27, 2016
590f8fc
First working version of major rework
Oct 31, 2016
3c4bd67
Fixes #2, #3, #8, #9, #11,#13
Nov 1, 2016
c0ae5c5
Initial version of ZoneMinder Binding.
Oct 21, 2016
89c8480
Every source file has been cleaned.
Oct 21, 2016
ff5baeb
Fixed missing libraries (jackson-*.jar)
Oct 21, 2016
1304943
Fixed mixing of new high priority and low priority parameter in Server
Oct 21, 2016
d599650
Improvements regarding Initialisation of Server Thing and Discovery of
Oct 21, 2016
80f7091
Fixed misconfiguration of Monitor from discovery, which resulted in
Oct 22, 2016
fad0b1a
Removed local variables representing OH config, instead the config class
Oct 22, 2016
6110ebe
Added handlers for all channels to avoid log entries in OpenHAB
Oct 22, 2016
134a688
Added missing handler for SourceTupe channel in Monitor
Oct 22, 2016
8d15d7d
First attempt to update channels from server.
Oct 22, 2016
5a50a51
Initial handling of DiskUsage and CPULoad
Oct 23, 2016
88287d3
Disk Usage fetched in low priority loop
Oct 23, 2016
9e06be6
Changed TimeUnit in low priority job to Minutes
Oct 23, 2016
c0c451a
Basic handling of initialisation problems. For all objects
Oct 23, 2016
d57217c
Fixed problem with Bridge staying offline with errornous config.
Oct 23, 2016
5d02224
Fixed missing break in switch, cause a lot of entries in the OH error
Oct 23, 2016
6d3d6cf
Before rework of API access
Oct 27, 2016
5d77e5c
First working version of major rework
Oct 31, 2016
27bed5e
Fixes #2, #3, #8, #9, #11,#13
Nov 1, 2016
38bae92
Merge branch 'master' of https://github.com/Mr-Eskildsen/openhab2-addons
Nov 1, 2016
3fef20d
Update README.md
Mr-Eskildsen Nov 1, 2016
771b382
Update README.md
Mr-Eskildsen Nov 1, 2016
383227e
Update README.md
Mr-Eskildsen Nov 2, 2016
cb33e5a
Update README.md
Mr-Eskildsen Nov 7, 2016
f92d82c
Update
Mr-Eskildsen Nov 7, 2016
a26397b
Fixes #15, #16, #17 and #18
Nov 8, 2016
7006f70
Fixes #15, #16 #17 and #18
Nov 8, 2016
d890ecc
Merged branch master into master
Nov 8, 2016
f4b8aa0
Merged master into hotfix
Nov 8, 2016
9209e74
Merged branch master into master
Nov 8, 2016
4b4daf1
Fixes #19
Nov 8, 2016
fcc6d27
Fixes #19
Nov 8, 2016
4bfb5db
Added support for REFRESH in handleCommand
Nov 8, 2016
b79974d
Corrected a TODO that was forgotten earlier
Nov 8, 2016
fc29327
Merged hotfix into master
Nov 8, 2016
552bd2f
Update README.md
Mr-Eskildsen Nov 17, 2016
6c51ef1
Merge branch 'master' of https://github.com/openhab/openhab2-addons
Nov 29, 2016
390e84c
Merge branch 'master' of https://github.com/Mr-Eskildsen/openhab2-addons
Nov 29, 2016
365553f
Removed unused code
Nov 29, 2016
54cf595
Create readme.md
Mr-Eskildsen Jan 1, 2017
c38bbe5
Update readme.md
Mr-Eskildsen Jan 1, 2017
4859f41
Update readme.md
Mr-Eskildsen Jan 1, 2017
20ba1ec
Delete README.md
Mr-Eskildsen Jan 1, 2017
3cd5065
Update readme.md
Mr-Eskildsen Jan 2, 2017
f2a7bf1
Update readme.md
Mr-Eskildsen Jan 2, 2017
f86ee1d
Update readme.md
Mr-Eskildsen Jan 2, 2017
e70f042
Create README.md
Mr-Eskildsen Jan 4, 2017
b02a296
Update README.md
Mr-Eskildsen Jan 4, 2017
d30a427
First working version, after refactoring session handling
Jan 12, 2017
d9c34ef
Release Candidate
Jan 15, 2017
6e765fc
Update README.md
Mr-Eskildsen Jan 15, 2017
d5b092e
Update README.md
Mr-Eskildsen Jan 15, 2017
b4348ab
Update README.md
Mr-Eskildsen Jan 15, 2017
6785973
Update README.md
Mr-Eskildsen Jan 15, 2017
2d0d359
Update README.md
Mr-Eskildsen Jan 15, 2017
45e17ca
Update README.md
Mr-Eskildsen Jan 15, 2017
22b489e
Update README.md
Mr-Eskildsen Jan 15, 2017
a09e8ab
Release Candidate
Jan 15, 2017
8826f8d
Release Candidate (published on GitHub 16/1-2017)
Jan 16, 2017
dcacc5f
Update README.md
Mr-Eskildsen Jan 16, 2017
cdcdde9
Fixed minor problem with refresh algorithm, and improved logging
Jan 17, 2017
1e88e0c
Removed unused code
Nov 29, 2016
4ba6c35
First working version, after refactoring session handling
Jan 12, 2017
c20704f
Release Candidate
Jan 15, 2017
ac39594
Release Candidate
Jan 15, 2017
30240b4
Release Candidate (published on GitHub 16/1-2017)
Jan 16, 2017
fd6e75d
Fixed minor problem with refresh algorithm, and improved logging
Jan 17, 2017
82946e4
Merge pull request #20 from Mr-Eskildsen/hotfix
Mr-Eskildsen Jan 17, 2017
48d50ca
Merged branch hotfix into hotfix
Jan 17, 2017
7fb4abe
Addreessed Comments from review
Jan 18, 2017
2db619e
Improved error handling to identify periodic disconnects
Jan 19, 2017
be01993
Release Candidate
Jan 22, 2017
b806cb4
Ready to final Review
Jan 22, 2017
57530aa
Merge remote-tracking branch 'origin/master' into hotfix
Jan 22, 2017
514f3ae
Merge branch 'hotfix'
Jan 22, 2017
d662e1d
Merge remote-tracking branch 'upstream/master'
Jan 23, 2017
9321184
Changes found during my own final review
Jan 23, 2017
422ed61
Fixed initialisation errors
Jan 24, 2017
d4211d6
Final checkin before requesting codereview
Jan 25, 2017
41ed6d7
Create README.md
Mr-Eskildsen Jan 25, 2017
8eb6c9a
Update README.md
Mr-Eskildsen Jan 25, 2017
91f06c0
Update README.md
Mr-Eskildsen Jan 25, 2017
02cbc97
Update README.md
Mr-Eskildsen Jan 26, 2017
071c3e5
Update README.md
Mr-Eskildsen Jan 26, 2017
3b7ec2d
Update README.md
Mr-Eskildsen Jan 26, 2017
2939a4c
Update README.md
Mr-Eskildsen Jan 26, 2017
577732f
Aligned repository to upstream master
Mr-Eskildsen Jan 26, 2017
d477fce
Aligning to upstream/master
Mr-Eskildsen Jan 26, 2017
ccd7ddf
Improved handling off ThingStatus and added additional logging
Jan 26, 2017
2a69b9d
Updated zoneminder4j so that ZoneMinder is correctly detected when it is
Jan 27, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 170 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,174 @@
## openHAB 2 Add-ons
# Zoneminder Binding
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you delete this file?


This repository contains add-ons that are implemenented using the new [Eclipse SmartHome APIs](https://www.eclipse.org/smarthome/documentation/development/bindings/how-to.html) of openHAB 2.
This binding offers integration to a ZoneMinder Server. It currently only offers to integrate to monitors (eg. cameras in ZoneMinder). It also only offers access to a limited set of values, as well as a even more limited option to update values in ZoneMinder. It requires at least ZoneMinder 1.29 with API enabled (option 'OPT_USE_API' in ZoneMinder must be enabled). The option 'OPT_TRIGGERS' must be anabled to allow openHAB to trip the ForceAlarm in ZoneMinder.

Note that all information about openHAB itself, the IDE setup and the contribution processes can be found in the [openhab-distro](https://github.com/openhab/openhab-distro) project, so please go there for any further details!
## Supported Things

## Add-ons in other repositories
Some add-ons (e.g. specific bindings such as [Z-Wave](https://github.com/openhab/org.openhab.binding.zwave)) are maintained in separate repositories in order to improve their management. In order to contribute to these bindings, you should follow the following steps -:
This binding supports the following thing types

1. Fork the repository on Github
2. Clone your repository to your local computer as described in the [Github tutorial](https://help.github.com/articles/cloning-a-repository/)
3. Open the openHAB Eclipse IDE
4. Select the *File | Import* menu option
5. Select *General | Existing Projects into Workspace* and click Next
6. Select the root directory where you made the local clone of the repository
7. Select the project and click *Next*
8. The project will now be imported and available in the Package Explorer
9. You may want to add the project to the *OH2 Add-ons* Working Set
<table>
<tr><td><b>Thing</b></td><td><b>Thing Type</b></td><td><b>Discovery</b></td><td><b>Description</b></td></tr>
<tr><td>ZoneMinder Server</td><td>Bridge</td><td>Manual</td><td>A ZoneMinder Server. Required version is minimum 1.29</td></tr>
<tr><td>ZoneMinder Monitor</td><td>Thing</td><td>Automatic</td><td>Monitor as defined in ZoneMinder Server</td></tr>
</table>

## Getting started / Discovery

The binding consists of a Bridge (the ZoneMinder Server it self), and a number of Things, which relates to the induvidual monitors in ZoneMinder. ZoneMinder things can be configured either through the online configuration utility via discovery, or manually through the 'zoneminder.things' configuration file. The Bridge will not be autodiscovered, this behaviour is by design. That is because the ZoneMinder API can be configured to communicate on custom ports, you can even change the url from the default /zm/ to something userdefined. That makes it meaningless to scan for a ZoneMinder Server. The Bridge must therefore be added manually, this can be done from PaperUI. After adding the Bridge it will go ONLINE, and after a short while and the discovery process for monitors will start. When a new monitor is discovered it will appear in the Inbox.


### Bridge ###

Channel | Type | Description
-------------- | --------- | ----------------------------------
online | Switch | Parameter indicating if the server is online
CPU load | Text | Current CPU Load of server
Disk Usage | text | Current Disk Usage on server

### Thing ###

Channel | Type | Description
-------------- | --------- | ----------------------------------
online | Switch | Parameter indicating if the monitor is online
enabled | Switch | Parameter indicating if the monitor is enabled
force-alarm | Switch | Parameter indicating if Force Alarm for the the monitor is active
alarm | Switch | true if monitor has an active alarm
recording | Text | true if monitor is recording
detailed-status| Text | Detailed status of monitor (Idle, Pre-alarm, Alarm, Alert, Recording)
event-cause | Text | Empty when there is no active event, else it contains the text with the cause of the event
function | Text | Text corresponding the value in ZoneMinder: None, Monitor, Modect, Record, Mocord, Nodect
capture-daemon | Switch | Run state of ZMC Daemon
analysis-daemon| Switch | Run state of ZMA Daemon
frame-daemon | Switch | Run state of ZMF Daemon

##Manual configuration##

###Things configuration###

```
Bridge zoneminder:server:ZoneMinderSample [ hostname="192.168.1.55", user="<USERNAME>", password="<PASSWORD>", telnet_port=6802, refresh_interval_disk_usage=1 ]
{
Thing monitor monitor_1 [ monitorId=1, monitorTriggerTimeout=120, monitorEventText="Trigger activated from OpenHAB" ]
}

```
###Items configuration###

```
/* *****************************************
* SERVER
* *****************************************/
Switch zmServer_Online "Zoneminder Online [%s]" <switch> {channel="zoneminder:server:ZoneMinderSample:online"}
Number zmServer_CpuLoad "ZoneMinder Server Load [%s]" {channel="zoneminder:server:ZoneMinderSample:cpu-load"}

Number zmServer_DiskUsage "ZoneMinder Disk Usage [%s]" {channel="zoneminder:server:ZoneMinderSample:disk-usage"}

/* *****************************************
* MONITOR 1
* *****************************************/
Switch zmMonitor1_Online "Online [%s]" <switch> {channel="zoneminder:monitor:ZoneMinderSample:monitor-1:online"}
Switch zmMonitor1_Enabled "Enabled [%s]" <switch> {channel="zoneminder:monitor:ZoneMinderSample:monitor-1:enabled"}
Switch zmMonitor1_ForceAlarm "Force Alarm [%s]" <switch> {channel="zoneminder:monitor:ZoneMinderSample:monitor-1:force-alarm"}
Switch zmMonitor1_EventState "Alarm [%s]" <switch> {channel="zoneminder:monitor:ZoneMinderSample:monitor-1:alarm"}
Switch zmMonitor1_Recording "Recording [%s]" <switch> {channel="zoneminder:monitor:ZoneMinderSample:monitor-1:recording"}
String zmMonitor1_DetailedStatus "Detailed Status [%s]" {channel="zoneminder:monitor:ZoneMinderSample:monitor-1:detailed-status"}
String zmMonitor1_EventCause "Event Cause [%s]" <switch> {channel="zoneminder:monitor:ZoneMinderSample:monitor-1:event-cause"}
String zmMonitor1_Function "Function [%s]" {channel="zoneminder:monitor:ZoneMinderSample:monitor-1:function"}
Switch zmMonitor1_CaptureState "Capture Daemon [%s]" <switch> {channel="zoneminder:monitor:ZoneMinderSample:monitor-1:capture-daemon"}
Switch zmMonitor1_AnalysisState "Analysis Daemon [%s]" <switch> {channel="zoneminder:monitor:ZoneMinderSample:monitor-1:analysis-daemon"}
Switch zmMonitor1_FrameState "Frame Daemon [%s]" <switch> {channel="zoneminder:monitor:ZoneMinderSample:monitor-1:frame-daemon"}


// Helpers
Switch zmMonitor1_Mode "Monitor active [%s]"
```

###Sample Rule###

```
rule "Monitor1 Alarm State"
when
Item zmMonitor1_EventState changed
then
if (zmMonitor1_EventState.state == ON) {
logInfo("zoneminder.rules", "ZoneMinder Alarm started")
}
else if (zmMonitor1_EventState.state == OFF) {
logInfo("zoneminder.rules", "ZoneMinder Alarm stopped")
}
end

rule "Monitor1 Recording State"
when
Item zmMonitor1_Recording changed
then
if (zmMonitor1_Recording.state == ON) {
logInfo("zoneminder.rules", "ZoneMinder recording started")
}
else if (zmMonitor1_Recording.state == OFF) {
logInfo("zoneminder.rules", "ZoneMinder recording stopped")
}
end


rule "Change Monitor1 Mode"
when
Item zmMonitor1_Mode changed
then
if (zmMonitor1_Mode.state==ON) {
sendCommand(zmMonitor1_Function, "Modect")
sendCommand(zmMonitor1_Enabled, ON)
}
else {
sendCommand(zmMonitor1_Function, "Monitor")
sendCommand(zmMonitor1_Enabled, OFF)
}
end
```


###Sitemap configuration###

```
sitemap zoneminder label="Zoneminder"
{
Frame {
Text item=zmServer_Online label="ZoneMinder Server [%s]" {
Frame {
Text item=zmServer_Online
Text item=zmServer_CpuLoad
Text item=zmServer_DiskUsage
}
}

Text item=zmMonitor1_Function label="(Monitor-1) [%s]" {
Frame {
Switch item=zmMonitor1_Enabled
Switch item=zmMonitor1_ForceAlarm
Text item=zmMonitor1_Online
Selection item=zmMonitor1_Function mappings=["None"=None, "Modect"=Modect, "Monitor"=Monitor, "Record"=Record, "Mocord"=Mocord, "Nodect"=Nodect]
Text item=zmMonitor1_EventState
Text item=zmMonitor1_Recording
Text item=zmMonitor1_DetailedStatus
Text item=zmMonitor1_EventCause
Text item=zmMonitor1_CaptureState
Text item=zmMonitor1_AnalysisState
Text item=zmMonitor1_FrameState
}
}
Frame label="Monitor Helpers" {
Switch item=zmMonitor1_Mode
}
}
}
```
##Troubleshooting##

<table>
<tr><td><b>Problem</b></td><td><b>Solution</b></td></tr>
<tr><td>Cannot connect to ZoneMinder Bridge</td><td>Check if you can logon to ZoneMinder from your openHAB server (with http).</td></tr>
<tr><td></td><td>Check that it is possible to establish a Telnet connection from openHAB server to Zoneminder Server</td></tr>
<tr><td>ZoneMinder Bridge is not comming ONLINE. It says: 'OFFLINE - COMMUNICATION_ERROR Cannot access ZoneMinder Server. Check provided usercredentials'</td><td>Check that the hostname is valid, if using a DNS name, make sure name is correct resolved. Also check that the given host can be accessed from a browser. Finally make sure to change the additional path from '/zm', if not using standard setup.</td></tr>
<tr><td>Cannot connect to ZoneMinder Bridge via HTTPS, using Letsencrypt certificate</td><td>Verify your Java version, if Java is below build 101, letsencrypt certificate isn't known by Java. Either use HTTP or upgrade Java to newest build. Please be aware that https support is provided as an experimental feature.</td></tr>
<tr><td>I have tried all of the above, it still doesn't work</td><td>Try to execute this from a commandline (on your openHAB server): curl -d "<username>=XXXX&<password>=YYYY&action=login&view=console" -c cookies.txt http://<yourzmip>/zm/index.php. Change <yourzmip>, <username> and <password> to the actual values. This will check if your server is accessible from the openHAB server.</td></tr>
</table>
File renamed without changes.
30 changes: 15 additions & 15 deletions addons/binding/create_openhab_binding_skeleton.cmd
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
@echo off
SETLOCAL
SET ARGC=0
FOR %%x IN (%*) DO SET /A ARGC+=1
IF %ARGC% NEQ 2 (
echo Usage: %0 BindingIdInCamelCase BindingIdInLowerCase
exit /B 1
)
mvn archetype:generate -N -DarchetypeGroupId=org.eclipse.smarthome.archetype -DarchetypeArtifactId=org.eclipse.smarthome.archetype.binding -DarchetypeVersion=0.9.0-SNAPSHOT -DgroupId=org.openhab.binding -DartifactId=org.openhab.binding.%2 -Dpackage=org.openhab.binding.%2 -DarchetypeCatalog='file://../archetype-catalog.xml' -Dversion=2.0.0-SNAPSHOT -DbindingId=%2 -DbindingIdCamelCase=%1 -DvendorName=openHAB -Dnamespace=org.openhab
@echo off


SETLOCAL
SET ARGC=0

FOR %%x IN (%*) DO SET /A ARGC+=1

IF %ARGC% NEQ 2 (
echo Usage: %0 BindingIdInCamelCase BindingIdInLowerCase
exit /B 1
)

mvn archetype:generate -N -DarchetypeGroupId=org.eclipse.smarthome.archetype -DarchetypeArtifactId=org.eclipse.smarthome.archetype.binding -DarchetypeVersion=0.9.0-SNAPSHOT -DgroupId=org.openhab.binding -DartifactId=org.openhab.binding.%2 -Dpackage=org.openhab.binding.%2 -DarchetypeCatalog='file://../archetype-catalog.xml' -Dversion=2.0.0-SNAPSHOT -DbindingId=%2 -DbindingIdCamelCase=%1 -DvendorName=openHAB -Dnamespace=org.openhab

ENDLOCAL
Empty file modified addons/binding/create_openhab_binding_skeleton.sh
100755 → 100644
Empty file.
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
/**
* Copyright (c) 2014-2016 by the respective copyright holders.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.avmfritz.internal.hardware;
import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.client.api.Response.CompleteListener;
import org.eclipse.jetty.client.api.Response.ContentListener;
import org.eclipse.jetty.client.api.Response.FailureListener;
import org.eclipse.jetty.client.api.Response.SuccessListener;
import org.eclipse.jetty.client.api.Result;
import org.eclipse.jetty.client.util.BufferingResponseListener;
import org.openhab.binding.avmfritz.internal.hardware.callbacks.FritzAhaCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Implementation of Jetty ContextExchange to handle callbacks
*
* @author Robert Bausdorf
*
*/
public class FritzahaContentExchange extends BufferingResponseListener
implements SuccessListener, FailureListener, ContentListener, CompleteListener {
/**
* logger
*/
private final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* Callback to execute on complete response
*/
private FritzAhaCallback callback;
/**
* Constructor
*
* @param callback Callback which execute method has to be called.
*/
public FritzahaContentExchange(FritzAhaCallback callback) {
this.callback = callback;
}
/**
* Log request success
*/
@Override
public void onSuccess(Response response) {
logger.debug("HTTP response {}", response.getStatus());
}
/**
* Log request failure
*/
@Override
public void onFailure(Response response, Throwable failure) {
logger.debug(failure.getLocalizedMessage());
}
/**
* Call the callbacks execute method on request completion.
*/
@Override
public void onComplete(Result result) {
logger.debug("response complete: " + this.getContentAsString());
this.callback.execute(result.getResponse().getStatus(), this.getContentAsString());
}
}
/**
* Copyright (c) 2014-2016 by the respective copyright holders.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.avmfritz.internal.hardware;

import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.client.api.Response.CompleteListener;
import org.eclipse.jetty.client.api.Response.ContentListener;
import org.eclipse.jetty.client.api.Response.FailureListener;
import org.eclipse.jetty.client.api.Response.SuccessListener;
import org.eclipse.jetty.client.api.Result;
import org.eclipse.jetty.client.util.BufferingResponseListener;
import org.openhab.binding.avmfritz.internal.hardware.callbacks.FritzAhaCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Implementation of Jetty ContextExchange to handle callbacks
*
* @author Robert Bausdorf
*
*/
public class FritzahaContentExchange extends BufferingResponseListener
implements SuccessListener, FailureListener, ContentListener, CompleteListener {
/**
* logger
*/
private final Logger logger = LoggerFactory.getLogger(this.getClass());

/**
* Callback to execute on complete response
*/
private FritzAhaCallback callback;

/**
* Constructor
*
* @param callback Callback which execute method has to be called.
*/
public FritzahaContentExchange(FritzAhaCallback callback) {
this.callback = callback;
}

/**
* Log request success
*/
@Override
public void onSuccess(Response response) {
logger.debug("HTTP response {}", response.getStatus());
}

/**
* Log request failure
*/
@Override
public void onFailure(Response response, Throwable failure) {
logger.debug(failure.getLocalizedMessage());
}

/**
* Call the callbacks execute method on request completion.
*/
@Override
public void onComplete(Result result) {
logger.debug("response complete: " + this.getContentAsString());
this.callback.execute(result.getResponse().getStatus(), this.getContentAsString());
}
}
Loading