From 9aaab37a17e829a3c94bf09f92d797385ca87edf Mon Sep 17 00:00:00 2001 From: diamond Date: Thu, 4 May 2017 05:59:04 +0300 Subject: [PATCH] auth --- .../bwssystems/HABridge/BridgeSettings.java | 2 +- .../HABridge/plugins/fibaro/FibaroInfo.java | 45 +++++++++++++++---- src/main/resources/public/scripts/app.js | 6 ++- .../resources/public/views/fibarodevice.html | 4 +- .../resources/public/views/fibaroscene.html | 4 +- src/main/resources/public/views/system.html | 13 +++++- 6 files changed, 58 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/bwssystems/HABridge/BridgeSettings.java b/src/main/java/com/bwssystems/HABridge/BridgeSettings.java index 200f267..d6d54f7 100644 --- a/src/main/java/com/bwssystems/HABridge/BridgeSettings.java +++ b/src/main/java/com/bwssystems/HABridge/BridgeSettings.java @@ -110,7 +110,7 @@ public class BridgeSettings extends BackupHandler { theBridgeSettings.setVeraAddress(theVeraList); theFibaroAddress = System.getProperty("fibaro.address"); - IpList theFibaroList = null; + IpList theFibaroList = null; if(theFibaroAddress != null) { try { theFibaroList = new Gson().fromJson(theFibaroAddress, IpList.class); diff --git a/src/main/java/com/bwssystems/HABridge/plugins/fibaro/FibaroInfo.java b/src/main/java/com/bwssystems/HABridge/plugins/fibaro/FibaroInfo.java index 019ff94..bbd6ae2 100644 --- a/src/main/java/com/bwssystems/HABridge/plugins/fibaro/FibaroInfo.java +++ b/src/main/java/com/bwssystems/HABridge/plugins/fibaro/FibaroInfo.java @@ -1,16 +1,20 @@ package com.bwssystems.HABridge.plugins.fibaro; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +import org.apache.commons.codec.binary.Base64; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.bwssystems.HABridge.NamedIP; -import com.bwssystems.HABridge.plugins.http.HTTPHandler; import com.google.gson.Gson; public class FibaroInfo { private static final Logger log = LoggerFactory.getLogger(FibaroInfo.class); - private HTTPHandler httpClient; private NamedIP fibaroAddress; // You can disable it if you want @@ -22,7 +26,6 @@ public class FibaroInfo public FibaroInfo(NamedIP addressName) { super(); - httpClient = new HTTPHandler(); fibaroAddress = addressName; } @@ -35,10 +38,8 @@ public class FibaroInfo Device[] all_devices = getAllDevices(); int count = 0; for(Device d : all_devices) - { if(d.roomID > 0 && useSaveLogs ? "true".equals(d.properties.saveLogs) : true) count++; - } Device[] devices = new Device[count]; int i = 0; @@ -62,7 +63,7 @@ public class FibaroInfo d.fibaroname = fibaroAddress.getName(); } - log.debug("Founded: " + devices.length + " devices"); + log.info("Founded: " + devices.length + " devices"); return devices; } @@ -92,7 +93,7 @@ public class FibaroInfo s.fibaroaddress = fibaroAddress.getIp(); s.fibaroname = fibaroAddress.getName(); } - System.out.println("Founded: " + count + " scenes"); + log.info("Founded: " + count + " scenes"); return result; } @@ -125,7 +126,35 @@ public class FibaroInfo private String request(String theUrl) { theUrl = "http://" + fibaroAddress.getIp() + "/api/" + theUrl; - return httpClient.doHttpRequest(theUrl, null, null, null, null); + String auth = new String(Base64.encodeBase64((fibaroAddress.getUsername() + ":" + fibaroAddress.getPassword()).getBytes())); + java.net.URL url; + java.net.HttpURLConnection connection; + String result = null; + try + { + url = new URL(theUrl); + connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setRequestProperty("Authorization", "Basic " + auth); + connection.setRequestProperty("Content-Type", "application/json;charset=utf-8"); + connection.setRequestProperty("X-Requested-With", "XMLHttpRequest"); + connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36"); + connection.connect(); + BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); + StringBuilder buffer = new StringBuilder(); + String line; + while((line = br.readLine()) != null) + buffer.append(line).append("\n"); + br.close(); + result = buffer.toString(); + } + catch(Exception e) + { + log.info("Error while get getJson: " + theUrl); + e.printStackTrace(); + return null; + } + return result; } private String replaceDigits(String name) diff --git a/src/main/resources/public/scripts/app.js b/src/main/resources/public/scripts/app.js index 1f8f8a2..3b3e5c2 100644 --- a/src/main/resources/public/scripts/app.js +++ b/src/main/resources/public/scripts/app.js @@ -1331,14 +1331,16 @@ app.controller ('SystemController', function ($scope, $location, bridgeService, } } }; - $scope.addFibarotoSettings = function (newfibaroname, newfibaroip) { + $scope.addFibarotoSettings = function (newfibaroname, newfibaroip, newfibarousername, newfibaropassword) { if($scope.bridge.settings.fibaroaddress === undefined || $scope.bridge.settings.fibaroaddress === null) { $scope.bridge.settings.fibaroaddress = { devices: [] }; } - var newFibaro = {name: newfibaroname, ip: newfibaroip } + var newFibaro = {name: newfibaroname, ip: newfibaroip, username: newfibarousername, password: newfibaropassword } $scope.bridge.settings.fibaroaddress.devices.push(newFibaro); $scope.newfibaroname = null; $scope.newfibaroip = null; + $scope.newfibarousername = null; + $scope.newfibaropassword = null; }; $scope.removeFibarotoSettings = function (fibaroname, fibaroip) { for(var i = $scope.bridge.settings.fibaroaddress.devices.length - 1; i >= 0; i--) { diff --git a/src/main/resources/public/views/fibarodevice.html b/src/main/resources/public/views/fibarodevice.html index 25c7145..fe8e513 100644 --- a/src/main/resources/public/views/fibarodevice.html +++ b/src/main/resources/public/views/fibarodevice.html @@ -2,8 +2,8 @@
  • Bridge Devices
  • Bridge Control
  • Logs
  • -
  • Vera Devices
  • -
  • Vera Scenes
  • +
  • Vera Devices
  • +
  • Vera Scenes
  • Fibaro Scenes
  • Bridge Devices
  • Bridge Control
  • Logs
  • -
  • Vera Devices
  • -
  • Vera Scenes
  • +
  • Vera Devices
  • +
  • Vera Scenes
  • Fibaro Devices
  • Name IP + Username + Password Manage {{fibaro.name}} {{fibaro.ip}} + {{fibaro.username}} + ******* + @@ -158,8 +163,14 @@ + + + ng-click="addFibarotoSettings(newfibaroname, newfibaroip, newfibarousername, newfibaropassword)">Add