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;

View File

@@ -1,4 +1,4 @@
package manage.FITBITData;
package device.FITBITData;
import com.fasterxml.jackson.annotation.JsonProperty;
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.JsonProperty;

View File

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

View File

@@ -1,11 +1,14 @@
package manage.FITBITData;
package device;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
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 {

View File

@@ -3,7 +3,7 @@ package device;
import java.util.Map;
import java.util.Set;
import manage.Rest;
import support.Rest;
public class Hue {
//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.model.devices.Device;
import de.fh_zwickau.informatik.sensor.model.devices.DeviceList;
import support.ZWaySimpleCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -21,15 +23,12 @@ public class Sensor {
public IZWayApi zwayApi;
private DeviceList allZWaveDevices;
private DeviceList devices;
private Integer nodeId;
public Sensor() {
this(null);
}
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)
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.util.Arrays;

View File

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

View File

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

View File

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

View File

@@ -1,233 +1,235 @@
package device;
import de.fh_zwickau.informatik.sensor.IZWayApiCallbacks;
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.devices.Device;
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.InstanceList;
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.modules.ModuleList;
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.NotificationList;
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.zwaveapi.controller.ZWaveController;
import de.fh_zwickau.informatik.sensor.model.zwaveapi.devices.ZWaveDevice;
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.
*
* @author <a href="mailto:luigi.derussis@uniupo.it">Luigi De Russis</a>
* @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
*/
public class ZWaySimpleCallback implements IZWayApiCallbacks {
@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 getLoginResponse(String s) {
this.logMessage("Login response: " + s);
}
@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 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 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 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 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 getDeviceCommandResponse(String s) {
this.logMessage("The device command is: " + s);
}
@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 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 deleteLocationResponse(boolean b) {
this.logMessage("Location has been deleted? " + b);
}
@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 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 deleteProfileResponse(boolean b) {
this.logMessage("Profile has been deleted? " + b);
}
@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 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 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 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 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 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);
}
/**
* Utility method to print the log messages of this class.
*
* @param message the {@link String} to print
*/
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
*/
private void logError(String error) {
Logger logger = LoggerFactory.getLogger(Sensor.class);
logger.error(error);
}
package support;
import de.fh_zwickau.informatik.sensor.IZWayApiCallbacks;
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.devices.Device;
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.InstanceList;
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.modules.ModuleList;
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.NotificationList;
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.zwaveapi.controller.ZWaveController;
import de.fh_zwickau.informatik.sensor.model.zwaveapi.devices.ZWaveDevice;
import device.Sensor;
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.
*
* @author <a href="mailto:luigi.derussis@uniupo.it">Luigi De Russis</a>
* @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
*/
public class ZWaySimpleCallback implements IZWayApiCallbacks {
@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 getLoginResponse(String s) {
this.logMessage("Login response: " + s);
}
@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 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 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 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 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 getDeviceCommandResponse(String s) {
this.logMessage("The device command is: " + s);
}
@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 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 deleteLocationResponse(boolean b) {
this.logMessage("Location has been deleted? " + b);
}
@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 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 deleteProfileResponse(boolean b) {
this.logMessage("Profile has been deleted? " + b);
}
@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 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 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 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 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 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);
}
/**
* Utility method to print the log messages of this class.
*
* @param message the {@link String} to print
*/
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
*/
private void logError(String error) {
Logger logger = LoggerFactory.getLogger(Sensor.class);
logger.error(error);
}
}

View File

@@ -1,13 +1,17 @@
import manage.FITBITData.FitBit;
public class Main {
public static void main(String[] args) throws Exception {
FitBit fitBit = new FitBit();
fitBit.getHoursSleep();
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.");
}
}
import org.junit.Test;
import device.FitBit;
public class TestFitbit {
@Test
public void test01() throws Exception {
FitBit fitBit = new FitBit();
fitBit.getHoursSleep();
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.");
}
}