This commit is contained in:
m_broelemann 2021-01-05 08:14:18 +01:00
commit 2a50b6f7c3
7 changed files with 20 additions and 94 deletions

@ -1 +1 @@
Subproject commit 594fc1ea61264673706a2e26120e3b4b95af4f2f
Subproject commit 2fddb6ae3ff3247f684c89c34d0d859b7b2076bc

View File

@ -1,7 +1,5 @@
package com.example.aped.communication;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
@ -21,6 +19,7 @@ import com.example.aped.MainActivity;
import com.example.aped.R;
import com.example.aped.utils.ExternalStorageHandler;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
@ -112,7 +111,16 @@ public class Communicator {
Request.Method.POST,
url,
xmlFile,
response -> System.out.println(response.toString()),
response -> {
try {
JSONObject jsonObject = new JSONObject(response.toString());
if (!jsonObject.getString("error").equals("")) {
makeToast(jsonObject.getString("error"));
}
} catch (JSONException e) {
Log.e("Communicator","JSON error on response during UPLOAD: " + e.getMessage());
}
},
error -> handleError(error, "UPLOAD XML")
);
requestQueue.add(request);

View File

@ -247,10 +247,14 @@ public class ConfigurationHandler implements IFAVORITES {
@NonNull
@Override
public String toString() {
List<String> favoriteNamesString = new ArrayList<>();
for (String favoriteName : favoriteNames){
favoriteNamesString.add("\"" + favoriteName + "\"");
}
return "{\n"
+ "\"address\":\"" + address + "\",\n"
+ "\"port\":\"" + port + "\",\n"
+ "\"favorites\":" + favoriteNames.toString() + "\n"
+ "\t\"address\":\"" + address + "\",\n"
+ "\t\"port\":\"" + port + "\",\n"
+ "\t\"favorites\":" + favoriteNamesString.toString() + "\n"
+ "}";
}
}

View File

@ -11,13 +11,6 @@ public interface IXML {
*/
List<String> getDeviceNames();
/**
* reads the value info of a given device from the XML.
* @param deviceName the name of the relevant device
* @return the value info as a Dictionary
*/
Dictionary<String, Object> getValueInfo(String deviceName);
/**
* reads the port information of a given device from the XML.
* @param deviceName the name of the relevant device

View File

@ -17,16 +17,6 @@ public class TestXML implements IXML {
return returnList;
}
@Override
public Dictionary<String, Object> getValueInfo(String deviceName) {
Dictionary<String, Object> returnDict = new Hashtable<>();
returnDict.put("type", "boolean");
returnDict.put("unit", "");
returnDict.put("Offset", 1.0);
returnDict.put("Factor", 1.0);
return returnDict;
}
@Override
public Dictionary<String, Object> getPort(String deviceName) {
Dictionary<String, Object> returnDict = new Hashtable<>();

View File

@ -70,52 +70,6 @@ public class XMLHandler implements IXML {
return returnList;
}
/**
* reads the value info from the XML file.
* @param deviceName the name of the relevant device
* @return the value info as a dictionary {'unit','type','Offset','Factor'}
*/
@Override
public Dictionary<String, Object> getValueInfo(final String deviceName) {
Dictionary<String, Object> returnDictionary = new Hashtable<>();
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
try {
XPathExpression xPathExpression = xPath.compile(
"//Device[@name='" + deviceName + "']/ValueInfo");
Element result = (Element) xPathExpression.evaluate(
root, XPathConstants.NODE);
returnDictionary.put("type", result.getAttribute("type"));
returnDictionary.put("unit", result.getAttribute("unit"));
NodeList childNodes = result.getChildNodes();
float offset = 0.0f;
float factor = 1.0f;
for (int i = 0; i < childNodes.getLength(); i++) {
switch (childNodes.item(i).getNodeName()) {
case "Offset":
offset = Float.parseFloat(childNodes.item(i)
.getTextContent());
break;
case "Factor":
factor = Float.parseFloat(childNodes.item(i)
.getTextContent());
break;
default:
break;
}
}
returnDictionary.put("offset", offset);
returnDictionary.put("factor", factor);
} catch (XPathExpressionException e) {
Log.e(
"XMLHandler",
"the XPath for getting the value info has errors:"
+ e.getMessage()
);
}
return returnDictionary;
}
/**
* reads the port information from the XML file.
* @param deviceName the name of the relevant device
@ -132,6 +86,7 @@ public class XMLHandler implements IXML {
Element result = (Element) xPathExpression.evaluate(
root, XPathConstants.NODE);
returnDictionary.put("protocol", result.getAttribute("protocol"));
returnDictionary.put("frequency", result.getAttribute("frequency"));
NodeList childNodes = result.getChildNodes();
List<String> pins = new ArrayList<>();
for (int i = 0; i < childNodes.getLength(); i++) {

View File

@ -43,30 +43,6 @@ public class XMLHandlerUnitTest {
}
}
@Test
public void TestInput_SimpleValueInfo(){
try{
XMLHandler xmlHandler = new XMLHandler(xmlFile);
Dictionary<String, Object> valueInfo = xmlHandler.getValueInfo("example");
assertEquals("{factor=1.0, type=boolean, unit=, offset=0.0}",valueInfo.toString());
}catch(IOException | ParserConfigurationException | SAXException e){
System.out.println("XMLHandler failed");
assert(false);
}
}
@Test
public void TestInput_ComplexValueInfo(){
try{
XMLHandler xmlHandler = new XMLHandler(xmlFile);
Dictionary<String, Object> valueInfo = xmlHandler.getValueInfo("sensorarray");
assertEquals("{factor=2.5, type=int, unit=°C, offset=1.2}",valueInfo.toString());
}catch(IOException | ParserConfigurationException | SAXException e){
System.out.println("XMLHandler failed");
assert(false);
}
}
@Test
public void TestInput_SimplePort(){
try{