Refactor classes

This commit is contained in:
2018-08-23 15:25:12 +02:00
parent 3798d18844
commit 09d8b7781f
13 changed files with 392 additions and 384 deletions

View File

@@ -1,4 +1,4 @@
package manage.FITBITData; package device.FITBITData;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

View File

@@ -1,4 +1,4 @@
package manage.FITBITData; package device.FITBITData;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

View File

@@ -1,4 +1,4 @@
package manage.FITBITData; package device.FITBITData;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;

View File

@@ -1,4 +1,4 @@
package manage.FITBITData; package device.FITBITData;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;

View File

@@ -1,11 +1,14 @@
package manage.FITBITData; package device;
import java.io.IOException; import java.io.IOException;
import java.util.Calendar; import java.util.Calendar;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import manage.AuthFITBIT; import device.FITBITData.HeartRate;
import device.FITBITData.Sleep;
import device.FITBITData.Steps;
import oauth.AuthFITBIT;
public class FitBit { public class FitBit {

View File

@@ -3,7 +3,7 @@ package device;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import manage.Rest; import support.Rest;
public class Hue { public class Hue {
//private String baseURL = "192.168.0.2"; //private String baseURL = "192.168.0.2";

View File

@@ -4,6 +4,8 @@ import de.fh_zwickau.informatik.sensor.IZWayApi;
import de.fh_zwickau.informatik.sensor.ZWayApiHttp; import de.fh_zwickau.informatik.sensor.ZWayApiHttp;
import de.fh_zwickau.informatik.sensor.model.devices.Device; import de.fh_zwickau.informatik.sensor.model.devices.Device;
import de.fh_zwickau.informatik.sensor.model.devices.DeviceList; import de.fh_zwickau.informatik.sensor.model.devices.DeviceList;
import support.ZWaySimpleCallback;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -21,15 +23,12 @@ public class Sensor {
public IZWayApi zwayApi; public IZWayApi zwayApi;
private DeviceList allZWaveDevices; private DeviceList allZWaveDevices;
private DeviceList devices; private DeviceList devices;
private Integer nodeId;
public Sensor() { public Sensor() {
this(null); this(null);
} }
public Sensor (Integer nodeId) { public Sensor (Integer nodeId) {
this.nodeId = nodeId;
// create an instance of the Z-Way library; all the params are mandatory (we are not going to use the remote service/id) // create an instance of the Z-Way library; all the params are mandatory (we are not going to use the remote service/id)
zwayApi = new ZWayApiHttp(ipAddress, 8083, "http", username, password, 0, false, new ZWaySimpleCallback()); zwayApi = new ZWayApiHttp(ipAddress, 8083, "http", username, password, 0, false, new ZWaySimpleCallback());

View File

@@ -1,4 +1,4 @@
package manage; package oauth;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;

View File

@@ -1,28 +1,28 @@
package manage; package oauth;
import com.google.api.client.http.GenericUrl; import com.google.api.client.http.GenericUrl;
import com.google.api.client.util.Key; import com.google.api.client.util.Key;
public class FITBITUrl extends GenericUrl { public class FITBITUrl extends GenericUrl {
@Key @Key
private String fields; private String fields;
public FITBITUrl(String encodedUrl) { public FITBITUrl(String encodedUrl) {
super(encodedUrl); super(encodedUrl);
} }
/** /**
* @return the fields * @return the fields
*/ */
public String getFields() { public String getFields() {
return fields; return fields;
} }
/** /**
* @param fields the fields to set * @param fields the fields to set
*/ */
public void setFields(String fields) { public void setFields(String fields) {
this.fields = fields; this.fields = fields;
} }
} }

View File

@@ -1,17 +1,17 @@
package manage; package oauth;
public class OAuth2ClientCredentials { public class OAuth2ClientCredentials {
/** Value of the "API Key". */ /** Value of the "API Key". */
public static final String API_KEY = "22CSTL"; //maybe togliere le virgolette public static final String API_KEY = "22CSTL"; //maybe togliere le virgolette
/** Value of the "API Secret". */ /** Value of the "API Secret". */
public static final String API_SECRET = "ea2452013abd35609940ce5601960a08"; //maybe togliere le virgolette public static final String API_SECRET = "ea2452013abd35609940ce5601960a08"; //maybe togliere le virgolette
/** Port in the "Callback URL". */ /** Port in the "Callback URL". */
public static final int PORT = 8080; public static final int PORT = 8080;
/** Domain name in the "Callback URL". */ /** Domain name in the "Callback URL". */
public static final String DOMAIN = "127.0.0.1"; public static final String DOMAIN = "127.0.0.1";
} }

View File

@@ -1,84 +1,84 @@
package manage; package support;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.google.gson.Gson; import com.google.gson.Gson;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
/** /**
* A generic class to perform HTTP calls and parse the resulting JSON. * A generic class to perform HTTP calls and parse the resulting JSON.
* *
* @author <a href="mailto:luigi.derussis@uniupo.it">Luigi De Russis</a> * @author <a href="mailto:luigi.derussis@uniupo.it">Luigi De Russis</a>
* @version 1.0 (18/05/2017) * @version 1.0 (18/05/2017)
*/ */
public class Rest { public class Rest {
// init gson // init gson
private static final Gson gson = new Gson(); private static final Gson gson = new Gson();
/** /**
* Perform a GET request towards a server * Perform a GET request towards a server
* *
* @param URL the @link{URL} to call * @param URL the @link{URL} to call
* @return the response, parsed from JSON * @return the response, parsed from JSON
*/ */
public static Map<String, ?> get(String URL) { public static Map<String, ?> get(String URL) {
// init // init
Map<String, ?> response = new HashMap<>(); Map<String, ?> response = new HashMap<>();
CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet request = new HttpGet(URL); HttpGet request = new HttpGet(URL);
CloseableHttpResponse result = null; CloseableHttpResponse result = null;
try { try {
result = httpclient.execute(request); result = httpclient.execute(request);
String json = EntityUtils.toString(result.getEntity()); String json = EntityUtils.toString(result.getEntity());
// do something useful with the response body // do something useful with the response body
response = gson.fromJson(json, Map.class); response = gson.fromJson(json, Map.class);
// should be inside a finally... // should be inside a finally...
result.close(); result.close();
httpclient.close(); httpclient.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
return response; return response;
} }
/** /**
* Perform a PUT request towards a server * Perform a PUT request towards a server
* *
* @param URL the @link{URL} to call * @param URL the @link{URL} to call
* @param contentBody the content body of the request * @param contentBody the content body of the request
* @param contentType the content type of the request * @param contentType the content type of the request
*/ */
public static void put(String URL, String contentBody, String contentType) { public static void put(String URL, String contentBody, String contentType) {
// init // init
CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPut request = new HttpPut(URL); HttpPut request = new HttpPut(URL);
StringEntity params = null; StringEntity params = null;
try { try {
params = new StringEntity(contentBody); params = new StringEntity(contentBody);
request.addHeader("content-type", contentType); request.addHeader("content-type", contentType);
request.setEntity(params); request.setEntity(params);
// I don't really care about the response // I don't really care about the response
HttpResponse result = httpclient.execute(request); HttpResponse result = httpclient.execute(request);
// should be in finally... // should be in finally...
httpclient.close(); httpclient.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }

View File

@@ -1,233 +1,235 @@
package device; package support;
import de.fh_zwickau.informatik.sensor.IZWayApiCallbacks; import de.fh_zwickau.informatik.sensor.IZWayApiCallbacks;
import de.fh_zwickau.informatik.sensor.model.devicehistory.DeviceHistory; import de.fh_zwickau.informatik.sensor.model.devicehistory.DeviceHistory;
import de.fh_zwickau.informatik.sensor.model.devicehistory.DeviceHistoryList; import de.fh_zwickau.informatik.sensor.model.devicehistory.DeviceHistoryList;
import de.fh_zwickau.informatik.sensor.model.devices.Device; import de.fh_zwickau.informatik.sensor.model.devices.Device;
import de.fh_zwickau.informatik.sensor.model.devices.DeviceList; import de.fh_zwickau.informatik.sensor.model.devices.DeviceList;
import de.fh_zwickau.informatik.sensor.model.instances.Instance; import de.fh_zwickau.informatik.sensor.model.instances.Instance;
import de.fh_zwickau.informatik.sensor.model.instances.InstanceList; import de.fh_zwickau.informatik.sensor.model.instances.InstanceList;
import de.fh_zwickau.informatik.sensor.model.locations.Location; import de.fh_zwickau.informatik.sensor.model.locations.Location;
import de.fh_zwickau.informatik.sensor.model.locations.LocationList; import de.fh_zwickau.informatik.sensor.model.locations.LocationList;
import de.fh_zwickau.informatik.sensor.model.modules.ModuleList; import de.fh_zwickau.informatik.sensor.model.modules.ModuleList;
import de.fh_zwickau.informatik.sensor.model.namespaces.NamespaceList; import de.fh_zwickau.informatik.sensor.model.namespaces.NamespaceList;
import de.fh_zwickau.informatik.sensor.model.notifications.Notification; import de.fh_zwickau.informatik.sensor.model.notifications.Notification;
import de.fh_zwickau.informatik.sensor.model.notifications.NotificationList; import de.fh_zwickau.informatik.sensor.model.notifications.NotificationList;
import de.fh_zwickau.informatik.sensor.model.profiles.Profile; import de.fh_zwickau.informatik.sensor.model.profiles.Profile;
import de.fh_zwickau.informatik.sensor.model.profiles.ProfileList; import de.fh_zwickau.informatik.sensor.model.profiles.ProfileList;
import de.fh_zwickau.informatik.sensor.model.zwaveapi.controller.ZWaveController; import de.fh_zwickau.informatik.sensor.model.zwaveapi.controller.ZWaveController;
import de.fh_zwickau.informatik.sensor.model.zwaveapi.devices.ZWaveDevice; import de.fh_zwickau.informatik.sensor.model.zwaveapi.devices.ZWaveDevice;
import org.slf4j.Logger; import device.Sensor;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Mandatory callback class for the Z-Way Library in use.
* Really trivial implementation: log all the responses at debug level. /**
* * Mandatory callback class for the Z-Way Library in use.
* @author <a href="mailto:luigi.derussis@uniupo.it">Luigi De Russis</a> * Really trivial implementation: log all the responses at debug level.
* @version 1.0 (24/05/2017) *
* @see <a href="https://github.com/pathec/ZWay-library-for-Java">Z-Way Library on GitHub</a> for documentation about the used library * @author <a href="mailto:luigi.derussis@uniupo.it">Luigi De Russis</a>
*/ * @version 1.0 (24/05/2017)
public class ZWaySimpleCallback implements IZWayApiCallbacks { * @see <a href="https://github.com/pathec/ZWay-library-for-Java">Z-Way Library on GitHub</a> for documentation about the used library
*/
@Override public class ZWaySimpleCallback implements IZWayApiCallbacks {
public void getStatusResponse(String s) {
this.logMessage("Status response: " + s); @Override
} public void getStatusResponse(String s) {
this.logMessage("Status response: " + s);
@Override }
public void getRestartResponse(Boolean aBoolean) {
this.logMessage("Restart response: " + aBoolean); @Override
} public void getRestartResponse(Boolean aBoolean) {
this.logMessage("Restart response: " + aBoolean);
@Override }
public void getLoginResponse(String s) {
this.logMessage("Login response: " + s); @Override
} public void getLoginResponse(String s) {
this.logMessage("Login response: " + s);
@Override }
public void getNamespacesResponse(NamespaceList namespaceList) {
this.logMessage("Namespaces are: " + namespaceList); @Override
} public void getNamespacesResponse(NamespaceList namespaceList) {
this.logMessage("Namespaces are: " + namespaceList);
@Override }
public void getModulesResponse(ModuleList moduleList) {
this.logMessage("Modules are: " + moduleList); @Override
} public void getModulesResponse(ModuleList moduleList) {
this.logMessage("Modules are: " + moduleList);
@Override }
public void getInstancesResponse(InstanceList instanceList) {
this.logMessage("Instances are: " + instanceList); @Override
} public void getInstancesResponse(InstanceList instanceList) {
this.logMessage("Instances are: " + instanceList);
@Override }
public void postInstanceResponse(Instance instance) {
this.logMessage("Received a POST for the instance: " + instance); @Override
} public void postInstanceResponse(Instance instance) {
this.logMessage("Received a POST for the instance: " + instance);
@Override }
public void getInstanceResponse(Instance instance) {
this.logMessage("The instance is: " + instance); @Override
} public void getInstanceResponse(Instance instance) {
this.logMessage("The instance is: " + instance);
@Override }
public void putInstanceResponse(Instance instance) {
this.logMessage("Received a PUT for the instance: " + instance); @Override
} public void putInstanceResponse(Instance instance) {
this.logMessage("Received a PUT for the instance: " + instance);
@Override }
public void deleteInstanceResponse(boolean b) {
this.logMessage("The instance has been deleted? " + String.valueOf(b)); @Override
} public void deleteInstanceResponse(boolean b) {
this.logMessage("The instance has been deleted? " + String.valueOf(b));
@Override }
public void getDevicesResponse(DeviceList deviceList) {
this.logMessage("Devices are: " + deviceList); @Override
} public void getDevicesResponse(DeviceList deviceList) {
this.logMessage("Devices are: " + deviceList);
@Override }
public void putDeviceResponse(Device device) {
this.logMessage("Received a PUT for device: " + device); @Override
} public void putDeviceResponse(Device device) {
this.logMessage("Received a PUT for device: " + device);
@Override }
public void getDeviceResponse(Device device) {
this.logMessage("The device is: " + device); @Override
} public void getDeviceResponse(Device device) {
this.logMessage("The device is: " + device);
@Override }
public void getDeviceCommandResponse(String s) {
this.logMessage("The device command is: " + s); @Override
} public void getDeviceCommandResponse(String s) {
this.logMessage("The device command is: " + s);
@Override }
public void getLocationsResponse(LocationList locationList) {
this.logMessage("Locations are: " + locationList); @Override
} public void getLocationsResponse(LocationList locationList) {
this.logMessage("Locations are: " + locationList);
@Override }
public void postLocationResponse(Location location) {
this.logMessage("Received a POST for location: " + location); @Override
} public void postLocationResponse(Location location) {
this.logMessage("Received a POST for location: " + location);
@Override }
public void getLocationResponse(Location location) {
this.logMessage("The location is: " + location); @Override
} public void getLocationResponse(Location location) {
this.logMessage("The location is: " + location);
@Override }
public void putLocationResponse(Location location) {
this.logMessage("Received a PUT for location: " + location); @Override
} public void putLocationResponse(Location location) {
this.logMessage("Received a PUT for location: " + location);
@Override }
public void deleteLocationResponse(boolean b) {
this.logMessage("Location has been deleted? " + b); @Override
} public void deleteLocationResponse(boolean b) {
this.logMessage("Location has been deleted? " + b);
@Override }
public void getProfilesResponse(ProfileList profileList) {
this.logMessage("Profiles are: " + profileList); @Override
} public void getProfilesResponse(ProfileList profileList) {
this.logMessage("Profiles are: " + profileList);
@Override }
public void postProfileResponse(Profile profile) {
this.logMessage("Received a POST for profile: " + profile); @Override
} public void postProfileResponse(Profile profile) {
this.logMessage("Received a POST for profile: " + profile);
@Override }
public void getProfileResponse(Profile profile) {
this.logMessage("The profile is: " + profile); @Override
} public void getProfileResponse(Profile profile) {
this.logMessage("The profile is: " + profile);
@Override }
public void putProfileResponse(Profile profile) {
this.logMessage("Received a PUT for profile: " + profile); @Override
} public void putProfileResponse(Profile profile) {
this.logMessage("Received a PUT for profile: " + profile);
@Override }
public void deleteProfileResponse(boolean b) {
this.logMessage("Profile has been deleted? " + b); @Override
} public void deleteProfileResponse(boolean b) {
this.logMessage("Profile has been deleted? " + b);
@Override }
public void getNotificationsResponse(NotificationList notificationList) {
this.logMessage("Notifications are: " + notificationList); @Override
} public void getNotificationsResponse(NotificationList notificationList) {
this.logMessage("Notifications are: " + notificationList);
@Override }
public void getNotificationResponse(Notification notification) {
this.logMessage("The notification is: " + notification); @Override
} public void getNotificationResponse(Notification notification) {
this.logMessage("The notification is: " + notification);
@Override }
public void putNotificationResponse(Notification notification) {
this.logMessage("Received a PUT for notification: " + notification); @Override
} public void putNotificationResponse(Notification notification) {
this.logMessage("Received a PUT for notification: " + notification);
@Override }
public void getDeviceHistoriesResponse(DeviceHistoryList deviceHistoryList) {
this.logMessage("Device histories are: " + deviceHistoryList); @Override
} public void getDeviceHistoriesResponse(DeviceHistoryList deviceHistoryList) {
this.logMessage("Device histories are: " + deviceHistoryList);
@Override }
public void getDeviceHistoryResponse(DeviceHistory deviceHistory) {
this.logMessage("The device history is: " + deviceHistory); @Override
} public void getDeviceHistoryResponse(DeviceHistory deviceHistory) {
this.logMessage("The device history is: " + deviceHistory);
@Override }
public void getZWaveDeviceResponse(ZWaveDevice zWaveDevice) {
this.logMessage("The Z-Wave device is: " + zWaveDevice); @Override
} public void getZWaveDeviceResponse(ZWaveDevice zWaveDevice) {
this.logMessage("The Z-Wave device is: " + zWaveDevice);
@Override }
public void getZWaveControllerResponse(ZWaveController zWaveController) {
this.logMessage("The Z-Wave controller is: " + zWaveController); @Override
} public void getZWaveControllerResponse(ZWaveController zWaveController) {
this.logMessage("The Z-Wave controller is: " + zWaveController);
@Override }
public void apiError(String s, boolean b) {
this.logError("API Error: " + s); @Override
} public void apiError(String s, boolean b) {
this.logError("API Error: " + s);
@Override }
public void httpStatusError(int i, String s, boolean b) {
this.logError("HTTP Status Error: " + i + s); @Override
} public void httpStatusError(int i, String s, boolean b) {
this.logError("HTTP Status Error: " + i + s);
@Override }
public void authenticationError() {
this.logError("Authentication Error"); @Override
} public void authenticationError() {
this.logError("Authentication Error");
@Override }
public void responseFormatError(String s, boolean b) {
this.logError("Wrong format: " + s); @Override
} public void responseFormatError(String s, boolean b) {
this.logError("Wrong format: " + s);
@Override }
public void message(int i, String s) {
this.logMessage("You've got a message: " + i + " " + s); @Override
} public void message(int i, String s) {
this.logMessage("You've got a message: " + i + " " + s);
/** }
* Utility method to print the log messages of this class.
* /**
* @param message the {@link String} to print * Utility method to print the log messages of this class.
*/ *
private void logMessage(String message) { * @param message the {@link String} to print
Logger logger = LoggerFactory.getLogger(Sensor.class); */
logger.debug(message); private void logMessage(String message) {
} Logger logger = LoggerFactory.getLogger(Sensor.class);
logger.debug(message);
/** }
* Utility method to print the error messages of this class.
* /**
* @param error the {@link String} to print * Utility method to print the error messages of this class.
*/ *
private void logError(String error) { * @param error the {@link String} to print
Logger logger = LoggerFactory.getLogger(Sensor.class); */
logger.error(error); private void logError(String error) {
} Logger logger = LoggerFactory.getLogger(Sensor.class);
logger.error(error);
}
} }

View File

@@ -1,13 +1,17 @@
import manage.FITBITData.FitBit; import org.junit.Test;
public class Main { import device.FitBit;
public static void main(String[] args) throws Exception {
FitBit fitBit = new FitBit(); public class TestFitbit {
fitBit.getHoursSleep();
@Test
System.out.println("Today's average heart-rate: "+fitBit.getHeartRate()); public void test01() throws Exception {
System.out.println("Today's hours of sleep: "+fitBit.getHoursSleep()); FitBit fitBit = new FitBit();
System.out.println("Today's steps: "+fitBit.getSteps()); fitBit.getHoursSleep();
System.out.println("Fine.");
} System.out.println("Today's average heart-rate: "+fitBit.getHeartRate());
} System.out.println("Today's hours of sleep: "+fitBit.getHoursSleep());
System.out.println("Today's steps: "+fitBit.getSteps());
System.out.println("Fine.");
}
}