added confighandler. UNTESTEDdiff --cached!
This commit is contained in:
parent
7936281924
commit
ec6e684cd9
@ -81,9 +81,12 @@ public class FileOutputVolleyRequest extends Request<JSONObject> {
|
||||
String requestBody = stringBuilder.toString();
|
||||
return requestBody.getBytes("utf-8");
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
Log.e("FileOutputVolleyRequest",
|
||||
"could not find the output file: " + e.getMessage());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Log.e("FileOutputVolleyRequest",
|
||||
"error while reading the output file: "
|
||||
+ e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -0,0 +1,256 @@
|
||||
package com.example.aped.utils;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* handles the read and write operations for the configuration file.
|
||||
*/
|
||||
public class ConfigurationHandler implements IFAVORITES {
|
||||
|
||||
/**
|
||||
* Subclass for storing the configuration data in an OOP conform manner.
|
||||
*/
|
||||
private class ConfigData {
|
||||
/** The address of the raspberry. **/
|
||||
private String address;
|
||||
/** The port of the raspberry. **/
|
||||
private int port;
|
||||
/** The names of the favorite devices. **/
|
||||
private List<String> favoriteNames;
|
||||
|
||||
/**
|
||||
* Getter for the address variable.
|
||||
* @return the address of the raspberry
|
||||
*/
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for the address variable.
|
||||
* @param pAddress the new address for the raspberry
|
||||
*/
|
||||
public void setAddress(final String pAddress) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the port variable.
|
||||
* @return the port of the raspberry
|
||||
*/
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for the port variable.
|
||||
* @param pPort the new port for the raspberry
|
||||
*/
|
||||
public void setPort(final int pPort) {
|
||||
this.port = pPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for the favoriteNames variable.
|
||||
* @return the favorite device names
|
||||
*/
|
||||
public List<String> getFavoriteNames() {
|
||||
return favoriteNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for the favoriteNames variable.
|
||||
* @param pFavoriteNames the new favorite device names
|
||||
*/
|
||||
public void setFavoriteNames(final List<String> pFavoriteNames) {
|
||||
this.favoriteNames = pFavoriteNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to convert an object of {@link ConfigData} class to String.
|
||||
* @return a String in JSON format
|
||||
*/
|
||||
@NonNull
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{\n"
|
||||
+ "\"address\":\"" + address + "\"\n"
|
||||
+ "\"port\":\"" + port + "\"\n"
|
||||
+ "\"favorites\":" + favoriteNames.toString() + "\"\n"
|
||||
+ "}";
|
||||
}
|
||||
}
|
||||
|
||||
/** The current configurations. **/
|
||||
private ConfigData configurations;
|
||||
/** The file path of the configuration file. **/
|
||||
private final String filePath;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param configFilePath the file path of the configuration file
|
||||
*/
|
||||
public ConfigurationHandler(final String configFilePath) {
|
||||
this.filePath = configFilePath;
|
||||
readJsonFile();
|
||||
}
|
||||
|
||||
//region public methods
|
||||
|
||||
/**
|
||||
* Method to get the current address of the raspberry from the configs.
|
||||
* @return the address of the raspberry
|
||||
*/
|
||||
public String getAddress() {
|
||||
return configurations.getAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to set the current address of the raspberry in the configs.
|
||||
* @param address the new address to be set
|
||||
* @return the adress after setting
|
||||
*/
|
||||
public String setAddress(final String address) {
|
||||
configurations.setAddress(address);
|
||||
writeJsonFile();
|
||||
return getAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the current port of the raspberry from the configs.
|
||||
* @return the address of the raspberry
|
||||
*/
|
||||
public int getPort() {
|
||||
return configurations.getPort();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to set the current port of the raspberry in the configs.
|
||||
* @param port the port to set
|
||||
* @return the port after setting
|
||||
*/
|
||||
public int setPort(final int port) {
|
||||
configurations.setPort(port);
|
||||
writeJsonFile();
|
||||
return getPort();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the favorite device names from the configs.
|
||||
* @return the favorite device names as a {@link List<String>}
|
||||
*/
|
||||
@Override
|
||||
public List<String> getFavorites() {
|
||||
return configurations.getFavoriteNames();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to add a name to the list of favorite device names in the config.
|
||||
* @param deviceName the name of the device to be added
|
||||
* @return the deviceName that has been added
|
||||
*/
|
||||
@Override
|
||||
public String addFavorite(final String deviceName) {
|
||||
List<String> favoriteNames = configurations.getFavoriteNames();
|
||||
if (!favoriteNames.contains(deviceName)) {
|
||||
favoriteNames.add(deviceName);
|
||||
configurations.setFavoriteNames(favoriteNames);
|
||||
writeJsonFile();
|
||||
}
|
||||
return deviceName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to remove a name from the list of favorites in the configs.
|
||||
* @param deviceName the name of the device to be removed
|
||||
* @return the name of the device that was removed
|
||||
*/
|
||||
@Override
|
||||
public String removeFavorite(final String deviceName) {
|
||||
List<String> favoriteNames = configurations.getFavoriteNames();
|
||||
if (favoriteNames.contains(deviceName)) {
|
||||
favoriteNames.remove(deviceName);
|
||||
configurations.setFavoriteNames(favoriteNames);
|
||||
writeJsonFile();
|
||||
}
|
||||
return deviceName;
|
||||
}
|
||||
|
||||
//endregion
|
||||
|
||||
//region private methods
|
||||
/**
|
||||
* Method to read the content of the configuration file.
|
||||
*/
|
||||
private void readJsonFile() {
|
||||
try {
|
||||
String jsonData = "";
|
||||
BufferedReader bufferedReader = new BufferedReader(
|
||||
new FileReader(filePath));
|
||||
String line;
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
jsonData += line + "\n";
|
||||
}
|
||||
bufferedReader.close();
|
||||
JSONObject jsonObject = new JSONObject(jsonData);
|
||||
configurations.setAddress(jsonObject.getString("address"));
|
||||
configurations.setPort(jsonObject.getInt("port"));
|
||||
List<String> favoriteNames = new ArrayList<>();
|
||||
JSONArray favorites = jsonObject.getJSONArray("favorites");
|
||||
for (int i = 0; i < favorites.length(); i++) {
|
||||
favoriteNames.add(favorites.getString(i));
|
||||
}
|
||||
configurations.setFavoriteNames(favoriteNames);
|
||||
} catch (FileNotFoundException e) {
|
||||
Log.e("Configuration Handler",
|
||||
"could not find the configuration file during READ: "
|
||||
+ e.getMessage());
|
||||
} catch (IOException e) {
|
||||
Log.e("Configuration Handler",
|
||||
"error while reading the configuration file: "
|
||||
+ e.getMessage());
|
||||
} catch (JSONException e) {
|
||||
Log.e("Configuration Handler",
|
||||
"error while parsing the data from the file "
|
||||
+ "to the configurations " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to write the current configurations to the configuration file.
|
||||
*/
|
||||
private void writeJsonFile() {
|
||||
try {
|
||||
String jsonString = configurations.toString();
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(
|
||||
filePath, false
|
||||
);
|
||||
fileOutputStream.write(jsonString.getBytes());
|
||||
fileOutputStream.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
Log.e("Configuration Handler",
|
||||
"could not find the configuration file during WRITE: "
|
||||
+ e.getMessage());
|
||||
} catch (IOException e) {
|
||||
Log.e("ConfigurationHandler",
|
||||
"error while writing to the configuration file: "
|
||||
+ e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
//endregion
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user