mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-19 08:28:46 +00:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
937fb5d32d | ||
|
|
bd60d63d0f | ||
|
|
439b081bd5 | ||
|
|
41f68f58b0 | ||
|
|
fa15cf3952 | ||
|
|
3ea7f2903f |
2
pom.xml
2
pom.xml
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
<groupId>com.bwssystems.HABridge</groupId>
|
<groupId>com.bwssystems.HABridge</groupId>
|
||||||
<artifactId>ha-bridge</artifactId>
|
<artifactId>ha-bridge</artifactId>
|
||||||
<version>0.3.1</version>
|
<version>0.3.2</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>HA Bridge</name>
|
<name>HA Bridge</name>
|
||||||
|
|||||||
@@ -75,7 +75,6 @@ public class HABridge {
|
|||||||
|
|
||||||
// start the upnp ssdp discovery listener
|
// start the upnp ssdp discovery listener
|
||||||
theUpnpListener = new UpnpListener(bridgeSettings);
|
theUpnpListener = new UpnpListener(bridgeSettings);
|
||||||
log.info("Done setup, application to run....");
|
|
||||||
theUpnpListener.startListening();
|
theUpnpListener.startListening();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ public class DeviceRepository {
|
|||||||
DeviceDescriptor theDevice = null;
|
DeviceDescriptor theDevice = null;
|
||||||
while (theIterator.hasNext()) {
|
while (theIterator.hasNext()) {
|
||||||
theDevice = theIterator.next();
|
theDevice = theIterator.next();
|
||||||
put(Integer.parseInt(theDevice.getId()), theDevice);
|
put(theDevice.getId(), theDevice);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -62,20 +62,20 @@ public class DeviceRepository {
|
|||||||
return devices.get(id);
|
return devices.get(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void put(int id, DeviceDescriptor aDescriptor) {
|
private void put(String id, DeviceDescriptor aDescriptor) {
|
||||||
devices.put(String.valueOf(id), aDescriptor);
|
devices.put(id, aDescriptor);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save(DeviceDescriptor aDescriptor) {
|
public void save(DeviceDescriptor aDescriptor) {
|
||||||
int id = random.nextInt(Integer.MAX_VALUE);
|
|
||||||
if(aDescriptor.getId() != null)
|
if(aDescriptor.getId() != null)
|
||||||
devices.remove(aDescriptor.getId());
|
devices.remove(aDescriptor.getId());
|
||||||
else
|
else
|
||||||
aDescriptor.setId(String.valueOf(id));
|
aDescriptor.setId(String.valueOf(random.nextInt(Integer.MAX_VALUE)));
|
||||||
put(id, aDescriptor);
|
put(aDescriptor.getId(), aDescriptor);
|
||||||
JsonTransformer aRenderer = new JsonTransformer();
|
JsonTransformer aRenderer = new JsonTransformer();
|
||||||
String jsonValue = aRenderer.render(findAll());
|
String jsonValue = aRenderer.render(findAll());
|
||||||
repositoryWriter(jsonValue, repositoryPath);
|
repositoryWriter(jsonValue, repositoryPath);
|
||||||
|
log.debug("Save device: " + aDescriptor.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
public String delete(DeviceDescriptor aDescriptor) {
|
public String delete(DeviceDescriptor aDescriptor) {
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ public class DeviceResource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void setupEndpoints() {
|
private void setupEndpoints() {
|
||||||
log.debug("Setting up endpoints");
|
log.info("HABridge device management service started.... ");
|
||||||
post(API_CONTEXT, "application/json", (request, response) -> {
|
post(API_CONTEXT, "application/json", (request, response) -> {
|
||||||
log.debug("Create a Device - request body: " + request.body());
|
log.debug("Create a Device - request body: " + request.body());
|
||||||
DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);
|
DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);
|
||||||
@@ -56,22 +56,24 @@ public class DeviceResource {
|
|||||||
log.debug("Create a Device - device json off URL:" + deviceEntry.getOffUrl());
|
log.debug("Create a Device - device json off URL:" + deviceEntry.getOffUrl());
|
||||||
|
|
||||||
deviceRepository.save(deviceEntry);
|
deviceRepository.save(deviceEntry);
|
||||||
log.debug("Created a Device");
|
log.debug("Created a Device: " + request.body());
|
||||||
|
|
||||||
response.status(201);
|
response.status(201);
|
||||||
return deviceEntry;
|
return deviceEntry;
|
||||||
}, new JsonTransformer());
|
}, new JsonTransformer());
|
||||||
|
|
||||||
put (API_CONTEXT + "/:id", "application/json", (request, response) -> {
|
put (API_CONTEXT + "/:id", "application/json", (request, response) -> {
|
||||||
log.debug("Saved a Device");
|
|
||||||
DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);
|
DeviceDescriptor device = new Gson().fromJson(request.body(), DeviceDescriptor.class);
|
||||||
DeviceDescriptor deviceEntry = deviceRepository.findOne(request.params(":id"));
|
DeviceDescriptor deviceEntry = deviceRepository.findOne(request.params(":id"));
|
||||||
if(deviceEntry == null){
|
if(deviceEntry == null){
|
||||||
|
log.debug("Could not save an edited Device Id: " + request.params(":id"));
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
log.debug("Saving an edited Device: " + deviceEntry.getName());
|
||||||
|
|
||||||
deviceEntry.setName(device.getName());
|
deviceEntry.setName(device.getName());
|
||||||
deviceEntry.setDeviceType(device.getDeviceType());
|
if(device.getDeviceType() != null)
|
||||||
|
deviceEntry.setDeviceType(device.getDeviceType());
|
||||||
deviceEntry.setOnUrl(device.getOnUrl());
|
deviceEntry.setOnUrl(device.getOnUrl());
|
||||||
deviceEntry.setOffUrl(device.getOffUrl());
|
deviceEntry.setOffUrl(device.getOffUrl());
|
||||||
|
|
||||||
|
|||||||
@@ -53,10 +53,11 @@ public class HueMulator {
|
|||||||
|
|
||||||
// This function sets up the sparkjava rest calls for the hue api
|
// This function sets up the sparkjava rest calls for the hue api
|
||||||
private void setupEndpoints() {
|
private void setupEndpoints() {
|
||||||
|
log.info("Hue emulator service started....");
|
||||||
// http://ip_address:port/api/{userId}/lights returns json objects of all lights configured
|
// http://ip_address:port/api/{userId}/lights returns json objects of all lights configured
|
||||||
get(HUE_CONTEXT + "/:userid/lights", "application/json", (request, response) -> {
|
get(HUE_CONTEXT + "/:userid/lights", "application/json", (request, response) -> {
|
||||||
String userId = request.params(":userid");
|
String userId = request.params(":userid");
|
||||||
log.info("hue lights list requested: " + userId + " from " + request.ip());
|
log.debug("hue lights list requested: " + userId + " from " + request.ip());
|
||||||
List<DeviceDescriptor> deviceList = repository.findAll();
|
List<DeviceDescriptor> deviceList = repository.findAll();
|
||||||
Map<String, DeviceResponse> deviceResponseMap = new HashMap<>();
|
Map<String, DeviceResponse> deviceResponseMap = new HashMap<>();
|
||||||
for (DeviceDescriptor device : deviceList) {
|
for (DeviceDescriptor device : deviceList) {
|
||||||
@@ -83,7 +84,7 @@ public class HueMulator {
|
|||||||
// http://ip_address:port/api/{userId} returns json objects for the full state
|
// http://ip_address:port/api/{userId} returns json objects for the full state
|
||||||
get(HUE_CONTEXT + "/:userid", "application/json", (request, response) -> {
|
get(HUE_CONTEXT + "/:userid", "application/json", (request, response) -> {
|
||||||
String userId = request.params(":userid");
|
String userId = request.params(":userid");
|
||||||
log.info("hue api full state requested: " + userId + " from " + request.ip());
|
log.debug("hue api full state requested: " + userId + " from " + request.ip());
|
||||||
List<DeviceDescriptor> descriptorList = repository.findAll();
|
List<DeviceDescriptor> descriptorList = repository.findAll();
|
||||||
if (descriptorList == null) {
|
if (descriptorList == null) {
|
||||||
response.status(404);
|
response.status(404);
|
||||||
@@ -107,13 +108,13 @@ public class HueMulator {
|
|||||||
get(HUE_CONTEXT + "/:userid/lights/:id", "application/json", (request, response) -> {
|
get(HUE_CONTEXT + "/:userid/lights/:id", "application/json", (request, response) -> {
|
||||||
String userId = request.params(":userid");
|
String userId = request.params(":userid");
|
||||||
String lightId = request.params(":id");
|
String lightId = request.params(":id");
|
||||||
log.info("hue light requested: " + lightId + "for user: " + userId + " from " + request.ip());
|
log.debug("hue light requested: " + lightId + " for user: " + userId + " from " + request.ip());
|
||||||
DeviceDescriptor device = repository.findOne(lightId);
|
DeviceDescriptor device = repository.findOne(lightId);
|
||||||
if (device == null) {
|
if (device == null) {
|
||||||
response.status(404);
|
response.status(404);
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
log.info("found device named: " + device.getName());
|
log.debug("found device named: " + device.getName());
|
||||||
}
|
}
|
||||||
DeviceResponse lightResponse = DeviceResponse.createResponse(device.getName(), device.getId());
|
DeviceResponse lightResponse = DeviceResponse.createResponse(device.getName(), device.getId());
|
||||||
|
|
||||||
@@ -129,14 +130,13 @@ public class HueMulator {
|
|||||||
*/
|
*/
|
||||||
String userId = request.params(":userid");
|
String userId = request.params(":userid");
|
||||||
String lightId = request.params(":id");
|
String lightId = request.params(":id");
|
||||||
log.info("hue state change requested: " + userId + " from " + request.ip());
|
log.debug("hue state change requested: " + userId + " from " + request.ip() + " body: " + request.body());
|
||||||
log.info("hue stage change body: " + request.body() );
|
|
||||||
|
|
||||||
DeviceState state = null;
|
DeviceState state = null;
|
||||||
try {
|
try {
|
||||||
state = mapper.readValue(request.body(), DeviceState.class);
|
state = mapper.readValue(request.body(), DeviceState.class);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.info("object mapper barfed on input", e);
|
log.error("Object mapper barfed on input of body.", e);
|
||||||
response.status(400);
|
response.status(400);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -144,6 +144,7 @@ public class HueMulator {
|
|||||||
DeviceDescriptor device = repository.findOne(lightId);
|
DeviceDescriptor device = repository.findOne(lightId);
|
||||||
if (device == null) {
|
if (device == null) {
|
||||||
response.status(404);
|
response.status(404);
|
||||||
|
log.error("Could not find devcie: " + lightId + " for hue state change request: " + userId + " from " + request.ip() + " body: " + request.body());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,6 +179,7 @@ public class HueMulator {
|
|||||||
//make call
|
//make call
|
||||||
if(!doHttpGETRequest(url)){
|
if(!doHttpGETRequest(url)){
|
||||||
response.status(503);
|
response.status(503);
|
||||||
|
log.error("Error on calling url to change device state: " + url);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,12 +190,12 @@ public class HueMulator {
|
|||||||
|
|
||||||
// This function executes the url from the device repository against the vera
|
// This function executes the url from the device repository against the vera
|
||||||
protected boolean doHttpGETRequest(String url) {
|
protected boolean doHttpGETRequest(String url) {
|
||||||
log.info("calling GET on URL: " + url);
|
log.debug("calling GET on URL: " + url);
|
||||||
HttpGet httpGet = new HttpGet(url);
|
HttpGet httpGet = new HttpGet(url);
|
||||||
try {
|
try {
|
||||||
HttpResponse response = httpClient.execute(httpGet);
|
HttpResponse response = httpClient.execute(httpGet);
|
||||||
EntityUtils.consume(response.getEntity()); //close out inputstream ignore content
|
EntityUtils.consume(response.getEntity()); //close out inputstream ignore content
|
||||||
log.info("GET on URL responded: " + response.getStatusLine().getStatusCode());
|
log.debug("GET on URL responded: " + response.getStatusLine().getStatusCode());
|
||||||
if(response.getStatusLine().getStatusCode() == 200){
|
if(response.getStatusLine().getStatusCode() == 200){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ public class UpnpListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void startListening(){
|
public void startListening(){
|
||||||
log.info("Starting UPNP Discovery Listener");
|
log.info("UPNP Discovery Listener started....");
|
||||||
|
|
||||||
try (DatagramSocket responseSocket = new DatagramSocket(upnpResponsePort);
|
try (DatagramSocket responseSocket = new DatagramSocket(upnpResponsePort);
|
||||||
MulticastSocket upnpMulticastSocket = new MulticastSocket(UPNP_DISCOVERY_PORT);) {
|
MulticastSocket upnpMulticastSocket = new MulticastSocket(UPNP_DISCOVERY_PORT);) {
|
||||||
@@ -100,7 +100,7 @@ public class UpnpListener {
|
|||||||
String discoveryTemplate = "HTTP/1.1 200 OK\r\n" +
|
String discoveryTemplate = "HTTP/1.1 200 OK\r\n" +
|
||||||
"CACHE-CONTROL: max-age=86400\r\n" +
|
"CACHE-CONTROL: max-age=86400\r\n" +
|
||||||
"EXT:\r\n" +
|
"EXT:\r\n" +
|
||||||
"LOCATION: http://%s:%s/upnp/ha-bridge/description.xml\r\n" +
|
"LOCATION: http://%s:%s/description.xml\r\n" +
|
||||||
"SERVER: FreeRTOS/6.0.5, UPnP/1.0, IpBridge/0.1\r\n" +
|
"SERVER: FreeRTOS/6.0.5, UPnP/1.0, IpBridge/0.1\r\n" +
|
||||||
"ST: urn:schemas-upnp-org:device:basic:1\r\n" +
|
"ST: urn:schemas-upnp-org:device:basic:1\r\n" +
|
||||||
"USN: uuid:Socket-1_0-221438K0100073::urn:schemas-upnp-org:device:basic:1\r\n\r\n";
|
"USN: uuid:Socket-1_0-221438K0100073::urn:schemas-upnp-org:device:basic:1\r\n\r\n";
|
||||||
|
|||||||
@@ -44,9 +44,10 @@ public class UpnpSettingsResource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void setupListener (BridgeSettings theSettings) {
|
private void setupListener (BridgeSettings theSettings) {
|
||||||
// http://ip_address:port/upnp/:id/description.xml which returns the xml configuration for the location of the hue emulator
|
log.info("Hue description service started....");
|
||||||
get(UPNP_CONTEXT + "/:id/description.xml", "application/xml", (request, response) -> {
|
// http://ip_adress:port/description.xml which returns the xml configuration for the hue emulator
|
||||||
log.info("upnp device settings requested: " + request.params(":id") + " from " + request.ip());
|
get("/description.xml", "application/xml", (request, response) -> {
|
||||||
|
log.debug("upnp device settings requested: " + request.params(":id") + " from " + request.ip());
|
||||||
String portNumber = Integer.toString(request.port());
|
String portNumber = Integer.toString(request.port());
|
||||||
String filledTemplate = String.format(hueTemplate, theSettings.getUpnpConfigAddress(), portNumber, theSettings.getUpnpConfigAddress());
|
String filledTemplate = String.format(hueTemplate, theSettings.getUpnpConfigAddress(), portNumber, theSettings.getUpnpConfigAddress());
|
||||||
log.debug("upnp device settings response: " + filledTemplate);
|
log.debug("upnp device settings response: " + filledTemplate);
|
||||||
|
|||||||
@@ -57,8 +57,14 @@ public class VeraInfo {
|
|||||||
Device theDevice = null;
|
Device theDevice = null;
|
||||||
while (theIterator.hasNext()) {
|
while (theIterator.hasNext()) {
|
||||||
theDevice = theIterator.next();
|
theDevice = theIterator.next();
|
||||||
theDevice.setRoom(roomMap.get(theDevice.getRoom()).getName());
|
if(theDevice.getRoom() != null && roomMap.get(theDevice.getRoom()) != null)
|
||||||
theDevice.setCategory(categoryMap.get(theDevice.getCategory()).getName());
|
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>");
|
||||||
}
|
}
|
||||||
|
|
||||||
ListIterator<Scene> theSecneIter = theSdata.getScenes().listIterator();
|
ListIterator<Scene> theSecneIter = theSdata.getScenes().listIterator();
|
||||||
@@ -71,13 +77,13 @@ public class VeraInfo {
|
|||||||
|
|
||||||
// This function executes the url against the vera
|
// This function executes the url against the vera
|
||||||
protected String doHttpGETRequest(String url) {
|
protected String doHttpGETRequest(String url) {
|
||||||
log.info("calling GET on URL: " + url);
|
log.debug("calling GET on URL: " + url);
|
||||||
HttpGet httpGet = new HttpGet(url);
|
HttpGet httpGet = new HttpGet(url);
|
||||||
try {
|
try {
|
||||||
HttpResponse response = httpClient.execute(httpGet);
|
HttpResponse response = httpClient.execute(httpGet);
|
||||||
String theContent = EntityUtils.toString(response.getEntity()); //read content for data
|
String theContent = EntityUtils.toString(response.getEntity()); //read content for data
|
||||||
EntityUtils.consume(response.getEntity()); //close out inputstream ignore content
|
EntityUtils.consume(response.getEntity()); //close out inputstream ignore content
|
||||||
log.info("GET on URL responded: " + response.getStatusLine().getStatusCode());
|
log.debug("GET on URL responded: " + response.getStatusLine().getStatusCode());
|
||||||
if(response.getStatusLine().getStatusCode() == 200){
|
if(response.getStatusLine().getStatusCode() == 200){
|
||||||
return theContent;
|
return theContent;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,7 @@
|
|||||||
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
|
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
|
||||||
<li><a href="http://www.bwssystems.com" target="_blank">Developed by BWS Systems</a></li>
|
<li><a href="http://www.bwssystems.com" target="_blank">Developed by BWS Systems</a></li>
|
||||||
<li><a href="http://www.amazon.com/echo" target="_blank">Amazon Echo</a></li>
|
<li><a href="http://www.amazon.com/echo" target="_blank">Amazon Echo</a></li>
|
||||||
<li><a href="#">HA Bridge Version 0.3.0</a></li>
|
<li><a href="#">HA Bridge Version 0.3.2</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|||||||
Reference in New Issue
Block a user