Compare commits

...

305 Commits

Author SHA1 Message Date
Admin
fd486588f5 Fixed more immediate issues
Fixes #272
Fixes #384
Fixes #387
Fixes #389
2017-01-24 09:31:48 -06:00
BWS Systems
58e1679529 updated jar version 2017-01-23 19:05:29 -06:00
Admin
7772a3de0f Immediate bug fixes
Fixes #378
Fixes #380

Added FAQ link in Help menu
2017-01-23 15:44:53 -06:00
BWS Systems
a348086910 Add FAQ 2017-01-22 18:56:10 -06:00
BWS Systems
31cfcb650b Merge pull request #376 from bwssytems/postv3.5.1changes
Create V4.0.0 from branch
2017-01-22 17:37:54 -06:00
bwssystems
90d56ab686 Added donate button to about menu. Fixed layouts to be consistent for
all pages.
2017-01-22 17:10:54 -06:00
Admin
8dad922eec Updated last text instructions for the beta. 2017-01-19 16:43:27 -06:00
Admin
e5f088cfb9 Finished editor update 2017-01-19 15:57:33 -06:00
BWS Systems
a550288847 add more detail to the systemctl setup on linux 2017-01-19 12:26:26 -06:00
BWS Systems
e0be1507f6 updated info about restricted port 80 2017-01-19 09:07:26 -06:00
audiofreak9
e9dc023971 Update README.md
Fixed missing open brace in the first Multiple Call Construct example
2017-01-16 18:00:04 -05:00
bwssystems
56ebe2d929 Fixed initialization of bridge control entries 2017-01-16 14:21:10 -06:00
bwssystems
aecadc529b addded logging for url item json decoding, fixed harmony activity json
decoding
2017-01-15 13:28:52 -06:00
bwssystems
7a55f6875f Fixing issues with beta, reverted back to Spark 2.3 2017-01-14 15:39:09 -06:00
Admin
382828f615 Updated issues with edit and update device 2017-01-12 10:57:28 -06:00
bwssystems
cd62538785 Updated editor, put in fix for nest status deserialize, updated
configured items display in tabs
2017-01-10 16:56:53 -06:00
Admin
b644b5e3cc Updated scrollable table header sorts for config, vera and harmony
Added upnp notification logic
2017-01-09 17:28:41 -06:00
Admin
6f544e1a7d Updated release with new editor
new angularJS V1.6.1
new spark V2.5.4

testing for new 4beta3
2017-01-09 13:19:03 -06:00
bwssystems
4744a68906 working on editor 2017-01-08 18:24:16 -06:00
Admin
010260429e New edit device in testing 2017-01-06 17:13:37 -06:00
Admin
e694660af3 starting new editor 2017-01-05 16:05:20 -06:00
Admin
a0a2d90617 javascript updates 2017-01-04 11:21:23 -06:00
Admin
5a292c98a2 Continue testing and fixing 4 beta 2017-01-03 16:36:45 -06:00
bwssystems
9155989791 Fixing items 4beta2.2 2017-01-02 13:27:53 -06:00
BWS Systems
bf9a332ecf Merge pull request #347 from pascalschwientek/patch-1
little readme fix
2017-01-01 09:48:23 -06:00
Pascal Schwientek
123ac65723 little readme fix 2017-01-01 09:57:35 +01:00
Admin
9c4eb58739 Fixed issues with 4beta1 2016-12-30 15:00:31 -06:00
Admin
363fb605a6 Adding new things for 4beta2 2016-12-29 16:16:01 -06:00
Admin
ea6c31494b Refactoring for Beta release complete..testing ensues 2016-12-29 15:56:38 -06:00
bwssystems
a3325aa048 refactoring.... 2016-12-28 18:42:08 -06:00
Admin
66d7306cda Continue Refactoring 2016-12-28 16:44:41 -06:00
bwssystems
b7d6d099a6 Small update 2016-12-27 11:19:24 -06:00
bwssystems
2e177f3d4a Refactoring ready for testing 2016-12-26 22:34:19 -06:00
bwssystems
d827605fa5 Conitnuation of refactoring 2016-12-26 11:28:06 -06:00
bwssystems
729d548d0e More refactoring 2016-12-24 12:59:25 -06:00
bwssystems
ca6b1ae32d continuation of HueMulator refactor 2016-12-24 12:09:50 -06:00
Admin
40a9eb95ac Continuation of refactoring hueMulator 2016-12-22 16:14:38 -06:00
BWS Systems
3a3e5180b1 Incorrect device update http type
Needs to be PUT instead of POST to update.
2016-12-22 09:52:27 -06:00
Admin
4225f4554e Add homemanager 2016-12-21 17:07:21 -06:00
Admin
b5a3bea803 Comitinue refactor of huemulator 2016-12-21 17:06:30 -06:00
Admin
2579949a23 Done testing HomeAssistant, refactoring HueMulator, fixed MQTT dim
control.
2016-12-20 18:20:10 -06:00
Admin
c9d76bed86 Testing HomeAssistant Implementation. 2016-12-19 15:57:45 -06:00
Admin
1500f7fb56 Merge remote-tracking branch 'origin/master' into postv3.5.1changes 2016-12-19 09:02:30 -06:00
BWS Systems
2f10135b1d Merge pull request #314 from melloware/master
Fixed spelling mistake in UDP Sender.
2016-12-19 08:39:18 -06:00
bwssystems
8b0aa6fe98 Added junit dependency 2016-12-17 13:07:58 -06:00
melloware
11eefa9efd Fixed spelling mistake in UDP Sender. 2016-12-17 14:04:06 -05:00
Admin
74c15db202 HomeAssistant continuation 2016-12-16 16:10:49 -06:00
BWS Systems
d0a63747c7 Add note for need of philps hue hub 2016-12-16 10:05:37 -06:00
Admin
74cccd6de3 implementing HomeAssistant 2016-12-15 16:56:25 -06:00
Admin
321b63f0fb update attributes for text files 2016-12-12 09:01:38 -06:00
Admin
4a105b94fd Continuing Home Assistant implementation. 2016-12-09 16:35:46 -06:00
Admin
7677ed08d8 Start building HomeAssistant interface. 2016-12-08 16:41:23 -06:00
Admin
2b1b70cf4f Dim/brighten rework. 2016-12-07 15:53:14 -06:00
Admin
9c252a61ba update nest-controller version 2016-12-05 16:47:30 -06:00
Admin
460931f6e0 continuation of requester filter code. 2016-11-30 15:05:54 -06:00
Admin
5746d271b1 Merge branch 'master' of https://github.com/bwssytems/ha-bridge 2016-11-29 16:40:12 -06:00
Admin
6f58d38224 finish base implementation of requester ip filtering within a url item.
not tested
2016-11-29 16:38:44 -06:00
BWS Systems
e6cde7a3bd Added warning Note and updated Alexa voice commands 2016-11-28 16:23:48 -06:00
BWS Systems
a325a28731 Merge pull request #264 from digiltd/patch-1
remove URL encode form Kodi volume instructions
2016-11-22 08:27:33 -06:00
Sam Turner
bf43d5c33e remove URL encode form Kodi volume instructions 2016-11-22 13:01:53 +00:00
Admin
dcc4041d7d Updating for requester filter and to be able to have multiple calls for
any type of device.
2016-11-21 16:45:01 -06:00
BWS Systems
a3f5b47260 update version 2016-11-21 08:18:46 -06:00
bwssystems
e7acff9f54 Fixed MQTT initialization error with maven build inclusion. Added URI
encoder to handle URI calls with URL characters.

Fixes #250
Fixes #251
Fixes #252
2016-11-19 14:12:32 -06:00
Admin
9c4b428c86 Added updates to README and also the latest release includes more
constructs for itmes and device buttons for delay and count.

Fixes #132
2016-11-18 15:34:20 -06:00
BWS Systems
80b3e87e44 remove harmony user and pass
These parameters are not needed anymore.
2016-11-18 15:20:56 -06:00
BWS Systems
e14482e232 Merge pull request #248 from bwssytems/postV3.2.2NewConnectors
Post v3.2.2 new connectors
2016-11-18 15:17:36 -06:00
Admin
08166c0ebf Finished MQTT support and some testing. Updated harmony-java-client to
v1.1.1 to fix authentication error.
2016-11-18 15:14:58 -06:00
BWS Systems
373515e1ed Clarify Software Alexa Statement
Thanks to digiltd for helping write the clarification.

Fixes #245
2016-11-17 09:52:50 -06:00
Admin
68f38e1d95 MQTT publish implementation, still without password. 2016-11-16 16:58:11 -06:00
Admin
90f2bce282 Fixed requester filter for multiple echos. Started MQTT MEssage screen. 2016-11-14 16:43:33 -06:00
Admin
3eba9b9245 Basic MQTT configuration implementation. Still needs username/pwd and
MQTT screen helper.
2016-11-11 16:42:20 -06:00
Admin
7d39b79e05 Added Requester logic filtering. Added logic for count and delay in
items and buttons.
2016-11-11 14:38:37 -06:00
BWS Systems
407b0e0bd5 Add declaration that software Alexa does not work with local hue bridges 2016-11-09 16:13:18 -06:00
BWS Systems
9baff8d403 Another update 2016-11-08 15:55:50 -06:00
BWS Systems
23a6c7059c Make directory in docs consistent
Fixes #226
2016-11-08 15:54:20 -06:00
Admin
84fb79f9d9 Fixed issue with "Test Dim" button not presenting the value selector
when there was a body with a dim command. Added ability to specifically
set the web server address port.

Fixes #225
Fixes #217
2016-11-08 11:27:43 -06:00
BWS Systems
3702de8efd Merge pull request #222 from TheOriginalAndrobot/master
Update documentation with Google Home info
2016-11-05 11:33:44 -05:00
Androbot
f0ab9afd66 Update documentation with Google Home info 2016-11-05 08:48:35 -07:00
Admin
e5c4e09543 Update readme for version. 2016-11-02 16:33:57 -05:00
Admin
425ac9fb91 Updated upnp response handling to warn on failure and not stop. Added
configuration of echo url for those overseas. Fixed overwriting device
issue when adding a manual device add or after a restart. Added default
IpV4 handling with override for ha-bridge. Fixed Dim URL http body
handling on PUT and POST. Added documentation for Kodi Volume handling.

Fixes #207
Fixes #202
Fixes #201
Fixes #198
Fixes #194
Fixes #193
2016-11-02 16:32:57 -05:00
BWS Systems
32203b65be Merge pull request #189 from bwssytems/postv3.1fixes
Fixes #129 
Fixes #161 
Fixes #166 
Fixes #168 
Fixes #172 
Fixes #173 
Fixes #174 
Fixes #181
2016-10-18 08:42:01 -05:00
Admin
7e9600d2a7 Update release version number. 2016-10-18 08:35:27 -05:00
BWS Systems
dcc470486f Merge pull request #185 from martinbutt/master
Docs fix
2016-10-14 08:17:26 -05:00
Martin Butt
330adbdd4c Docs fix 2016-10-13 15:21:27 -07:00
Admin
23d43b0136 I got some more info from another source about the discovery responses
of a real hue. It sends three responses not just one. Also I have
updated the S/N UUID and the Hue Bridge ID creation as they are
different.
2016-10-10 13:23:29 -05:00
Admin
004276d3ea Changed handling of upnp rsponses from the upnp listener and the
huemulator udp calls to use the same response port.
2016-10-07 16:29:55 -05:00
Admin
e90e0f69ef Updated headers content type to be just "application/json" instead of
"application/json; charset utf-8".
2016-10-04 13:34:47 -05:00
Admin
7163a12384 Updating upnp responses to be more inline with what the hue hub does. To
do so, added the mac address as the id in the bridge id an, serial
number and uuid
2016-10-03 15:07:09 -05:00
Admin
bb65650e53 Added ip check if upnp config address is not available on the current
host. Added checks for content type and body for put and post requests.
2016-09-29 16:21:15 -05:00
Admin
7f7e96465b Switched default web port to be 80. This should resolve some issues with
first time configs and external apps. Changed upnp listener response to
previous style. Set default for link button presses status to true.
2016-09-29 09:25:47 -05:00
Admin
8ff7bc0120 Updated upnp response for M-SEARCH again. Updated devices to have
numbering more in line with how the hue bridge is done. Added special
generation of hue device unique id as the philips api spec shows.  Added
a renumber function to convert id's.
2016-09-28 16:18:53 -05:00
Admin
4da5f65d89 Updatting upnp responses 2016-09-26 17:03:30 -05:00
Admin
faa67827c6 Updated configuration items for hue responses. ADded call thru for hue
devices configured for state info. added dim content body.
2016-09-23 15:46:39 -05:00
Admin
bbce1f4235 Merge remote-tracking branch 'origin/master' into postv3.1fixes 2016-09-22 09:27:02 -05:00
BWS Systems
8575deadf1 Update readne.md for a missed version change
Changed line for download of ha-bridge jar
2016-09-22 09:02:28 -05:00
Admin
ad4015927c Add check for host is down exception on upnp send in listener and not
shutdown only a warning.
2016-09-21 16:33:57 -05:00
BWS Systems
f7df6951b0 Update README.md
Updated version to be the latest in the command examples
2016-09-20 08:29:01 -05:00
Admin
c20d046b30 Updated upnp response to be closer to the hue bridge v2 spec. Updated
handling for is on when bri is 0. Updated exec to check for empty call.
2016-09-15 15:41:59 -05:00
BWS Systems
20dedec8ab Merge pull request #158 from bwssytems/v3-updates
V3.1.0 updates
2016-09-01 11:11:40 -05:00
Admin
580c037b1e updated for unused imports 2016-09-01 11:07:34 -05:00
Admin
77cb064d60 Finished testing fixes and enhancements. 2016-09-01 11:03:11 -05:00
Admin
0e4319ea1d testing for changes: 3.0.0 not storing extended values (2.0.7 does),
update state not always working, Pi3 systemd not starting,
habridge.config created readable by everyone
2016-08-31 16:31:51 -05:00
BWS Systems
c61e623e23 Merge pull request #150 from tylerstraub/patch-1
Update README.md with caveats for using rc.local for starting on raspbian.
2016-08-02 15:45:54 -05:00
Tyler Straub
669483f686 Update README.md
Proposal to add some additional text under the Linux Automation section related to Issue #148. 

see: https://github.com/bwssytems/ha-bridge/issues/148
2016-08-02 12:20:02 -07:00
BWS Systems
5a59747bc9 Merge pull request #141 from bwssytems/hal-integration
Hal integration and other enhancement and fixes
2016-07-19 11:48:49 -05:00
Admin
53ec096c95 Updated version based on enhancement and bug implementations
Fixes #120
Fixes #122
Fixes #123
Fixes #135
Fixes #137
Fixes #138
Fixes #139
2016-07-19 11:42:07 -05:00
Admin
8ccb768391 Updated group 0 handling to be per hue api requirements. 2016-07-15 16:27:53 -05:00
Admin
9d84e2a180 fixed dim addition in bulk add
modified savef button to be active always in bridge settings
2016-07-12 16:26:53 -05:00
Admin
6a5fae583f Updating bridge control to activate save button on list updates. 2016-07-11 16:32:54 -05:00
Admin
8d15d0a0fb Tested api changes 2016-06-09 16:44:11 -05:00
Admin
d4b8b70a83 Added Hue Error Handling objects. Adding whitelsit control. 2016-06-08 16:41:46 -05:00
Admin
a276f97776 Testing api 1.10.0 2016-06-07 16:39:59 -05:00
Admin
9438b25538 Testing upnp response structures. 2016-06-06 16:42:46 -05:00
Admin
6c15ca2c3b Updated the Hue API v 1.11 and tested.
Testing the upnp changes, ongoing.
2016-06-03 16:25:00 -05:00
Admin
39782fa339 Updateing for API V 1.11 and Bridge V2 HUE handling. 2016-06-02 16:51:02 -05:00
Admin
d7e29e2ee5 Finished implementation of HAL calls to the API. Testing next. 2016-05-26 16:06:48 -05:00
Admin
6b4693eaaf Added Home control, Thermostat and other enhancements. 2016-05-25 16:09:29 -05:00
Admin
7f816b03d5 Fixed sizing of scrollable table to be dynamic. Added Appliances,
Theater and Custom devices to HAL interface. Added Select ALL feature
for bulk add screens.
2016-05-24 16:42:05 -05:00
Admin
ed3db4427b Updated view consistency. Fixed HAL bulk add 2016-05-23 16:45:47 -05:00
bwssystems
80ca8c3ca3 updated html pages to reference hal tab 2016-05-21 16:59:03 -05:00
bwssystems
e43473734e Finished draft HAL integration for lights type only 2016-05-21 16:49:12 -05:00
Admin
8a468b8352 First cut at HAL itegration 2016-05-20 16:13:48 -05:00
bwssystems
51ce10cfc7 Fixed build device button in Harmony Device helper tab.
Fixes #116
2016-05-15 12:25:28 -05:00
Admin
b5f7144c9c Update readme with a script construction. 2016-05-11 14:36:58 -05:00
Admin
86a931d383 added exec:// type for loops. updated button maptypeid naming.
Fixes #114
Fixes #115
2016-05-11 12:23:43 -05:00
Admin
3e890721c5 Fixed handling of dim and brigthen requests as they were not setting the
state appropriately for other systems. Fixed handling of data return
from an http call if the entity was empty.

Fixes #102
Fixes #107
2016-05-10 12:29:37 -05:00
Admin
62d1c64a3d Updated entitiy handling in HueMulator http call. 2016-05-06 08:30:37 -05:00
Admin
c025b186cd Updated a comment for the seb server port 2016-05-05 14:13:06 -05:00
Admin
e999c3a969 Update spelling in readme 2016-05-05 10:45:03 -05:00
Admin
351403e611 Fixed success parsing on http to return success. 2016-05-04 14:38:37 -05:00
Admin
c773477a43 Added delete dialog for confirmation. Fixed bug with text in
intensity.byte for vera device tab. Fixed issue with parsing replies for
http requests. Fixed Hue device bulk add. recommit


Fixes #100
Fixes #102
Fixes #104
Fixes #105
2016-05-04 11:45:51 -05:00
BWS Systems
5d1f0ce3b6 update versions in unit file 2016-05-02 15:41:01 -05:00
Admin
7e0fd6c21b Updated the register with hue function to not send a user name when
linking as this has been deprecated by Philips.

Fixes #99
2016-04-29 16:12:52 -05:00
BWS Systems
3bf52f5da0 Updated Readme
Added comment for Hue proxy and using the link button.
2016-04-29 12:54:08 -05:00
BWS Systems
bd856d8f9e Fixed spelling 2016-04-29 12:44:30 -05:00
Admin
73b2be752e Issue with http/https handling trying to token out line. Updated Readme
Fixes #96
Fixes #98
2016-04-29 12:27:15 -05:00
Admin
dda7a7a34a Merge remote-tracking branch 'origin/add-color-args' 2016-04-29 11:26:06 -05:00
Admin
f238e05533 Fixed bug where device state object was dropped from hue api device
response object. This caused devices to appear offline and invalid
response interpretation by the echo.

Fixes #93
2016-04-28 12:12:49 -05:00
Admin
aaaebd0c05 Merged add-color-args branch to master.
Fixes #72
Fixes #81
Fixes #82
Fixes #85
Fixes #87
Fixes #88
Fixes #89
Fixes #90
2016-04-27 14:42:52 -05:00
Admin
9a1924422e Updated Readme 2016-04-27 13:07:49 -05:00
Admin
e446c618ce MAde the generate buttons consistent in the UI. Updated the Readme. 2016-04-27 12:40:54 -05:00
Admin
60d35acff9 update readme for formatting 2016-04-27 09:11:11 -05:00
Admin
c9adab53a9 Updated selections in device add or editor and updated loggin for upnp
listener and updated the readme
2016-04-27 09:08:41 -05:00
Admin
72b6b2027b Finished updating UPNP handling with ports and errors and testing for
new features.

Updateding README
2016-04-26 16:49:26 -05:00
Admin
60239bad82 Finished testing and refactoring for udp/tcp and http 2016-04-26 10:12:36 -05:00
Admin
aecd589308 Finished implementation of headers and tested. Added calls for TCP
request with UDP and also added the value replacement for dimming, this
needs to be tested.
2016-04-25 16:48:59 -05:00
Admin
ee45cee8e3 Fixed error handling in for loops 2016-04-22 14:00:47 -05:00
Admin
21e5dfb338 added multiple command execution. Added commandline Exec. NEeds testing 2016-04-22 13:59:12 -05:00
Admin
b73a4cd666 Finished testing and updating HUE passthru handling 2016-04-22 10:58:54 -05:00
Admin
05418fdda1 Updated passthru after more testing 2016-04-21 15:41:06 -05:00
Admin
a717fd7c68 Add new classes. 2016-04-20 16:44:39 -05:00
Admin
8408d7350e More testing on HUE passthru. 2016-04-20 16:44:02 -05:00
Admin
3ba8f56db2 Completed Hue passthru 2016-04-18 16:41:15 -05:00
Admin
7a0946e3b7 Continue adding HUE pass thru. 2016-04-15 16:30:34 -05:00
Admin
50c9369d71 Added config override for server port. Updated Readme. Continued
enhancements to pass color to a real hue.
2016-04-14 17:12:06 -05:00
Admin
6c2a34f507 Updated code for collor passing. 2016-04-04 16:36:05 -05:00
Admin
ee2c105040 Started implementation 2016-04-01 16:33:55 -05:00
Admin
3ac83912f3 Fixed success return validation on calls and implemented validation
catching logic for URL issues.

Fixes #73
Fixes #75
2016-03-29 16:24:12 -05:00
Admin
e62fcf7765 Adding URL formatting for various characters and updating test for
success return.
2016-03-28 16:36:21 -05:00
Admin
9c3d95f177 Finished implementing and testing state change and bug fixes from
testing

Fixes #44
Fixes #60
Fixes #61
Fixes #63
Fixes #66
Fixes #69
2016-03-21 16:37:13 -05:00
Admin
926a7f50dc Finished adding dim versus on, utf-8 and track state and return on api
calls.
2016-03-18 16:38:18 -05:00
Admin
ad820a68c9 Updated C/F setting. Started to implement UTF-8 2016-03-16 17:13:03 -05:00
Admin
73f0f766f7 Added more logging and updated nest controller version to fix issues
with range temp setting in nest.
2016-03-09 16:45:21 -06:00
Admin
113ce0ca59 Updated logging with new nest module. 2016-03-08 16:40:32 -06:00
bwssystems
a5860417c1 Update null numberoflogmessages 2016-02-27 12:56:42 -06:00
bwssystems
48af3d84a2 Finished configurable logging view and control.
Fixes #51
Fixes #52
2016-02-27 12:48:19 -06:00
Admin
8ce0483e54 Adding log level control 2016-02-26 16:38:41 -06:00
Admin
1a2024d92b Tables in all views updated to scrollable/sortable. Refactored
controllers for views. debugging logging message return for messages
with invalid text for json parsing.
2016-02-25 16:42:35 -06:00
Admin
8198919a27 playing with scrollable table for log 2016-02-24 16:46:52 -06:00
Admin
614734a2aa Adding logs window. 2016-02-23 16:52:00 -06:00
Admin
58fccb1fa7 fixed about issue. Looking into log view and changed log impl to logback
within slf4j.
2016-02-22 16:38:57 -06:00
Admin
77d3084b01 Finished slider for testint items with percentage items. Many refactors
and fixes.

Fixes #43
Fixes #46
Fixes #47
Fixes #48
2016-02-19 16:34:04 -06:00
Admin
922bb54143 add js and css dialog files 2016-02-18 16:40:25 -06:00
Admin
8586bbd965 Found slider and popup dialog. 2016-02-18 16:39:41 -06:00
Admin
d64a028f30 removed slider.js and .css changing to a nicer slider 2016-02-18 08:49:49 -06:00
Admin
fff30d17d6 Finished updating settings editor and reinit control. Starting number
slider for input.
2016-02-17 16:41:34 -06:00
Admin
13fa5dea73 update ui components. 2016-02-16 16:53:01 -06:00
Admin
1d6a4c1432 Updated UI for configuration and added shutdown of harmony and nest
connections.
2016-02-16 16:52:12 -06:00
bwssystems
9cb275230e Updating editing for Settings UI. 2016-02-13 17:02:51 -06:00
Admin
c97ab2cd38 Fixed bulk add race conditions. added save capability into UI. UI
settings layout update.
2016-02-12 15:51:38 -06:00
Admin
7b45ca9438 added class backup handler to git 2016-02-11 16:52:08 -06:00
Admin
e6da9950d6 Added config settings page and refactored backup handling and bridge
settings and cotntrol. Next is to add the editing to the config screen.
2016-02-11 16:51:11 -06:00
Admin
20328b15d8 Added config file handling for the next step in config screens. 2016-02-10 16:47:58 -06:00
Admin
2ff73e5672 Added reinitialize and stop commands in prep for building the config
file handling.
2016-02-09 16:41:05 -06:00
Admin
cca9a6be78 Finished button sleep time addition. Updated documentation for
button.sleep and corrected the device DB line and updated ip list
directives for vera.address and harmony.address.

Fixes #39
Fixes #40
Fixes #42
2016-02-08 16:21:34 -06:00
Admin
5d5b68209a Updated test notifcation to a toast, added config button sleep time. 2016-02-05 16:10:47 -06:00
Admin
d2e906caa3 Fixed variable mis name in apps.js for olddevicename 2016-02-03 14:32:33 -06:00
Admin
a1708f2a88 Vera device and scene creation testing and added Copy capability to add
an already configured device with a different name.

Fixes #32
Fixes #38
2016-02-02 12:04:48 -06:00
Admin
49c3b85894 Fixed the vera device and scene build. 2016-02-01 16:34:04 -06:00
Admin
f9f5a3a878 Updated code in the html and javascript to select the coorect name for
the button press command. Fixed the calculation of celsius conversion
for setting nest temperatures.

Fixes #33
Fixes #34
2016-01-29 13:49:34 -06:00
Admin
2565183ee9 Finished implementation for multiple vera support. Tweaks to the UI.
Fixes #19
Fixes #21
Fixes #23
Fixes #26
Fixes #28
Fixes #30
Fixes #31
2016-01-28 16:39:20 -06:00
Admin
a6bb1ae3aa Fixed the harmony button to be a label and not the name. Implementation
of multi batch add completed.
2016-01-27 16:54:23 -06:00
Admin
4bc91be88b Added backup and restore for device db functionality. Testing multi
button press for harmony.
2016-01-26 16:47:16 -06:00
Admin
315fd31270 implemented multi button build for devices. more testing needed. fixed
tab display on first view.
2016-01-25 16:35:22 -06:00
Admin
09787fe08d Fixed undefined name error in nest setup and celsius conversion for the
nest thermostat.
2016-01-22 12:20:24 -06:00
Admin
37d346f558 Finished Nest implementation and default ip selection to not be
127.0.0.1

Fixes #12
Fixes #20
2016-01-21 16:44:24 -06:00
Admin
ac59398aa0 Testing nest functionality 2016-01-20 16:46:21 -06:00
Admin
87073435fc Updated code for IP address selection on startup fi none given. 2016-01-13 14:54:35 -06:00
Admin
8687f3482a Continuation of nest implementation 2016-01-12 16:34:05 -06:00
Admin
32a5f26ddd Added new object 2016-01-11 16:45:49 -06:00
Admin
c28f07d628 Continuation of nest implementation. 2016-01-11 16:45:02 -06:00
Admin
d3cc961dfb Initial updates for nest control 2016-01-08 16:14:42 -06:00
Admin
1b3d826f28 Fixed Readme format errors 2015-12-29 10:18:51 -06:00
Admin
c8f4d89a45 Update Readme for clarity on upnp.config.address usage. Also, updated
the 'ask alexa' section with more details.
2015-12-29 10:14:26 -06:00
Admin
2b335d6b9b Updated upnp listener logging. Fixed issue with HUE emulation for
configuration. HUE mobile app now connects.
2015-12-17 16:13:46 -06:00
Admin
b27bb5eef8 Updating hue emulation repsonses using hue android app. fixed issues in
returning the hue config and for requesting the mac address.
2015-12-16 16:41:08 -06:00
Admin
3c54ccd56d Finished implementation of generic UDP handling. This will support the
LimitlessLED Bridge. Updated Readme for UDP and more on configuration
setup. Fixed issue with HueMulator handliong due to devices without
mapTypes.

Fixes #6.
2015-12-07 16:34:37 -06:00
Admin
cf772334c4 Start adding UDP handling. 2015-12-04 16:21:11 -06:00
Admin
5a843f7569 Fixed handling when no harmony hub is given. dev.mode was updated as
well.

This closes #14.
2015-12-03 14:36:23 -06:00
Admin
2a52783bb1 Final release of multiple harmony hubs and fixes for GUI.
This closes #5 and #13
2015-12-02 15:42:09 -06:00
Admin
195f1854ec Remove Current Activity object as it is not needed. 2015-12-02 15:35:35 -06:00
Admin
2e5596a6e4 Pre-release of multiple Harmony Hub Support 2015-12-02 15:34:30 -06:00
Admin
9fc13c6c45 Conitinuation of multiple harmony hub implementation. 2015-12-01 16:40:29 -06:00
Admin
4b4d4e36c7 Update filtering capability. Still errors in html/angular. 2015-11-30 16:43:14 -06:00
Admin
1e7bdc560b Start adding components to handle multiple harmony hubs. 2015-11-24 14:27:21 -06:00
Admin
aff0f8d64c Removed the doling out of hue devices on a get call. 2015-11-23 15:17:17 -06:00
Admin
7a812d6e6b Updated VeraInfo to use default http client. Finished work on doling out
devices in the resource manager. Caveat, this did not work well with the
echo. Next version will roll this back.
2015-11-23 15:14:38 -06:00
Admin
26f2105801 First try at doling out lights when requested. 2015-11-20 16:40:43 -06:00
Admin
feef345a3b Update for start of virtualize multiple respnses 2015-11-19 16:43:30 -06:00
Admin
314ae58ebd This closes #8, closes #9, closes #10 and closes #11.
Finished device, scene and activity tracking, updated upnp handling,
updated HUE API config handling and test on and off calls.
2015-11-18 16:31:11 -06:00
Admin
d8b6232ac1 First draft and start of tracking for scenes, devices, activities.
Implemented Harmony activity list so far.
2015-11-17 16:40:24 -06:00
Admin
41e22ee64d Refactor TestUrl call in apps.js. Updated UI to only have test buttons
after a device is added.
2015-11-16 16:38:29 -06:00
Admin
be2fbcd4cb Update script for test on and off. Incorrect body sent for off commands
error return was interpretted incorrectly.
2015-11-16 14:53:42 -06:00
Admin
14e7f37522 Updated HUE API for generic config requests. Updated config parameters
to be more real.
2015-11-13 16:17:49 -06:00
Admin
e3f5946c9d Updated handling to be CORS compatible. 2015-11-13 11:40:13 -06:00
Admin
12eab16f21 updated Readme and enhance emulator logging code. 2015-11-13 11:26:19 -06:00
Admin
3df68047a9 Updated handling of dimming/brightness request. This could be buggy if
the echo changed to not send an on command when changing dimming. The
HUE state change API is not consistent for brightness handling.
2015-11-13 10:57:18 -06:00
Admin
0dd652f82a Updated device return for HUE emulation based on new information of LUX
bulb handling in the API from the Philips dev info.
2015-11-12 16:31:01 -06:00
Admin
53af1a4dfd Final tweaks to the order of sections in the readme 2015-11-11 15:24:17 -06:00
Admin
816a0025b1 Finished updating upnp for the readme 2015-11-11 15:16:42 -06:00
Admin
405562809a finished adding harmony items to readme 2015-11-11 14:39:13 -06:00
Admin
4c87c6fce8 Update readme some more. 2015-11-11 13:29:06 -06:00
Admin
2fd0f7748b Some more readme updates. 2015-11-11 13:19:50 -06:00
Admin
ed5f3b4b3c More readme updates. 2015-11-11 12:43:38 -06:00
Admin
aad09b7527 More updates to readme 2015-11-11 11:28:41 -06:00
Admin
0ae66da085 Continuation of readme updating. 2015-11-11 11:26:42 -06:00
Admin
d344b764da Updated HUE REst descriptions. 2015-11-10 16:48:30 -06:00
Admin
c85b67fb9f Next update for HUE API description 2015-11-10 16:25:16 -06:00
Admin
a23d662444 Adding supported HUE API calls descriptions. 2015-11-10 13:57:01 -06:00
Admin
4b98f799c2 Updated vera returns for scened and devices. 2015-11-10 11:51:21 -06:00
Admin
acba2b5cae Another config rest api clarification. 2015-11-10 11:45:40 -06:00
Admin
4dc818296a Updated Configuration REST API commands. 2015-11-10 11:40:41 -06:00
Admin
40123ed858 Updating document for REST usage. 2015-11-09 16:47:38 -06:00
Admin
718ba5a5c2 Updated build in pom.xml to include classes that were removed for
minimize. This required a dummy clas setup as well in the Harmony
Server.
2015-11-04 11:00:46 -06:00
Admin
2e6944d840 Missed new java class in release. 2015-11-03 15:26:56 -06:00
Admin
e29f12905d Finalized handling for barmony. Added a version call for use in api.
Updated version handling in the pom.xml and code. Removed
vtwocompatibility as it should not be used.
2015-11-03 15:16:01 -06:00
Admin
408b79d5d8 Finished coding need to test. 2015-11-02 16:46:48 -06:00
Admin
bf5ad2e23c Adding button selection control for harmony. in progress.... 2015-10-30 16:30:30 -05:00
Admin
59f1db285d Updating pom configuration for adding harmony control. 2015-10-29 15:59:00 -05:00
Admin
203ed0b5d3 Updated pom to use new harmony client version. 2015-10-28 16:42:23 -05:00
Admin
b443d16a11 First Beta of Harmony configuration for testing. 2015-10-27 16:39:13 -05:00
Admin
295b1e1a30 Adding Harmony Hub Control directly into the Bridge. First iteration
updates the ui harness. Also, updated settings in the configuration
page.
2015-10-26 16:35:51 -05:00
Admin
c872f3543d Updated Hue Device emulation to be Lux bulbs as most things this bridge
will emulate will not be color friendly....
2015-10-23 16:16:18 -05:00
Admin
23f2d2716d Update Readme.md, typo with bracket placement. 2015-10-13 16:36:53 -05:00
Admin
7c1d6e40b8 Updated math to use Math.round to help get better values. Updated code
for determining if Vera is available so as to not show those screens.
Updated file handling as there were issues due to no checks for file
handling, this will improve for windows.
2015-10-13 16:30:45 -05:00
Admin
c5fbd5d1f0 Updated math variable execution to use net.java.dev.eval package. Safer
and more robust than using JavaScript Engine Eval. Also, added checks if
a default vera address is uses , "1.1.1.1", that we ignore the vera
helpers to not throw errors.
2015-10-12 16:34:21 -05:00
Admin
aebde7ee48 Updated the bridge to handle a new dimming and value control context,
$intensity.mat{(X*1)} for custom calculations. Also, added helpers for
generating URLS on the value contexts.
2015-10-09 14:28:05 -05:00
Admin
c8fb93eeb6 Add testing and error pop ups where needed. 2015-10-05 16:19:09 -05:00
Admin
1602ed004a Updated test methods for special PUT/POST calls. Cleaned up device.db
file write to not keep junk around.
2015-10-05 15:50:46 -05:00
Admin
7514e36edb Updated bridge to be robust on put/post calls and testing. HAd to add a
body for off types.
2015-10-02 16:35:17 -05:00
Admin
2789d8c180 Cleanup code for variable usage. Remvoed dummy test classes. 2015-09-29 08:48:27 -05:00
Admin
af1777aeb3 Updated UI header menu to not switch page to Home everytime. 2015-09-18 10:12:15 -05:00
Admin
fc2d587e1a update project synchronization settings. 2015-09-14 09:10:21 -05:00
Admin
416b4d3fda Updated logging for upnp listener and description service. Added method
in huemulator for "/*" which was present in the armzilla version for the
Harmony Hub. Updated the readme.
2015-09-11 16:01:38 -05:00
Admin
9666273840 Fixed issue with not adding device by checking for an httpVerb always. 2015-09-10 16:38:36 -05:00
Admin
774bc8a36b Update upnp listener module to even be less strict in normal mode as I
have found instances of devices that do not follow the pure upnp
standard. Updated README as it was wrong for the upnp.device.db...it had
an s after device. updated the code to return back to config after
adding or updating a device.
2015-09-10 16:08:29 -05:00
Admin
74d4548beb Fixed saving of updated device context when special commands are used.
Updated create user in hue emulator to handle no device tpye or usernmae
given. Fixed parameter settings as they were incorrect. Updated config
display to show new parameters.
2015-09-03 11:44:26 -05:00
Admin
eecf0f9875 ADded more for editing special get/put/post and started addimg more to
bridge settings in UI.
2015-09-02 16:42:40 -05:00
Admin
ed96b5ad81 another readme update 2015-09-02 13:27:30 -05:00
Admin
9f7d3ea331 update Readme 2015-09-02 13:08:27 -05:00
Admin
68de92bb74 Updtated bridge to be able to emulate the armzilla upnp responses.
Adding optional field edits - in progress.
2015-09-02 13:06:59 -05:00
Admin
392a46c3d8 uodate readme again 2015-09-01 14:08:49 -05:00
Admin
568569248a update readme 2015-09-01 14:04:40 -05:00
Admin
d61d10b5b6 Added settings for strict upnp mode of checking ig it is a request for a
hue or not before responding.
Updated Readme
2015-09-01 13:59:35 -05:00
Admin
7294dbf175 Added the Post Put capabilities for the http eecution. cleaned up css
for list ordering. Cleaned up device management calls.
2015-09-01 13:40:53 -05:00
Admin
6c99358f95 Updated upnp response listener code. Updated hue api. Updated input
areas for urls to be textareas to handle long strings. Added sorting to
all lists.
2015-08-31 16:44:38 -05:00
Admin
eee0394f20 updated upnp discovery calls to be more like the hue. 2015-08-28 15:56:36 -05:00
Admin
d87f3bc541 Fix mapping for scene room mapping as well. doh! 2015-08-27 11:47:00 -05:00
Admin
e38374f749 Fixes bwssytems/ha-bridge#1 2015-08-27 10:04:08 -05:00
Admin
937fb5d32d Final fix for resolving room mapping. Also applied this fix to category
mappign in SDATA as well.
2015-08-27 08:44:09 -05:00
Admin
bd60d63d0f Fix issue with devices with no rooms or categories. 2015-08-26 16:52:44 -05:00
Admin
439b081bd5 Updated vera info class logging calls to be debug only. 2015-08-26 11:48:51 -05:00
Admin
41f68f58b0 Updated Hue description response emulation. Updated the management of
devices in the map. Updated logging information to be less in normal
mode.
2015-08-26 11:26:49 -05:00
Admin
fa15cf3952 update upnp description.xml response to come from root instead of
/upnp.....
2015-08-25 16:42:30 -05:00
Admin
3ea7f2903f commit for any latent changes. 2015-08-25 10:19:02 -05:00
Admin
7746938c62 Updated upnp discovery logic to only respond to the echo request for a
upnp device and updated hue response when getting lights to be more
accurate with the hue.
2015-08-21 16:06:06 -05:00
Admin
96074628fb Merge remote-tracking branch 'origin/master' 2015-08-21 08:56:05 -05:00
Admin
9dc8d8f8bc Updated call for hue user create/addition call. Updated upnp response
variables.
2015-08-20 16:49:12 -05:00
Admin
8de39a8bee Work on naming scenes 2015-08-19 16:43:46 -05:00
Admin
626f0641cc Changed navigation to use nav tab pills look. 2015-08-19 15:17:24 -05:00
Admin
020da99e1c Update .gitignore attributes for eclipse 2015-08-18 14:18:24 -05:00
Admin
014911a568 remove eclipse files from git
Do not need eclipse config files.
2015-08-18 11:46:40 -05:00
Admin
77ec373381 Updated pom.xml to include log4j-over-slf4j for http client to work
properly and make sure the minimize jar function does not remove it.
Also removed call to VeraInfo in the main program as it is not needed.
2015-08-18 11:16:00 -05:00
Admin
8d9357cf9e Finished normalizing vera devices and scenes.
Separated device add and editng pages.
2015-08-17 16:10:38 -05:00
Admin
b9076d1f2e Updating how controllers work together with shared data. 2015-08-14 16:19:51 -05:00
Admin
4a80ce4230 Add view setup for angular and pages. 2015-08-13 16:29:01 -05:00
Admin
29e7980939 Update readme for system name change and new properties. 2015-08-13 08:53:48 -05:00
Admin
c6132f2b03 Adding new classes to handle vera and updated distribution to include js
and css files in the jar.
2015-08-13 08:46:31 -05:00
Admin
f8bebe38c0 Update application from amazon-echo-bridge-compact to ha-bridge.
Try to remove most code naming away from amazon.

Fix package names being incorrect.

Added Vera query functionality, needs some fine tuning.
2015-08-12 16:44:48 -05:00
Admin
3911f802ee Updated the ignore for target dir to load the jar. 2015-08-07 15:00:59 -05:00
Admin
e52e0150fc Updated version for release 2015-08-05 16:28:12 -05:00
Admin
561d3cfed5 Updated persistence 2015-08-05 16:21:20 -05:00
Admin
cf92620617 Added persistence for configuration 2015-08-04 16:48:10 -05:00
Admin
5bdbca72c8 updated upnp url contents 2015-07-29 11:53:20 -05:00
Admin
c31d0bc709 updated command lines that were incorrect 2015-07-29 11:02:14 -05:00
173 changed files with 16583 additions and 1217 deletions

2
.gitattributes vendored
View File

@@ -1,5 +1,5 @@
# Auto detect text files and perform LF normalization
* text=auto
text=auto
# Custom for Visual Studio
*.cs diff=csharp

5
.gitignore vendored
View File

@@ -1,4 +1,3 @@
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
@@ -9,3 +8,7 @@ buildNumber.properties
*.iml
data
.idea
/target/
/.settings/
/start.bat
/.classpath

23
.project Normal file
View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ha-bridge</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

View File

@@ -1,5 +0,0 @@
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

View File

@@ -1,4 +0,0 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

1094
README.md

File diff suppressed because one or more lines are too long

144
kodivolume.md Normal file
View File

@@ -0,0 +1,144 @@
# Kodi volume control using dim commands & JSON-RPC
You can use HA Bridge to adjust the Kodi software volume output. This allows you to use dim commands and set the volume level with percentage values.
### What is JSON-RPC?
The short answer is JSON-RPC an interface to communicate with Kodi. [See the official Kodi Wiki for more info](http://kodi.wiki/view/JSON-RPC_API)
### Setup Kodi to allow control through JSON-RPC
In Kodi navigate to Settings/Services/Control ([screenshot](http://kodi.wiki/view/Settings/Services/Control))
Turn **ON** the following:
- Allow control of Kodi via HTTP
- Allow remote control from applications on this system
- Allow remote control from applications on other systems
Change the **username** to something unique and set a strong **password**.
Make a note of the **PORT**
### Adding the device to HA Bridge
Access the HA Bridge Configuration in your browser and open the **Manual Add** tab.
#### Name
Give the device a unique name that doesnt include **“volume”** as it will cause conflicts with the Echos built in volume controls. A device name of **“cody sound”** works well.
#### Device type
Select **TCP** in the dropdown
### URLs
This section might seem a little long winded and if you know what you are doing then feel free to jump ahead.
#### Building the URL
We need to log into the Kodi web server without having to fill in the popup each time. You can do this by putting the username and password in the URL. It is not a good idea to do this on other websites as it does put your password in clear view, but for your local network it is fine.
Use the example below replacing the relevant sections with the details that you defined in the Kodi settings screen in the first step. Replacing the IP with the address of the machine that Kodi is running on.
```
http://KODI_USERNAME:KODI_PASSWORD@192.168.1.123:8080/jsonrpc
```
##### Testing the URL in a browser
Before you continue, open your custom URL in a browser (making sure Kodi is running). If all is working as it should you will see a big page of JSON that starts with:
``` json
{
"description": "JSON-RPC API of XBMC",
"id": "http://xbmc.org/jsonrpc/ServiceDescription.json",
```
If you dont see something that looks like the code above, then go back and double check your settings.
#### The JSON request
The URL is what connects you to Kodi, JSON is what is used to communicate with it. The JSON that is used to set the volume level is:
``` json
{"jsonrpc":"2.0","method":"Application.SetVolume","params":{"volume":100},"id":1}
```
##### Joining the URL and JSON
Join the two together by adding `?request=` to the end of the URL and then add the JSON to the end of the request. You will end up with something like:
```
http://KODI_USERNAME:KODI_PASSWORD@192.168.1.123:8080/jsonrpc?request={"jsonrpc":"2.0","method":"Application.SetVolume","params":{"volume":100},"id":1}
```
### Testing the request in a browser
Go ahead and test the combined URL/JSON in a browser changing **100** to whatever level you want to set. Kodi should adjust the volume accordingly, try a few different levels to be sure it is working correctly.
The browser will reformat the URL each time you press return so dont build the URL in the browser bar without making a copy first.
Ideally build the URL in a text document which you can easily edit and then copy/paste each time.
### Prepare the three URLs
You want to end up with three full URLs in your text file, one for each of the commands.
**ON**
```
http://KODI_USERNAME:KODI_PASSWORD@192.168.1.123:8080/jsonrpc?request={"jsonrpc":"2.0","method":"Application.SetVolume","params":{"volume":100},"id":1}
```
**DIM**
```
http://KODI_USERNAME:KODI_PASSWORD@192.168.1.123:8080/jsonrpc?request={"jsonrpc":"2.0","method":"Application.SetVolume","params":{"volume":45},"id":1}
```
**OFF**
```
http://KODI_USERNAME:KODI_PASSWORD@192.168.1.123:8080/jsonrpc?request={"jsonrpc":"2.0","method":"Application.SetVolume","params":{"volume":0},"id":1}
```
### Test the three URLS
You should now be able to control the volume of Kodi using the structured URL you built above in a browser.
If you cant get it to work in a browser then you wont be able to get it to work in HA Bridge.
### Manually adding the device
Add a new manual device and give it a name e.g. “Cody Sound”
Set `Device type` to `Custom`
Use the same URL for all three (ON, OFF, DIM)
```
http://KODI_USERNAME:KODI_PASSWORD@192.168.1.123:8080/jsonrpc?request=
```
* `HTTP Verb` to `POST`
* `Content type` to `application/json`
**Content body On**
```json
{"jsonrpc":"2.0","method":"Application.SetVolume","params":{"volume":100},"id":1}
```
**Content body Dim**
```json
{"jsonrpc":"2.0","method":"Application.SetVolume","params":{"volume":${intensity.percent}},"id":1}
```
**Content body Off**
```json
{"jsonrpc":"2.0","method":"Application.SetVolume","params":{"volume":0},"id":1}
```
### HA Bridge
Save and test the button in the Bridge Devices tab and hopefully it should turn the volume up in Kodi.
### Controlling the Device
You can use the commands as listed in the [README](https://github.com/bwssytems/ha-bridge#ask-alexa)
“Set Cody Sound to 50 percent”
“Cody Sound to 70 percent”
Remembering that “Turn on Cody Sound” will set the volume to 100%, and “Turn off Cody Sound” will mute.

236
pom.xml
View File

@@ -3,84 +3,218 @@
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>
<groupId>com.bwssystems.HABridge</groupId>
<artifactId>ha-bridge</artifactId>
<version>4.0.2</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>
<name>HA Bridge</name>
<description>Emulates a Philips Hue bridge to allow the Amazon Echo to hook up to other HA systems, i.e. Vera or Harmony Hub or Nest, 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>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>Eclipse Paho Repo</id>
<url>https://repo.eclipse.org/content/repositories/paho-releases/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.bwssytems</groupId>
<artifactId>harmony-java-client</artifactId>
<version>1.1.1</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.bwssytems</groupId>
<artifactId>nest-controller</artifactId>
<version>1.0.13</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.2</version>
<version>2.3</version>
<exclusions>
<exclusion>
<artifactId>slf4j-simple</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.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>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>net.java.dev.eval</groupId>
<artifactId>eval</artifactId>
<version>0.5</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.0-beta4</version>
</dependency>
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-core</artifactId>
<version>4.0.7</version>
</dependency>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</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>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>version.properties</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>version.properties</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>META-INF/*.txt</exclude>
<exclude>META-INF/maven/**</exclude>
<exclude>about_files/**</exclude>
</excludes>
</filter>
<filter>
<artifact>*:*</artifact>
</filter>
<filter>
<artifact>org.slf4j:slf4j-api</artifact>
<includes>
<include>**</include>
</includes>
</filter>
<filter>
<artifact>commons-logging:commons-logging</artifact>
<includes>
<include>**</include>
</includes>
</filter>
<filter>
<artifact>xpp3:xpp3</artifact>
<includes>
<include>**</include>
</includes>
</filter>
<filter>
<artifact>org.igniterealtime.smack:*</artifact>
<includes>
<include>**</include>
</includes>
</filter>
<filter>
<artifact>com.github.bwssytems:harmony-java-client</artifact>
<includes>
<include>**</include>
</includes>
</filter>
<filter>
<artifact>org.eclipse.paho:org.eclipse.paho.client.mqttv3</artifact>
<includes>
<include>**</include>
</includes>
</filter>
</filters>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.bwssystems.HABridge.HABridge</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,25 @@
package com.bwssystems.HABridge;
public class BridgeControlDescriptor {
private boolean reinit;
private boolean stop;
public BridgeControlDescriptor() {
super();
this.reinit = false;
this.stop = false;
}
public boolean isReinit() {
return reinit;
}
public void setReinit(boolean reinit) {
this.reinit = reinit;
}
public boolean isStop() {
return stop;
}
public void setStop(boolean stop) {
this.stop = stop;
}
}

View File

@@ -0,0 +1,286 @@
package com.bwssystems.HABridge;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import org.apache.http.conn.util.InetAddressUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.util.BackupHandler;
import com.bwssystems.HABridge.util.JsonTransformer;
import com.google.gson.Gson;
public class BridgeSettings extends BackupHandler {
private static final Logger log = LoggerFactory.getLogger(BridgeSettings.class);
private BridgeSettingsDescriptor theBridgeSettings;
private BridgeControlDescriptor bridgeControl;
public BridgeSettings() {
super();
bridgeControl = new BridgeControlDescriptor();
theBridgeSettings = new BridgeSettingsDescriptor();
String ipV6Stack = System.getProperty("ipV6Stack");
if(ipV6Stack == null || !ipV6Stack.equalsIgnoreCase("true")) {
System.setProperty("java.net.preferIPv4Stack" , "true");
}
}
public BridgeControlDescriptor getBridgeControl() {
return bridgeControl;
}
public BridgeSettingsDescriptor getBridgeSettingsDescriptor() {
return theBridgeSettings;
}
public void buildSettings() {
String addressString = null;
String theVeraAddress = null;
String theHarmonyAddress = null;
String configFileProperty = System.getProperty("config.file");
if(configFileProperty == null) {
Path filePath = Paths.get(Configuration.CONFIG_FILE);
if(Files.exists(filePath) && Files.isReadable(filePath))
configFileProperty = Configuration.CONFIG_FILE;
}
String serverPortOverride = System.getProperty("server.port");
String serverIpOverride = System.getProperty("server.ip");
if(configFileProperty != null)
{
log.info("reading from config file: " + configFileProperty);
theBridgeSettings.setConfigfile(configFileProperty);
_loadConfig();
}
else
{
log.info("reading from system properties");
theBridgeSettings.setNumberoflogmessages(Configuration.NUMBER_OF_LOG_MESSAGES);
theBridgeSettings.setFarenheit(true);
theBridgeSettings.setConfigfile(Configuration.CONFIG_FILE);
theBridgeSettings.setServerPort(System.getProperty("server.port", Configuration.DEFAULT_WEB_PORT));
theBridgeSettings.setUpnpConfigAddress(System.getProperty("upnp.config.address"));
theBridgeSettings.setUpnpDeviceDb(System.getProperty("upnp.device.db"));
theBridgeSettings.setUpnpResponsePort(System.getProperty("upnp.response.port", Configuration.UPNP_RESPONSE_PORT));
theVeraAddress = System.getProperty("vera.address");
IpList theVeraList = null;
if(theVeraAddress != null) {
try {
theVeraList = new Gson().fromJson(theVeraAddress, IpList.class);
} catch (Exception e) {
try {
theVeraList = new Gson().fromJson("{devices:[{name:default,ip:" + theVeraAddress + "}]}", IpList.class);
} catch (Exception et) {
log.error("Cannot parse vera.address, not set with message: " + e.getMessage(), e);
theVeraList = null;
}
}
}
theBridgeSettings.setVeraAddress(theVeraList);
theHarmonyAddress = System.getProperty("harmony.address");
IpList theHarmonyList = null;
if(theHarmonyAddress != null) {
try {
theHarmonyList = new Gson().fromJson(theHarmonyAddress, IpList.class);
} catch (Exception e) {
try {
theHarmonyList = new Gson().fromJson("{devices:[{name:default,ip:" + theHarmonyAddress + "}]}", IpList.class);
} catch (Exception et) {
log.error("Cannot parse harmony.address, not set with message: " + e.getMessage(), e);
theHarmonyList = null;
}
}
}
theBridgeSettings.setHarmonyAddress(theHarmonyList);
theBridgeSettings.setUpnpStrict(Boolean.parseBoolean(System.getProperty("upnp.strict", "true")));
theBridgeSettings.setTraceupnp(Boolean.parseBoolean(System.getProperty("trace.upnp", "false")));
theBridgeSettings.setButtonsleep(Integer.parseInt(System.getProperty("button.sleep", Configuration.DEFAULT_BUTTON_SLEEP)));
theBridgeSettings.setNestuser(System.getProperty("nest.user"));
theBridgeSettings.setNestpwd(System.getProperty("nest.pwd"));
}
if(theBridgeSettings.getUpnpConfigAddress() == null || theBridgeSettings.getUpnpConfigAddress().equals("")) {
addressString = checkIpAddress(null, true);
if(addressString != null) {
theBridgeSettings.setUpnpConfigAddress(addressString);
log.info("Adding " + addressString + " as our default upnp config address.");
}
else
log.error("Cannot get ip address of this host.");
}
else {
addressString = checkIpAddress(theBridgeSettings.getUpnpConfigAddress(), false);
if(addressString == null)
log.warn("The upnp config address, " + theBridgeSettings.getUpnpConfigAddress() + ", does not match any known IP's on this host.");
}
if(theBridgeSettings.getUpnpResponsePort() == null)
theBridgeSettings.setUpnpResponsePort(Configuration.UPNP_RESPONSE_PORT);
if(theBridgeSettings.getServerPort() == null)
theBridgeSettings.setServerPort(Configuration.DEFAULT_WEB_PORT);
if(theBridgeSettings.getUpnpDeviceDb() == null)
theBridgeSettings.setUpnpDeviceDb(Configuration.DEVICE_DB_DIRECTORY);
if(theBridgeSettings.getNumberoflogmessages() == null)
theBridgeSettings.setNumberoflogmessages(new Integer(Configuration.NUMBER_OF_LOG_MESSAGES));
if(theBridgeSettings.getNumberoflogmessages() <= 0)
theBridgeSettings.setNumberoflogmessages(new Integer(Configuration.NUMBER_OF_LOG_MESSAGES));
if(theBridgeSettings.getButtonsleep() == null)
theBridgeSettings.setButtonsleep(Integer.parseInt(Configuration.DEFAULT_BUTTON_SLEEP));
if(theBridgeSettings.getButtonsleep() < 0)
theBridgeSettings.setButtonsleep(Integer.parseInt(Configuration.DEFAULT_BUTTON_SLEEP));
theBridgeSettings.setVeraconfigured(theBridgeSettings.isValidVera());
theBridgeSettings.setHarmonyconfigured(theBridgeSettings.isValidHarmony());
theBridgeSettings.setNestConfigured(theBridgeSettings.isValidNest());
theBridgeSettings.setHueconfigured(theBridgeSettings.isValidHue());
theBridgeSettings.setHalconfigured(theBridgeSettings.isValidHal());
theBridgeSettings.setMqttconfigured(theBridgeSettings.isValidMQTT());
theBridgeSettings.setHassconfigured(theBridgeSettings.isValidHass());
if(serverPortOverride != null)
theBridgeSettings.setServerPort(serverPortOverride);
if(serverIpOverride != null)
theBridgeSettings.setWebaddress(serverIpOverride);
setupParams(Paths.get(theBridgeSettings.getConfigfile()), ".cfgbk", "habridge.config-");
}
public void loadConfig() {
if(theBridgeSettings.getConfigfile() != null)
_loadConfig();
}
private void _loadConfig() {
Path configPath = Paths.get(theBridgeSettings.getConfigfile());
_loadConfig(configPath);
}
private void _loadConfig(Path aPath) {
String jsonContent = configReader(aPath);
if(jsonContent == null)
return;
try {
theBridgeSettings = new Gson().fromJson(jsonContent, BridgeSettingsDescriptor.class);
} catch (Exception e) {
log.warn("Issue loading values from file: " + aPath.toUri().toString() + ", Gson convert failed.");
theBridgeSettings = new BridgeSettingsDescriptor();
theBridgeSettings.setConfigfile(aPath.toString());
}
}
public void save(BridgeSettingsDescriptor newBridgeSettings) {
log.debug("Save HA Bridge settings.");
Path configPath = Paths.get(theBridgeSettings.getConfigfile());
JsonTransformer aRenderer = new JsonTransformer();
String jsonValue = aRenderer.render(newBridgeSettings);
configWriter(jsonValue, configPath);
_loadConfig(configPath);
}
private void configWriter(String content, Path filePath) {
if(Files.exists(filePath) && !Files.isWritable(filePath)){
log.error("Error file is not writable: " + filePath);
return;
}
if(Files.notExists(filePath.getParent())) {
try {
Files.createDirectories(filePath.getParent());
} catch (IOException e) {
log.error("Error creating the directory: " + filePath + " message: " + e.getMessage(), e);
}
}
try {
Path target = null;
if(Files.exists(filePath)) {
target = FileSystems.getDefault().getPath(filePath.getParent().toString(), "habridge.config.old");
Files.move(filePath, target);
}
Files.write(filePath, content.getBytes(), StandardOpenOption.CREATE);
// set attributes to be for user only
// using PosixFilePermission to set file permissions
Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
// add owners permission
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_WRITE);
try {
Files.setPosixFilePermissions(filePath, perms);
} catch(UnsupportedOperationException e) {
log.info("Cannot set permissions for config file on this system as it is not supported. Continuing");
}
if(target != null)
Files.delete(target);
} catch (IOException e) {
log.error("Error writing the file: " + filePath + " message: " + e.getMessage(), e);
}
}
private String configReader(Path filePath) {
String content = null;
if(Files.notExists(filePath) || !Files.isReadable(filePath)){
log.warn("Error reading the file: " + filePath + " - Does not exist or is not readable. continuing...");
return null;
}
try {
content = new String(Files.readAllBytes(filePath));
} catch (IOException e) {
log.error("Error reading the file: " + filePath + " message: " + e.getMessage(), e);
}
return content;
}
private String checkIpAddress(String ipAddress, boolean checkForLocalhost) {
Enumeration<NetworkInterface> ifs = null;
try {
ifs = NetworkInterface.getNetworkInterfaces();
} catch(SocketException e) {
log.error("checkIpAddress cannot get ip address of this host, Exiting with message: " + e.getMessage(), e);
return null;
}
String addressString = null;
InetAddress address = null;
while (ifs.hasMoreElements() && addressString == null) {
NetworkInterface xface = ifs.nextElement();
Enumeration<InetAddress> addrs = xface.getInetAddresses();
String name = xface.getName();
int IPsPerNic = 0;
while (addrs.hasMoreElements() && IPsPerNic == 0) {
address = addrs.nextElement();
if (InetAddressUtils.isIPv4Address(address.getHostAddress())) {
log.debug(name + " ... has IPV4 addr " + address);
if(checkForLocalhost && (!name.equalsIgnoreCase(Configuration.LOOP_BACK_INTERFACE) || !address.getHostAddress().equalsIgnoreCase(Configuration.LOOP_BACK_ADDRESS))) {
IPsPerNic++;
addressString = address.getHostAddress();
log.debug("checkIpAddress found " + addressString + " from interface " + name);
}
else if(ipAddress != null && ipAddress.equalsIgnoreCase(address.getHostAddress())){
addressString = ipAddress;
IPsPerNic++;
}
}
}
}
return addressString;
}
}

View File

@@ -0,0 +1,309 @@
package com.bwssystems.HABridge;
import java.util.List;
import java.util.Map;
import com.bwssystems.HABridge.api.hue.HueConstants;
import com.bwssystems.HABridge.api.hue.WhitelistEntry;
public class BridgeSettingsDescriptor {
private String upnpconfigaddress;
private Integer serverport;
private Integer upnpresponseport;
private String upnpdevicedb;
private IpList veraaddress;
private IpList harmonyaddress;
private Integer buttonsleep;
private boolean upnpstrict;
private boolean traceupnp;
private String nestuser;
private String nestpwd;
private boolean veraconfigured;
private boolean harmonyconfigured;
private boolean nestconfigured;
private boolean farenheit;
private String configfile;
private Integer numberoflogmessages;
private IpList hueaddress;
private boolean hueconfigured;
private IpList haladdress;
private String haltoken;
private boolean halconfigured;
private Map<String, WhitelistEntry> whitelist;
private boolean settingsChanged;
private String myechourl;
private String webaddress;
private IpList mqttaddress;
private boolean mqttconfigured;
private IpList hassaddress;
private boolean hassconfigured;
private String hubversion;
public BridgeSettingsDescriptor() {
super();
this.upnpstrict = true;
this.traceupnp = false;
this.nestconfigured = false;
this.veraconfigured = false;
this.harmonyconfigured = false;
this.hueconfigured = false;
this.halconfigured = false;
this.mqttconfigured = false;
this.hassconfigured = false;
this.farenheit = true;
this.whitelist = null;
this.settingsChanged = false;
this.myechourl = "echo.amazon.com/#cards";
this.webaddress = "0.0.0.0";
this.hubversion = HueConstants.HUB_VERSION;
}
public String getUpnpConfigAddress() {
return upnpconfigaddress;
}
public void setUpnpConfigAddress(String upnpConfigAddress) {
this.upnpconfigaddress = upnpConfigAddress;
}
public Integer getServerPort() {
return serverport;
}
public void setServerPort(Integer serverPort) {
this.serverport = serverPort;
}
public void setServerPort(String serverPort) {
this.serverport = Integer.valueOf(serverPort);
}
public Integer getUpnpResponsePort() {
return upnpresponseport;
}
public void setUpnpResponsePort(Integer upnpResponsePort) {
this.upnpresponseport = upnpResponsePort;
}
public void setUpnpResponsePort(String upnpResponsePort) {
this.upnpresponseport = Integer.valueOf(upnpResponsePort);
}
public String getUpnpDeviceDb() {
return upnpdevicedb;
}
public void setUpnpDeviceDb(String upnpDeviceDb) {
this.upnpdevicedb = upnpDeviceDb;
}
public IpList getVeraAddress() {
return veraaddress;
}
public void setVeraAddress(IpList veraAddress) {
this.veraaddress = veraAddress;
}
public IpList getHarmonyAddress() {
return harmonyaddress;
}
public void setHarmonyAddress(IpList harmonyaddress) {
this.harmonyaddress = harmonyaddress;
}
public boolean isUpnpStrict() {
return upnpstrict;
}
public void setUpnpStrict(boolean upnpStrict) {
this.upnpstrict = upnpStrict;
}
public boolean isTraceupnp() {
return traceupnp;
}
public void setTraceupnp(boolean traceupnp) {
this.traceupnp = traceupnp;
}
public String getNestuser() {
return nestuser;
}
public void setNestuser(String nestuser) {
this.nestuser = nestuser;
}
public String getNestpwd() {
return nestpwd;
}
public void setNestpwd(String nestpwd) {
this.nestpwd = nestpwd;
}
public boolean isVeraconfigured() {
return veraconfigured;
}
public void setVeraconfigured(boolean veraconfigured) {
this.veraconfigured = veraconfigured;
}
public boolean isHarmonyconfigured() {
return harmonyconfigured;
}
public void setHarmonyconfigured(boolean harmonyconfigured) {
this.harmonyconfigured = harmonyconfigured;
}
public boolean isNestConfigured() {
return nestconfigured;
}
public void setNestConfigured(boolean isNestConfigured) {
this.nestconfigured = isNestConfigured;
}
public Integer getButtonsleep() {
return buttonsleep;
}
public void setButtonsleep(Integer buttonsleep) {
this.buttonsleep = buttonsleep;
}
public String getConfigfile() {
return configfile;
}
public void setConfigfile(String configfile) {
this.configfile = configfile;
}
public Integer getNumberoflogmessages() {
return numberoflogmessages;
}
public void setNumberoflogmessages(Integer numberoflogmessages) {
this.numberoflogmessages = numberoflogmessages;
}
public boolean isFarenheit() {
return farenheit;
}
public void setFarenheit(boolean farenheit) {
this.farenheit = farenheit;
}
public IpList getHueaddress() {
return hueaddress;
}
public void setHueaddress(IpList hueaddress) {
this.hueaddress = hueaddress;
}
public boolean isHueconfigured() {
return hueconfigured;
}
public void setHueconfigured(boolean hueconfigured) {
this.hueconfigured = hueconfigured;
}
public IpList getHaladdress() {
return haladdress;
}
public void setHaladdress(IpList haladdress) {
this.haladdress = haladdress;
}
public String getHaltoken() {
return haltoken;
}
public void setHaltoken(String haltoken) {
this.haltoken = haltoken;
}
public boolean isHalconfigured() {
return halconfigured;
}
public void setHalconfigured(boolean halconfigured) {
this.halconfigured = halconfigured;
}
public Map<String, WhitelistEntry> getWhitelist() {
return whitelist;
}
public void setWhitelist(Map<String, WhitelistEntry> whitelist) {
this.whitelist = whitelist;
}
public boolean isSettingsChanged() {
return settingsChanged;
}
public void setSettingsChanged(boolean settingsChanged) {
this.settingsChanged = settingsChanged;
}
public String getMyechourl() {
return myechourl;
}
public void setMyechourl(String myechourl) {
this.myechourl = myechourl;
}
public String getWebaddress() {
return webaddress;
}
public void setWebaddress(String webaddress) {
this.webaddress = webaddress;
}
public IpList getMqttaddress() {
return mqttaddress;
}
public void setMqttaddress(IpList mqttaddress) {
this.mqttaddress = mqttaddress;
}
public boolean isMqttconfigured() {
return mqttconfigured;
}
public void setMqttconfigured(boolean mqttconfigured) {
this.mqttconfigured = mqttconfigured;
}
public IpList getHassaddress() {
return hassaddress;
}
public void setHassaddress(IpList hassaddress) {
this.hassaddress = hassaddress;
}
public boolean isHassconfigured() {
return hassconfigured;
}
public void setHassconfigured(boolean hassconfigured) {
this.hassconfigured = hassconfigured;
}
public String getHubversion() {
return hubversion;
}
public void setHubversion(String hubversion) {
this.hubversion = hubversion;
}
public Boolean isValidVera() {
if(this.getVeraAddress() == null || this.getVeraAddress().getDevices().size() <= 0)
return false;
List<NamedIP> devicesList = this.getVeraAddress().getDevices();
if(devicesList.get(0).getIp().contains(Configuration.DEFAULT_ADDRESS))
return false;
return true;
}
public Boolean isValidHarmony() {
if(this.getHarmonyAddress() == null || this.getHarmonyAddress().getDevices().size() <= 0)
return false;
List<NamedIP> devicesList = this.getHarmonyAddress().getDevices();
if(devicesList.get(0).getIp().contains(Configuration.DEFAULT_ADDRESS))
return false;
return true;
}
public Boolean isValidNest() {
if(this.getNestpwd() == null || this.getNestpwd().equals(""))
return false;
if(this.getNestuser() == null || this.getNestuser().equals(""))
return false;
return true;
}
public Boolean isValidHue() {
if(this.getHueaddress() == null || this.getHueaddress().getDevices().size() <= 0)
return false;
List<NamedIP> devicesList = this.getHueaddress().getDevices();
if(devicesList.get(0).getIp().contains(Configuration.DEFAULT_ADDRESS))
return false;
return true;
}
public Boolean isValidHal() {
if(this.getHaladdress() == null || this.getHaladdress().getDevices().size() <= 0)
return false;
List<NamedIP> devicesList = this.getHaladdress().getDevices();
if(devicesList.get(0).getIp().contains(Configuration.DEFAULT_ADDRESS))
return false;
if(this.getHaltoken() == null || this.getHaltoken().equals(""))
return false;
return true;
}
public Boolean isValidMQTT() {
if(this.getMqttaddress() == null || this.getMqttaddress().getDevices().size() <= 0)
return false;
List<NamedIP> devicesList = this.getMqttaddress().getDevices();
if(devicesList.get(0).getIp().contains(Configuration.DEFAULT_ADDRESS))
return false;
return true;
}
public Boolean isValidHass() {
if(this.getHassaddress() == null || this.getHassaddress().getDevices().size() <= 0)
return false;
List<NamedIP> devicesList = this.getHassaddress().getDevices();
if(devicesList.get(0).getIp().contains(Configuration.DEFAULT_ADDRESS))
return false;
return true;
}
}

View File

@@ -0,0 +1,16 @@
package com.bwssystems.HABridge;
public class Configuration {
public final static String DEVICE_DB_DIRECTORY = "data/device.db";
public final static String UPNP_RESPONSE_PORT = "50000";
public final static String DEFAULT_ADDRESS = "1.1.1.1";
public final static String LOOP_BACK_ADDRESS = "127.0.0.1";
public final static String LOOP_BACK_INTERFACE = "lo";
public final static String DEFAULT_WEB_PORT = "80";
public final static String DEFAULT_BUTTON_SLEEP = "100";
public static final int UPNP_DISCOVERY_PORT = 1900;
public static final String UPNP_MULTICAST_ADDRESS = "239.255.255.250";
public static final String CONFIG_FILE = "data/habridge.config";
public static final int NUMBER_OF_LOG_MESSAGES = 512;
public static final long UPNP_NOTIFY_TIMEOUT = 20000;
}

View File

@@ -0,0 +1,80 @@
package com.bwssystems.HABridge;
import java.util.ArrayList;
public class DeviceMapTypes {
public final static String[] CUSTOM_DEVICE = { "custom", "Custom"};
public final static String[] VERA_DEVICE = { "veraDevice", "Vera Device"};
public final static String[] VERA_SCENE = { "veraScene", "Vera Scene"};
public final static String[] HARMONY_ACTIVITY = { "harmonyActivity", "Harmony Activity"};
public final static String[] HARMONY_BUTTON = { "harmonyButton", "Harmony Button"};
public final static String[] NEST_HOMEAWAY = { "nestHomeAway", "Nest Home Status"};
public final static String[] NEST_THERMO_SET = { "nestThermoSet", "Nest Thermostat"};
public final static String[] HUE_DEVICE = { "hueDevice", "Hue Device"};
public final static String[] HAL_DEVICE = { "halDevice", "HAL Device"};
public final static String[] HAL_BUTTON = { "halButton", "HAL Button"};
public final static String[] HAL_HOME = { "halHome", "HAL Home Status"};
public final static String[] HAL_THERMO_SET = { "halThermoSet", "HAL Thermostat"};
public final static String[] MQTT_MESSAGE = { "mqttMessage", "MQTT Message"};
public final static String[] EXEC_DEVICE_COMPAT = { "exec", "Execute Script/Program"};
public final static String[] CMD_DEVICE = { "cmdDevice", "Execute Command/Script/Program"};
public final static String[] HASS_DEVICE = { "hassDevice", "HomeAssistant Device"};
public final static String[] TCP_DEVICE = { "tcpDevice", "TCP Device"};
public final static String[] TCP_DEVICE_COMPAT = { "TCP", "TCP Device"};
public final static String[] UDP_DEVICE = { "udpDevice", "UDP Device"};
public final static String[] UDP_DEVICE_COMPAT = { "UDP", "UDP Device"};
public final static String[] HTTP_DEVICE = { "httpDevice", "HTTP Device"};
public final static int typeIndex = 0;
public final static int displayIndex = 1;
ArrayList<String[]> deviceMapTypes;
public DeviceMapTypes() {
super();
deviceMapTypes = new ArrayList<String[]>();
deviceMapTypes.add(CMD_DEVICE);
deviceMapTypes.add(HAL_DEVICE);
deviceMapTypes.add(HAL_HOME);
deviceMapTypes.add(HAL_THERMO_SET);
deviceMapTypes.add(HAL_BUTTON);
deviceMapTypes.add(HASS_DEVICE);
deviceMapTypes.add(HTTP_DEVICE);
deviceMapTypes.add(HUE_DEVICE);
deviceMapTypes.add(MQTT_MESSAGE);
deviceMapTypes.add(NEST_HOMEAWAY);
deviceMapTypes.add(NEST_THERMO_SET);
deviceMapTypes.add(TCP_DEVICE);
deviceMapTypes.add(UDP_DEVICE);
deviceMapTypes.add(VERA_DEVICE);
deviceMapTypes.add(VERA_SCENE);
deviceMapTypes.add(HARMONY_ACTIVITY);
deviceMapTypes.add(HARMONY_BUTTON);
}
public static int getTypeIndex() {
return typeIndex;
}
public static int getDisplayIndex() {
return displayIndex;
}
public ArrayList<String[]> getDeviceMapTypes() {
return deviceMapTypes;
}
public Boolean validateType(String type) {
if(type == null || type.trim().isEmpty())
return false;
for(String[] mapType : deviceMapTypes) {
if(type.trim().contentEquals(mapType[typeIndex]))
return true;
}
if(type.trim().contentEquals(EXEC_DEVICE_COMPAT[typeIndex]))
return true;
if(type.trim().contentEquals(TCP_DEVICE_COMPAT[typeIndex]))
return true;
if(type.trim().contentEquals(UDP_DEVICE_COMPAT[typeIndex]))
return true;
return false;
}
}

View File

@@ -0,0 +1,97 @@
package com.bwssystems.HABridge;
import static spark.Spark.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.devicemanagmeent.*;
import com.bwssystems.HABridge.hue.HueMulator;
import com.bwssystems.HABridge.upnp.UpnpListener;
import com.bwssystems.HABridge.upnp.UpnpSettingsResource;
import com.bwssystems.HABridge.util.UDPDatagramSender;
public class HABridge {
/*
* 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.
*
*
*/
public static void main(String[] args) {
Logger log = LoggerFactory.getLogger(HABridge.class);
DeviceResource theResources;
HomeManager homeManager;
HueMulator theHueMulator;
UDPDatagramSender udpSender;
UpnpSettingsResource theSettingResponder;
UpnpListener theUpnpListener;
SystemControl theSystem;
BridgeSettings bridgeSettings;
Version theVersion;
theVersion = new Version();
log.info("HA Bridge (v" + theVersion.getVersion() + ") starting....");
bridgeSettings = new BridgeSettings();
while(!bridgeSettings.getBridgeControl().isStop()) {
bridgeSettings.buildSettings();
log.info("HA Bridge initializing....");
// sparkjava config directive to set ip address for the web server to listen on
ipAddress(bridgeSettings.getBridgeSettingsDescriptor().getWebaddress());
// sparkjava config directive to set port for the web server to listen on
port(bridgeSettings.getBridgeSettingsDescriptor().getServerPort());
// sparkjava config directive to set html static file location for Jetty
staticFileLocation("/public");
// setup system control api first
theSystem = new SystemControl(bridgeSettings, theVersion);
theSystem.setupServer();
// setup the UDP Datagram socket to be used by the HueMulator and the upnpListener
udpSender = UDPDatagramSender.createUDPDatagramSender(bridgeSettings.getBridgeSettingsDescriptor().getUpnpResponsePort());
if(udpSender == null) {
bridgeSettings.getBridgeControl().setStop(true);
}
else {
//Setup the device connection homes through the manager
homeManager = new HomeManager();
homeManager.buildHomes(bridgeSettings.getBridgeSettingsDescriptor(), udpSender);
// setup the class to handle the resource setup rest api
theResources = new DeviceResource(bridgeSettings.getBridgeSettingsDescriptor(), homeManager);
// setup the class to handle the upnp response rest api
theSettingResponder = new UpnpSettingsResource(bridgeSettings.getBridgeSettingsDescriptor());
theSettingResponder.setupServer();
// setup the class to handle the hue emulator rest api
theHueMulator = new HueMulator(bridgeSettings.getBridgeSettingsDescriptor(), theResources.getDeviceRepository(), homeManager);
theHueMulator.setupServer();
// wait for the sparkjava initialization of the rest api classes to be complete
awaitInitialization();
// start the upnp ssdp discovery listener
theUpnpListener = new UpnpListener(bridgeSettings.getBridgeSettingsDescriptor(), bridgeSettings.getBridgeControl(), udpSender);
if(theUpnpListener.startListening())
log.info("HA Bridge (v" + theVersion.getVersion() + ") reinitialization requessted....");
else
bridgeSettings.getBridgeControl().setStop(true);
if(bridgeSettings.getBridgeSettingsDescriptor().isSettingsChanged())
bridgeSettings.save(bridgeSettings.getBridgeSettingsDescriptor());
homeManager.closeHomes();
udpSender.closeResponseSocket();
}
bridgeSettings.getBridgeControl().setReinit(false);
stop();
}
log.info("HA Bridge (v" + theVersion.getVersion() + ") exiting....");
System.exit(0);
}
}

View File

@@ -0,0 +1,9 @@
package com.bwssystems.HABridge;
import com.bwssystems.HABridge.devicemanagmeent.ResourceHandler;
import com.bwssystems.HABridge.hue.HueMulatorHandler;
public interface Home extends HueMulatorHandler, ResourceHandler {
public Home createHome(BridgeSettingsDescriptor bridgeSettings);
public void closeHome();
}

View File

@@ -0,0 +1,96 @@
package com.bwssystems.HABridge;
import java.util.HashMap;
import java.util.Map;
import com.bwssystems.HABridge.devicemanagmeent.ResourceHandler;
import com.bwssystems.HABridge.plugins.NestBridge.NestHome;
import com.bwssystems.HABridge.plugins.exec.CommandHome;
import com.bwssystems.HABridge.plugins.hal.HalHome;
import com.bwssystems.HABridge.plugins.harmony.HarmonyHome;
import com.bwssystems.HABridge.plugins.hass.HassHome;
import com.bwssystems.HABridge.plugins.http.HTTPHome;
import com.bwssystems.HABridge.plugins.hue.HueHome;
import com.bwssystems.HABridge.plugins.mqtt.MQTTHome;
import com.bwssystems.HABridge.plugins.tcp.TCPHome;
import com.bwssystems.HABridge.plugins.udp.UDPHome;
import com.bwssystems.HABridge.plugins.vera.VeraHome;
import com.bwssystems.HABridge.util.UDPDatagramSender;
public class HomeManager {
Map<String, Home> homeList;
Map<String, Home> resourceList;
public HomeManager() {
homeList = new HashMap<String, Home>();
resourceList = new HashMap<String, Home>();
}
// factory method
public void buildHomes(BridgeSettingsDescriptor bridgeSettings, UDPDatagramSender aUdpDatagramSender) {
Home aHome = null;
//setup the harmony connection if available
aHome = new HarmonyHome(bridgeSettings);
resourceList.put(DeviceMapTypes.HARMONY_ACTIVITY[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.HARMONY_ACTIVITY[DeviceMapTypes.typeIndex], aHome);
resourceList.put(DeviceMapTypes.HARMONY_BUTTON[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.HARMONY_BUTTON[DeviceMapTypes.typeIndex], aHome);
//setup the nest connection if available
aHome = new NestHome(bridgeSettings);
resourceList.put(DeviceMapTypes.NEST_HOMEAWAY[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.NEST_HOMEAWAY[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.NEST_THERMO_SET[DeviceMapTypes.typeIndex], aHome);
//setup the hue passtrhu configuration if available
aHome = new HueHome(bridgeSettings);
resourceList.put(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex], aHome);
//setup the hal configuration if available
aHome = new HalHome(bridgeSettings);
resourceList.put(DeviceMapTypes.HAL_DEVICE[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.HAL_DEVICE[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.HAL_BUTTON[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.HAL_HOME[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.HAL_THERMO_SET[DeviceMapTypes.typeIndex], aHome);
//setup the mqtt handlers if available
aHome = new MQTTHome(bridgeSettings);
resourceList.put(DeviceMapTypes.MQTT_MESSAGE[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.MQTT_MESSAGE[DeviceMapTypes.typeIndex], aHome);
//setup the HomeAssistant configuration if available
aHome = new HassHome(bridgeSettings);
resourceList.put(DeviceMapTypes.HASS_DEVICE[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.HASS_DEVICE[DeviceMapTypes.typeIndex], aHome);
//setup the command execution Home
aHome = new CommandHome(bridgeSettings);
homeList.put(DeviceMapTypes.EXEC_DEVICE_COMPAT[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.CMD_DEVICE[DeviceMapTypes.typeIndex], aHome);
//setup the http handler Home
aHome = new HTTPHome(bridgeSettings);
homeList.put(DeviceMapTypes.HTTP_DEVICE[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.CUSTOM_DEVICE[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.VERA_DEVICE[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.VERA_SCENE[DeviceMapTypes.typeIndex], aHome);
//setup the tcp handler Home
aHome = new TCPHome(bridgeSettings);
homeList.put(DeviceMapTypes.TCP_DEVICE[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.TCP_DEVICE_COMPAT[DeviceMapTypes.typeIndex], aHome);
//setup the udp handler Home
aHome = new UDPHome(bridgeSettings, aUdpDatagramSender);
homeList.put(DeviceMapTypes.UDP_DEVICE[DeviceMapTypes.typeIndex], aHome);
homeList.put(DeviceMapTypes.UDP_DEVICE_COMPAT[DeviceMapTypes.typeIndex], aHome);
aHome = new VeraHome(bridgeSettings);
resourceList.put(DeviceMapTypes.VERA_DEVICE[DeviceMapTypes.typeIndex], aHome);
resourceList.put(DeviceMapTypes.VERA_SCENE[DeviceMapTypes.typeIndex], aHome);
}
public Home findHome(String type) {
return homeList.get(type);
}
public ResourceHandler findResource(String type) {
return resourceList.get(type);
}
public void closeHomes() {
}
}

View File

@@ -0,0 +1,16 @@
package com.bwssystems.HABridge;
import java.util.List;
public class IpList {
private List<NamedIP> devices;
public List<NamedIP> getDevices() {
return devices;
}
public void setDevices(List<NamedIP> devices) {
this.devices = devices;
}
}

View File

@@ -0,0 +1,40 @@
package com.bwssystems.HABridge;
public class NamedIP {
private String name;
private String ip;
private String port;
private String username;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

View File

@@ -0,0 +1,283 @@
package com.bwssystems.HABridge;
import static spark.Spark.get;
import static spark.Spark.options;
import static spark.Spark.post;
import static spark.Spark.put;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.dao.BackupFilename;
import com.bwssystems.HABridge.util.JsonTransformer;
import com.bwssystems.HABridge.util.TextStringFormatter;
import com.bwssystems.logservices.LoggerInfo;
import com.bwssystems.logservices.LoggingForm;
import com.bwssystems.logservices.LoggingManager;
import com.google.gson.Gson;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.read.CyclicBufferAppender;
public class SystemControl {
private static final Logger log = LoggerFactory.getLogger(SystemControl.class);
public static final String CYCLIC_BUFFER_APPENDER_NAME = "CYCLIC";
private LoggerContext lc;
private static final String SYSTEM_CONTEXT = "/system";
private BridgeSettings bridgeSettings;
private Version version;
private CyclicBufferAppender<ILoggingEvent> cyclicBufferAppender;
private DateFormat dateFormat;
private LoggingManager theLogServiceMgr;
public SystemControl(BridgeSettings theBridgeSettings, Version theVersion) {
this.bridgeSettings = theBridgeSettings;
this.version = theVersion;
this.lc = (LoggerContext) LoggerFactory.getILoggerFactory();
this.dateFormat = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss.SSS");
reacquireCBA();
theLogServiceMgr = new LoggingManager();
theLogServiceMgr.init();
}
// This function sets up the sparkjava rest calls for the hue api
public void setupServer() {
log.info("System control service started....");
// http://ip_address:port/system/habridge/version gets the version of this bridge instance
get (SYSTEM_CONTEXT + "/habridge/version", "application/json", (request, response) -> {
log.debug("Get HA Bridge version: v" + version.getVersion());
response.status(HttpStatus.SC_OK);
return "{\"version\":\"" + version.getVersion() + "\"}";
});
// http://ip_address:port/system/logmsgs gets the log messages for the bridge
get (SYSTEM_CONTEXT + "/logmsgs", "application/json", (request, response) -> {
log.debug("Get logmsgs.");
response.status(HttpStatus.SC_OK);
String logMsgs;
int count = -1;
if(cyclicBufferAppender == null)
reacquireCBA();
if (cyclicBufferAppender != null) {
count = cyclicBufferAppender.getLength();
}
logMsgs = "[";
if (count == -1) {
logMsgs = logMsgs + "{\"message\":\"Failed to locate CyclicBuffer\"}";
} else if (count == 0) {
logMsgs = logMsgs + "{\"message\":\"No logging events to display\"}";
} else {
LoggingEvent le;
for (int i = 0; i < count; i++) {
le = (LoggingEvent) cyclicBufferAppender.get(i);
logMsgs = logMsgs + ( i > 0?",{":"{") + "\"time\":\"" + dateFormat.format(le.getTimeStamp()) + "\",\"level\":\"" + le.getLevel().levelStr + "\",\"component\":\"" + le.getLoggerName() + "\",\"message\":\"" + TextStringFormatter.forJSON(le.getFormattedMessage()) + "\"}";
}
}
logMsgs = logMsgs + "]";
response.status(200);
return logMsgs;
});
// http://ip_address:port/system/logmgmt/loggers gets the logger info for the bridge
get (SYSTEM_CONTEXT + "/logmgmt/loggers/:all", "application/json", (request, response) -> {
log.debug("Get loggers info with showAll argument: " + request.params(":all"));
Boolean showAll = false;
if(request.params(":all").equals("true"))
showAll = true;
theLogServiceMgr.setShowAll(showAll);
theLogServiceMgr.init();
response.status(200);
return theLogServiceMgr.getConfiguredLoggers();
}, new JsonTransformer());
// http://ip_address:port/system/logmgmt/update CORS request
options(SYSTEM_CONTEXT + "/logmgmt/update", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
// http://ip_address:port/system/logmgmt/update which changes logging parameters for the process
put(SYSTEM_CONTEXT + "/logmgmt/update", "application/json", (request, response) -> {
log.debug("update loggers: " + request.body());
response.status(200);
LoggerInfo updateLoggers[];
updateLoggers = new Gson().fromJson(request.body(), LoggerInfo[].class);
LoggingForm theModel = theLogServiceMgr.getModel();
theModel.setUpdatedLoggers(Arrays.asList(updateLoggers));
theLogServiceMgr.updateLogLevels();
return theLogServiceMgr.getConfiguredLoggers();
}, new JsonTransformer());
// http://ip_address:port/system/settings which returns the bridge configuration settings
get(SYSTEM_CONTEXT + "/settings", "application/json", (request, response) -> {
log.debug("bridge settings requested from " + request.ip());
response.status(200);
return bridgeSettings.getBridgeSettingsDescriptor();
}, new JsonTransformer());
// http://ip_address:port/system/settings CORS request
options(SYSTEM_CONTEXT + "/settings", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
// http://ip_address:port/system/settings which returns the bridge configuration settings
put(SYSTEM_CONTEXT + "/settings", "application/json", (request, response) -> {
log.debug("save bridge settings requested from " + request.ip() + " with body: " + request.body());
BridgeSettingsDescriptor newBridgeSettings = new Gson().fromJson(request.body(), BridgeSettingsDescriptor.class);
bridgeSettings.save(newBridgeSettings);
response.status(200);
return bridgeSettings.getBridgeSettingsDescriptor();
}, new JsonTransformer());
// http://ip_address:port/system/control/reinit CORS request
options(SYSTEM_CONTEXT + "/control/reinit", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
// http://ip_address:port/system/control/reinit sets the parameter reinit the server
put(SYSTEM_CONTEXT + "/control/reinit", "application/json", (request, response) -> {
return reinit();
});
// http://ip_address:port/system/control/stop CORS request
options(SYSTEM_CONTEXT + "/control/stop", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
// http://ip_address:port/system/control/stop sets the parameter stop the server
put(SYSTEM_CONTEXT + "/control/stop", "application/json", (request, response) -> {
return stop();
});
// http://ip_address:port/system/backup/available returns a list of config backup filenames
get (SYSTEM_CONTEXT + "/backup/available", "application/json", (request, response) -> {
log.debug("Get backup filenames");
response.status(HttpStatus.SC_OK);
return bridgeSettings.getBackups();
}, new JsonTransformer());
// http://ip_address:port/system/backup/create CORS request
options(SYSTEM_CONTEXT + "/backup/create", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
put (SYSTEM_CONTEXT + "/backup/create", "application/json", (request, response) -> {
log.debug("Create backup: " + request.body());
BackupFilename aFilename = new Gson().fromJson(request.body(), BackupFilename.class);
BackupFilename returnFilename = new BackupFilename();
returnFilename.setFilename(bridgeSettings.backup(aFilename.getFilename()));
return returnFilename;
}, new JsonTransformer());
// http://ip_address:port/system/backup/delete CORS request
options(SYSTEM_CONTEXT + "/backup/delete", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "POST");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
post (SYSTEM_CONTEXT + "/backup/delete", "application/json", (request, response) -> {
log.debug("Delete backup: " + request.body());
BackupFilename aFilename = new Gson().fromJson(request.body(), BackupFilename.class);
if(aFilename != null)
bridgeSettings.deleteBackup(aFilename.getFilename());
else
log.warn("No filename given for delete backup.");
return null;
}, new JsonTransformer());
// http://ip_address:port/system/backup/restore CORS request
options(SYSTEM_CONTEXT + "/backup/restore", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "POST");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
post (SYSTEM_CONTEXT + "/backup/restore", "application/json", (request, response) -> {
log.debug("Restore backup: " + request.body());
BackupFilename aFilename = new Gson().fromJson(request.body(), BackupFilename.class);
if(aFilename != null) {
bridgeSettings.restoreBackup(aFilename.getFilename());
bridgeSettings.loadConfig();
}
else
log.warn("No filename given for restore backup.");
return bridgeSettings.getBridgeSettingsDescriptor();
}, new JsonTransformer());
}
void reacquireCBA() {
cyclicBufferAppender = (CyclicBufferAppender<ILoggingEvent>) lc.getLogger(
Logger.ROOT_LOGGER_NAME).getAppender(CYCLIC_BUFFER_APPENDER_NAME);
cyclicBufferAppender.setMaxSize(bridgeSettings.getBridgeSettingsDescriptor().getNumberoflogmessages());
}
protected void pingListener() {
try {
byte[] buf = new byte[256];
String testData = "M-SEARCH * HTTP/1.1\nHOST: " + Configuration.UPNP_MULTICAST_ADDRESS + ":" + Configuration.UPNP_DISCOVERY_PORT + "ST: urn:schemas-upnp-org:device:CloudProxy:1\nMAN: \"ssdp:discover\"\nMX: 3";
buf = testData.getBytes();
MulticastSocket socket = new MulticastSocket(Configuration.UPNP_DISCOVERY_PORT);
InetAddress group = InetAddress.getByName(Configuration.UPNP_MULTICAST_ADDRESS);
DatagramPacket packet;
packet = new DatagramPacket(buf, buf.length, group, Configuration.UPNP_DISCOVERY_PORT);
socket.send(packet);
socket.close();
}
catch (IOException e) {
log.warn("Error pinging listener. " + e.getMessage());
}
}
public String reinit() {
bridgeSettings.getBridgeControl().setReinit(true);
pingListener();
return "{\"control\":\"reiniting\"}";
}
public String stop() {
bridgeSettings.getBridgeControl().setStop(true);
pingListener();
return "{\"control\":\"stopping\"}";
}
}

View File

@@ -0,0 +1,50 @@
package com.bwssystems.HABridge;
import java.io.InputStream;
import java.util.Properties;
public final class Version {
private String version;
private String groupId;
private String artifactId;
private Properties prop;
public Version()
{
InputStream resourceAsStream =
(InputStream) this.getClass().getResourceAsStream(
"/version.properties"
);
this.prop = new Properties();
try
{
this.prop.load( resourceAsStream );
this.version = this.prop.getProperty("version");
this.groupId = this.prop.getProperty("groupId");
this.artifactId = this.prop.getProperty("artifactId");
}
catch (Exception e)
{
this.version = "0.0.0";
this.groupId = "no group";
this.artifactId = "no artifact";
}
}
public String getVersion() {
return version;
}
public String getGroupId() {
return groupId;
}
public String getArtifactId() {
return artifactId;
}
}

View File

@@ -0,0 +1,87 @@
package com.bwssystems.HABridge.api;
import com.google.gson.JsonElement;
public class CallItem {
private JsonElement item;
private Integer count;
private Integer delay;
private String type;
private String filterIPs;
private String httpVerb;
private String httpBody;
private String httpHeaders;
private String contentType;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getFilterIPs() {
return filterIPs;
}
public void setFilterIPs(String filterIPs) {
this.filterIPs = filterIPs;
}
public JsonElement getItem() {
return item;
}
public void setItem(JsonElement item) {
this.item = item;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Integer getDelay() {
return delay;
}
public void setDelay(Integer delay) {
this.delay = delay;
}
public String getHttpVerb() {
return httpVerb;
}
public void setHttpVerb(String httpVerb) {
this.httpVerb = httpVerb;
}
public String getHttpBody() {
return httpBody;
}
public void setHttpBody(String httpBody) {
this.httpBody = httpBody;
}
public String getHttpHeaders() {
return httpHeaders;
}
public void setHttpHeaders(String httpHeaders) {
this.httpHeaders = httpHeaders;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
}

View File

@@ -0,0 +1,30 @@
package com.bwssystems.HABridge.api;
import java.lang.reflect.Type;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
public class CallItemDeserializer implements JsonDeserializer<CallItem> {
@Override
public CallItem deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext ctx)
{
CallItem aCallItem = new CallItem();
JsonObject jsonObj = json.getAsJsonObject();
JsonElement jsonElem;
jsonElem = jsonObj.get("item");
aCallItem.setItem(jsonElem);
jsonElem = jsonObj.get("delay");
aCallItem.setDelay(jsonElem.getAsInt());
jsonElem = jsonObj.get("count");
aCallItem.setCount(jsonElem.getAsInt());
jsonElem = jsonObj.get("type");
aCallItem.setType(jsonElem.getAsString());
jsonElem = jsonObj.get("filterIPs");
aCallItem.setFilterIPs(jsonElem.getAsString());
return aCallItem;
}
}

View File

@@ -0,0 +1,18 @@
package com.bwssystems.HABridge.api;
public class NameValue {
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@@ -0,0 +1,13 @@
package com.bwssystems.HABridge.api;
public class SuccessUserResponse {
private UserCreateResponse success;
public UserCreateResponse getSuccess() {
return success;
}
public void setSuccess(UserCreateResponse success) {
this.success = success;
}
}

View File

@@ -0,0 +1,19 @@
package com.bwssystems.HABridge.api;
public class UserCreateRequest {
private String devicetype;
private String username;
public String getDevicetype() {
return devicetype;
}
public void setDevicetype(String devicetype) {
this.devicetype = devicetype;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}

View File

@@ -0,0 +1,13 @@
package com.bwssystems.HABridge.api;
public class UserCreateResponse {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}

View File

@@ -0,0 +1,96 @@
package com.bwssystems.HABridge.api.hue;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
/**
* 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 luminaireuniqueid;
private String uniqueid;
private String swversion;
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 String getLuminaireuniqueid() {
return luminaireuniqueid;
}
public void setLuminaireuniqueid(String luminaireuniqueid) {
this.luminaireuniqueid = luminaireuniqueid;
}
public static DeviceResponse createResponse(DeviceDescriptor device){
DeviceResponse response = new DeviceResponse();
response.setState(device.getDeviceState());
response.setName(device.getName());
response.setUniqueid(device.getUniqueid());
response.setManufacturername("Philips");
response.setType("Dimmable light");
response.setModelid("LWB004");
response.setSwversion("66012040");
response.setLuminaireuniqueid(null);
return response;
}
}

View File

@@ -1,5 +1,6 @@
package com.bwssytems.HABridge.api.hue;
package com.bwssystems.HABridge.api.hue;
// import java.util.ArrayList;
import java.util.List;
/**
@@ -7,7 +8,7 @@ import java.util.List;
*/
public class DeviceState {
private boolean on;
private int bri = 255;
private int bri;
private int hue;
private int sat;
private String effect;
@@ -16,6 +17,7 @@ public class DeviceState {
private String colormode;
private boolean reachable;
private List<Double> xy;
// private int transitiontime;
public boolean isOn() {
return on;
@@ -96,7 +98,32 @@ public class DeviceState {
public void setXy(List<Double> xy) {
this.xy = xy;
}
// public int getTransitiontime() {
// return transitiontime;
// }
// public void setTransitiontime(int transitiontime) {
// this.transitiontime = transitiontime;
// }
public static DeviceState createDeviceState() {
DeviceState newDeviceState = new DeviceState();
newDeviceState.fillIn();
// newDeviceState.setColormode("none");
// ArrayList<Double> doubleArray = new ArrayList<Double>();
// doubleArray.add(new Double(0));
// doubleArray.add(new Double(0));
// newDeviceState.setXy(doubleArray);
return newDeviceState;
}
public void fillIn() {
if(this.getAlert() == null)
this.setAlert("none");
if(this.getEffect() == null)
this.setEffect("none");
this.setReachable(true);
}
@Override
public String toString() {
return "DeviceState{" +

View File

@@ -0,0 +1,18 @@
package com.bwssystems.HABridge.api.hue;
public class DeviceTypes {
private Boolean bridge;
private String[] lights;
public Boolean getBridge() {
return bridge;
}
public void setBridge(Boolean bridge) {
this.bridge = bridge;
}
public String[] getLights() {
return lights;
}
public void setLights(String[] lights) {
this.lights = lights;
}
}

View File

@@ -0,0 +1,43 @@
package com.bwssystems.HABridge.api.hue;
import java.util.List;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
public class GroupResponse {
private DeviceState action;
private String[] lights;
private String name;
public DeviceState getAction() {
return action;
}
public void setAction(DeviceState action) {
this.action = action;
}
public String[] getLights() {
return lights;
}
public void setLights(String[] lights) {
this.lights = lights;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static GroupResponse createGroupResponse(List<DeviceDescriptor> deviceList) {
String[] theList = new String[deviceList.size()];
int i = 0;
for (DeviceDescriptor device : deviceList) {
theList[i] = device.getId();
i++;
}
GroupResponse theResponse = new GroupResponse();
theResponse.setAction(DeviceState.createDeviceState());
theResponse.setName("Lightset 0");
theResponse.setLights(theList);
return theResponse;
}
}

View File

@@ -0,0 +1,86 @@
package com.bwssystems.HABridge.api.hue;
import java.util.HashMap;
import java.util.Map;
import com.bwssystems.HABridge.api.hue.DeviceResponse;
import com.google.gson.JsonObject;
/**
* Created by arm on 4/14/15.
*/
public class HueApiResponse {
private Map<String, DeviceResponse> lights;
private Map<String, JsonObject> scenes;
private Map<String, JsonObject> groups;
private Map<String, JsonObject> schedules;
private Map<String, JsonObject> sensors;
private Map<String, JsonObject> rules;
private HueConfig config;
public HueApiResponse(String name, String ipaddress, Map<String, WhitelistEntry> awhitelist, String emulateHubVersion) {
super();
this.setConfig(HueConfig.createConfig(name, ipaddress, awhitelist, emulateHubVersion));
this.setRules(new HashMap<>());
this.setSensors(new HashMap<>());
this.setSchedules(new HashMap<>());
this.setGroups(new HashMap<>());
this.setScenes(new HashMap<>());
}
public Map<String, DeviceResponse> getLights() {
return lights;
}
public void setLights(Map<String, DeviceResponse> lights) {
this.lights = lights;
}
public Map<String, JsonObject> getScenes() {
return scenes;
}
public void setScenes(Map<String, JsonObject> scenes) {
this.scenes = scenes;
}
public Map<String, JsonObject> getGroups() {
return groups;
}
public void setGroups(Map<String, JsonObject> groups) {
this.groups = groups;
}
public Map<String, JsonObject> getSchedules() {
return schedules;
}
public void setSchedules(Map<String, JsonObject> schedules) {
this.schedules = schedules;
}
public Map<String, JsonObject> getSensors() {
return sensors;
}
public void setSensors(Map<String, JsonObject> sensors) {
this.sensors = sensors;
}
public Map<String, JsonObject> getRules() {
return rules;
}
public void setRules(Map<String, JsonObject> rules) {
this.rules = rules;
}
public HueConfig getConfig() {
return config;
}
public void setConfig(HueConfig config) {
this.config = config;
}
}

View File

@@ -0,0 +1,275 @@
package com.bwssystems.HABridge.api.hue;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;
public class HueConfig
{
private Boolean portalservices;
private String gateway;
private String mac;
private String swversion;
private String apiversion;
private Boolean linkbutton;
private String ipaddress;
private Integer proxyport;
private Swupdate swupdate;
private String netmask;
private String name;
private Boolean dhcp;
private String UTC;
private String proxyaddress;
private String localtime;
private String timezone;
private String zigbeechannel;
private String modelid;
private String bridgeid;
private Boolean factorynew;
private String replacesbridgeid;
private Map<String, WhitelistEntry> whitelist;
public static HueConfig createConfig(String name, String ipaddress, Map<String, WhitelistEntry> awhitelist, String emulateHubVersion) {
HueConfig aConfig = new HueConfig();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("UTC"));
aConfig.setMac(HueConfig.getMacAddress(ipaddress));
aConfig.setApiversion(HueConstants.API_VERSION);
aConfig.setPortalservices(false);
aConfig.setGateway(ipaddress);
aConfig.setSwversion(emulateHubVersion);
aConfig.setLinkbutton(true);
aConfig.setIpaddress(ipaddress);
aConfig.setProxyport(0);
aConfig.setSwupdate(Swupdate.createSwupdate());
aConfig.setNetmask("255.255.255.0");
aConfig.setName(name);
aConfig.setDhcp(true);
aConfig.setUtc(dateFormatGmt.format(new Date()));
aConfig.setProxyaddress("none");
aConfig.setLocaltime(dateFormat.format(new Date()));
aConfig.setTimezone(TimeZone.getDefault().getID());
aConfig.setZigbeechannel("6");
aConfig.setBridgeid(HuePublicConfig.createConfig(name, ipaddress, emulateHubVersion).getHueBridgeIdFromMac());
aConfig.setModelid(HueConstants.MODEL_ID);
aConfig.setFactorynew(false);
aConfig.setReplacesbridgeid(null);
aConfig.setWhitelist(awhitelist);
return aConfig;
}
private static String getMacAddress(String addr)
{
InetAddress ip;
StringBuilder sb = new StringBuilder();
try {
ip = InetAddress.getByName(addr);
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
}
} catch (UnknownHostException e) {
sb.append("00:00:88:00:bb:ee");
} catch (SocketException e){
sb.append("00:00:88:00:bb:ee");
} catch (Exception e){
sb.append("00:00:88:00:bb:ee");
}
return sb.toString();
}
public Boolean getPortalservices() {
return portalservices;
}
public void setPortalservices(Boolean portalservices) {
this.portalservices = portalservices;
}
public String getGateway() {
return gateway;
}
public void setGateway(String gateway) {
this.gateway = gateway;
}
public String getMac() {
return mac;
}
public void setMac(String mac) {
this.mac = mac;
}
public String getSwversion() {
return swversion;
}
public void setSwversion(String swversion) {
this.swversion = swversion;
}
public Boolean getLinkbutton() {
return linkbutton;
}
public void setLinkbutton(Boolean linkbutton) {
this.linkbutton = linkbutton;
}
public String getIpaddress() {
return ipaddress;
}
public void setIpaddress(String ipaddress) {
this.ipaddress = ipaddress;
}
public Integer getProxyport() {
return proxyport;
}
public void setProxyport(Integer proxyport) {
this.proxyport = proxyport;
}
public Swupdate getSwupdate() {
return swupdate;
}
public void setSwupdate(Swupdate swupdate) {
this.swupdate = swupdate;
}
public String getNetmask() {
return netmask;
}
public void setNetmask(String netmask) {
this.netmask = netmask;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getDhcp() {
return dhcp;
}
public void setDhcp(Boolean dhcp) {
this.dhcp = dhcp;
}
public String getUtc() {
return UTC;
}
public void setUtc(String utc) {
this.UTC = utc;
}
public String getProxyaddress() {
return proxyaddress;
}
public void setProxyaddress(String proxyaddress) {
this.proxyaddress = proxyaddress;
}
public Map<String, WhitelistEntry> getWhitelist() {
return whitelist;
}
public void setWhitelist(Map<String, WhitelistEntry> whitelist) {
this.whitelist = whitelist;
}
public String getApiversion() {
return apiversion;
}
public void setApiversion(String apiversion) {
this.apiversion = apiversion;
}
public String getLocaltime() {
return localtime;
}
public void setLocaltime(String localtime) {
this.localtime = localtime;
}
public String getTimezone() {
return timezone;
}
public void setTimezone(String timezone) {
this.timezone = timezone;
}
public String getZigbeechannel() {
return zigbeechannel;
}
public void setZigbeechannel(String zigbeechannel) {
this.zigbeechannel = zigbeechannel;
}
public String getModelid() {
return modelid;
}
public void setModelid(String modelid) {
this.modelid = modelid;
}
public String getBridgeid() {
return bridgeid;
}
public void setBridgeid(String bridgeid) {
this.bridgeid = bridgeid;
}
public Boolean getFactorynew() {
return factorynew;
}
public void setFactorynew(Boolean factorynew) {
this.factorynew = factorynew;
}
public String getReplacesbridgeid() {
return replacesbridgeid;
}
public void setReplacesbridgeid(String replacesbridgeid) {
this.replacesbridgeid = replacesbridgeid;
}
}

View File

@@ -0,0 +1,8 @@
package com.bwssystems.HABridge.api.hue;
public class HueConstants {
public final static String HUB_VERSION = "01036562";
public final static String API_VERSION = "1.15.0";
public final static String MODEL_ID = "BSB002";
public final static String UUID_PREFIX = "2f402f80-da50-11e1-9b23-";
}

View File

@@ -0,0 +1,19 @@
package com.bwssystems.HABridge.api.hue;
public class HueError {
private HueErrorDetails error;
public HueError(HueErrorDetails error) {
super();
this.error = error;
}
public HueErrorDetails getError() {
return error;
}
public void setError(HueErrorDetails error) {
this.error = error;
}
}

View File

@@ -0,0 +1,56 @@
package com.bwssystems.HABridge.api.hue;
public class HueErrorDetails {
private String type;
private String address;
private String description;
private String method_name;
private String resource_name;
private String value;
public HueErrorDetails(String type, String address, String description, String method_name, String resource_name,
String value) {
super();
this.type = type;
this.address = address;
this.description = description;
this.method_name = method_name;
this.resource_name = resource_name;
this.value = value;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getMethod_name() {
return method_name;
}
public void setMethod_name(String method_name) {
this.method_name = method_name;
}
public String getResource_name() {
return resource_name;
}
public void setResource_name(String resource_name) {
this.resource_name = resource_name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@@ -0,0 +1,32 @@
package com.bwssystems.HABridge.api.hue;
import java.util.ArrayList;
public class HueErrorResponse {
private ArrayList<HueError> theErrors;
public static HueErrorResponse createResponse(String type, String address, String description, String method_name, String resource_name, String value) {
HueErrorResponse theErrorResp = new HueErrorResponse();
theErrorResp.addError(new HueError(new HueErrorDetails(type, address, description, method_name, resource_name, value)));
return theErrorResp;
}
public HueErrorResponse() {
super();
theErrors = new ArrayList<HueError>();
}
public void addError(HueError anError) {
theErrors.add(anError);
}
public HueError[] getTheErrors() {
HueError theList[] = new HueError[theErrors.size()];
theList = theErrors.toArray(theList);
return theList;
}
public void setTheErrors(ArrayList<HueError> theErrors) {
this.theErrors = theErrors;
}
}

View File

@@ -0,0 +1,151 @@
package com.bwssystems.HABridge.api.hue;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.StringTokenizer;
public class HuePublicConfig
{
private String name;
private String apiversion;
private String swversion;
private String mac;
private String bridgeid;
private String replacesbridgeid;
private Boolean factorynew;
private String modelid;
public static HuePublicConfig createConfig(String name, String ipaddress, String emulateHubVersion) {
HuePublicConfig aConfig = new HuePublicConfig();
aConfig.setMac(HuePublicConfig.getMacAddress(ipaddress));
aConfig.setApiversion(HueConstants.API_VERSION);
aConfig.setSwversion(emulateHubVersion);
aConfig.setName(name);
aConfig.setBridgeid(aConfig.getHueBridgeIdFromMac());
aConfig.setModelid(HueConstants.MODEL_ID);
aConfig.setFactorynew(false);
aConfig.setReplacesbridgeid(null);
return aConfig;
}
private static String getMacAddress(String addr)
{
InetAddress ip;
StringBuilder sb = new StringBuilder();
try {
ip = InetAddress.getByName(addr);
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
}
} catch (UnknownHostException e) {
sb.append("00:00:88:00:bb:ee");
} catch (SocketException e){
sb.append("00:00:88:00:bb:ee");
} catch (Exception e){
sb.append("00:00:88:00:bb:ee");
}
return sb.toString();
}
public String getSNUUIDFromMac()
{
StringTokenizer st = new StringTokenizer(this.getMac(), ":");
String bridgeUUID = "";
while(st.hasMoreTokens()) {
bridgeUUID = bridgeUUID + st.nextToken();
}
bridgeUUID = bridgeUUID.toLowerCase();
return bridgeUUID.toLowerCase();
}
protected String getHueBridgeIdFromMac()
{
String cleanMac = this.getSNUUIDFromMac();
String bridgeId = cleanMac.substring(0, 6) + "FFFE" + cleanMac.substring(6);
return bridgeId.toUpperCase();
}
public String getMac() {
return mac;
}
public void setMac(String mac) {
this.mac = mac;
}
public String getSwversion() {
return swversion;
}
public void setSwversion(String swversion) {
this.swversion = swversion;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getApiversion() {
return apiversion;
}
public void setApiversion(String apiversion) {
this.apiversion = apiversion;
}
public String getModelid() {
return modelid;
}
public void setModelid(String modelid) {
this.modelid = modelid;
}
public String getBridgeid() {
return bridgeid;
}
public void setBridgeid(String bridgeid) {
this.bridgeid = bridgeid;
}
public Boolean getFactorynew() {
return factorynew;
}
public void setFactorynew(Boolean factorynew) {
this.factorynew = factorynew;
}
public String getReplacesbridgeid() {
return replacesbridgeid;
}
public void setReplacesbridgeid(String replacesbridgeid) {
this.replacesbridgeid = replacesbridgeid;
}
}

View File

@@ -0,0 +1,136 @@
package com.bwssystems.HABridge.api.hue;
// import java.util.ArrayList;
import java.util.List;
/**
* Created by arm on 4/14/15.
*/
public class StateChangeBody {
private boolean on;
private int bri;
private int hue;
private int sat;
private String effect;
private int ct;
private String alert;
private List<Double> xy;
private int transitiontime;
private int bri_inc;
private int hue_inc;
private int sat_inc;
private List<Double> xy_inc;
private int ct_inc;
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 List<Double> getXy() {
return xy;
}
public void setXy(List<Double> xy) {
this.xy = xy;
}
public int getTransitiontime() {
return transitiontime;
}
public void setTransitiontime(int transitiontime) {
this.transitiontime = transitiontime;
}
public int getBri_inc() {
return bri_inc;
}
public void setBri_inc(int bri_inc) {
this.bri_inc = bri_inc;
}
public int getHue_inc() {
return hue_inc;
}
public void setHue_inc(int hue_inc) {
this.hue_inc = hue_inc;
}
public int getSat_inc() {
return sat_inc;
}
public void setSat_inc(int sat_inc) {
this.sat_inc = sat_inc;
}
public List<Double> getXy_inc() {
return xy_inc;
}
public void setXy_inc(List<Double> xy_inc) {
this.xy_inc = xy_inc;
}
public int getCt_inc() {
return ct_inc;
}
public void setCt_inc(int ct_inc) {
this.ct_inc = ct_inc;
}
}

View File

@@ -0,0 +1,68 @@
package com.bwssystems.HABridge.api.hue;
public class Swupdate
{
private Integer updatestate;
private Boolean checkforupdate;
private DeviceTypes devicetypes;
private String text;
private Boolean notify;
private String url;
public static Swupdate createSwupdate() {
Swupdate aSwupdate = new Swupdate();
aSwupdate.setUpdatestate(0);
aSwupdate.setCheckforupdate(false);
aSwupdate.setDevicetypes(new DeviceTypes());
aSwupdate.setNotify(false);
aSwupdate.setText("");
aSwupdate.setUrl("");
return aSwupdate;
}
public Boolean getCheckforupdate() {
return checkforupdate;
}
public void setCheckforupdate(Boolean checkforupdate) {
this.checkforupdate = checkforupdate;
}
public DeviceTypes getDevicetypes() {
return devicetypes;
}
public void setDevicetypes(DeviceTypes devicetypes) {
this.devicetypes = devicetypes;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Boolean getNotify() {
return notify;
}
public void setNotify(Boolean notify) {
this.notify = notify;
}
public Integer getUpdatestate() {
return updatestate;
}
public void setUpdatestate(Integer updatestate) {
this.updatestate = updatestate;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}

View File

@@ -0,0 +1,49 @@
package com.bwssystems.HABridge.api.hue;
import java.text.SimpleDateFormat;
import java.util.Date;
public class WhitelistEntry
{
private String lastUseDate;
private String createDate;
private String name;
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
public static WhitelistEntry createEntry(String devicetype) {
WhitelistEntry anEntry = new WhitelistEntry();
anEntry.setName(devicetype);
anEntry.setCreateDate(getCurrentDate());
anEntry.setLastUseDate(getCurrentDate());
return anEntry;
}
public static String getCurrentDate() {
return dateFormat.format(new Date());
}
public String getLastUseDate() {
return lastUseDate;
}
public void setLastUseDate(String lastUseDate) {
this.lastUseDate = lastUseDate;
}
public String getCreateDate() {
return createDate;
}
public void setCreateDate(String createDate) {
this.createDate = createDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@@ -0,0 +1,13 @@
package com.bwssystems.HABridge.dao;
public class BackupFilename {
private String filename;
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
}

View File

@@ -0,0 +1,200 @@
package com.bwssystems.HABridge.dao;
import com.bwssystems.HABridge.api.hue.DeviceState;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/*
* Object to handle the device configuration
*/
public class DeviceDescriptor{
@SerializedName("id")
@Expose
private String id;
@SerializedName("uniqueid")
@Expose
private String uniqueid;
@SerializedName("name")
@Expose
private String name;
@SerializedName("mapId")
@Expose
private String mapId;
@SerializedName("mapType")
@Expose
private String mapType;
@SerializedName("deviceType")
@Expose
private String deviceType;
@SerializedName("targetDevice")
@Expose
private String targetDevice;
@SerializedName("offUrl")
@Expose
private String offUrl;
@SerializedName("dimUrl")
@Expose
private String dimUrl;
@SerializedName("onUrl")
@Expose
private String onUrl;
@SerializedName("headers")
@Expose
private String headers;
@SerializedName("httpVerb")
@Expose
private String httpVerb;
@SerializedName("contentType")
@Expose
private String contentType;
@SerializedName("contentBody")
@Expose
private String contentBody;
@SerializedName("contentBodyOff")
@Expose
private String contentBodyOff;
@SerializedName("contentBodyDim")
@Expose
private String contentBodyDim;
private DeviceState deviceState;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMapId() {
return mapId;
}
public void setMapId(String mapId) {
this.mapId = mapId;
}
public String getMapType() {
return mapType;
}
public void setMapType(String mapType) {
this.mapType = mapType;
}
public String getDeviceType() {
return deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
public String getTargetDevice() {
return targetDevice;
}
public void setTargetDevice(String targetDevice) {
this.targetDevice = targetDevice;
}
public String getOffUrl() {
return offUrl;
}
public void setOffUrl(String offUrl) {
this.offUrl = offUrl;
}
public String getDimUrl() {
return dimUrl;
}
public void setDimUrl(String dimUrl) {
this.dimUrl = dimUrl;
}
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;
}
public String getUniqueid() {
return uniqueid;
}
public void setUniqueid(String uniqueid) {
this.uniqueid = uniqueid;
}
public String getHeaders() {
return headers;
}
public void setHeaders(String headers) {
this.headers = headers;
}
public String getHttpVerb() {
return httpVerb;
}
public void setHttpVerb(String httpVerb) {
this.httpVerb = httpVerb;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getContentBody() {
return contentBody;
}
public void setContentBody(String contentBody) {
this.contentBody = contentBody;
}
public String getContentBodyOff() {
return contentBodyOff;
}
public void setContentBodyOff(String contentBodyOff) {
this.contentBodyOff = contentBodyOff;
}
public String getContentBodyDim() {
return contentBodyDim;
}
public void setContentBodyDim(String contentBodyDim) {
this.contentBodyDim = contentBodyDim;
}
public DeviceState getDeviceState() {
if(deviceState == null)
deviceState = DeviceState.createDeviceState();
return deviceState;
}
public void setDeviceState(DeviceState deviceState) {
this.deviceState = deviceState;
}
}

View File

@@ -0,0 +1,189 @@
package com.bwssystems.HABridge.dao;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.xml.bind.DatatypeConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.util.BackupHandler;
import com.bwssystems.HABridge.util.JsonTransformer;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.List;
/*
* This is an in memory list to manage the configured devices and saves the list as a JSON string to a file for later
* loading.
*/
public class DeviceRepository extends BackupHandler {
private Map<String, DeviceDescriptor> devices;
private Path repositoryPath;
private Gson gson;
private Integer nextId;
private Logger log = LoggerFactory.getLogger(DeviceRepository.class);
public DeviceRepository(String deviceDb) {
super();
gson =
new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.create();
repositoryPath = null;
repositoryPath = Paths.get(deviceDb);
setupParams(repositoryPath, ".bk", "device.db-");
nextId = 0;
_loadRepository(repositoryPath);
}
public void loadRepository() {
if(repositoryPath != null)
_loadRepository(repositoryPath);
}
private void _loadRepository(Path aPath){
String jsonContent = repositoryReader(aPath);
devices = new HashMap<String, DeviceDescriptor>();
if(jsonContent != null)
{
DeviceDescriptor list[] = gson.fromJson(jsonContent, DeviceDescriptor[].class);
for(int i = 0; i < list.length; i++) {
put(list[i].getId(), list[i]);
if(Integer.decode(list[i].getId()) > nextId) {
nextId = Integer.decode(list[i].getId());
}
}
}
}
public List<DeviceDescriptor> findAll() {
List<DeviceDescriptor> list = new ArrayList<DeviceDescriptor>(devices.values());
return list;
}
public DeviceDescriptor findOne(String id) {
return devices.get(id);
}
private void put(String id, DeviceDescriptor aDescriptor) {
devices.put(id, aDescriptor);
}
public void save(DeviceDescriptor[] descriptors) {
String theNames = "";
for(int i = 0; i < descriptors.length; i++) {
if(descriptors[i].getId() != null && descriptors[i].getId().length() > 0)
devices.remove(descriptors[i].getId());
else {
nextId++;
descriptors[i].setId(String.valueOf(nextId));
}
if(descriptors[i].getUniqueid() == null || descriptors[i].getUniqueid().length() == 0) {
BigInteger bigInt = BigInteger.valueOf(Integer.decode(descriptors[i].getId()));
byte[] theBytes = bigInt.toByteArray();
String hexValue = DatatypeConverter.printHexBinary(theBytes);
descriptors[i].setUniqueid("00:17:88:5E:D3:" + hexValue + "-" + hexValue);
}
put(descriptors[i].getId(), descriptors[i]);
theNames = theNames + " " + descriptors[i].getName() + ", ";
}
String jsonValue = gson.toJson(findAll());
repositoryWriter(jsonValue, repositoryPath);
log.debug("Save device(s): " + theNames);
}
public void renumber() {
List<DeviceDescriptor> list = new ArrayList<DeviceDescriptor>(devices.values());
Iterator<DeviceDescriptor> deviceIterator = list.iterator();
Map<String, DeviceDescriptor> newdevices = new HashMap<String, DeviceDescriptor>();;
nextId = 0;
log.debug("Renumber devices.");
while(deviceIterator.hasNext()) {
nextId++;
DeviceDescriptor theDevice = deviceIterator.next();
theDevice.setId(String.valueOf(nextId));
BigInteger bigInt = BigInteger.valueOf(nextId);
byte[] theBytes = bigInt.toByteArray();
String hexValue = DatatypeConverter.printHexBinary(theBytes);
theDevice.setUniqueid("00:17:88:5E:D3:" + hexValue + "-" + hexValue);
newdevices.put(theDevice.getId(), theDevice);
}
devices = newdevices;
String jsonValue = gson.toJson(findAll());
repositoryWriter(jsonValue, repositoryPath);
}
public String delete(DeviceDescriptor aDescriptor) {
if (aDescriptor != null) {
devices.remove(aDescriptor.getId());
JsonTransformer aRenderer = new JsonTransformer();
String jsonValue = aRenderer.render(findAll());
repositoryWriter(jsonValue, repositoryPath);
return "Device with id '" + aDescriptor.getId() + "' deleted";
} else {
return "Device not found";
}
}
private void repositoryWriter(String content, Path filePath) {
if(Files.exists(filePath) && !Files.isWritable(filePath)){
log.error("Error file is not writable: " + filePath);
return;
}
if(Files.notExists(filePath.getParent())) {
try {
Files.createDirectories(filePath.getParent());
} catch (IOException e) {
log.error("Error creating the directory: " + filePath + " message: " + e.getMessage(), e);
}
}
try {
Path target = null;
if(Files.exists(filePath)) {
target = FileSystems.getDefault().getPath(filePath.getParent().toString(), "device.db.old");
Files.move(filePath, target);
}
Files.write(filePath, content.getBytes(), StandardOpenOption.CREATE);
if(target != null)
Files.delete(target);
} catch (IOException e) {
log.error("Error writing the file: " + filePath + " message: " + e.getMessage(), e);
}
}
private String repositoryReader(Path filePath) {
String content = null;
if(Files.notExists(filePath) || !Files.isReadable(filePath)){
log.warn("Error reading the file: " + filePath + " - Does not exist or is not readable. continuing...");
return null;
}
try {
content = new String(Files.readAllBytes(filePath));
} catch (IOException e) {
log.error("Error reading the file: " + filePath + " message: " + e.getMessage(), e);
}
return content;
}
}

View File

@@ -0,0 +1,18 @@
package com.bwssystems.HABridge.dao;
public class ErrorMessage {
private String message;
public ErrorMessage(String message) {
super();
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

View File

@@ -0,0 +1,299 @@
package com.bwssystems.HABridge.devicemanagmeent;
import static spark.Spark.get;
import static spark.Spark.options;
import static spark.Spark.post;
import static spark.Spark.put;
import static spark.Spark.delete;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.BridgeSettingsDescriptor;
import com.bwssystems.HABridge.DeviceMapTypes;
import com.bwssystems.HABridge.HomeManager;
import com.bwssystems.HABridge.dao.BackupFilename;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.dao.DeviceRepository;
import com.bwssystems.HABridge.dao.ErrorMessage;
import com.bwssystems.HABridge.util.JsonTransformer;
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;
private HomeManager homeManager;
private static final Set<String> supportedVerbs = new HashSet<>(Arrays.asList("get", "put", "post"));
public DeviceResource(BridgeSettingsDescriptor theSettings, HomeManager aHomeManager) {
this.deviceRepository = new DeviceRepository(theSettings.getUpnpDeviceDb());
homeManager = aHomeManager;
setupEndpoints();
}
public DeviceRepository getDeviceRepository() {
return deviceRepository;
}
private void setupEndpoints() {
log.info("HABridge device management service started.... ");
// http://ip_address:port/api/devices CORS request
options(API_CONTEXT, "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
post(API_CONTEXT, "application/json", (request, response) -> {
log.debug("Create a Device(s) - request body: " + request.body());
DeviceDescriptor devices[];
if(request.body().substring(0,1).equalsIgnoreCase("[") == true) {
devices = new Gson().fromJson(request.body(), DeviceDescriptor[].class);
}
else {
devices = new Gson().fromJson("[" + request.body() + "]", DeviceDescriptor[].class);
}
for(int i = 0; i < devices.length; i++) {
if(devices[i].getContentBody() != null ) {
if (devices[i].getContentType() == null || devices[i].getHttpVerb() == null || !supportedVerbs.contains(devices[i].getHttpVerb().toLowerCase())) {
response.status(HttpStatus.SC_BAD_REQUEST);
log.debug("Bad http verb in create a Device(s): " + request.body());
return new ErrorMessage("Bad http verb in create a Device(s): " + request.body() + " ");
}
}
}
deviceRepository.save(devices);
log.debug("Created a Device(s): " + request.body());
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.status(HttpStatus.SC_CREATED);
return devices;
}, new JsonTransformer());
// http://ip_address:port/api/devices/:id CORS request
options(API_CONTEXT + "/:id", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
put (API_CONTEXT + "/:id", "application/json", (request, response) -> {
log.debug("Edit a Device - request body: " + request.body());
DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);
if(deviceRepository.findOne(request.params(":id")) == null){
log.debug("Could not save an edited device, Device Id not found: " + request.params(":id"));
response.status(HttpStatus.SC_BAD_REQUEST);
return new ErrorMessage("Could not save an edited device, Device Id not found: " + request.params(":id") + " ");
}
else
{
log.debug("Saving an edited Device: " + device.getName());
if (device.getDeviceType() != null)
device.setDeviceType(device.getDeviceType());
DeviceDescriptor[] theDevices = new DeviceDescriptor[1];
theDevices[0] = device;
deviceRepository.save(theDevices);
response.status(HttpStatus.SC_OK);
}
return device;
}, 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);
response.status(HttpStatus.SC_OK);
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) {
response.status(HttpStatus.SC_NOT_FOUND);
return new ErrorMessage("Could not find, id: " + request.params(":id") + " ");
}
else
response.status(HttpStatus.SC_OK);
return descriptor;
}, new JsonTransformer());
delete (API_CONTEXT + "/:id", "application/json", (request, response) -> {
String anId = request.params(":id");
log.debug("Delete a device: " + anId);
DeviceDescriptor deleted = deviceRepository.findOne(anId);
if(deleted == null) {
response.status(HttpStatus.SC_NOT_FOUND);
return new ErrorMessage("Could not delete, id: " + anId + " not found. ");
}
else
{
deviceRepository.delete(deleted);
response.status(HttpStatus.SC_OK);
}
return null;
}, new JsonTransformer());
get (API_CONTEXT + "/vera/devices", "application/json", (request, response) -> {
log.debug("Get vera devices");
response.status(HttpStatus.SC_OK);
return homeManager.findResource(DeviceMapTypes.VERA_DEVICE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.VERA_DEVICE[DeviceMapTypes.typeIndex]);
}, new JsonTransformer());
get (API_CONTEXT + "/vera/scenes", "application/json", (request, response) -> {
log.debug("Get vera scenes");
response.status(HttpStatus.SC_OK);
return homeManager.findResource(DeviceMapTypes.VERA_DEVICE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.VERA_SCENE[DeviceMapTypes.typeIndex]);
}, new JsonTransformer());
get (API_CONTEXT + "/harmony/activities", "application/json", (request, response) -> {
log.debug("Get harmony activities");
response.status(HttpStatus.SC_OK);
return homeManager.findResource(DeviceMapTypes.HARMONY_ACTIVITY[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.HARMONY_ACTIVITY[DeviceMapTypes.typeIndex]);
}, new JsonTransformer());
get (API_CONTEXT + "/harmony/show", "application/json", (request, response) -> {
log.debug("Get harmony current activity");
return homeManager.findResource(DeviceMapTypes.HARMONY_ACTIVITY[DeviceMapTypes.typeIndex]).getItems("current_activity");
}, new JsonTransformer());
get (API_CONTEXT + "/harmony/devices", "application/json", (request, response) -> {
log.debug("Get harmony devices");
response.status(HttpStatus.SC_OK);
return homeManager.findResource(DeviceMapTypes.HARMONY_BUTTON[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.HARMONY_BUTTON[DeviceMapTypes.typeIndex]);
}, new JsonTransformer());
get (API_CONTEXT + "/nest/items", "application/json", (request, response) -> {
log.debug("Get nest items");
response.status(HttpStatus.SC_OK);
return homeManager.findResource(DeviceMapTypes.NEST_HOMEAWAY[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.NEST_HOMEAWAY[DeviceMapTypes.typeIndex]);
}, new JsonTransformer());
get (API_CONTEXT + "/hue/devices", "application/json", (request, response) -> {
log.debug("Get hue items");
response.status(HttpStatus.SC_OK);
return homeManager.findResource(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex]);
}, new JsonTransformer());
get (API_CONTEXT + "/hal/devices", "application/json", (request, response) -> {
log.debug("Get hal items");
response.status(HttpStatus.SC_OK);
return homeManager.findResource(DeviceMapTypes.HAL_DEVICE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.HAL_DEVICE[DeviceMapTypes.typeIndex]);
}, new JsonTransformer());
get (API_CONTEXT + "/mqtt/devices", "application/json", (request, response) -> {
log.debug("Get MQTT brokers");
response.status(HttpStatus.SC_OK);
return homeManager.findResource(DeviceMapTypes.MQTT_MESSAGE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.MQTT_MESSAGE[DeviceMapTypes.typeIndex]);
}, new JsonTransformer());
get (API_CONTEXT + "/hass/devices", "application/json", (request, response) -> {
log.debug("Get HomeAssistant Clients");
response.status(HttpStatus.SC_OK);
return homeManager.findResource(DeviceMapTypes.HASS_DEVICE[DeviceMapTypes.typeIndex]).getItems(DeviceMapTypes.HASS_DEVICE[DeviceMapTypes.typeIndex]);
}, new JsonTransformer());
get (API_CONTEXT + "/map/types", "application/json", (request, response) -> {
log.debug("Get map types");
return new DeviceMapTypes().getDeviceMapTypes();
}, new JsonTransformer());
// http://ip_address:port/api/devices/exec/renumber CORS request
options(API_CONTEXT + "/exec/renumber", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "POST");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
post (API_CONTEXT + "/exec/renumber", "application/json", (request, response) -> {
log.debug("Renumber devices.");
deviceRepository.renumber();
return null;
}, new JsonTransformer());
get (API_CONTEXT + "/backup/available", "application/json", (request, response) -> {
log.debug("Get backup filenames");
response.status(HttpStatus.SC_OK);
return deviceRepository.getBackups();
}, new JsonTransformer());
// http://ip_address:port/api/devices/backup/create CORS request
options(API_CONTEXT + "/backup/create", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
put (API_CONTEXT + "/backup/create", "application/json", (request, response) -> {
log.debug("Create backup: " + request.body());
BackupFilename aFilename = new Gson().fromJson(request.body(), BackupFilename.class);
BackupFilename returnFilename = new BackupFilename();
returnFilename.setFilename(deviceRepository.backup(aFilename.getFilename()));
return returnFilename;
}, new JsonTransformer());
// http://ip_address:port/api/devices/backup/delete CORS request
options(API_CONTEXT + "/backup/delete", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "POST");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
post (API_CONTEXT + "/backup/delete", "application/json", (request, response) -> {
log.debug("Delete backup: " + request.body());
BackupFilename aFilename = new Gson().fromJson(request.body(), BackupFilename.class);
if(aFilename != null)
deviceRepository.deleteBackup(aFilename.getFilename());
else
log.warn("No filename given for delete backup.");
return null;
}, new JsonTransformer());
// http://ip_address:port/api/devices/backup/restore CORS request
options(API_CONTEXT + "/backup/restore", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "POST");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html; charset=utf-8");
return "";
});
post (API_CONTEXT + "/backup/restore", "application/json", (request, response) -> {
log.debug("Restore backup: " + request.body());
BackupFilename aFilename = new Gson().fromJson(request.body(), BackupFilename.class);
if(aFilename != null) {
deviceRepository.restoreBackup(aFilename.getFilename());
deviceRepository.loadRepository();
}
else
log.warn("No filename given for restore backup.");
return null;
}, new JsonTransformer());
}
}

View File

@@ -0,0 +1,5 @@
package com.bwssystems.HABridge.devicemanagmeent;
public interface ResourceHandler {
public Object getItems(String type);
}

View File

@@ -0,0 +1,103 @@
package com.bwssystems.HABridge.hue;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.DatatypeConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.java.dev.eval.Expression;
public class BrightnessDecode {
private static final Logger log = LoggerFactory.getLogger(BrightnessDecode.class);
private static final String INTENSITY_PERCENT = "${intensity.percent}";
private static final String INTENSITY_BYTE = "${intensity.byte}";
private static final String INTENSITY_MATH = "${intensity.math(";
private static final String INTENSITY_MATH_VALUE = "X";
private static final String INTENSITY_MATH_CLOSE = ")}";
public static int calculateIntensity(int setIntensity, Integer targetBri, Integer targetBriInc) {
if (targetBri != null) {
setIntensity = targetBri;
} else if (targetBriInc != null) {
if ((setIntensity + targetBriInc) <= 0)
setIntensity = targetBriInc;
else if ((setIntensity + targetBriInc) > 254)
setIntensity = targetBriInc;
else
setIntensity = setIntensity + targetBriInc;
}
return setIntensity;
}
/*
* 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: intensity.byte : 0-254 brightness. this is raw from
* the echo intensity.percent : 0-100, adjusted for the vera
* intensity.math(X*1) : where X is the value from the interface call and
* can use net.java.dev.eval math
*/
public static String replaceIntensityValue(String request, int intensity, boolean isHex) {
if (request == null) {
return null;
}
if (request.contains(INTENSITY_BYTE)) {
if (isHex) {
BigInteger bigInt = BigInteger.valueOf(intensity);
byte[] theBytes = bigInt.toByteArray();
String hexValue = DatatypeConverter.printHexBinary(theBytes);
request = request.replace(INTENSITY_BYTE, hexValue);
} else {
String intensityByte = String.valueOf(intensity);
request = request.replace(INTENSITY_BYTE, intensityByte);
}
} else if (request.contains(INTENSITY_PERCENT)) {
int percentBrightness = (int) Math.round(intensity / 255.0 * 100);
if (isHex) {
BigInteger bigInt = BigInteger.valueOf(percentBrightness);
byte[] theBytes = bigInt.toByteArray();
String hexValue = DatatypeConverter.printHexBinary(theBytes);
request = request.replace(INTENSITY_PERCENT, hexValue);
} else {
String intensityPercent = String.valueOf(percentBrightness);
request = request.replace(INTENSITY_PERCENT, intensityPercent);
}
} else if (request.contains(INTENSITY_MATH)) {
Map<String, BigDecimal> variables = new HashMap<String, BigDecimal>();
String mathDescriptor = request.substring(request.indexOf(INTENSITY_MATH) + INTENSITY_MATH.length(),
request.indexOf(INTENSITY_MATH_CLOSE));
variables.put(INTENSITY_MATH_VALUE, new BigDecimal(intensity));
try {
log.debug("Math eval is: " + mathDescriptor + ", Where " + INTENSITY_MATH_VALUE + " is: "
+ String.valueOf(intensity));
Expression exp = new Expression(mathDescriptor);
BigDecimal result = exp.eval(variables);
Integer endResult = Math.round(result.floatValue());
if (isHex) {
BigInteger bigInt = BigInteger.valueOf(endResult);
byte[] theBytes = bigInt.toByteArray();
String hexValue = DatatypeConverter.printHexBinary(theBytes);
request = request.replace(INTENSITY_MATH + mathDescriptor + INTENSITY_MATH_CLOSE, hexValue);
} else {
request = request.replace(INTENSITY_MATH + mathDescriptor + INTENSITY_MATH_CLOSE,
endResult.toString());
}
} catch (Exception e) {
log.warn("Could not execute Math: " + mathDescriptor, e);
}
}
return request;
}
// Helper Method
public static String calculateReplaceIntensityValue(String request, int theIntensity, Integer targetBri, Integer targetBriInc, boolean isHex) {
return replaceIntensityValue(request, calculateIntensity(theIntensity, targetBri, targetBriInc), isHex);
}
}

View File

@@ -0,0 +1,811 @@
package com.bwssystems.HABridge.hue;
import com.bwssystems.HABridge.BridgeSettingsDescriptor;
import com.bwssystems.HABridge.DeviceMapTypes;
import com.bwssystems.HABridge.HomeManager;
import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.api.UserCreateRequest;
import com.bwssystems.HABridge.api.hue.DeviceResponse;
import com.bwssystems.HABridge.api.hue.DeviceState;
import com.bwssystems.HABridge.api.hue.GroupResponse;
import com.bwssystems.HABridge.api.hue.HueApiResponse;
import com.bwssystems.HABridge.api.hue.HueError;
import com.bwssystems.HABridge.api.hue.HueErrorResponse;
import com.bwssystems.HABridge.api.hue.HuePublicConfig;
import com.bwssystems.HABridge.api.hue.StateChangeBody;
import com.bwssystems.HABridge.api.hue.WhitelistEntry;
import com.bwssystems.HABridge.dao.*;
import com.bwssystems.HABridge.plugins.hue.HueDeviceIdentifier;
import com.bwssystems.HABridge.plugins.hue.HueHome;
import com.bwssystems.HABridge.util.JsonTransformer;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import static spark.Spark.get;
import static spark.Spark.options;
import static spark.Spark.post;
import static spark.Spark.put;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.UUID;
/**
* 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 HUE_CONTEXT = "/api";
private DeviceRepository repository;
private HomeManager homeManager;
private HueHome myHueHome;
private BridgeSettingsDescriptor bridgeSettings;
private Gson aGsonHandler;
private DeviceMapTypes validMapTypes;
public HueMulator(BridgeSettingsDescriptor theBridgeSettings, DeviceRepository aDeviceRepository, HomeManager aHomeManager) {
repository = aDeviceRepository;
validMapTypes = new DeviceMapTypes();
bridgeSettings = theBridgeSettings;
homeManager= aHomeManager;
myHueHome = (HueHome) homeManager.findHome(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex]);
aGsonHandler = new GsonBuilder().create();
}
// This function sets up the sparkjava rest calls for the hue api
public void setupServer() {
log.info("Hue emulator service started....");
// http://ip_address:port/api/{userId}/groups returns json objects of
// all groups configured
get(HUE_CONTEXT + "/:userid/groups", "application/json", (request, response) -> {
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
return basicListHandler("groups", request.params(":userid"), request.ip());
});
// http://ip_address:port/api/{userId}/groups/{groupId} returns json
// object for specified group. Only 0 is supported
get(HUE_CONTEXT + "/:userid/groups/:groupid", "application/json", (request, response) -> {
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
return groupsListHandler(request.params(":groupid"), request.params(":userid"), request.ip());
} , new JsonTransformer());
// http://ip_address:port/api/{userId}/scenes returns json objects of
// all scenes configured
get(HUE_CONTEXT + "/:userid/scenes", "application/json", (request, response) -> {
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
return basicListHandler("scenes", request.params(":userid"), request.ip());
});
// http://ip_address:port/api/{userId}/schedules returns json objects of
// all schedules configured
get(HUE_CONTEXT + "/:userid/schedules", "application/json", (request, response) -> {
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
return basicListHandler("schedules", request.params(":userid"), request.ip());
});
// http://ip_address:port/api/{userId}/sensors returns json objects of
// all sensors configured
get(HUE_CONTEXT + "/:userid/sensors", "application/json", (request, response) -> {
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
return basicListHandler("sensors", request.params(":userid"), request.ip());
});
// http://ip_address:port/api/{userId}/rules returns json objects of all
// rules configured
get(HUE_CONTEXT + "/:userid/rules", "application/json", (request, response) -> {
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
return basicListHandler("rules", request.params(":userid"), request.ip());
});
// http://ip_address:port/api/{userId}/resourcelinks returns json
// objects of all resourcelinks configured
get(HUE_CONTEXT + "/:userid/resourcelinks", "application/json", (request, response) -> {
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
return basicListHandler("resourcelinks", request.params(":userid"), request.ip());
});
// http://ip_address:port/api/{userId}/lights returns json objects of
// all lights configured
get(HUE_CONTEXT + "/:userid/lights", "application/json", (request, response) -> {
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
return lightsListHandler(request.params(":userid"), request.ip());
} , new JsonTransformer());
// http://ip_address:port/api/{userId}/lights/ returns json objects of
// all lights configured
get(HUE_CONTEXT + "/:userid/lights/", "application/json", (request, response) -> {
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
return lightsListHandler(request.params(":userid"), request.ip());
} , new JsonTransformer());
// http://ip_address:port/api CORS request
options(HUE_CONTEXT, "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html");
return "";
});
// http://ip_address:port/api with body of user request returns json
// object for a success of user add
post(HUE_CONTEXT, "application/json", (request, response) -> {
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
return userAdd(request.body(), request.ip(), false);
});
// http://ip_address:port/api/* CORS request
options(HUE_CONTEXT + "/*", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html");
return "";
});
// http://ip_address:port/api/* with body of user request returns json
// object for a success of user add - This method is for Harmony Hub
post(HUE_CONTEXT + "/*", "application/json", (request, response) -> {
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
return userAdd(request.body(), request.ip(), true);
});
// http://ip_address:port/api/config returns json objects for the public
// config when no user is given
get(HUE_CONTEXT + "/config", "application/json", (request, response) -> {
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
return getConfig(null, request.ip());
} , new JsonTransformer());
// http://ip_address:port/api/{userId}/config returns json objects for
// the config
get(HUE_CONTEXT + "/:userid/config", "application/json", (request, response) -> {
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
return getConfig(request.params(":userid"), request.ip());
} , new JsonTransformer());
// http://ip_address:port/api/{userId} returns json objects for the full
// state
get(HUE_CONTEXT + "/:userid", "application/json", (request, response) -> {
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
return getFullState(request.params(":userid"), request.ip());
} , new JsonTransformer());
// http://ip_address:port/api/{userId}/ returns json objects for the full
// state
get(HUE_CONTEXT + "/:userid/", "application/json", (request, response) -> {
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
return getFullState(request.params(":userid"), request.ip());
} , 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) -> {
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
return getLight(request.params(":userid"), request.params(":id"), request.ip());
} , new JsonTransformer());
// http://ip_address:port/api/:userid/lights/:id/bridgeupdatestate CORS
// request
options(HUE_CONTEXT + "/:userid/lights/:id/bridgeupdatestate", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html");
return "";
});
// http://ip_address:port/api/{userId}/lights/{lightId}/bridgeupdatestate
// uses json object to update the internal bridge lights state.
// THIS IS NOT A HUE API CALL... It is for state management if so
// desired.
put(HUE_CONTEXT + "/:userid/lights/:id/bridgeupdatestate", "application/json", (request, response) -> {
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
return updateState(request.params(":userid"), request.params(":id"), request.body(), request.ip());
});
// http://ip_address:port/api/:userid/lights/:id/state CORS request
options(HUE_CONTEXT + "/:userid/lights/:id/state", "application/json", (request, response) -> {
response.status(HttpStatus.SC_OK);
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.header("Access-Control-Allow-Methods", "GET, POST, PUT");
response.header("Access-Control-Allow-Headers", request.headers("Access-Control-Request-Headers"));
response.header("Content-Type", "text/html");
return "";
});
// 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) -> {
response.header("Access-Control-Allow-Origin", request.headers("Origin"));
response.type("application/json");
response.status(HttpStatus.SC_OK);
return changeState(request.params(":userid"), request.params(":id"), request.body(), request.ip());
});
}
private String formatSuccessHueResponse(StateChangeBody stateChanges, String body, String lightId,
DeviceState deviceState, Integer targetBri, Integer targetBriInc) {
String responseString = "[";
boolean notFirstChange = false;
if (body.contains("\"on\"")) {
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/on\":";
if (stateChanges.isOn()) {
responseString = responseString + "true}}";
} else {
responseString = responseString + "false}}";
}
if (deviceState != null) {
deviceState.setOn(stateChanges.isOn());
if(!deviceState.isOn() && deviceState.getBri() == 254)
deviceState.setBri(0);
}
notFirstChange = true;
}
if (body.contains("\"bri\"")) {
if (notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/bri\":" + stateChanges.getBri()
+ "}}";
if (deviceState != null)
deviceState.setBri(stateChanges.getBri());
notFirstChange = true;
}
if (body.contains("\"bri_inc\"")) {
if (notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/bri_inc\":"
+ stateChanges.getBri_inc() + "}}";
// INFO: Bright inc check for deviceState needs to be outside of
// this method
if (deviceState != null)
deviceState.setBri(BrightnessDecode.calculateIntensity(deviceState.getBri(), targetBri, targetBriInc));
notFirstChange = true;
}
if (body.contains("\"ct\"")) {
if (notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/ct\":" + stateChanges.getCt()
+ "}}";
if (deviceState != null)
deviceState.setCt(stateChanges.getCt());
notFirstChange = true;
}
if (body.contains("\"xy\"")) {
if (notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/xy\":" + stateChanges.getXy()
+ "}}";
if (deviceState != null)
deviceState.setXy(stateChanges.getXy());
notFirstChange = true;
}
if (body.contains("\"hue\"")) {
if (notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/hue\":" + stateChanges.getHue()
+ "}}";
if (deviceState != null)
deviceState.setHue(stateChanges.getHue());
notFirstChange = true;
}
if (body.contains("\"sat\"")) {
if (notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/sat\":" + stateChanges.getSat()
+ "}}";
if (deviceState != null)
deviceState.setSat(stateChanges.getSat());
notFirstChange = true;
}
if (body.contains("\"ct_inc\"")) {
if (notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/ct_inc\":"
+ stateChanges.getCt_inc() + "}}";
if (deviceState != null)
deviceState.setCt(deviceState.getCt() + stateChanges.getCt_inc());
notFirstChange = true;
}
if (body.contains("\"xy_inc\"")) {
if (notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/xy_inc\":"
+ stateChanges.getXy_inc() + "}}";
if (deviceState != null)
deviceState.setXy(stateChanges.getXy());
notFirstChange = true;
}
if (body.contains("\"hue_inc\"")) {
if (notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/hue_inc\":"
+ stateChanges.getHue_inc() + "}}";
if (deviceState != null)
deviceState.setHue(deviceState.getHue() + stateChanges.getHue_inc());
notFirstChange = true;
}
if (body.contains("\"sat_inc\"")) {
if (notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/sat_inc\":"
+ stateChanges.getSat_inc() + "}}";
if (deviceState != null)
deviceState.setSat(deviceState.getSat() + stateChanges.getSat_inc());
notFirstChange = true;
}
if (body.contains("\"effect\"")) {
if (notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/effect\":"
+ stateChanges.getEffect() + "}}";
if (deviceState != null)
deviceState.setEffect(stateChanges.getEffect());
notFirstChange = true;
}
if (body.contains("\"transitiontime\"")) {
if (notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/transitiontime\":"
+ stateChanges.getTransitiontime() + "}}";
// if(deviceState != null)
// deviceState.setTransitiontime(state.getTransitiontime());
notFirstChange = true;
}
if (body.contains("\"alert\"")) {
if (notFirstChange)
responseString = responseString + ",";
responseString = responseString + "{\"success\":{\"/lights/" + lightId + "/state/alert\":"
+ stateChanges.getAlert() + "}}";
if (deviceState != null)
deviceState.setAlert(stateChanges.getAlert());
notFirstChange = true;
}
if(deviceState.isOn() && deviceState.getBri() <= 0)
deviceState.setBri(254);
if(!deviceState.isOn() && (targetBri != null || targetBriInc != null))
deviceState.setOn(true);
responseString = responseString + "]";
return responseString;
}
private String getNewUserID() {
UUID uid = UUID.randomUUID();
StringTokenizer st = new StringTokenizer(uid.toString(), "-");
String newUser = "";
while (st.hasMoreTokens()) {
newUser = newUser + st.nextToken();
}
return newUser;
}
private HueError[] validateWhitelistUser(String aUser, boolean strict) {
String validUser = null;
boolean found = false;
if (aUser != null && !aUser.equalsIgnoreCase("undefined") && !aUser.equalsIgnoreCase("null")
&& !aUser.equalsIgnoreCase("")) {
if (bridgeSettings.getWhitelist() != null) {
Set<String> theUserIds = bridgeSettings.getWhitelist().keySet();
Iterator<String> userIterator = theUserIds.iterator();
while (userIterator.hasNext()) {
validUser = userIterator.next();
if (validUser.equals(aUser))
found = true;
}
}
if (!found && !strict) {
if (bridgeSettings.getWhitelist() == null) {
Map<String, WhitelistEntry> awhitelist = new HashMap<>();
bridgeSettings.setWhitelist(awhitelist);
}
bridgeSettings.getWhitelist().put(aUser, WhitelistEntry.createEntry("auto insert user"));
bridgeSettings.setSettingsChanged(true);
found = true;
}
}
if (!found) {
log.debug("Valudate user, No User supplied");
return HueErrorResponse.createResponse("1", "/api/" + aUser, "unauthorized user", null, null, null).getTheErrors();
}
return null;
}
private Boolean filterByRequester(String requesterFilterList, String anAddress) {
if (requesterFilterList == null || requesterFilterList.length() == 0)
return true;
HashMap<String, String> addressMap;
addressMap = new HashMap<String, String>();
if (requesterFilterList.contains(",")) {
String[] theArray = requesterFilterList.split(",");
for (String v : theArray) {
addressMap.put(v.trim(), v.trim());
}
} else
addressMap.put(requesterFilterList.trim(), requesterFilterList.trim());
if (addressMap.containsKey(anAddress))
return true;
return false;
}
private String basicListHandler(String type, String userId, String requestIp) {
log.debug("hue " + type + " list requested: " + userId + " from " + requestIp);
HueError[] theErrors = validateWhitelistUser(userId, false);
if (theErrors != null)
return aGsonHandler.toJson(theErrors);
return "{}";
}
private Object groupsListHandler(String groupId, String userId, String requestIp) {
log.debug("hue group 0 list requested: " + userId + " from " + requestIp);
HueError[] theErrors = null;
theErrors = validateWhitelistUser(userId, false);
if (theErrors == null) {
if (groupId.equalsIgnoreCase("0")) {
GroupResponse theResponse = GroupResponse.createGroupResponse(repository.findAll());
return theResponse;
}
theErrors = HueErrorResponse.createResponse("3", userId + "/groups/" + groupId, "Object not found", null, null, null).getTheErrors();
}
return theErrors;
}
private Object lightsListHandler(String userId, String requestIp) {
HueError[] theErrors = null;
Map<String, DeviceResponse> deviceResponseMap = null;
if (bridgeSettings.isTraceupnp())
log.info("Traceupnp: hue lights list requested: " + userId + " from " + requestIp);
log.debug("hue lights list requested: " + userId + " from " + requestIp);
theErrors = validateWhitelistUser(userId, false);
if (theErrors == null) {
List<DeviceDescriptor> deviceList = repository.findAll();
deviceResponseMap = new HashMap<String, DeviceResponse>();
for (DeviceDescriptor device : deviceList) {
DeviceResponse deviceResponse = null;
if ((device.getMapType() != null && device.getMapType().equalsIgnoreCase(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex]))) {
HueDeviceIdentifier deviceId = aGsonHandler.fromJson(device.getOnUrl(), HueDeviceIdentifier.class);
deviceResponse = myHueHome.getHueDeviceInfo(deviceId, device);
}
if (deviceResponse == null)
deviceResponse = DeviceResponse.createResponse(device);
deviceResponseMap.put(device.getId(), deviceResponse);
}
}
if (theErrors != null)
return theErrors;
return deviceResponseMap;
}
private String userAdd(String body, String ipAddress, boolean followingSlash) {
UserCreateRequest aNewUser = null;
String newUser = null;
String aDeviceType = null;
if (bridgeSettings.isTraceupnp())
log.info("Traceupnp: hue api user create requested: " + body + " from " + ipAddress);
log.debug("hue api user create requested: " + body + " from " + ipAddress);
if (body != null && !body.isEmpty()) {
aNewUser = aGsonHandler.fromJson(body, UserCreateRequest.class);
newUser = aNewUser.getUsername();
aDeviceType = aNewUser.getDevicetype();
}
if (newUser == null)
newUser = getNewUserID();
validateWhitelistUser(newUser, false);
if (aDeviceType == null)
aDeviceType = "<not given>";
if (bridgeSettings.isTraceupnp())
log.info("Traceupnp: hue api user create requested for device type: " + aDeviceType + " and username: "
+ newUser + (followingSlash ? " /api/ called" : ""));
log.debug("hue api user create requested for device type: " + aDeviceType + " and username: " + newUser + (followingSlash ? " /api/ called" : ""));
return "[{\"success\":{\"username\":\"" + newUser + "\"}}]";
}
private Object getConfig(String userId, String ipAddress) {
if (bridgeSettings.isTraceupnp())
log.info("Traceupnp: hue api/:userid/config config requested: " + userId + " from " + ipAddress);
log.debug("hue api config requested: " + userId + " from " + ipAddress);
if (validateWhitelistUser(userId, true) != null) {
log.debug("Valudate user, No User supplied, returning public config");
HuePublicConfig apiResponse = HuePublicConfig.createConfig("Philips hue",
bridgeSettings.getUpnpConfigAddress(), bridgeSettings.getHubversion());
return apiResponse;
}
HueApiResponse apiResponse = new HueApiResponse("Philips hue", bridgeSettings.getUpnpConfigAddress(),
bridgeSettings.getWhitelist(), bridgeSettings.getHubversion());
return apiResponse.getConfig();
}
@SuppressWarnings("unchecked")
private Object getFullState(String userId, String ipAddress) {
log.debug("hue api full state requested: " + userId + " from " + ipAddress);
HueError[] theErrors = validateWhitelistUser(userId, false);
if (theErrors != null)
return theErrors;
HueApiResponse apiResponse = new HueApiResponse("Philips hue", bridgeSettings.getUpnpConfigAddress(),
bridgeSettings.getWhitelist(), bridgeSettings.getHubversion());
Object aReturn = this.lightsListHandler(userId, ipAddress);
Map<String, DeviceResponse> deviceList = new HashMap<String, DeviceResponse>();
if(aReturn.getClass() == deviceList.getClass()) {
deviceList = (Map<String, DeviceResponse>) aReturn;
apiResponse.setLights(deviceList);
}
else {
return aReturn;
}
return apiResponse;
}
private Object getLight(String userId, String lightId, String ipAddress) {
log.debug("hue light requested: " + lightId + " for user: " + userId + " from " + ipAddress);
HueError[] theErrors = validateWhitelistUser(userId, false);
if (theErrors != null)
return theErrors;
DeviceDescriptor device = repository.findOne(lightId);
if (device == null) {
// response.status(HttpStatus.SC_NOT_FOUND);
return HueErrorResponse.createResponse("3", "/api/" + userId + "/lights/" + lightId, "Object not found", null, null, null).getTheErrors();
} else {
log.debug("found device named: " + device.getName());
}
DeviceResponse lightResponse = null;
if ((device.getMapType() != null && device.getMapType().equalsIgnoreCase(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex]))) {
HueDeviceIdentifier deviceId = aGsonHandler.fromJson(device.getOnUrl(), HueDeviceIdentifier.class);
lightResponse = myHueHome.getHueDeviceInfo(deviceId, device);
} else
lightResponse = DeviceResponse.createResponse(device);
return lightResponse;
}
private String updateState(String userId, String lightId, String body, String ipAddress) {
String responseString = null;
StateChangeBody theStateChanges = null;
DeviceState state = null;
Integer targetBri = null;
Integer targetBriInc = null;
log.debug("Update state requested: " + userId + " from " + ipAddress + " body: " + body);
HueError[] theErrors = validateWhitelistUser(userId, false);
if (theErrors != null)
return aGsonHandler.toJson(theErrors);
theStateChanges = aGsonHandler.fromJson(body, StateChangeBody.class);
if (theStateChanges == null) {
log.warn("Could not parse state change body. Light state not changed.");
return aGsonHandler.toJson(HueErrorResponse.createResponse("2", "/lights/" + lightId,
"Could not parse state change body.", null, null, null).getTheErrors(), HueError[].class);
}
DeviceDescriptor device = repository.findOne(lightId);
if (device == null) {
log.warn("Could not find device: " + lightId + " for hue state change request: " + userId + " from "
+ ipAddress + " body: " + body);
return aGsonHandler.toJson(HueErrorResponse.createResponse("3", "/lights/" + lightId,
"Could not find device.", "/lights/" + lightId, null, null).getTheErrors(), HueError[].class);
}
if (body.contains("\"bri_inc\""))
targetBriInc = new Integer(theStateChanges.getBri_inc());
else if (body.contains("\"bri\"")) {
targetBri = new Integer(theStateChanges.getBri());
}
state = device.getDeviceState();
if (state == null)
state = DeviceState.createDeviceState();
responseString = this.formatSuccessHueResponse(theStateChanges, body, lightId, state, targetBri, targetBriInc);
device.setDeviceState(state);
return responseString;
}
private String changeState(String userId, String lightId, String body, String ipAddress) {
String responseString = null;
String url = null;
StateChangeBody theStateChanges = null;
DeviceState state = null;
Integer targetBri = null;
Integer targetBriInc = null;
MultiCommandUtil aMultiUtil = new MultiCommandUtil();
aMultiUtil.setTheDelay(bridgeSettings.getButtonsleep());
aMultiUtil.setDelayDefault(bridgeSettings.getButtonsleep());
aMultiUtil.setSetCount(1);
log.debug("hue state change requested: " + userId + " from " + ipAddress + " body: " + body);
HueError[] theErrors = validateWhitelistUser(userId, false);
if (theErrors != null)
return aGsonHandler.toJson(theErrors);
theStateChanges = aGsonHandler.fromJson(body, StateChangeBody.class);
if (theStateChanges == null) {
log.warn("Could not parse state change body. Light state not changed.");
return aGsonHandler.toJson(HueErrorResponse.createResponse("2", "/lights/" + lightId,
"Could not parse state change body.", null, null, null).getTheErrors(), HueError[].class);
}
DeviceDescriptor device = repository.findOne(lightId);
if (device == null) {
log.warn("Could not find device: " + lightId + " for hue state change request: " + userId + " from "
+ ipAddress + " body: " + body);
return aGsonHandler.toJson(HueErrorResponse.createResponse("3", "/lights/" + lightId,
"Could not find device.", "/lights/" + lightId, null, null).getTheErrors(), HueError[].class);
}
if (body.contains("\"bri_inc\"")) {
targetBriInc = new Integer(theStateChanges.getBri_inc());
}
else if (body.contains("\"bri\"")) {
targetBri = new Integer(theStateChanges.getBri());
}
state = device.getDeviceState();
if (state == null)
state = DeviceState.createDeviceState();
if (targetBri != null || targetBriInc != null) {
url = device.getDimUrl();
if (url == null || url.length() == 0)
url = device.getOnUrl();
} else {
if (theStateChanges.isOn()) {
url = device.getOnUrl();
} else if (!theStateChanges.isOn()) {
url = device.getOffUrl();
}
}
// code for backwards compatibility
if(!(device.getMapType() != null && device.getMapType().equalsIgnoreCase(DeviceMapTypes.HUE_DEVICE[DeviceMapTypes.typeIndex]))) {
if(url == null)
url = device.getOnUrl();
}
if (url != null && !url.equals("")) {
if (!url.startsWith("[")) {
if (url.startsWith("{\"item"))
url = "[" + url + "]";
else {
if(url.startsWith("{"))
url = "[{\"item\":" + url + "}]";
else
url = "[{\"item\":\"" + url + "\"}]";
}
} else if(!url.startsWith("[{\"item\""))
url = "[{\"item\":" + url + "}]";
log.debug("Decode Json for url items: " + url);
CallItem[] callItems = null;
try {
callItems = aGsonHandler.fromJson(url, CallItem[].class);
} catch(JsonSyntaxException e) {
log.warn("Could not decode Json for url items: " + lightId + " for hue state change request: " + userId + " from "
+ ipAddress + " body: " + body + " url items: " + url);
return aGsonHandler.toJson(HueErrorResponse.createResponse("3", "/lights/" + lightId,
"Could decode json in request", "/lights/" + lightId, null, null).getTheErrors(), HueError[].class);
}
for (int i = 0; callItems != null && i < callItems.length; i++) {
if(!filterByRequester(callItems[i].getFilterIPs(), ipAddress)) {
log.debug("filter for requester address not present in list: " + callItems[i].getFilterIPs() + " with request ip of: " + ipAddress);
continue;
}
if (callItems[i].getCount() != null && callItems[i].getCount() > 0)
aMultiUtil.setSetCount(callItems[i].getCount());
else
aMultiUtil.setSetCount(1);
// code for backwards compatibility
if((callItems[i].getType() == null || callItems[i].getType().trim().isEmpty())) {
if(validMapTypes.validateType(device.getMapType()))
callItems[i].setType(device.getMapType().trim());
else if(validMapTypes.validateType(device.getDeviceType()))
callItems[i].setType(device.getDeviceType().trim());
else
callItems[i].setType(DeviceMapTypes.CUSTOM_DEVICE[DeviceMapTypes.typeIndex]);
}
if (callItems[i].getType() != null) {
for (int x = 0; x < aMultiUtil.getSetCount(); x++) {
if (x > 0 || i > 0) {
try {
Thread.sleep(aMultiUtil.getTheDelay());
} catch (InterruptedException e) {
// ignore
}
}
if (callItems[i].getDelay() != null && callItems[i].getDelay() > 0)
aMultiUtil.setTheDelay(callItems[i].getDelay());
else
aMultiUtil.setTheDelay(aMultiUtil.getDelayDefault());
responseString = homeManager.findHome(callItems[i].getType().trim()).deviceHandler(callItems[i], aMultiUtil, lightId, state.getBri(), targetBri, targetBriInc, device, body);
}
}
}
} else {
log.warn("Could not find url: " + lightId + " for hue state change request: " + userId + " from "
+ ipAddress + " body: " + body);
responseString = aGsonHandler.toJson(HueErrorResponse.createResponse("3", "/lights/" + lightId,
"Could not find url.", "/lights/" + lightId, null, null).getTheErrors(), HueError[].class);
}
if (responseString == null || !responseString.contains("[{\"error\":")) {
responseString = this.formatSuccessHueResponse(theStateChanges, body, lightId, state, targetBri, targetBriInc);
device.setDeviceState(state);
}
return responseString;
}
}

View File

@@ -0,0 +1,8 @@
package com.bwssystems.HABridge.hue;
import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
public interface HueMulatorHandler {
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity, Integer targetBri, Integer targetBriInc, DeviceDescriptor device, String body);
}

View File

@@ -0,0 +1,31 @@
package com.bwssystems.HABridge.hue;
public class MultiCommandUtil {
private Integer setCount;
private Integer theDelay;
private Integer delayDefault;
public Integer getSetCount() {
return setCount;
}
public void setSetCount(Integer setCount) {
this.setCount = setCount;
}
public Integer getTheDelay() {
return theDelay;
}
public void setTheDelay(Integer theDelay) {
this.theDelay = theDelay;
}
public Integer getDelayDefault() {
return delayDefault;
}
public void setDelayDefault(Integer delayDefault) {
this.delayDefault = delayDefault;
}
}

View File

@@ -0,0 +1,191 @@
package com.bwssystems.HABridge.plugins.NestBridge;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.BridgeSettingsDescriptor;
import com.bwssystems.HABridge.DeviceMapTypes;
import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.BrightnessDecode;
import com.bwssystems.HABridge.hue.MultiCommandUtil;
import com.bwssystems.nest.controller.Home;
import com.bwssystems.nest.controller.Nest;
import com.bwssystems.nest.controller.NestSession;
import com.bwssystems.nest.controller.Thermostat;
import com.bwssystems.nest.protocol.error.LoginException;
import com.bwssystems.nest.protocol.status.WhereDetail;
import com.bwssystems.nest.protocol.status.WhereItem;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class NestHome implements com.bwssystems.HABridge.Home {
private static final Logger log = LoggerFactory.getLogger(NestHome.class);
private NestSession theSession;
private Nest theNest;
private ArrayList<NestItem> nestItems;
private Gson aGsonHandler;
private Boolean isFarenheit;
private Boolean validNest;
public NestHome(BridgeSettingsDescriptor bridgeSettings) {
super();
createHome(bridgeSettings);
}
@Override
public Object getItems(String type) {
if(!validNest)
return null;
if(nestItems == null) {
nestItems = new ArrayList<NestItem>();
Set<String> homeNames = theNest.getHomeNames();
Home aHome = null;
NestItem anItem = null;
for(String name : homeNames) {
aHome = theNest.getHome(name);
anItem = new NestItem();
anItem.setId(name);
anItem.setName(aHome.getDetail().getName());
anItem.setType("Home");
anItem.setLocation(aHome.getDetail().getLocation());
nestItems.add(anItem);
}
Thermostat thermo = null;
Set<String> thermoNames = theNest.getThermostatNames();
for(String name : thermoNames) {
thermo = theNest.getThermostat(name);
anItem = new NestItem();
anItem.setId(name);
anItem.setType("Thermostat");
String where = null;
String homeName= null;
Boolean found = false;
for(String aHomeName : homeNames) {
WhereDetail aDetail = theNest.getWhere(aHomeName);
ListIterator<WhereItem> anIterator = aDetail.getWheres().listIterator();
while(anIterator.hasNext()) {
WhereItem aWhereItem = (WhereItem) anIterator.next();
if(aWhereItem.getWhereId().equals(thermo.getDeviceDetail().getWhereId())) {
where = aWhereItem.getName();
homeName = theNest.getHome(aHomeName).getDetail().getName();
found = true;
break;
}
}
if(found)
break;
}
anItem.setName(where + "(" + name.substring(name.length() - 4) + ")");
anItem.setLocation(where + " - " + homeName);
nestItems.add(anItem);
}
}
return nestItems;
}
@Override
public void closeHome() {
if(theSession != null) {
theNest.endNestSession();
theNest = null;
theSession = null;
nestItems = null;
}
}
@Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity,
Integer targetBri,Integer targetBriInc, DeviceDescriptor device, String body) {
String responseString = null;
log.debug("executing HUE api request to set away for nest " + anItem.getType() + ": " + anItem.getItem().toString());
if(!validNest) {
log.warn("Should not get here, no Nest available");
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Should not get here, no Nest available\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
} else if (anItem.getType() != null && anItem.getType().trim().equalsIgnoreCase(DeviceMapTypes.NEST_HOMEAWAY[DeviceMapTypes.typeIndex])) {
NestInstruction homeAway = null;
if(anItem.getItem().isJsonObject())
homeAway = aGsonHandler.fromJson(anItem.getItem(), NestInstruction.class);
else
homeAway = aGsonHandler.fromJson(anItem.getItem().getAsString(), NestInstruction.class);
theNest.getHome(homeAway.getName()).setAway(homeAway.getAway());
} else if (anItem.getType() != null && anItem.getType().trim().equalsIgnoreCase(DeviceMapTypes.NEST_THERMO_SET[DeviceMapTypes.typeIndex])) {
NestInstruction thermoSetting = null;
if(anItem.getItem().isJsonObject())
thermoSetting = aGsonHandler.fromJson(anItem.getItem(), NestInstruction.class);
else
thermoSetting = aGsonHandler.fromJson(anItem.getItem().getAsString(), NestInstruction.class);
if (thermoSetting.getControl().equalsIgnoreCase("temp")) {
if (targetBri != null) {
if (isFarenheit)
thermoSetting
.setTemp(
String.valueOf((Double
.parseDouble(BrightnessDecode.calculateReplaceIntensityValue(thermoSetting.getTemp(),
intensity, targetBri, targetBriInc, false)) - 32.0) / 1.8));
else
thermoSetting
.setTemp(
String.valueOf(Double.parseDouble(BrightnessDecode.calculateReplaceIntensityValue(thermoSetting.getTemp(),
intensity, targetBri, targetBriInc, false))));
log.debug("Setting thermostat: " + thermoSetting.getName() + " to "
+ thermoSetting.getTemp() + "C");
theNest.getThermostat(thermoSetting.getName())
.setTargetTemperature(Float.parseFloat(thermoSetting.getTemp()));
}
} else if (thermoSetting.getControl().contains("range")
|| thermoSetting.getControl().contains("heat")
|| thermoSetting.getControl().contains("cool")
|| thermoSetting.getControl().contains("off")) {
log.debug("Setting thermostat target type: " + thermoSetting.getName() + " to "
+ thermoSetting.getControl());
theNest.getThermostat(thermoSetting.getName()).setTargetType(thermoSetting.getControl());
} else if (thermoSetting.getControl().contains("fan")) {
log.debug("Setting thermostat fan mode: " + thermoSetting.getName() + " to "
+ thermoSetting.getControl().substring(4));
theNest.getThermostat(thermoSetting.getName())
.setFanMode(thermoSetting.getControl().substring(4));
} else {
log.warn("no valid Nest control info: " + thermoSetting.getControl());
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"no valid Nest control info\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
}
}
return responseString;
}
@Override
public com.bwssystems.HABridge.Home createHome(BridgeSettingsDescriptor bridgeSettings) {
theSession = null;
theNest = null;
nestItems = null;
validNest = bridgeSettings.isValidNest();
aGsonHandler = null;
log.info("Nest Home created." + (validNest ? "" : " No Nest configured."));
if(validNest) {
aGsonHandler = new GsonBuilder().create();
isFarenheit = bridgeSettings.isFarenheit();
try {
theSession = new NestSession(bridgeSettings.getNestuser(), bridgeSettings.getNestpwd());
theNest = new Nest(theSession);
} catch (LoginException e) {
log.error("Caught Login Exception, setting Nest to invalid....");
validNest = false;
theSession = null;
}
}
return this;
}
}

View File

@@ -0,0 +1,33 @@
package com.bwssystems.HABridge.plugins.NestBridge;
public class NestInstruction {
private String name;
private Boolean away;
private String control;
private String temp;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getAway() {
return away;
}
public void setAway(Boolean away) {
this.away = away;
}
public String getControl() {
return control;
}
public void setControl(String control) {
this.control = control;
}
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
}

View File

@@ -0,0 +1,41 @@
package com.bwssystems.HABridge.plugins.NestBridge;
import java.io.UnsupportedEncodingException;
public class NestItem {
private String name;
private String id;
private String type;
private String location;
public String getName() {
return name;
}
public void setName(String name) {
byte ptext[];
String theLabel = new String(name);
try {
ptext = theLabel.getBytes("ISO-8859-1");
this.name = new String(ptext, "UTF-8");
} catch (UnsupportedEncodingException e) {
this.name = theLabel;
}
}
public String getId() {
return id;
}
public void setId(String anid) {
id = anid;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}

View File

@@ -0,0 +1,81 @@
package com.bwssystems.HABridge.plugins.exec;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.BridgeSettingsDescriptor;
import com.bwssystems.HABridge.Home;
import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.BrightnessDecode;
import com.bwssystems.HABridge.hue.MultiCommandUtil;
public class CommandHome implements Home {
private static final Logger log = LoggerFactory.getLogger(CommandHome.class);
public CommandHome(BridgeSettingsDescriptor bridgeSettings) {
super();
createHome(bridgeSettings);
}
@Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int itensity, Integer targetBri, Integer targetBriInc, DeviceDescriptor device, String body) {
log.debug("Exec Request called with url: " + anItem.getItem().getAsString());
String responseString = null;
String intermediate;
if (anItem.getItem().toString().contains("exec://"))
intermediate = anItem.getItem().getAsString().substring(anItem.getItem().getAsString().indexOf("://") + 3);
else
intermediate = anItem.getItem().getAsString();
String anError = doExecRequest(intermediate,
BrightnessDecode.calculateIntensity(itensity, targetBri, targetBriInc), lightId);
if (anError != null) {
responseString = anError;
}
return responseString;
}
private String doExecRequest(String anItem, int intensity, String lightId) {
log.debug("Executing request: " + anItem);
String responseString = null;
if (anItem != null && !anItem.equalsIgnoreCase("")) {
try {
Process p = Runtime.getRuntime().exec(BrightnessDecode.replaceIntensityValue(anItem, intensity, false));
log.debug("Process running: " + p.isAlive());
} catch (IOException e) {
log.warn("Could not execute request: " + anItem, e);
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Error on calling out to device\", \"parameter\": \"/lights/" + lightId
+ "state\"}}]";
}
} else {
log.warn("Could not execute request. Request is empty.");
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Error on calling out to device\", \"parameter\": \"/lights/" + lightId
+ "state\"}}]";
}
return responseString;
}
@Override
public Home createHome(BridgeSettingsDescriptor bridgeSettings) {
log.info("Command Home for system program execution created.");
return this;
}
@Override
public Object getItems(String type) {
// noop
return null;
}
@Override
public void closeHome() {
// noop
}
}

View File

@@ -0,0 +1,17 @@
package com.bwssystems.HABridge.plugins.hal;
import java.util.List;
import com.google.gson.annotations.SerializedName;
public class DeviceElements {
@SerializedName(value="DeviceElements", alternate={"SceneElements", "GroupElements", "HVACElements", "MacroElements", "IrElements", "IrButtons"})
private List<DeviceName> DeviceElements;
public List<DeviceName> getDeviceElements() {
return DeviceElements;
}
public void setDeviceElements(List<DeviceName> deviceElements) {
DeviceElements = deviceElements;
}
}

View File

@@ -0,0 +1,17 @@
package com.bwssystems.HABridge.plugins.hal;
import com.google.gson.annotations.SerializedName;
public class DeviceName {
@SerializedName(value="DeviceName", alternate={"SceneName", "GroupName", "HVACName", "MacroName", "IrName", "IrButton"})
private String DeviceName;
public String getDeviceName() {
return DeviceName;
}
public void setDeviceName(String deviceName) {
DeviceName = deviceName;
}
}

View File

@@ -0,0 +1,39 @@
package com.bwssystems.HABridge.plugins.hal;
public class HalDevice {
private String haldevicetype;
private String haldevicename;
private String haladdress;
private String halname;
private DeviceElements buttons;
public String getHaldevicetype() {
return haldevicetype;
}
public void setHaldevicetype(String haldevicetype) {
this.haldevicetype = haldevicetype;
}
public String getHaldevicename() {
return haldevicename;
}
public void setHaldevicename(String haldevicename) {
this.haldevicename = haldevicename;
}
public String getHaladdress() {
return haladdress;
}
public void setHaladdress(String haladdress) {
this.haladdress = haladdress;
}
public String getHalname() {
return halname;
}
public void setHalname(String halname) {
this.halname = halname;
}
public DeviceElements getButtons() {
return buttons;
}
public void setButtons(DeviceElements buttons) {
this.buttons = buttons;
}
}

View File

@@ -0,0 +1,167 @@
package com.bwssystems.HABridge.plugins.hal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.BridgeSettingsDescriptor;
import com.bwssystems.HABridge.Home;
import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.api.NameValue;
import com.bwssystems.HABridge.api.hue.HueError;
import com.bwssystems.HABridge.api.hue.HueErrorResponse;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.BrightnessDecode;
import com.bwssystems.HABridge.hue.MultiCommandUtil;
import com.bwssystems.HABridge.plugins.http.HTTPHandler;
import com.google.gson.Gson;
public class HalHome implements Home {
private static final Logger log = LoggerFactory.getLogger(HalHome.class);
private Map<String, HalInfo> hals;
private Boolean validHal;
private HTTPHandler anHttpHandler;
public HalHome(BridgeSettingsDescriptor bridgeSettings) {
super();
createHome(bridgeSettings);
}
@Override
public Object getItems(String type) {
if(!validHal)
return null;
log.debug("consolidating devices for hues");
List<HalDevice> theResponse = null;
Iterator<String> keys = hals.keySet().iterator();
List<HalDevice> deviceList = new ArrayList<HalDevice>();
while(keys.hasNext()) {
String key = keys.next();
theResponse = hals.get(key).getLights();
if(theResponse != null)
addHalDevices(deviceList, theResponse, key);
else {
log.warn("Cannot get lights for Hal with name: " + key + ", skipping this HAL.");
continue;
}
theResponse = hals.get(key).getAppliances();
if(theResponse != null)
addHalDevices(deviceList, theResponse, key);
else
log.warn("Cannot get appliances for Hal with name: " + key);
theResponse = hals.get(key).getTheatre();
if(theResponse != null)
addHalDevices(deviceList, theResponse, key);
else
log.warn("Cannot get theatre for Hal with name: " + key);
theResponse = hals.get(key).getCustom();
if(theResponse != null)
addHalDevices(deviceList, theResponse, key);
else
log.warn("Cannot get custom for Hal with name: " + key);
theResponse = hals.get(key).getHVAC();
if(theResponse != null)
addHalDevices(deviceList, theResponse, key);
else
log.warn("Cannot get HVAC for Hal with name: " + key);
theResponse = hals.get(key).getHome(key);
if(theResponse != null)
addHalDevices(deviceList, theResponse, key);
else
log.warn("Cannot get Homes for Hal with name: " + key);
theResponse = hals.get(key).getGroups();
if(theResponse != null)
addHalDevices(deviceList, theResponse, key);
else
log.warn("Cannot get Groups for Hal with name: " + key);
theResponse = hals.get(key).getMacros();
if(theResponse != null)
addHalDevices(deviceList, theResponse, key);
else
log.warn("Cannot get Macros for Hal with name: " + key);
theResponse = hals.get(key).getScenes();
if(theResponse != null)
addHalDevices(deviceList, theResponse, key);
else
log.warn("Cannot get Scenes for Hal with name: " + key);
theResponse = hals.get(key).getButtons();
if(theResponse != null)
addHalDevices(deviceList, theResponse, key);
else
log.warn("Cannot get Buttons for Hal with name: " + key);
}
return deviceList;
}
private Boolean addHalDevices(List<HalDevice> theDeviceList, List<HalDevice> theSourceList, String theKey) {
if(!validHal)
return null;
Iterator<HalDevice> devices = theSourceList.iterator();
while(devices.hasNext()) {
HalDevice theDevice = devices.next();
HalDevice aNewHalDevice = new HalDevice();
aNewHalDevice.setHaldevicetype(theDevice.getHaldevicetype());
aNewHalDevice.setHaldevicename(theDevice.getHaldevicename());
aNewHalDevice.setButtons(theDevice.getButtons());
aNewHalDevice.setHaladdress(hals.get(theKey).getHalAddress().getIp());
aNewHalDevice.setHalname(theKey);
theDeviceList.add(aNewHalDevice);
}
anHttpHandler = new HTTPHandler();
return true;
}
@Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity,
Integer targetBri,Integer targetBriInc, DeviceDescriptor device, String body) {
log.debug("executing HUE api request to HAL Http " + anItem.getItem().getAsString());
String responseString = null;
String anUrl = BrightnessDecode.calculateReplaceIntensityValue(anItem.getItem().getAsString(),
intensity, targetBri, targetBriInc, false);
String aBody;
aBody = BrightnessDecode.calculateReplaceIntensityValue(anItem.getHttpBody(),
intensity, targetBri, targetBriInc, false);
// make call
if (anHttpHandler.doHttpRequest(anUrl, anItem.getHttpVerb(), anItem.getContentType(), aBody,
new Gson().fromJson(anItem.getHttpHeaders(), NameValue[].class)) == null) {
log.warn("Error on calling url to change device state: " + anUrl);
responseString = new Gson().toJson(HueErrorResponse.createResponse("6", "/lights/" + lightId,
"Error on calling url to change device state", "/lights/"
+ lightId + "state", null, null).getTheErrors(), HueError[].class);
}
return responseString;
}
@Override
public Home createHome(BridgeSettingsDescriptor bridgeSettings) {
validHal = bridgeSettings.isValidHal();
log.info("HAL Home created." + (validHal ? "" : " No HAL devices configured."));
if(!validHal)
return null;
hals = new HashMap<String, HalInfo>();
Iterator<NamedIP> theList = bridgeSettings.getHaladdress().getDevices().iterator();
while(theList.hasNext()) {
NamedIP aHal = theList.next();
try {
hals.put(aHal.getName(), new HalInfo(aHal, bridgeSettings.getHaltoken()));
} catch (Exception e) {
log.error("Cannot get hal client (" + aHal.getName() + ") setup, Exiting with message: " + e.getMessage(), e);
return null;
}
}
return this;
}
@Override
public void closeHome() {
// noop
}
}

View File

@@ -0,0 +1,182 @@
package com.bwssystems.HABridge.plugins.hal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.HABridge.plugins.http.HTTPHandler;
import com.bwssystems.HABridge.util.TextStringFormatter;
import com.google.gson.Gson;
public class HalInfo {
private static final Logger log = LoggerFactory.getLogger(HalInfo.class);
private static final String DEVICE_REQUEST = "/DeviceData!DeviceCmd=GetNames!DeviceType=";
private static final String HVAC_REQUEST = "/HVACData!HVACCmd=GetNames";
private static final String GROUP_REQUEST = "/GroupData!GroupCmd=GetNames";
private static final String MACRO_REQUEST = "/MacroData!MacroCmd=GetNames";
private static final String SCENE_REQUEST = "/SceneData!SceneCmd=GetNames";
private static final String IRDATA_REQUEST = "/IrData!IRCmd=GetNames";
private static final String IRBUTTON_REQUEST = "/IrData!IRCmd=GetButtons!IrDevice=";
private static final String TOKEN_REQUEST = "?Token=";
private static final String LIGHT_REQUEST = "Light";
private static final String APPL_REQUEST = "Appl";
// private static final String VIDEO_REQUEST = "Video";
private static final String THEATRE_REQUEST = "Theatre";
private static final String CUSTOM_REQUEST = "Custom";
private static final String HVAC_TYPE = "HVAC";
private static final String HOME_TYPE = "Home";
private static final String GROUP_TYPE = "Group";
private static final String MACRO_TYPE = "Macro";
private static final String SCENE_TYPE = "Scene";
private static final String IRDATA_TYPE = "IrData";
private HTTPHandler httpClient;
private NamedIP halAddress;
private String theToken;
public HalInfo(NamedIP addressName, String aGivenToken) {
super();
httpClient = new HTTPHandler();
halAddress = addressName;
theToken = aGivenToken;
}
public List<HalDevice> getLights() {
return getHalDevices(DEVICE_REQUEST + LIGHT_REQUEST + TOKEN_REQUEST, LIGHT_REQUEST);
}
public List<HalDevice> getAppliances() {
return getHalDevices(DEVICE_REQUEST + APPL_REQUEST + TOKEN_REQUEST, APPL_REQUEST);
}
public List<HalDevice> getTheatre() {
return getHalDevices(DEVICE_REQUEST + THEATRE_REQUEST + TOKEN_REQUEST, THEATRE_REQUEST);
}
public List<HalDevice> getCustom() {
return getHalDevices(DEVICE_REQUEST + CUSTOM_REQUEST + TOKEN_REQUEST, CUSTOM_REQUEST);
}
public List<HalDevice> getHVAC() {
return getHalDevices(HVAC_REQUEST + TOKEN_REQUEST, HVAC_TYPE);
}
public List<HalDevice> getGroups() {
return getHalDevices(GROUP_REQUEST + TOKEN_REQUEST, GROUP_TYPE);
}
public List<HalDevice> getMacros() {
return getHalDevices(MACRO_REQUEST + TOKEN_REQUEST, MACRO_TYPE);
}
public List<HalDevice> getScenes() {
return getHalDevices(SCENE_REQUEST + TOKEN_REQUEST, SCENE_TYPE);
}
public List<HalDevice> getButtons() {
List<HalDevice> irDataDevices = getHalDevices(IRDATA_REQUEST + TOKEN_REQUEST, IRDATA_TYPE);
return getDeviceButtons(irDataDevices);
}
public List<HalDevice> getHome(String theDeviceName) {
List<HalDevice> deviceList = null;
deviceList = new ArrayList<HalDevice>();
HalDevice aNewHalDevice = new HalDevice();
aNewHalDevice.setHaldevicetype(HOME_TYPE);
aNewHalDevice.setHaldevicename(theDeviceName);
deviceList.add(aNewHalDevice);
return deviceList;
}
private List<HalDevice> getHalDevices(String apiType, String deviceType) {
DeviceElements theHalApiResponse = null;
List<HalDevice> deviceList = null;
String theUrl = null;
String theData;
theUrl = "http://" + halAddress.getIp() + apiType + theToken;
theData = httpClient.doHttpRequest(theUrl, null, null, null, null);
if(theData != null) {
log.debug("GET " + deviceType + " HalApiResponse - data: " + theData);
theHalApiResponse = new Gson().fromJson(theData, DeviceElements.class);
if(theHalApiResponse.getDeviceElements() == null) {
StatusDescription theStatus = new Gson().fromJson(theData, StatusDescription.class);
if(theStatus.getStatus() == null) {
log.warn("Cannot get an devices for type " + deviceType + " for hal " + halAddress.getName() + " as response is not parsable.");
}
else {
log.warn("Cannot get an devices for type " + deviceType + " for hal " + halAddress.getName() + ". Status: " + theStatus.getStatus() + ", with description: " + theStatus.getDescription());
}
return deviceList;
}
deviceList = new ArrayList<HalDevice>();
Iterator<DeviceName> theDeviceNames = theHalApiResponse.getDeviceElements().iterator();
while(theDeviceNames.hasNext()) {
DeviceName theDevice = theDeviceNames.next();
HalDevice aNewHalDevice = new HalDevice();
aNewHalDevice.setHaldevicetype(deviceType);
aNewHalDevice.setHaldevicename(theDevice.getDeviceName());
deviceList.add(aNewHalDevice);
}
}
else {
log.warn("Get Hal device types " + deviceType + " for " + halAddress.getName() + " - returned null, no data.");
}
return deviceList;
}
private List<HalDevice> getDeviceButtons(List<HalDevice> theIrDevices) {
DeviceElements theHalApiResponse = null;
List<HalDevice> deviceList = null;
String theUrl = null;
String theData;
if(theIrDevices == null)
return null;
Iterator<HalDevice> theHalDevices = theIrDevices.iterator();
deviceList = new ArrayList<HalDevice>();
while (theHalDevices.hasNext()) {
HalDevice theHalDevice = theHalDevices.next();
theUrl = "http://" + halAddress.getIp() + IRBUTTON_REQUEST + TextStringFormatter.forQuerySpaceUrl(theHalDevice.getHaldevicename()) + TOKEN_REQUEST + theToken;
theData = httpClient.doHttpRequest(theUrl, null, null, null, null);
if (theData != null) {
log.debug("GET IrData for IR Device " + theHalDevice.getHaldevicename() + " HalApiResponse - data: " + theData);
theHalApiResponse = new Gson().fromJson(theData, DeviceElements.class);
if (theHalApiResponse.getDeviceElements() == null) {
StatusDescription theStatus = new Gson().fromJson(theData, StatusDescription.class);
if (theStatus.getStatus() == null) {
log.warn("Cannot get buttons for IR Device " + theHalDevice.getHaldevicename() + " for hal "
+ halAddress.getName() + " as response is not parsable.");
} else {
log.warn("Cannot get buttons for IR Device " + theHalDevice.getHaldevicename() + " for hal "
+ halAddress.getName() + ". Status: " + theStatus.getStatus() + ", with description: "
+ theStatus.getDescription());
}
return deviceList;
}
theHalDevice.setButtons(theHalApiResponse);
deviceList.add(theHalDevice);
} else {
log.warn("Get Hal buttons for IR Device " + theHalDevice.getHaldevicename() + " for "
+ halAddress.getName() + " - returned null, no data.");
}
}
return deviceList;
}
public NamedIP getHalAddress() {
return halAddress;
}
public void setHalAddress(NamedIP halAddress) {
this.halAddress = halAddress;
}
}

View File

@@ -0,0 +1,18 @@
package com.bwssystems.HABridge.plugins.hal;
public class StatusDescription {
private String Status;
private String Description;
public String getStatus() {
return Status;
}
public void setStatus(String status) {
Status = status;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
}

View File

@@ -0,0 +1,46 @@
package com.bwssystems.HABridge.plugins.harmony;
public class ButtonPress {
private String device;
private String button;
private Integer delay;
private Integer count;
private String hub;
public String getDevice() {
return device;
}
public void setDevice(String device) {
this.device = device;
}
public String getButton() {
return button;
}
public void setButton(String button) {
this.button = button;
}
public Integer getDelay() {
return delay;
}
public void setDelay(Integer delay) {
this.delay = delay;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Boolean isValid() {
if (device != null && !device.isEmpty()){
if (button != null && !button.isEmpty())
return true;
}
return false;
}
public String getHub() {
return hub;
}
public void setHub(String hub) {
this.hub = hub;
}
}

View File

@@ -0,0 +1,69 @@
package com.bwssystems.HABridge.plugins.harmony;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.whistlingfish.harmony.config.Activity;
import net.whistlingfish.harmony.config.Device;
import net.whistlingfish.harmony.config.HarmonyConfig;
public class DevModeResponse {
final Logger log = LoggerFactory.getLogger(DevModeResponse.class);
private final static String powerOff = "PowerOff";
private HarmonyConfig harmonyConfig;
private Activity currentActivity;
public DevModeResponse() {
super();
harmonyConfig = HarmonyConfig.parse(dataReader("/config.data"));
this.currentActivity = harmonyConfig.getActivityByName(powerOff);
}
public Activity getCurrentActivity() {
return currentActivity;
}
public void setCurrentActivity(Activity currentActivity) {
this.currentActivity = currentActivity;
}
public List<Activity> getActivities() {
return harmonyConfig.getActivities();
}
public List<Device> getDevices() {
return harmonyConfig.getDevices();
}
public HarmonyConfig getConfig() {
return harmonyConfig;
}
private String dataReader(String filePath) {
String content = null;
try {
InputStream input = getClass().getResourceAsStream(filePath);
OutputStream out = new ByteArrayOutputStream();
int read;
byte[] bytes = new byte[1024];
while ((read = input.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
content = out.toString();
} catch (IOException e) {
log.error("Error reading the file: " + filePath + " message: " + e.getMessage(), e);
}
return content;
}
}

View File

@@ -0,0 +1,31 @@
package com.bwssystems.HABridge.plugins.harmony;
import java.io.UnsupportedEncodingException;
import net.whistlingfish.harmony.config.Activity;
public class HarmonyActivity {
private String hub;
private Activity activity;
public String getHub() {
return hub;
}
public void setHub(String hub) {
this.hub = hub;
}
public Activity getActivity() {
return activity;
}
public void setActivity(Activity activity) {
byte ptext[];
String theLabel = activity.getLabel();
try {
ptext = theLabel.getBytes("ISO-8859-1");
activity.setLabel(new String(ptext, "UTF-8"));
} catch (UnsupportedEncodingException e) {
activity.setLabel(theLabel);
}
this.activity = activity;
}
}

View File

@@ -0,0 +1,30 @@
package com.bwssystems.HABridge.plugins.harmony;
import java.io.UnsupportedEncodingException;
import net.whistlingfish.harmony.config.Device;
public class HarmonyDevice {
private Device device;
private String hub;
public Device getDevice() {
return device;
}
public void setDevice(Device device) {
byte ptext[];
String theLabel = device.getLabel();
try {
ptext = theLabel.getBytes("ISO-8859-1");
device.setLabel(new String(ptext, "UTF-8"));
} catch (UnsupportedEncodingException e) {
device.setLabel(theLabel);
}
this.device = device;
}
public String getHub() {
return hub;
}
public void setHub(String hub) {
this.hub = hub;
}
}

View File

@@ -0,0 +1,132 @@
package com.bwssystems.HABridge.plugins.harmony;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.whistlingfish.harmony.HarmonyClient;
import net.whistlingfish.harmony.config.Activity;
import net.whistlingfish.harmony.config.Device;
import net.whistlingfish.harmony.config.HarmonyConfig;
public class HarmonyHandler {
private static final Logger log = LoggerFactory.getLogger(HarmonyHandler.class);
private HarmonyClient harmonyClient;
private Boolean noopCalls;
private Boolean devMode;
private DevModeResponse devResponse;
public HarmonyHandler(HarmonyClient theClient, Boolean noopCallsSetting, DevModeResponse devResponseSetting) {
super();
noopCalls = noopCallsSetting;
devMode = Boolean.TRUE;
devResponse = null;
if(devResponseSetting == null)
devMode = Boolean.FALSE;
else
devResponse = devResponseSetting;
harmonyClient = theClient;
}
public List<Activity> getActivities() {
log.debug("Harmony api activities list requested.");
if(devMode)
return devResponse.getActivities();
return harmonyClient.getConfig().getActivities();
}
public List<Device> getDevices() {
log.debug("Harmony api device list requested.");
if(devMode)
return devResponse.getDevices();
return harmonyClient.getConfig().getDevices();
}
public HarmonyConfig getConfig() {
log.debug("Harmony api config requested.");
if(devMode)
return devResponse.getConfig();
return harmonyClient.getConfig();
}
public Activity getCurrentActivity() {
log.debug("Harmony api current sctivity requested.");
if(devMode)
return devResponse.getCurrentActivity();
return harmonyClient.getCurrentActivity();
}
public Boolean startActivity(RunActivity anActivity) {
log.debug("Harmony api start activity requested for: " + anActivity.getName() + " noop mode: " + noopCalls);
if (anActivity.isValid()) {
try {
if (noopCalls || devMode) {
if(devMode)
{
if(anActivity != null)
devResponse.setCurrentActivity(devResponse.getConfig().getActivityByName(anActivity.getName()));
}
log.info("noop mode: Harmony api start activity requested for: " + anActivity.getName());
}
else
harmonyClient.startActivity(Integer.parseInt(anActivity.getName()));
} catch (IllegalArgumentException e) {
try {
if (!noopCalls)
harmonyClient.startActivityByName(anActivity.getName());
} catch (IllegalArgumentException ei) {
log.error("Error in finding activity: " + anActivity.getName());
return false;
}
}
} else {
log.error("Error in finding activity: " + anActivity.getName());
return false;
}
return true;
}
public Boolean pressButton(ButtonPress aDeviceButton) {
log.debug("Harmony api press a button requested for device: " + aDeviceButton.getDevice() + " and a for button: " + aDeviceButton.getButton() + " noop mode: " + noopCalls);
if (aDeviceButton.isValid()) {
try {
if (noopCalls || devMode) {
log.info("noop mode: Harmony api press a button requested for device: " + aDeviceButton.getDevice() + " and a for button: " + aDeviceButton.getButton());
}
else
harmonyClient.pressButton(Integer.parseInt(aDeviceButton.getDevice()), aDeviceButton.getButton());
} catch (IllegalArgumentException e) {
try {
if (!noopCalls)
harmonyClient.pressButton(aDeviceButton.getDevice(), aDeviceButton.getButton());
} catch (IllegalArgumentException ei) {
log.error("Error in finding device: " + aDeviceButton.getDevice() +" and a button: " + aDeviceButton.getButton());
return false;
}
}
} else {
log.error("Error in finding device: " + aDeviceButton.getDevice() +" and a button: " + aDeviceButton.getButton());
return false;
}
return true;
}
public void shutdown() {
log.debug("Harmony api shutdown requested.");
if(devMode)
return;
harmonyClient.disconnect();
harmonyClient = null;
}
}

View File

@@ -0,0 +1,254 @@
package com.bwssystems.HABridge.plugins.harmony;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.BridgeSettingsDescriptor;
import com.bwssystems.HABridge.DeviceMapTypes;
import com.bwssystems.HABridge.Home;
import com.bwssystems.HABridge.IpList;
import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.MultiCommandUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.whistlingfish.harmony.config.Activity;
import net.whistlingfish.harmony.config.Device;
public class HarmonyHome implements Home {
private static final Logger log = LoggerFactory.getLogger(HarmonyHome.class);
private Map<String, HarmonyServer> hubs;
private Boolean isDevMode;
private Boolean validHarmony;
private Gson aGsonHandler;
public HarmonyHome(BridgeSettingsDescriptor bridgeSettings) {
super();
createHome(bridgeSettings);
}
@Override
public void closeHome() {
if(!validHarmony)
return;
if(isDevMode || hubs == null)
return;
Iterator<String> keys = hubs.keySet().iterator();
while(keys.hasNext()) {
String key = keys.next();
hubs.get(key).getMyHarmony().shutdown();
}
hubs = null;
}
public HarmonyHandler getHarmonyHandler(String aName) {
if(!validHarmony)
return null;
HarmonyHandler aHandler = null;
if(aName == null || aName.equals("")) {
aName = "default";
}
if(hubs.get(aName) == null) {
Set<String> keys = hubs.keySet();
if(!keys.isEmpty()) {
aHandler = hubs.get(keys.toArray()[0]).getMyHarmony();
}
else
aHandler = null;
}
else
aHandler = hubs.get(aName).getMyHarmony();
return aHandler;
}
public List<HarmonyActivity> getActivities() {
Iterator<String> keys = hubs.keySet().iterator();
ArrayList<HarmonyActivity> activityList = new ArrayList<HarmonyActivity>();
if(!validHarmony)
return null;
while(keys.hasNext()) {
String key = keys.next();
Iterator<Activity> activities = hubs.get(key).getMyHarmony().getActivities().iterator();
while(activities.hasNext()) {
HarmonyActivity anActivity = new HarmonyActivity();
anActivity.setActivity(activities.next());
anActivity.setHub(key);
activityList.add(anActivity);
}
}
return activityList;
}
public List<HarmonyActivity> getCurrentActivities() {
Iterator<String> keys = hubs.keySet().iterator();
ArrayList<HarmonyActivity> activityList = new ArrayList<HarmonyActivity>();
if(!validHarmony)
return null;
while(keys.hasNext()) {
String key = keys.next();
Activity theActivity = hubs.get(key).getMyHarmony().getCurrentActivity();
HarmonyActivity anActivity = new HarmonyActivity();
anActivity.setActivity(theActivity);
anActivity.setHub(key);
activityList.add(anActivity);
}
return activityList;
}
public List<HarmonyDevice> getDevices() {
Iterator<String> keys = hubs.keySet().iterator();
ArrayList<HarmonyDevice> deviceList = new ArrayList<HarmonyDevice>();
if(!validHarmony)
return null;
while(keys.hasNext()) {
String key = keys.next();
Iterator<Device> devices = hubs.get(key).getMyHarmony().getDevices().iterator();
while(devices.hasNext()) {
HarmonyDevice aDevice = new HarmonyDevice();
aDevice.setDevice(devices.next());
aDevice.setHub(key);
deviceList.add(aDevice);
}
}
return deviceList;
}
@Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity,
Integer targetBri,Integer targetBriInc, DeviceDescriptor device, String body) {
String responseString = null;
log.debug("executing HUE api request to change " + anItem.getType() + " to Harmony: " + device.getName());
if(!validHarmony) {
log.warn("Should not get here, no harmony configured");
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Should not get here, no harmony configured\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
} else {
if(anItem.getType().trim().equalsIgnoreCase(DeviceMapTypes.HARMONY_ACTIVITY[DeviceMapTypes.typeIndex]))
{
RunActivity anActivity = null;
if(anItem.getItem().isJsonObject())
anActivity = aGsonHandler.fromJson(anItem.getItem(), RunActivity.class);
else
anActivity = aGsonHandler.fromJson(anItem.getItem().getAsString(), RunActivity.class);
if(anActivity.getHub() == null || anActivity.getHub().isEmpty())
anActivity.setHub(device.getTargetDevice());
HarmonyHandler myHarmony = getHarmonyHandler(anActivity.getHub());
if (myHarmony == null) {
log.warn("Should not get here, no harmony hub available");
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Should not get here, no harmony hub available\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
} else {
myHarmony.startActivity(anActivity);
}
} else if(anItem.getType().trim().equalsIgnoreCase(DeviceMapTypes.HARMONY_BUTTON[DeviceMapTypes.typeIndex])) {
String url = anItem.getItem().toString();
if (url.substring(0, 1).equalsIgnoreCase("{")) {
url = "[" + url + "]";
}
ButtonPress[] deviceButtons = aGsonHandler.fromJson(url, ButtonPress[].class);
Integer theCount = 1;
for(int z = 0; z < deviceButtons.length; z++) {
if(deviceButtons[z].getCount() != null && deviceButtons[z].getCount() > 0)
theCount = deviceButtons[z].getCount();
for(int y = 0; y < theCount; y++) {
if( y > 0 || z > 0) {
try {
Thread.sleep(aMultiUtil.getTheDelay());
} catch (InterruptedException e) {
// ignore
}
}
if (anItem.getDelay() != null && anItem.getDelay() > 0)
aMultiUtil.setTheDelay(anItem.getDelay());
else
aMultiUtil.setTheDelay(aMultiUtil.getDelayDefault());
log.debug("pressing button: " + deviceButtons[z].getDevice() + " - " + deviceButtons[z].getButton() + " - iteration: " + String.valueOf(z) + " - count: " + String.valueOf(y));
if(deviceButtons[z].getHub() == null || deviceButtons[z].getHub().isEmpty())
deviceButtons[z].setHub(device.getTargetDevice());
HarmonyHandler myHarmony = getHarmonyHandler(deviceButtons[z].getHub());
if (myHarmony == null)
log.warn("Button Press - Should not get here, no harmony hub available");
else
myHarmony.pressButton(deviceButtons[z]);
}
}
}
}
return responseString;
}
@Override
public Home createHome(BridgeSettingsDescriptor bridgeSettings) {
isDevMode = Boolean.parseBoolean(System.getProperty("dev.mode", "false"));
validHarmony = bridgeSettings.isValidHarmony();
log.info("Harmony Home created." + (validHarmony ? "" : " No Harmony devices configured.") + (isDevMode ? " DevMode is set." : ""));
if(validHarmony || isDevMode) {
hubs = new HashMap<String, HarmonyServer>();
aGsonHandler =
new GsonBuilder()
.create();
if(isDevMode) {
NamedIP devModeIp = new NamedIP();
devModeIp.setIp("10.10.10.10");
devModeIp.setName("devMode");
List<NamedIP> theList = new ArrayList<NamedIP>();
theList.add(devModeIp);
IpList thedevList = new IpList();
thedevList.setDevices(theList);
bridgeSettings.setHarmonyAddress(thedevList);
}
Iterator<NamedIP> theList = bridgeSettings.getHarmonyAddress().getDevices().iterator();
while(theList.hasNext() && validHarmony) {
NamedIP aHub = theList.next();
boolean loopControl = true;
int retryCount = 0;
while(loopControl) {
try {
hubs.put(aHub.getName(), HarmonyServer.setup(bridgeSettings, isDevMode, aHub));
loopControl = false;
} catch (Exception e) {
if(retryCount > 3) {
log.error("Cannot get harmony client (" + aHub.getName() + ") setup, Exiting with message: " + e.getMessage(), e);
loopControl = false;
} else {
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// ignore
}
}
retryCount++;
}
}
}
if(hubs.isEmpty())
validHarmony = false;
}
return this;
}
@Override
public Object getItems(String type) {
if(validHarmony) {
if(type.equalsIgnoreCase(DeviceMapTypes.HARMONY_ACTIVITY[DeviceMapTypes.typeIndex]))
return getActivities();
if(type.equalsIgnoreCase(DeviceMapTypes.HARMONY_BUTTON[DeviceMapTypes.typeIndex]))
return getDevices();
if(type.equalsIgnoreCase("current_activity"))
return getCurrentActivities();
}
return null;
}
}

View File

@@ -0,0 +1,86 @@
package com.bwssystems.HABridge.plugins.harmony;
import static java.lang.String.format;
import javax.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.BridgeSettingsDescriptor;
import com.bwssystems.HABridge.NamedIP;
import com.google.inject.Guice;
import com.google.inject.Injector;
import net.whistlingfish.harmony.ActivityChangeListener;
import net.whistlingfish.harmony.HarmonyClient;
import net.whistlingfish.harmony.HarmonyClientModule;
import net.whistlingfish.harmony.config.Activity;
import net.whistlingfish.harmony.protocol.OAReplyProvider;
public class HarmonyServer {
@Inject
private HarmonyClient harmonyClient;
private HarmonyHandler myHarmony;
private DevModeResponse devResponse;
private OAReplyProvider dummyProvider;
private NamedIP myNameAndIP;
private Boolean isDevMode;
private Logger log = LoggerFactory.getLogger(HarmonyServer.class);
public HarmonyServer(NamedIP theHarmonyAddress) {
super();
myHarmony = null;
dummyProvider = null;
myNameAndIP = theHarmonyAddress;
isDevMode = false;
}
public static HarmonyServer setup(BridgeSettingsDescriptor bridgeSettings, Boolean harmonyDevMode, NamedIP theHarmonyAddress) throws Exception {
if(!bridgeSettings.isValidHarmony() && harmonyDevMode) {
return new HarmonyServer(theHarmonyAddress);
}
Injector injector = null;
if(!harmonyDevMode)
injector = Guice.createInjector(new HarmonyClientModule());
HarmonyServer mainObject = new HarmonyServer(theHarmonyAddress);
if(!harmonyDevMode)
injector.injectMembers(mainObject);
mainObject.execute(bridgeSettings, harmonyDevMode);
return mainObject;
}
private void execute(BridgeSettingsDescriptor mySettings, Boolean harmonyDevMode) throws Exception {
Boolean noopCalls = Boolean.parseBoolean(System.getProperty("noop.calls", "false"));
isDevMode = harmonyDevMode;
String modeString = "";
if(dummyProvider != null)
log.debug("something is very wrong as dummyProvider is not null...");
if(isDevMode)
modeString = " (development mode)";
else if(noopCalls)
modeString = " (no op calls to harmony)";
log.info("setup initiated " + modeString + "....");
if(isDevMode)
{
harmonyClient = null;
devResponse = new DevModeResponse();
}
else {
devResponse = null;
harmonyClient.addListener(new ActivityChangeListener() {
@Override
public void activityStarted(Activity activity) {
log.info(format("activity changed: [%d] %s", activity.getId(), activity.getLabel()));
}
});
harmonyClient.connect(myNameAndIP.getIp());
}
myHarmony = new HarmonyHandler(harmonyClient, noopCalls, devResponse);
}
public HarmonyHandler getMyHarmony() {
return myHarmony;
}
}

View File

@@ -0,0 +1,27 @@
package com.bwssystems.HABridge.plugins.harmony;
public class RunActivity {
private String name;
private String hub;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean isValid() {
if (name != null && !name.isEmpty())
return true;
return false;
}
public String getHub() {
return hub;
}
public void setHub(String hub) {
this.hub = hub;
}
}

View File

@@ -0,0 +1,29 @@
package com.bwssystems.HABridge.plugins.hass;
import java.util.Map;
import com.google.gson.JsonElement;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Field {
@SerializedName("fields")
@Expose
private Map<String, JsonElement> fields;
public Field(Map<String, JsonElement> fields) {
super();
this.fields = fields;
}
public Map<String, JsonElement> getFields() {
return fields;
}
public void setFields(Map<String, JsonElement> fields) {
this.fields = fields;
}
}

View File

@@ -0,0 +1,31 @@
package com.bwssystems.HABridge.plugins.hass;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
public class FieldDeserializer implements JsonDeserializer<Field> {
private Map<String, JsonElement> fields;
@Override
public Field deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext ctx)
{
JsonObject obj = json.getAsJsonObject();
String theKey;
fields = new HashMap<String, JsonElement>();
for(Entry<String, JsonElement> entry:obj.entrySet()){
theKey = entry.getKey();
JsonElement theRawDetail = obj.get(theKey);
fields.put(theKey, theRawDetail);
}
return new Field(fields);
}
}

View File

@@ -0,0 +1,32 @@
package com.bwssystems.HABridge.plugins.hass;
public class HassCommand {
private String entityId;
private String hassName;
private String state;
private String bri;
public String getEntityId() {
return entityId;
}
public void setEntityId(String entityId) {
this.entityId = entityId;
}
public String getHassName() {
return hassName;
}
public void setHassName(String hassName) {
this.hassName = hassName;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getBri() {
return bri;
}
public void setBri(String bri) {
this.bri = bri;
}
}

View File

@@ -0,0 +1,39 @@
package com.bwssystems.HABridge.plugins.hass;
public class HassDevice {
private State deviceState;
private String deviceName;
private String domain;
private String hassaddress;
private String hassname;
public State getDeviceState() {
return deviceState;
}
public void setDeviceState(State deviceState) {
this.deviceState = deviceState;
}
public String getDeviceName() {
return deviceName;
}
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getHassaddress() {
return hassaddress;
}
public void setHassaddress(String hassaddress) {
this.hassaddress = hassaddress;
}
public String getHassname() {
return hassname;
}
public void setHassname(String hassname) {
this.hassname = hassname;
}
}

View File

@@ -0,0 +1,160 @@
package com.bwssystems.HABridge.plugins.hass;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.BridgeSettingsDescriptor;
import com.bwssystems.HABridge.Home;
import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.BrightnessDecode;
import com.bwssystems.HABridge.hue.MultiCommandUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
public class HassHome implements Home {
private static final Logger log = LoggerFactory.getLogger(HassHome.class);
private Map<String, HomeAssistant> hassMap;
private Boolean validHass;
private Gson aGsonHandler;
public HassHome(BridgeSettingsDescriptor bridgeSettings) {
super();
createHome(bridgeSettings);
}
@Override
public Home createHome(BridgeSettingsDescriptor bridgeSettings) {
hassMap = null;
aGsonHandler = null;
validHass = bridgeSettings.isValidHass();
log.info("HomeAssistant Home created." + (validHass ? "" : " No HomeAssistants configured."));
if(validHass) {
hassMap = new HashMap<String,HomeAssistant>();
aGsonHandler =
new GsonBuilder()
.create();
Iterator<NamedIP> theList = bridgeSettings.getHassaddress().getDevices().iterator();
while(theList.hasNext() && validHass) {
NamedIP aHass = theList.next();
try {
hassMap.put(aHass.getName(), new HomeAssistant(aHass));
} catch (Exception e) {
log.error("Cannot get hass (" + aHass.getName() + ") setup, Exiting with message: " + e.getMessage(), e);
validHass = false;
}
}
}
return this;
}
public HomeAssistant getHomeAssistant(String aName) {
if(!validHass)
return null;
HomeAssistant aHomeAssistant;
if(aName == null || aName.equals("")) {
aHomeAssistant = null;
log.debug("Cannot get HomeAssistant for name as it is empty.");
}
else {
aHomeAssistant = hassMap.get(aName);
log.debug("Retrieved a HomeAssistant for name: " + aName);
}
return aHomeAssistant;
}
@Override
public Object getItems(String type) {
log.debug("consolidating devices for hass");
if(!validHass)
return null;
List<State> theResponse = null;
Iterator<String> keys = hassMap.keySet().iterator();
List<HassDevice> deviceList = new ArrayList<HassDevice>();
while(keys.hasNext()) {
String key = keys.next();
theResponse = hassMap.get(key).getDevices();
if(theResponse != null)
addHassDevices(deviceList, theResponse, key);
else {
log.warn("Cannot get devices for HomeAssistant with name: " + key + ", skipping this Hass.");
continue;
}
}
return deviceList;
}
private Boolean addHassDevices(List<HassDevice> theDeviceList, List<State> theSourceList, String theKey) {
if(!validHass)
return null;
Iterator<State> devices = theSourceList.iterator();
while(devices.hasNext()) {
State theDevice = devices.next();
HassDevice aNewHassDevice = new HassDevice();
aNewHassDevice.setDeviceState(theDevice);
aNewHassDevice.setHassaddress(hassMap.get(theKey).getHassAddress().getIp());
aNewHassDevice.setHassname(theKey);
JsonElement friendlyName = theDevice.getAttributes().get("friendly_name");
if(friendlyName == null)
aNewHassDevice.setDeviceName(theDevice.getEntityId());
else
aNewHassDevice.setDeviceName(friendlyName.getAsString());
aNewHassDevice.setDomain(theDevice.getEntityId().substring(0, theDevice.getEntityId().indexOf(".")));
theDeviceList.add(aNewHassDevice);
}
return true;
}
@Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity,
Integer targetBri,Integer targetBriInc, DeviceDescriptor device, String body) {
String theReturn = null;
log.debug("executing HUE api request to send message to HomeAssistant: " + anItem.getItem().toString());
if(!validHass) {
log.warn("Should not get here, no HomeAssistant clients configured");
theReturn = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Should not get here, no HomeAssistants configured\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
} else {
HassCommand hassCommand = null;
if(anItem.getItem().isJsonObject())
hassCommand = aGsonHandler.fromJson(anItem.getItem(), HassCommand.class);
else
hassCommand = aGsonHandler.fromJson(anItem.getItem().getAsString(), HassCommand.class);
hassCommand.setBri(BrightnessDecode.replaceIntensityValue(hassCommand.getBri(),
BrightnessDecode.calculateIntensity(intensity, targetBri, targetBriInc), false));
HomeAssistant homeAssistant = getHomeAssistant(hassCommand.getHassName());
if (homeAssistant == null) {
log.warn("Should not get here, no HomeAssistants available");
theReturn = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Should not get here, no HiomeAssistant clients available\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
} else {
log.debug("calling HomeAssistant: " + hassCommand.getHassName() + " - "
+ hassCommand.getEntityId() + " - " + hassCommand.getState() + " - " + hassCommand.getBri());
homeAssistant.callCommand(hassCommand);
}
}
return theReturn;
}
@Override
public void closeHome() {
if(!validHass)
return;
Iterator<String> keys = hassMap.keySet().iterator();
while(keys.hasNext()) {
String key = keys.next();
hassMap.get(key).closeClient();
}
}
}

View File

@@ -0,0 +1,99 @@
package com.bwssystems.HABridge.plugins.hass;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.HABridge.api.NameValue;
import com.bwssystems.HABridge.plugins.http.HTTPHandler;
import com.google.gson.Gson;
public class HomeAssistant {
private static final Logger log = LoggerFactory.getLogger(HomeAssistant.class);
private NamedIP hassAddress;
private HTTPHandler anHttpHandler;
public HomeAssistant(NamedIP addressName) {
super();
anHttpHandler = new HTTPHandler();
hassAddress = addressName;
}
public NamedIP getHassAddress() {
return hassAddress;
}
public void setHassAddress(NamedIP hassAddress) {
this.hassAddress = hassAddress;
}
public Boolean callCommand(HassCommand aCommand) {
log.debug("calling HomeAssistant: " + aCommand.getHassName() + " - "
+ aCommand.getEntityId() + " - " + aCommand.getState() + " - " + aCommand.getBri());
String aUrl = "http://" + hassAddress.getIp() + ":" + hassAddress.getPort() + "/api/services/" + aCommand.getEntityId().substring(0, aCommand.getEntityId().indexOf("."));
String aBody = "{\"entity_id\":\"" + aCommand.getEntityId() + "\"";
NameValue[] headers = null;
if(hassAddress.getPassword() != null && !hassAddress.getPassword().isEmpty()) {
NameValue password = new NameValue();
password.setName("x-ha-access");
password.setValue(hassAddress.getPassword());
headers = new NameValue[1];
headers[0] = password;
}
if(aCommand.getState().equalsIgnoreCase("on")) {
aUrl = aUrl + "/turn_on";
if(aCommand.getBri() != null)
aBody = aBody + ",\"state\":\"on\",\"attributes\":{\"brightness\":" + aCommand.getBri() + "}}";
else
aBody = aBody + "}";
}
else {
aUrl = aUrl + "/turn_off";
aBody = aBody + "}";
}
String theData = anHttpHandler.doHttpRequest(aUrl, HttpPost.METHOD_NAME, "application/json", aBody, headers);
log.debug("call Command return is: <" + theData + ">");
return true;
}
public List<State> getDevices() {
List<State> theDeviceStates = null;
State[] theHassStates;
String theUrl = null;
String theData;
NameValue[] headers = null;
if(hassAddress.getPassword() != null && !hassAddress.getPassword().isEmpty()) {
NameValue password = new NameValue();
password.setName("x-ha-access");
password.setValue(hassAddress.getPassword());
headers = new NameValue[1];
headers[0] = password;
}
theUrl = "http://" + hassAddress.getIp() + ":" + hassAddress.getPort() + "/api/states";
theData = anHttpHandler.doHttpRequest(theUrl, HttpGet.METHOD_NAME, "application/json", null, headers);
if(theData != null) {
log.debug("GET Hass States - data: " + theData);
theHassStates = new Gson().fromJson(theData, State[].class);
if(theHassStates == null) {
log.warn("Cannot get an devices for HomeAssistant " + hassAddress.getName() + " as response is not parsable.");
}
else {
theDeviceStates = new ArrayList<State>(Arrays.asList(theHassStates));
}
}
else
log.warn("Cannot get an devices for HomeAssistant " + hassAddress.getName() + " http call failed.");
return theDeviceStates;
}
protected void closeClient() {
anHttpHandler.closeHandler();
}
}

View File

@@ -0,0 +1,60 @@
package com.bwssystems.HABridge.plugins.hass;
import java.util.Map;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Service {
@SerializedName("domain")
@Expose
private String domain;
@SerializedName("services")
@Expose
private Map<String, ServiceElement> services;
public Service(String domain, Map<String, ServiceElement> services) {
super();
this.domain = domain;
this.services = services;
}
/**
*
* @return
* The domain
*/
public String getDomain() {
return domain;
}
/**
*
* @param domain
* The domain
*/
public void setDomain(String domain) {
this.domain = domain;
}
/**
*
* @return
* The services
*/
public Map<String, ServiceElement> getServices() {
return services;
}
/**
*
* @param domain
* The services
*/
public void setServices(Map<String, ServiceElement> services) {
this.services = services;
}
}

View File

@@ -0,0 +1,36 @@
package com.bwssystems.HABridge.plugins.hass;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
public class ServiceDeserializer implements JsonDeserializer<Service> {
private Map<String, ServiceElement> services;
private String domain;
@Override
public Service deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext ctx)
{
JsonObject objServices = json.getAsJsonObject();
String theKey;
domain = objServices.get("domain").getAsString();
JsonObject obj = objServices.get("services").getAsJsonObject();
services = new HashMap<String, ServiceElement>();
for(Entry<String, JsonElement> entry:obj.entrySet()){
ServiceElement theServiceElement = new ServiceElement();
theKey = entry.getKey();
JsonObject theRawDetail = obj.getAsJsonObject(theKey);
theServiceElement.setDescription(theRawDetail.get("description").getAsString());
theServiceElement.setField(ctx.deserialize(theRawDetail.get("fields"), Field.class));
services.put(theKey, theServiceElement);
}
return new Service(domain, services);
}
}

View File

@@ -0,0 +1,52 @@
package com.bwssystems.HABridge.plugins.hass;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class ServiceElement {
@SerializedName("description")
@Expose
private String description;
@SerializedName("fields")
@Expose
private Field fields;
/**
*
* @return
* The description
*/
public String getDescription() {
return description;
}
/**
*
* @param description
* The description
*/
public void setDescription(String description) {
this.description = description;
}
/**
*
* @return
* The fields
*/
public Field getFields() {
return fields;
}
/**
*
* @param fields
* The fields
*/
public void setField(Field fields) {
this.fields = fields;
}
}

View File

@@ -0,0 +1,117 @@
package com.bwssystems.HABridge.plugins.hass;
import java.util.Map;
import com.google.gson.JsonElement;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class State {
@SerializedName("attributes")
@Expose
private Map<String, JsonElement> attributes;
@SerializedName("entity_id")
@Expose
private String entityId;
@SerializedName("last_changed")
@Expose
private String lastChanged;
@SerializedName("last_updated")
@Expose
private String lastUpdated;
@SerializedName("state")
@Expose
private String state;
/**
*
* @return
* The attributes
*/
public Map<String, JsonElement> getAttributes() {
return attributes;
}
/**
*
* @param attributes
* The attributes
*/
public void setAttributes(Map<String, JsonElement> attributes) {
this.attributes = attributes;
}
/**
*
* @return
* The entityId
*/
public String getEntityId() {
return entityId;
}
/**
*
* @param entityId
* The entity_id
*/
public void setEntityId(String entityId) {
this.entityId = entityId;
}
/**
*
* @return
* The lastChanged
*/
public String getLastChanged() {
return lastChanged;
}
/**
*
* @param lastChanged
* The last_changed
*/
public void setLastChanged(String lastChanged) {
this.lastChanged = lastChanged;
}
/**
*
* @return
* The lastUpdated
*/
public String getLastUpdated() {
return lastUpdated;
}
/**
*
* @param lastUpdated
* The last_updated
*/
public void setLastUpdated(String lastUpdated) {
this.lastUpdated = lastUpdated;
}
/**
*
* @return
* The state
*/
public String getState() {
return state;
}
/**
*
* @param state
* The state
*/
public void setState(String state) {
this.state = state;
}
}

View File

@@ -0,0 +1,149 @@
package com.bwssystems.HABridge.plugins.http;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.api.NameValue;
public class HTTPHandler {
private static final Logger log = LoggerFactory.getLogger(HTTPHandler.class);
private HttpClient httpClient;
private CloseableHttpClient httpclientSSL;
private SSLContext sslcontext;
private SSLConnectionSocketFactory sslsf;
private RequestConfig globalConfig;
public HTTPHandler() {
httpClient = HttpClients.createDefault();
// Trust own CA and all self-signed certs
sslcontext = SSLContexts.createDefault();
// Allow TLSv1 protocol only
sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1,TLSv1.1,TLSv1.2" }, null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
httpclientSSL = HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultRequestConfig(globalConfig).build();
}
// This function executes the url from the device repository against the
// target as http or https as defined
public String doHttpRequest(String url, String httpVerb, String contentType, String body, NameValue[] headers) {
HttpUriRequest request = null;
String theContent = null;
URI theURI = null;
ContentType parsedContentType = null;
StringEntity requestBody = null;
if (contentType != null && !contentType.trim().isEmpty()) {
parsedContentType = ContentType.parse(contentType);
if (body != null && body.length() > 0)
requestBody = new StringEntity(body, parsedContentType);
}
try {
theURI = new URI(url);
} catch (URISyntaxException e1) {
log.warn("Error creating URI http request: " + url + " with message: " + e1.getMessage());
return null;
}
try {
if (httpVerb == null || httpVerb.trim().isEmpty() || HttpGet.METHOD_NAME.equalsIgnoreCase(httpVerb)) {
request = new HttpGet(theURI);
} else if (HttpPost.METHOD_NAME.equalsIgnoreCase(httpVerb)) {
HttpPost postRequest = new HttpPost(theURI);
if (requestBody != null)
postRequest.setEntity(requestBody);
request = postRequest;
} else if (HttpPut.METHOD_NAME.equalsIgnoreCase(httpVerb)) {
HttpPut putRequest = new HttpPut(theURI);
if (requestBody != null)
putRequest.setEntity(requestBody);
request = putRequest;
}
else
request = new HttpGet(theURI);
} catch (IllegalArgumentException e) {
log.warn("Error creating outbound http request: IllegalArgumentException in log", e);
return null;
}
log.debug("Making outbound call in doHttpRequest: " + request);
if (headers != null && headers.length > 0) {
for (int i = 0; i < headers.length; i++) {
request.setHeader(headers[i].getName(), headers[i].getValue());
}
}
try {
HttpResponse response;
if (url.startsWith("https"))
response = httpclientSSL.execute(request);
else
response = httpClient.execute(request);
log.debug((httpVerb == null ? "GET" : httpVerb) + " execute on URL responded: "
+ response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300) {
if (response.getEntity() != null) {
try {
theContent = EntityUtils.toString(response.getEntity(), Charset.forName("UTF-8")); // read
// content
// for
// data
EntityUtils.consume(response.getEntity()); // close out
// inputstream
// ignore
// content
} catch (Exception e) {
log.debug("Error ocurred in handling response entity after successful call, still responding success. "
+ e.getMessage(), e);
}
}
if (theContent == null)
theContent = "";
}
} catch (IOException e) {
log.warn("Error calling out to HA gateway: IOException in log", e);
}
return theContent;
}
public HttpClient getHttpClient() {
return httpClient;
}
public CloseableHttpClient getHttpclientSSL() {
return httpclientSSL;
}
public void closeHandler() {
httpClient = null;
try {
httpclientSSL.close();
} catch (IOException e) {
// noop
}
httpclientSSL = null;
}
}

View File

@@ -0,0 +1,81 @@
package com.bwssystems.HABridge.plugins.http;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.BridgeSettingsDescriptor;
import com.bwssystems.HABridge.Home;
import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.api.NameValue;
import com.bwssystems.HABridge.api.hue.HueError;
import com.bwssystems.HABridge.api.hue.HueErrorResponse;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.BrightnessDecode;
import com.bwssystems.HABridge.hue.MultiCommandUtil;
import com.google.gson.Gson;
public class HTTPHome implements Home {
private static final Logger log = LoggerFactory.getLogger(HTTPHome.class);
private HTTPHandler anHttpHandler;
public HTTPHome(BridgeSettingsDescriptor bridgeSettings) {
super();
createHome(bridgeSettings);
}
@Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity,
Integer targetBri,Integer targetBriInc, DeviceDescriptor device, String body) {
String responseString = null;
//Backwards Compatibility Items
if(anItem.getHttpVerb() == null || anItem.getHttpVerb().isEmpty())
{
if(device.getHttpVerb() != null && !device.getHttpVerb().isEmpty())
anItem.setHttpVerb(device.getHttpVerb());
}
if(anItem.getHttpHeaders() == null || anItem.getHttpHeaders().isEmpty()) {
if(device.getHeaders() != null && !device.getHeaders().isEmpty() )
anItem.setHttpHeaders(device.getHeaders());
}
log.debug("executing HUE api request to Http "
+ (anItem.getHttpVerb() == null ? "GET" : anItem.getHttpVerb()) + ": "
+ anItem.getItem().getAsString());
String anUrl = BrightnessDecode.calculateReplaceIntensityValue(anItem.getItem().getAsString(),
intensity, targetBri, targetBriInc, false);
String aBody;
aBody = BrightnessDecode.calculateReplaceIntensityValue(anItem.getHttpBody(),
intensity, targetBri, targetBriInc, false);
// make call
if (anHttpHandler.doHttpRequest(anUrl, anItem.getHttpVerb(), anItem.getContentType(), aBody,
new Gson().fromJson(anItem.getHttpHeaders(), NameValue[].class)) == null) {
log.warn("Error on calling url to change device state: " + anUrl);
responseString = new Gson().toJson(HueErrorResponse.createResponse("6", "/lights/" + lightId,
"Error on calling url to change device state", "/lights/"
+ lightId + "state", null, null).getTheErrors(), HueError[].class);
}
return responseString;
}
@Override
public Home createHome(BridgeSettingsDescriptor bridgeSettings) {
anHttpHandler = new HTTPHandler();
log.info("Http Home created.");
return this;
}
@Override
public Object getItems(String type) {
// Not a resource
return null;
}
@Override
public void closeHome() {
anHttpHandler.closeHandler();
}
}

View File

@@ -0,0 +1,35 @@
package com.bwssystems.HABridge.plugins.hue;
import com.bwssystems.HABridge.api.hue.DeviceResponse;
public class HueDevice {
private DeviceResponse device;
private String huedeviceid;
private String hueaddress;
private String huename;
public DeviceResponse getDevice() {
return device;
}
public void setDevice(DeviceResponse adevice) {
this.device = adevice;
}
public String getHuedeviceid() {
return huedeviceid;
}
public void setHuedeviceid(String huedeviceid) {
this.huedeviceid = huedeviceid;
}
public String getHueaddress() {
return hueaddress;
}
public void setHueaddress(String ahueaddress) {
this.hueaddress = ahueaddress;
}
public String getHuename() {
return huename;
}
public void setHuename(String ahuename) {
this.huename = ahuename;
}
}

View File

@@ -0,0 +1,25 @@
package com.bwssystems.HABridge.plugins.hue;
public class HueDeviceIdentifier {
private String hueName;
private String ipAddress;
private String deviceId;
public String getHueName() {
return hueName;
}
public void setHueName(String hueName) {
this.hueName = hueName;
}
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
}

View File

@@ -0,0 +1,125 @@
package com.bwssystems.HABridge.plugins.hue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.BridgeSettingsDescriptor;
import com.bwssystems.HABridge.Home;
import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.api.hue.DeviceResponse;
import com.bwssystems.HABridge.api.hue.HueApiResponse;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.MultiCommandUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class HueHome implements Home {
private static final Logger log = LoggerFactory.getLogger(HueHome.class);
private Map<String, HueInfo> hues;
private Boolean validHue;
private Gson aGsonHandler;
public HueHome(BridgeSettingsDescriptor bridgeSettings) {
super();
createHome(bridgeSettings);
}
@Override
public Object getItems(String type) {
log.debug("consolidating devices for hues");
if(!validHue)
return null;
Iterator<String> keys = hues.keySet().iterator();
ArrayList<HueDevice> deviceList = new ArrayList<HueDevice>();
while(keys.hasNext()) {
String key = keys.next();
HueApiResponse theResponse = hues.get(key).getHueApiResponse();
if(theResponse != null) {
Map<String, DeviceResponse> theDevices = theResponse.getLights();
if(theDevices != null) {
Iterator<String> deviceKeys = theDevices.keySet().iterator();
while(deviceKeys.hasNext()) {
String theDeviceKey = deviceKeys.next();
HueDevice aNewHueDevice = new HueDevice();
aNewHueDevice.setDevice(theDevices.get(theDeviceKey));
aNewHueDevice.setHuedeviceid(theDeviceKey);
aNewHueDevice.setHueaddress(hues.get(key).getHueAddress().getIp());
aNewHueDevice.setHuename(key);
deviceList.add(aNewHueDevice);
}
}
else {
deviceList = null;
break;
}
}
else
log.warn("Cannot get lights for Hue with name: " + key);
}
return deviceList;
}
public DeviceResponse getHueDeviceInfo(HueDeviceIdentifier deviceId, DeviceDescriptor device) {
if(!validHue)
return null;
DeviceResponse deviceResponse = null;
HueInfo aHueInfo = hues.get(device.getTargetDevice());
deviceResponse = aHueInfo.getHueDeviceInfo(deviceId.getDeviceId(), device);
return deviceResponse;
}
@Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity,
Integer targetBri,Integer targetBriInc, DeviceDescriptor device, String body) {
if(!validHue)
return null;
String responseString = null;
HueDeviceIdentifier deviceId = null;
if(anItem.getItem().isJsonObject())
deviceId = aGsonHandler.fromJson(anItem.getItem(), HueDeviceIdentifier.class);
else
deviceId = aGsonHandler.fromJson(anItem.getItem().getAsString(), HueDeviceIdentifier.class);
if(deviceId.getHueName() == null || deviceId.getHueName().isEmpty())
deviceId.setHueName(device.getTargetDevice());
HueInfo theHue = hues.get(deviceId.getHueName());
// make call
responseString = theHue.changeState(deviceId, lightId, body);
return responseString;
}
@Override
public Home createHome(BridgeSettingsDescriptor bridgeSettings) {
validHue = bridgeSettings.isValidHue();
log.info("Hue passthru Home created." + (validHue ? "" : " No Hue passtrhu systems configured."));
if(validHue) {
hues = new HashMap<String, HueInfo>();
Iterator<NamedIP> theList = bridgeSettings.getHueaddress().getDevices().iterator();
while(theList.hasNext()) {
NamedIP aHue = theList.next();
hues.put(aHue.getName(), new HueInfo(aHue));
}
aGsonHandler = new GsonBuilder().create();
}
return this;
}
@Override
public void closeHome() {
if(!validHue)
return;
Iterator<String> keys = hues.keySet().iterator();
while(keys.hasNext()) {
String key = keys.next();
hues.get(key).closeHue();;
}
}
}

View File

@@ -0,0 +1,199 @@
package com.bwssystems.HABridge.plugins.hue;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.HABridge.api.SuccessUserResponse;
import com.bwssystems.HABridge.api.UserCreateRequest;
import com.bwssystems.HABridge.api.hue.DeviceResponse;
import com.bwssystems.HABridge.api.hue.HueApiResponse;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.plugins.http.HTTPHandler;
import com.google.gson.Gson;
public class HueInfo {
private static final Logger log = LoggerFactory.getLogger(HueInfo.class);
private HTTPHandler httpClient;
private NamedIP hueAddress;
public static final String HUE_REQUEST = "/api";
public HueInfo(NamedIP addressName) {
super();
httpClient = new HTTPHandler();
hueAddress = addressName;
}
public HueApiResponse getHueApiResponse() {
log.debug("Get hue info to hue " + hueAddress.getName());
HueApiResponse theHueApiResponse = null;
if(hueAddress.getUsername() == null) {
registerWithHue();
if(hueAddress.getUsername() == null) {
log.warn("Could not register with hue: " + hueAddress.getName());
}
}
String theUrl = "http://" + hueAddress.getIp() + HUE_REQUEST + "/" + hueAddress.getUsername();
String theData;
boolean loopControl = true;
int retryCount = 0;
while(loopControl) {
if(retryCount > 3) {
log.warn("Max Retry reached to get Hue data from " + hueAddress.getName());
loopControl = false;
break;
} else if (retryCount > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// ignore
}
}
theUrl = "http://" + hueAddress.getIp() + HueUtil.HUE_REQUEST + "/" + hueAddress.getUsername();
theData = httpClient.doHttpRequest(theUrl, null, null, null, null);
if(theData != null) {
log.debug("GET HueApiResponse - data: " + theData);
if(theData.contains("[{\"error\":")) {
if(theData.contains("unauthorized user")) {
registerWithHue();
if(hueAddress.getUsername() == null) {
log.warn("Retry Register to Hue for " + hueAddress.getName() + " returned error: " + theData);
}
retryCount++;
}
else {
log.warn("GET HueApiResponse for " + hueAddress.getName() + " - returned error: " + theData);
return null;
}
}
else {
theHueApiResponse = new Gson().fromJson(theData, HueApiResponse.class);
log.debug("GET HueApiResponse for " + hueAddress.getName() + " - Gson parse - name: " + theHueApiResponse.getConfig().getName() + ", mac addr: " + theHueApiResponse.getConfig().getMac());
loopControl = false;
}
}
else {
log.warn("GET HueApiResponse for " + hueAddress.getName() + " - returned null, no data.");
loopControl = false;
}
}
return theHueApiResponse;
}
public String registerWithHue() {
UserCreateRequest theLogin = new UserCreateRequest();
theLogin.setDevicetype("HABridge#MyMachine");
HttpPost postRequest = new HttpPost("http://" + hueAddress.getIp() + HUE_REQUEST);
ContentType parsedContentType = ContentType.parse("application/json");
StringEntity requestBody = new StringEntity(new Gson().toJson(theLogin), parsedContentType);
HttpResponse response = null;
postRequest.setEntity(requestBody);
HttpClient anHttpClient = httpClient.getHttpClient();
try {
response = anHttpClient.execute(postRequest);
log.debug("registerWithHue - POST execute on " + hueAddress.getName() + "URL responded: " + response.getStatusLine().getStatusCode());
if(response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300){
String theBody = EntityUtils.toString(response.getEntity());
log.debug("registerWithHue response data: " + theBody);
if(theBody.contains("[{\"error\":")) {
if(theBody.contains("link button not")) {
log.warn("registerWithHue needs link button pressed on HUE bridge: " + hueAddress.getName());
}
else
log.warn("registerWithHue returned an unexpected error: " + theBody);
}
else {
SuccessUserResponse[] theResponses = new Gson().fromJson(theBody, SuccessUserResponse[].class); //read content for data, SuccessUserResponse[].class);
hueAddress.setUsername(theResponses[0].getSuccess().getUsername());
}
}
EntityUtils.consume(response.getEntity()); //close out inputstream ignore content
} catch (IOException e) {
log.warn("Error logging into HUE: IOException in log", e);
}
return hueAddress.getUsername();
}
public DeviceResponse getHueDeviceInfo(String hueDeviceId, DeviceDescriptor device) {
log.debug("Get hue device info to "+ hueAddress.getName() + " passthru id " + hueDeviceId);
String responseString = null;
DeviceResponse deviceResponse = null;
if(hueAddress.getUsername() == null)
registerWithHue();
if (hueAddress.getUsername() != null) {
// make call
responseString = httpClient.doHttpRequest(
"http://" + hueAddress.getIp() + "/api/" + hueAddress.getUsername()
+ "/lights/" + hueDeviceId,
HttpGet.METHOD_NAME, "application/json", null, null);
if (responseString == null) {
log.warn("Error on calling hue device to get state: " + device.getName());
deviceResponse = DeviceResponse.createResponse(device);
} else if (responseString.contains("[{\"error\":") && responseString.contains("unauthorized user")) {
log.warn("Error on calling hue device to get state: " + device.getName() + " with errors: " + responseString);
deviceResponse = DeviceResponse.createResponse(device);
} else {
deviceResponse = new Gson().fromJson(responseString, DeviceResponse.class);
if (deviceResponse != null)
deviceResponse.setName(device.getName());
}
} else {
log.warn("Error on calling hue device to get state: " + device.getName() + " Could not register with hue: " + hueAddress.getName());
deviceResponse = DeviceResponse.createResponse(device);
}
return deviceResponse;
}
public String changeState(HueDeviceIdentifier deviceId, String lightId, String body) {
log.debug("Changing state for ha-bridge id " + lightId + " to " + deviceId.getHueName() + " passthru id " + deviceId.getDeviceId() + " with state " + body );
String responseString = null;
if(hueAddress.getUsername() == null)
registerWithHue();
if (hueAddress.getUsername() != null) {
responseString = httpClient.doHttpRequest(
"http://" + deviceId.getIpAddress() + "/api/" + hueAddress.getUsername()
+ "/lights/" + deviceId.getDeviceId() + "/state",
HttpPut.METHOD_NAME, "application/json", body, null);
if (responseString == null) {
log.warn("Error on calling Hue passthru to change device state: " + deviceId.getHueName());
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Error on calling HUE to change device state\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
} else if (responseString.contains("[{\"error\":")) {
if(responseString.contains("unauthorized user")) {
}
log.warn("Error occurred when calling Hue Passthru: " + responseString);
}
} else {
log.warn("Error on calling hue device to change state: " + deviceId.getHueName() + " Could not register with hue: " + hueAddress.getName());
}
return responseString;
}
public void closeHue() {
httpClient.closeHandler();
httpClient = null;
}
public NamedIP getHueAddress() {
return hueAddress;
}
public void setHueAddress(NamedIP hueAddress) {
this.hueAddress = hueAddress;
}
}

View File

@@ -0,0 +1,56 @@
package com.bwssystems.HABridge.plugins.hue;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.api.SuccessUserResponse;
import com.bwssystems.HABridge.api.UserCreateRequest;
import com.bwssystems.HABridge.plugins.http.HTTPHandler;
import com.google.gson.Gson;
public class HueUtil {
private static final Logger log = LoggerFactory.getLogger(HueUtil.class);
public static final String HUE_REQUEST = "/api";
public static final String registerWithHue(HTTPHandler anHttpHandler, String ipAddress, String aName, String theUser) {
UserCreateRequest theLogin = new UserCreateRequest();
theLogin.setDevicetype("HABridge#MyMachine");
HttpPost postRequest = new HttpPost("http://" + ipAddress + HUE_REQUEST);
ContentType parsedContentType = ContentType.parse("application/json");
StringEntity requestBody = new StringEntity(new Gson().toJson(theLogin), parsedContentType);
HttpResponse response = null;
postRequest.setEntity(requestBody);
HttpClient anHttpClient = anHttpHandler.getHttpClient();
try {
response = anHttpClient.execute(postRequest);
log.debug("POST execute on URL responded: " + response.getStatusLine().getStatusCode());
if(response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300){
String theBody = EntityUtils.toString(response.getEntity());
log.debug("registerWithHue response data: " + theBody);
if(theBody.contains("[{\"error\":")) {
if(theBody.contains("link button not")) {
log.warn("registerWithHue needs link button pressed on HUE bridge: " + aName);
}
else
log.warn("registerWithHue returned an unexpected error: " + theBody);
}
else {
SuccessUserResponse[] theResponses = new Gson().fromJson(theBody, SuccessUserResponse[].class); //read content for data, SuccessUserResponse[].class);
theUser = theResponses[0].getSuccess().getUsername();
}
}
EntityUtils.consume(response.getEntity()); //close out inputstream ignore content
} catch (IOException e) {
log.warn("Error logging into HUE: IOException in log", e);
}
return theUser;
}
}

View File

@@ -0,0 +1,30 @@
package com.bwssystems.HABridge.plugins.mqtt;
import com.bwssystems.HABridge.NamedIP;
public class MQTTBroker {
private String clientId;
private String ip;
public MQTTBroker(NamedIP brokerConfig) {
super();
this.setIp(brokerConfig.getIp());
this.setClientId(brokerConfig.getName());
}
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
}

View File

@@ -0,0 +1,72 @@
package com.bwssystems.HABridge.plugins.mqtt;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.MqttSecurityException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.NamedIP;
public class MQTTHandler {
private static final Logger log = LoggerFactory.getLogger(MQTTHandler.class);
private NamedIP myConfig;
private MqttClient myClient;
private int qos = 1;
public MQTTHandler(NamedIP aConfig) {
super();
log.info("Setting up handler for name: " + aConfig.getName());
MemoryPersistence persistence = new MemoryPersistence();
myConfig = aConfig;
try {
myClient = new MqttClient("tcp://" + myConfig.getIp(), myConfig.getName(), persistence);
} catch (MqttException e) {
log.error("Could not create MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage());
}
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
connOpts.setAutomaticReconnect(true);
if(aConfig.getUsername() != null && aConfig.getUsername().trim().length() > 0) {
if(aConfig.getPassword() != null && aConfig.getPassword().trim().length() > 0) {
connOpts.setUserName(aConfig.getUsername().trim());
connOpts.setPassword(aConfig.getPassword().trim().toCharArray());
}
}
try {
myClient.connect(connOpts);
} catch (MqttSecurityException e) {
log.error("Could not connect MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage());
} catch (MqttException e) {
log.error("Could not connect MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage());
}
}
public void publishMessage(String topic, String content) {
MqttMessage message = new MqttMessage(content.getBytes());
message.setQos(qos);
try {
myClient.publish(topic, message);
} catch (MqttPersistenceException e) {
log.error("Could not publish to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage());
} catch (MqttException e) {
log.error("Could not publish to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage());
}
}
public NamedIP getMyConfig() {
return myConfig;
}
public void shutdown() {
try {
myClient.disconnect();
} catch (MqttException e) {
log.warn("Could not disconnect MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp());
}
}
}

View File

@@ -0,0 +1,140 @@
package com.bwssystems.HABridge.plugins.mqtt;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.BridgeSettingsDescriptor;
import com.bwssystems.HABridge.Home;
import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.BrightnessDecode;
import com.bwssystems.HABridge.hue.MultiCommandUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class MQTTHome implements Home {
private static final Logger log = LoggerFactory.getLogger(MQTTHome.class);
private Map<String, MQTTHandler> handlers;
private Boolean validMqtt;
private Gson aGsonHandler;
public MQTTHome(BridgeSettingsDescriptor bridgeSettings) {
super();
createHome(bridgeSettings);
}
@Override
public void closeHome() {
if(!validMqtt)
return;
log.debug("Shutting down MQTT handlers.");
if(handlers != null && !handlers.isEmpty()) {
Iterator<String> keys = handlers.keySet().iterator();
while(keys.hasNext()) {
String key = keys.next();
handlers.get(key).shutdown();
}
}
}
public MQTTHandler getMQTTHandler(String aName) {
if(!validMqtt)
return null;
MQTTHandler aHandler;
if(aName == null || aName.equals("")) {
aHandler = null;
log.debug("Cannot get MQTT handler for name as it is empty.");
}
else {
aHandler = handlers.get(aName);
log.debug("Retrieved a MQTT hanlder for name: " + aName);
}
return aHandler;
}
@Override
public Object getItems(String type) {
if(!validMqtt)
return null;
Iterator<String> keys = handlers.keySet().iterator();
ArrayList<MQTTBroker> deviceList = new ArrayList<MQTTBroker>();
while(keys.hasNext()) {
String key = keys.next();
MQTTHandler aHandler = handlers.get(key);
MQTTBroker aDevice = new MQTTBroker(aHandler.getMyConfig());
deviceList.add(aDevice);
}
return deviceList;
}
@Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity,
Integer targetBri,Integer targetBriInc, DeviceDescriptor device, String body) {
String responseString = null;
log.debug("executing HUE api request to send message to MQTT broker: " + anItem.getItem().toString());
if (validMqtt) {
String mqttObject = null;
if(anItem.getItem().isJsonObject())
mqttObject = BrightnessDecode.calculateReplaceIntensityValue(anItem.getItem().getAsString(),
intensity, targetBri, targetBriInc, false);
else
mqttObject = BrightnessDecode.calculateReplaceIntensityValue(anItem.getItem().toString(),
intensity, targetBri, targetBriInc, false);
if (mqttObject.substring(0, 1).equalsIgnoreCase("{"))
mqttObject = "[" + mqttObject + "]";
MQTTMessage[] mqttMessages = aGsonHandler.fromJson(mqttObject, MQTTMessage[].class);
Integer theCount = 1;
for(int z = 0; z < mqttMessages.length; z++) {
if(mqttMessages[z].getCount() != null && mqttMessages[z].getCount() > 0)
theCount = mqttMessages[z].getCount();
for(int y = 0; y < theCount; y++) {
if( y > 0 || z > 0) {
log.debug("publishing message: " + mqttMessages[y].getClientId() + " - "
+ mqttMessages[y].getTopic() + " - " + mqttMessages[y].getMessage()
+ " - count: " + String.valueOf(z));
MQTTHandler mqttHandler = getMQTTHandler(mqttMessages[y].getClientId());
if (mqttHandler == null) {
log.warn("Should not get here, no mqtt hanlder available");
} else {
mqttHandler.publishMessage(mqttMessages[y].getTopic(), mqttMessages[y].getMessage());
}
}
}
}
} else {
log.warn("Should not get here, no mqtt brokers configured");
responseString = "[{\"error\":{\"type\": 6, \"address\": \"/lights/" + lightId
+ "\",\"description\": \"Should not get here, no mqtt brokers configured\", \"parameter\": \"/lights/"
+ lightId + "state\"}}]";
}
return responseString;
}
@Override
public Home createHome(BridgeSettingsDescriptor bridgeSettings) {
validMqtt = bridgeSettings.isValidMQTT();
log.info("MQTT Home created." + (validMqtt ? "" : " No MQTT Clients configured."));
if(validMqtt) {
aGsonHandler =
new GsonBuilder()
.create();
handlers = new HashMap<String, MQTTHandler>();
Iterator<NamedIP> theList = bridgeSettings.getMqttaddress().getDevices().iterator();
while(theList.hasNext()) {
NamedIP aClientConfig = theList.next();
MQTTHandler aHandler = new MQTTHandler(aClientConfig);
if(aHandler != null)
handlers.put(aClientConfig.getName(), aHandler);
}
}
return this;
}
}

View File

@@ -0,0 +1,39 @@
package com.bwssystems.HABridge.plugins.mqtt;
public class MQTTMessage {
private String clientId;
private String topic;
private String message;
private Integer delay;
private Integer count;
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Integer getDelay() {
return delay;
}
public void setDelay(Integer delay) {
this.delay = delay;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
}

View File

@@ -0,0 +1,89 @@
package com.bwssystems.HABridge.plugins.tcp;
import java.io.DataOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.xml.bind.DatatypeConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.BridgeSettingsDescriptor;
import com.bwssystems.HABridge.Home;
import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.BrightnessDecode;
import com.bwssystems.HABridge.hue.MultiCommandUtil;
public class TCPHome implements Home {
private static final Logger log = LoggerFactory.getLogger(TCPHome.class);
private byte[] sendData;
public TCPHome(BridgeSettingsDescriptor bridgeSettings) {
super();
createHome(bridgeSettings);
}
@Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity,
Integer targetBri,Integer targetBriInc, DeviceDescriptor device, String body) {
log.debug("executing HUE api request to TCP: " + anItem.getItem().getAsString());
String intermediate = anItem.getItem().getAsString().substring(anItem.getItem().getAsString().indexOf("://") + 3);
String hostPortion = intermediate.substring(0, intermediate.indexOf('/'));
String theUrlBody = intermediate.substring(intermediate.indexOf('/') + 1);
String hostAddr = null;
String port = null;
InetAddress IPAddress = null;
if (hostPortion.contains(":")) {
hostAddr = hostPortion.substring(0, intermediate.indexOf(':'));
port = hostPortion.substring(intermediate.indexOf(':') + 1);
} else
hostAddr = hostPortion;
try {
IPAddress = InetAddress.getByName(hostAddr);
} catch (UnknownHostException e) {
// noop
}
if (theUrlBody.startsWith("0x")) {
theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, intensity, targetBri, targetBriInc, true);
sendData = DatatypeConverter.parseHexBinary(theUrlBody.substring(2));
} else {
theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, intensity, targetBri, targetBriInc, false);
sendData = theUrlBody.getBytes();
}
try {
Socket dataSendSocket = new Socket(IPAddress, Integer.parseInt(port));
DataOutputStream outToClient = new DataOutputStream(dataSendSocket.getOutputStream());
outToClient.write(sendData);
outToClient.flush();
dataSendSocket.close();
} catch (Exception e) {
// noop
}
return null;
}
@Override
public Home createHome(BridgeSettingsDescriptor bridgeSettings) {
log.info("TCP Home created.");
return this;
}
@Override
public Object getItems(String type) {
// Not a resource
return null;
}
@Override
public void closeHome() {
// noop
}
}

View File

@@ -0,0 +1,87 @@
package com.bwssystems.HABridge.plugins.udp;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.xml.bind.DatatypeConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.BridgeSettingsDescriptor;
import com.bwssystems.HABridge.Home;
import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.BrightnessDecode;
import com.bwssystems.HABridge.hue.MultiCommandUtil;
import com.bwssystems.HABridge.util.UDPDatagramSender;
public class UDPHome implements Home {
private static final Logger log = LoggerFactory.getLogger(UDPHome.class);
private UDPDatagramSender theUDPDatagramSender;
private byte[] sendData;
public UDPHome(BridgeSettingsDescriptor bridgeSettings, UDPDatagramSender aUDPDatagramSender) {
super();
theUDPDatagramSender = aUDPDatagramSender;
createHome(bridgeSettings);
}
@Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity,
Integer targetBri,Integer targetBriInc, DeviceDescriptor device, String body) {
log.debug("executing HUE api request to UDP: " + anItem.getItem().getAsString());
String intermediate = anItem.getItem().getAsString().substring(anItem.getItem().getAsString().indexOf("://") + 3);
String hostPortion = intermediate.substring(0, intermediate.indexOf('/'));
String theUrlBody = intermediate.substring(intermediate.indexOf('/') + 1);
String hostAddr = null;
String port = null;
InetAddress IPAddress = null;
if (hostPortion.contains(":")) {
hostAddr = hostPortion.substring(0, intermediate.indexOf(':'));
port = hostPortion.substring(intermediate.indexOf(':') + 1);
} else
hostAddr = hostPortion;
try {
IPAddress = InetAddress.getByName(hostAddr);
} catch (UnknownHostException e) {
// noop
}
if (theUrlBody.startsWith("0x")) {
theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, intensity, targetBri, targetBriInc, true);
sendData = DatatypeConverter.parseHexBinary(theUrlBody.substring(2));
} else {
theUrlBody = BrightnessDecode.calculateReplaceIntensityValue(theUrlBody, intensity, targetBri, targetBriInc, false);
sendData = theUrlBody.getBytes();
}
try {
theUDPDatagramSender.sendUDPResponse(sendData, IPAddress, Integer.parseInt(port));
} catch (NumberFormatException e) {
// noop
} catch (IOException e) {
// noop
}
return null;
}
@Override
public Home createHome(BridgeSettingsDescriptor bridgeSettings) {
log.info("UDP Home created.");
return this;
}
@Override
public Object getItems(String type) {
// Not a resource
return null;
}
@Override
public void closeHome() {
// TODO Auto-generated method stub
}
}

View File

@@ -0,0 +1,112 @@
package com.bwssystems.HABridge.plugins.vera;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.BridgeSettingsDescriptor;
import com.bwssystems.HABridge.DeviceMapTypes;
import com.bwssystems.HABridge.Home;
import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.HABridge.api.CallItem;
import com.bwssystems.HABridge.dao.DeviceDescriptor;
import com.bwssystems.HABridge.hue.MultiCommandUtil;
import com.bwssystems.HABridge.plugins.vera.luupRequests.Device;
import com.bwssystems.HABridge.plugins.vera.luupRequests.Scene;
import com.bwssystems.HABridge.plugins.vera.luupRequests.Sdata;
public class VeraHome implements Home {
private static final Logger log = LoggerFactory.getLogger(VeraHome.class);
private Map<String, VeraInfo> veras;
private Boolean validVera;
public VeraHome(BridgeSettingsDescriptor bridgeSettings) {
super();
createHome(bridgeSettings);
}
public List<Device> getDevices() {
log.debug("consolidating devices for veras");
Iterator<String> keys = veras.keySet().iterator();
ArrayList<Device> deviceList = new ArrayList<Device>();
while(keys.hasNext()) {
String key = keys.next();
Sdata theSdata = veras.get(key).getSdata();
if(theSdata != null) {
Iterator<Device> devices = theSdata.getDevices().iterator();
while(devices.hasNext()) {
deviceList.add(devices.next());
}
}
else {
deviceList = null;
break;
}
}
return deviceList;
}
public List<Scene> getScenes() {
log.debug("consolidating scenes for veras");
Iterator<String> keys = veras.keySet().iterator();
ArrayList<Scene> sceneList = new ArrayList<Scene>();
while(keys.hasNext()) {
String key = keys.next();
Sdata theSdata = veras.get(key).getSdata();
if(theSdata != null) {
Iterator<Scene> scenes = theSdata.getScenes().iterator();
while(scenes.hasNext()) {
sceneList.add(scenes.next());
}
}
else {
sceneList = null;
break;
}
}
return sceneList;
}
@Override
public String deviceHandler(CallItem anItem, MultiCommandUtil aMultiUtil, String lightId, int intensity,
Integer targetBri,Integer targetBriInc, DeviceDescriptor device, String body) {
// Not a device handler
return null;
}
@Override
public Object getItems(String type) {
if(validVera) {
if(type.equalsIgnoreCase(DeviceMapTypes.VERA_DEVICE[DeviceMapTypes.typeIndex]))
return getDevices();
if(type.equalsIgnoreCase(DeviceMapTypes.VERA_SCENE[DeviceMapTypes.typeIndex]))
return getScenes();
}
return null;
}
@Override
public Home createHome(BridgeSettingsDescriptor bridgeSettings) {
validVera = bridgeSettings.isValidVera();
log.info("Vera Home created." + (validVera ? "" : " No Veras configured."));
if(validVera) {
veras = new HashMap<String, VeraInfo>();
Iterator<NamedIP> theList = bridgeSettings.getVeraAddress().getDevices().iterator();
while(theList.hasNext()) {
NamedIP aVera = theList.next();
veras.put(aVera.getName(), new VeraInfo(aVera));
}
}
return this;
}
@Override
public void closeHome() {
// TODO Auto-generated method stub
}
}

View File

@@ -0,0 +1,85 @@
package com.bwssystems.HABridge.plugins.vera;
import java.util.HashMap;
import java.util.ListIterator;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.NamedIP;
import com.bwssystems.HABridge.plugins.http.HTTPHandler;
import com.bwssystems.HABridge.plugins.vera.luupRequests.Categorie;
import com.bwssystems.HABridge.plugins.vera.luupRequests.Device;
import com.bwssystems.HABridge.plugins.vera.luupRequests.Room;
import com.bwssystems.HABridge.plugins.vera.luupRequests.Scene;
import com.bwssystems.HABridge.plugins.vera.luupRequests.Sdata;
import com.google.gson.Gson;
public class VeraInfo {
private static final Logger log = LoggerFactory.getLogger(VeraInfo.class);
private HTTPHandler httpClient;
private static final String SDATA_REQUEST = ":3480/data_request?id=sdata&output_format=json";
private NamedIP veraAddress;
public VeraInfo(NamedIP addressName) {
super();
httpClient = new HTTPHandler();
veraAddress = addressName;
}
public Sdata getSdata() {
Sdata theSdata = null;
String theUrl = "http://" + veraAddress.getIp() + SDATA_REQUEST;
String theData;
theData = httpClient.doHttpRequest(theUrl, null, null, null, null);
if(theData != null) {
theSdata = new Gson().fromJson(theData, Sdata.class);
log.debug("GET sdata - full: " + theSdata.getFull() + ", version: " + theSdata.getVersion());
denormalizeSdata(theSdata);
}
return theSdata;
}
private void denormalizeSdata(Sdata theSdata) {
Map<String,Room> roomMap = new HashMap<String,Room>();
for (Room i : theSdata.getRooms()) roomMap.put(i.getId(),i);
Map<String,Categorie> categoryMap = new HashMap<String,Categorie>();
for (Categorie i : theSdata.getCategoriess()) categoryMap.put(i.getId(),i);
Categorie controllerCat = new Categorie();
controllerCat.setName("Controller");
controllerCat.setId("0");
categoryMap.put(controllerCat.getId(),controllerCat);
ListIterator<Device> theIterator = theSdata.getDevices().listIterator();
Device theDevice = null;
while (theIterator.hasNext()) {
theDevice = theIterator.next();
if(theDevice.getRoom() != null && roomMap.get(theDevice.getRoom()) != null)
theDevice.setRoom(roomMap.get(theDevice.getRoom()).getName());
else
theDevice.setRoom("no room");
if(theDevice.getCategory() != null && categoryMap.get(theDevice.getCategory()) != null)
theDevice.setCategory(categoryMap.get(theDevice.getCategory()).getName());
else
theDevice.setCategory("<unknown>");
theDevice.setVeraaddress(veraAddress.getIp());
theDevice.setVeraname(veraAddress.getName());
}
ListIterator<Scene> theSecneIter = theSdata.getScenes().listIterator();
Scene theScene = null;
while (theSecneIter.hasNext()) {
theScene = theSecneIter.next();
if(theScene.getRoom() != null && roomMap.get(theScene.getRoom()) != null)
theScene.setRoom(roomMap.get(theScene.getRoom()).getName());
else
theScene.setRoom("no room");
theScene.setVeraaddress(veraAddress.getIp());
theScene.setVeraname(veraAddress.getName());
}
}
}

View File

@@ -0,0 +1,19 @@
package com.bwssystems.HABridge.plugins.vera.luupRequests;
public class Categorie {
private String name;
private String id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}

View File

@@ -0,0 +1,97 @@
package com.bwssystems.HABridge.plugins.vera.luupRequests;
public class Device {
private String name;
private String altid;
private String id;
private String category;
private String subcategory;
private String room;
private String parent;
private String status;
private String level;
private String state;
private String comment;
private String veraname;
private String veraaddress;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAltid() {
return altid;
}
public void setAltid(String altid) {
this.altid = altid;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getSubcategory() {
return subcategory;
}
public void setSubcategory(String subcategory) {
this.subcategory = subcategory;
}
public String getRoom() {
return room;
}
public void setRoom(String room) {
this.room = room;
}
public String getParent() {
return parent;
}
public void setParent(String parent) {
this.parent = parent;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getVeraname() {
return veraname;
}
public void setVeraname(String veraname) {
this.veraname = veraname;
}
public String getVeraaddress() {
return veraaddress;
}
public void setVeraaddress(String veraaddress) {
this.veraaddress = veraaddress;
}
}

View File

@@ -0,0 +1,26 @@
package com.bwssystems.HABridge.plugins.vera.luupRequests;
public class Room {
private String name;
private String id;
private String section;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSection() {
return section;
}
public void setSection(String section) {
this.section = section;
}
}

View File

@@ -0,0 +1,47 @@
package com.bwssystems.HABridge.plugins.vera.luupRequests;
public class Scene {
private String active;
private String name;
private String id;
private String room;
private String veraname;
private String veraaddress;
public String getActive() {
return active;
}
public void setActive(String active) {
this.active = active;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getRoom() {
return room;
}
public void setRoom(String room) {
this.room = room;
}
public String getVeraname() {
return veraname;
}
public void setVeraname(String veraname) {
this.veraname = veraname;
}
public String getVeraaddress() {
return veraaddress;
}
public void setVeraaddress(String veraaddress) {
this.veraaddress = veraaddress;
}
}

View File

@@ -0,0 +1,140 @@
package com.bwssystems.HABridge.plugins.vera.luupRequests;
import java.util.List;
public class Sdata {
private String full;
private String version;
private String model;
private String zwave_heal;
private String temperature;
private String serial_number;
private String fwd1;
private String fwd2;
private String ir;
private String irtx;
private String loadtime;
private String dataversion;
private String state;
private String comment;
private List<Section> sections;
private List<Room> rooms;
private List<Scene> scenes;
private List<Device> devices;
private List<Categorie> categories;
public String getFull() {
return full;
}
public void setFull(String full) {
this.full = full;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getZwave_heal() {
return zwave_heal;
}
public void setZwave_heal(String zwave_heal) {
this.zwave_heal = zwave_heal;
}
public String getTemperature() {
return temperature;
}
public void setTemperature(String temperature) {
this.temperature = temperature;
}
public String getSerial_number() {
return serial_number;
}
public void setSerial_number(String serial_number) {
this.serial_number = serial_number;
}
public String getFwd1() {
return fwd1;
}
public void setFwd1(String fwd1) {
this.fwd1 = fwd1;
}
public String getFwd2() {
return fwd2;
}
public void setFwd2(String fwd2) {
this.fwd2 = fwd2;
}
public String getIr() {
return ir;
}
public void setIr(String ir) {
this.ir = ir;
}
public String getIrtx() {
return irtx;
}
public void setIrtx(String irtx) {
this.irtx = irtx;
}
public String getLoadtime() {
return loadtime;
}
public void setLoadtime(String loadtime) {
this.loadtime = loadtime;
}
public String getDataversion() {
return dataversion;
}
public void setDataversion(String dataversion) {
this.dataversion = dataversion;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public List<Section> getSections() {
return sections;
}
public void setSections(List<Section> sections) {
this.sections = sections;
}
public List<Room> getRooms() {
return rooms;
}
public void setRooms(List<Room> rooms) {
this.rooms = rooms;
}
public List<Scene> getScenes() {
return scenes;
}
public void setScenes(List<Scene> scenes) {
this.scenes = scenes;
}
public List<Device> getDevices() {
return devices;
}
public void setDevices(List<Device> devices) {
this.devices = devices;
}
public List<Categorie> getCategoriess() {
return categories;
}
public void setCategoriess(List<Categorie> categories) {
this.categories = categories;
}
}

View File

@@ -0,0 +1,19 @@
package com.bwssystems.HABridge.plugins.vera.luupRequests;
public class Section {
private String name;
private String id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}

View File

@@ -0,0 +1,275 @@
package com.bwssystems.HABridge.upnp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.bwssystems.HABridge.BridgeControlDescriptor;
import com.bwssystems.HABridge.BridgeSettingsDescriptor;
import com.bwssystems.HABridge.Configuration;
import com.bwssystems.HABridge.api.hue.HueConstants;
import com.bwssystems.HABridge.api.hue.HuePublicConfig;
import com.bwssystems.HABridge.util.UDPDatagramSender;
import java.io.IOException;
import java.net.*;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Enumeration;
import org.apache.http.conn.util.*;
public class UpnpListener {
private Logger log = LoggerFactory.getLogger(UpnpListener.class);
private UDPDatagramSender theUDPDatagramSender;
private int httpServerPort;
private String responseAddress;
private boolean strict;
private boolean traceupnp;
private BridgeControlDescriptor bridgeControl;
private String bridgeId;
private String bridgeSNUUID;
private HuePublicConfig aHueConfig;
private String responseTemplate1 = "HTTP/1.1 200 OK\r\n" +
"HOST: %s:%s\r\n" +
"CACHE-CONTROL: max-age=100\r\n" +
"EXT:\r\n" +
"LOCATION: http://%s:%s/description.xml\r\n" +
"SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/" + HueConstants.API_VERSION + "\r\n" +
"hue-bridgeid: %s\r\n" +
"ST: upnp:rootdevice\r\n" +
"USN: uuid:" + HueConstants.UUID_PREFIX + "%s::upnp:rootdevice\r\n\r\n";
private String responseTemplate2 = "HTTP/1.1 200 OK\r\n" +
"HOST: %s:%s\r\n" +
"CACHE-CONTROL: max-age=100\r\n" +
"EXT:\r\n" +
"LOCATION: http://%s:%s/description.xml\r\n" +
"SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/" + HueConstants.API_VERSION + "\r\n" +
"hue-bridgeid: %s\r\n" +
"ST: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n" +
"USN: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n\r\n";
private String responseTemplate3 = "HTTP/1.1 200 OK\r\n" +
"HOST: %s:%s\r\n" +
"CACHE-CONTROL: max-age=100\r\n" +
"EXT:\r\n" +
"LOCATION: http://%s:%s/description.xml\r\n" +
"SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/" + HueConstants.API_VERSION + "\r\n" +
"hue-bridgeid: %s\r\n" +
"ST: urn:schemas-upnp-org:device:basic:1\r\n" +
"USN: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n\r\n";
private String notifyTemplate = "NOTIFY * HTTP/1.1\r\n" +
"HOST: %s:%s\r\n" +
"CACHE-CONTROL: max-age=100\r\n" +
"LOCATION: http://%s:%s/description.xml\r\n" +
"SERVER: Linux/3.14.0 UPnP/1.0 IpBridge/" + HueConstants.API_VERSION + "\r\n" +
"NTS: ssdp:alive\r\n" +
"hue-bridgeid: %s\r\n" +
"NT: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n" +
"USN: uuid:" + HueConstants.UUID_PREFIX + "%s\r\n\r\n";
public UpnpListener(BridgeSettingsDescriptor theSettings, BridgeControlDescriptor theControl, UDPDatagramSender aUdpDatagramSender) {
super();
theUDPDatagramSender = aUdpDatagramSender;
httpServerPort = Integer.valueOf(theSettings.getServerPort());
responseAddress = theSettings.getUpnpConfigAddress();
strict = theSettings.isUpnpStrict();
traceupnp = theSettings.isTraceupnp();
bridgeControl = theControl;
aHueConfig = HuePublicConfig.createConfig("temp", responseAddress, HueConstants.HUB_VERSION);
bridgeId = aHueConfig.getBridgeid();
bridgeSNUUID = aHueConfig.getSNUUIDFromMac();
}
@SuppressWarnings("resource")
public boolean startListening(){
log.info("UPNP Discovery Listener starting....");
MulticastSocket upnpMulticastSocket = null;
Enumeration<NetworkInterface> ifs = null;
try {
upnpMulticastSocket = new MulticastSocket(Configuration.UPNP_DISCOVERY_PORT);
} catch(IOException e){
log.error("Upnp Discovery Port is in use, or restricted by admin (try running with sudo or admin privs): " + Configuration.UPNP_DISCOVERY_PORT + " with message: " + e.getMessage());
return false;
}
InetSocketAddress socketAddress = new InetSocketAddress(Configuration.UPNP_MULTICAST_ADDRESS, Configuration.UPNP_DISCOVERY_PORT);
try {
ifs = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
log.error("Could not get network interfaces for this machine: " + e.getMessage());
return false;
}
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();
if (traceupnp)
log.info("Traceupnp: " + name + " ... has addr " + addr);
else
log.debug(name + " ... has addr " + addr);
if (InetAddressUtils.isIPv4Address(addr.getHostAddress())) {
IPsPerNic++;
}
}
log.debug("Checking " + name + " to our interface set");
if (IPsPerNic > 0) {
try {
upnpMulticastSocket.joinGroup(socketAddress, xface);
if (traceupnp)
log.info("Traceupnp: Adding " + name + " to our interface set");
else
log.debug("Adding " + name + " to our interface set");
} catch (IOException e) {
log.warn("Multicast join failed for: " + socketAddress.getHostName() + " to interface: "
+ xface.getName() + " with message: " + e.getMessage());
}
}
}
log.info("UPNP Discovery Listener running and ready....");
boolean loopControl = true;
boolean error = false;
try {
upnpMulticastSocket.setSoTimeout((int) Configuration.UPNP_NOTIFY_TIMEOUT);
} catch (SocketException e1) {
log.warn("Could not sent soTimeout on multi-cast socket");
}
Instant current, previous;
previous = Instant.now();
while (loopControl) { // trigger shutdown here
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
try {
upnpMulticastSocket.receive(packet);
if (isSSDPDiscovery(packet)) {
try {
sendUpnpResponse(packet.getAddress(), packet.getPort());
} catch (IOException e) {
log.warn("UpnpListener encountered an error sending upnp response packet. IP: " + packet.getAddress().getHostAddress() + " with message: " + e.getMessage());
log.debug("UpnpListener send upnp exception: ", e);
}
}
current = Instant.now();
if(ChronoUnit.MILLIS.between(previous, current) > Configuration.UPNP_NOTIFY_TIMEOUT) {
sendUpnpNotify(socketAddress.getAddress(), upnpMulticastSocket);
previous = Instant.now();
}
} catch (SocketTimeoutException e) {
sendUpnpNotify(socketAddress.getAddress(), upnpMulticastSocket);
} catch (IOException e) {
log.error("UpnpListener encountered an error reading socket. Shutting down", e);
error = true;
}
if (error || bridgeControl.isReinit() || bridgeControl.isStop()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// noop
}
loopControl = false;
}
}
upnpMulticastSocket.close();
if (bridgeControl.isReinit())
log.info("UPNP Discovery Listener - ended, restart found");
if (bridgeControl.isStop())
log.info("UPNP Discovery Listener - ended, stop found");
if (!bridgeControl.isStop() && !bridgeControl.isReinit()) {
log.info("UPNP Discovery Listener - ended, error found");
return false;
}
return bridgeControl.isReinit();
}
/**
* ssdp discovery packet detection
*/
protected boolean isSSDPDiscovery(DatagramPacket packet){
//Only respond to discover request for strict upnp form
String packetString = new String(packet.getData(), 0, packet.getLength());
if(packetString != null && packetString.startsWith("M-SEARCH * HTTP/1.1") && packetString.contains("\"ssdp:discover\"")){
log.debug("isSSDPDiscovery Found message to be an M-SEARCH message.");
log.debug("isSSDPDiscovery Got SSDP packet from " + packet.getAddress().getHostAddress() + ":" + packet.getPort() + ", body: " + packetString);
if(strict && (packetString.contains("ST: urn:schemas-upnp-org:device:basic:1") || packetString.contains("ST: upnp:rootdevice") || packetString.contains("ST: ssdp:all")))
{
if(traceupnp) {
log.info("Traceupnp: isSSDPDiscovery found message to be an M-SEARCH message.");
log.info("Traceupnp: isSSDPDiscovery found message to be valid under strict rules - strict: " + strict);
log.info("Traceupnp: SSDP packet from " + packet.getAddress().getHostAddress() + ":" + packet.getPort() + ", body: " + packetString);
}
else
log.debug("isSSDPDiscovery found message to be valid under strict rules - strict: " + strict);
return true;
}
else if (!strict)
{
if(traceupnp) {
log.info("Traceupnp: isSSDPDiscovery found message to be an M-SEARCH message.");
log.info("Traceupnp: isSSDPDiscovery found message to be valid under loose rules - strict: " + strict);
log.info("Traceupnp: SSDP packet from " + packet.getAddress().getHostAddress() + ":" + packet.getPort() + ", body: " + packetString);
}
else
log.debug("isSSDPDiscovery found message to be valid under loose rules - strict: " + strict);
return true;
}
}
else {
// log.debug("isSSDPDiscovery found message to not be valid - strict: " + strict);
// log.debug("SSDP packet from " + packet.getAddress().getHostAddress() + ":" + packet.getPort() + ", body: " + packetString);
}
return false;
}
protected void sendUpnpResponse(InetAddress requester, int sourcePort) throws IOException {
String discoveryResponse = null;
discoveryResponse = String.format(responseTemplate1, Configuration.UPNP_MULTICAST_ADDRESS, Configuration.UPNP_DISCOVERY_PORT, responseAddress, httpServerPort, bridgeId, bridgeSNUUID);
if(traceupnp) {
log.info("Traceupnp: sendUpnpResponse discovery responseTemplate1 is <<<" + discoveryResponse + ">>>");
}
else
log.debug("sendUpnpResponse discovery responseTemplate1 is <<<" + discoveryResponse + ">>>");
theUDPDatagramSender.sendUDPResponse(discoveryResponse.getBytes(), requester, sourcePort);
discoveryResponse = String.format(responseTemplate2, Configuration.UPNP_MULTICAST_ADDRESS, Configuration.UPNP_DISCOVERY_PORT, responseAddress, httpServerPort, bridgeId, bridgeSNUUID, bridgeSNUUID);
if(traceupnp) {
log.info("Traceupnp: sendUpnpResponse discovery responseTemplate2 is <<<" + discoveryResponse + ">>>");
}
else
log.debug("sendUpnpResponse discovery responseTemplate2 is <<<" + discoveryResponse + ">>>");
theUDPDatagramSender.sendUDPResponse(discoveryResponse.getBytes(), requester, sourcePort);
discoveryResponse = String.format(responseTemplate3, Configuration.UPNP_MULTICAST_ADDRESS, Configuration.UPNP_DISCOVERY_PORT, responseAddress, httpServerPort, bridgeId, bridgeSNUUID);
if(traceupnp) {
log.info("Traceupnp: sendUpnpResponse discovery responseTemplate3 is <<<" + discoveryResponse + ">>>");
}
else
log.debug("sendUpnpResponse discovery responseTemplate3 is <<<" + discoveryResponse + ">>>");
theUDPDatagramSender.sendUDPResponse(discoveryResponse.getBytes(), requester, sourcePort);
}
protected void sendUpnpNotify(InetAddress aSocketAddress, MulticastSocket theUpnpMulticastSocket) {
String notifyData = null;
log.debug("Sending notify packet for upnp.");
notifyData = String.format(notifyTemplate, Configuration.UPNP_MULTICAST_ADDRESS, Configuration.UPNP_DISCOVERY_PORT, responseAddress, httpServerPort, bridgeId, bridgeSNUUID, bridgeSNUUID);
if(traceupnp) {
log.info("Traceupnp: sendUpnpNotify notifyTemplate is <<<" + notifyData + ">>>");
}
DatagramPacket notifyPacket = new DatagramPacket(notifyData.getBytes(), notifyData.length(), aSocketAddress, Configuration.UPNP_DISCOVERY_PORT);
try {
theUpnpMulticastSocket.send(notifyPacket);
} catch (IOException e1) {
log.warn("UpnpListener encountered an error sending upnp notify packet. IP: " + notifyPacket.getAddress().getHostAddress() + " with message: " + e1.getMessage());
log.debug("UpnpListener send upnp notify exception: ", e1);
}
}
}

Some files were not shown because too many files have changed in this diff Show More