Skip to content
Snippets Groups Projects

Dev

Merged René Schöne requested to merge dev into master
244 files
+ 32633
2157
Compare changes
  • Side-by-side
  • Inline

Files

package de.tudresden.inf.st.eraser.benchmark;
import com.opencsv.CSVReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.File;
import java.io.FileReader;
import java.util.Arrays;
public class Benchmark {
//read every 5 s from csv
//activity CSV
/**
* Col 1: smartphone acceleration x
* Col 2: smartphone acceleration y
* Col 3: smartphone acceleration z
* Col 4: smartphone rotation x
* Col 5: smartphone rotation y
* Col 6: smartphone rotation z
* Col 7: watch acceleration x
* Col 8: watch acceleration y
* Col 9: watch acceleration z
* Col 10: watch rotation x
* Col 11: watch rotation y
* Col 12: watch rotation z/*/
//preference CSV
/**
* Col 1: Activity
* Col 2: watch brightness range "bright, medium, dimmer, dark"*/
private String a_csv_file_path;
private String p_csv_file_path;
private static final Logger logger = LogManager.getLogger(Benchmark.class);
private static final String ERASER_ITEM_URI = "http://localhost:4567/model/items/";
//TODO ITEM_NAME HAS TO BE DISCUSSED
private static final String[] ACTIVITY_ITEM_NAME = {
"m_accel_x", "m_accel_y", "m_accel_z", "m_rotation_x",
"m_rotation_y", "m_rotation_z", "w_accel_x", "w_accel_y",
"w_accel_z", "w_rotation_x", "w_rotation_y", "w_rotation_z"};
private static final String[] PREFERENCE_ITEM_NAME = {
"w_brightness"
};
private static boolean flag2 = true;
//csv_type is activity or preference
Benchmark(String a_csv_file_path, String p_csv_file_path) {
this.a_csv_file_path = a_csv_file_path;
this.p_csv_file_path = p_csv_file_path;
}
void start(){
String PREFERENCE_URL = "http://localhost:4567/model/items/iris1_item/state";
String ACTIVITY_URL = "http://localhost:4567/activity/current";
int TIME_PERIOD = 5000;
File a_file;
File p_file;
FileReader a_file_reader;
FileReader p_file_reader;
CSVReader a_csv_reader;
CSVReader p_csv_reader;
String[] a_next_record;
String[] p_next_record;
boolean flag1;
a_file=new File(a_csv_file_path);
p_file= new File(p_csv_file_path);
try {
a_file_reader =new FileReader(a_file);
p_file_reader=new FileReader(p_file);
a_csv_reader = new CSVReader(a_file_reader);
p_csv_reader = new CSVReader(p_file_reader);
while ((((a_next_record = a_csv_reader.readNext())!= null) && flag2)){
try{Thread.sleep(TIME_PERIOD);}catch (InterruptedException e){e.printStackTrace();}
String[] values = Arrays.copyOf(a_next_record,12);
setNewValue(values, ACTIVITY_ITEM_NAME,"activity");
HttpResponse response= Request.Get(ACTIVITY_URL).execute().returnResponse();
String status = response.getStatusLine().toString();
if(status.contains("200")){
flag1 = true;
logger.info("activity should be (read direct from CSV): " + a_next_record[12]);
logger.info(EntityUtils.toString(response.getEntity()));
logger.info("get activity from web server: response 200 ok");
}else{
flag1 = false;
flag2 = false;
logger.info("can not get the activity from the web server");
}
while((((p_next_record = p_csv_reader.readNext()) != null) && flag1)) {
try{Thread.sleep(TIME_PERIOD);}catch (InterruptedException e){e.printStackTrace();}
String[] values1 = Arrays.copyOf(p_next_record,2);
setNewValue(values1, PREFERENCE_ITEM_NAME,"preference");
HttpResponse response1= Request.Get(PREFERENCE_URL).execute().returnResponse();
String status1=response1.getStatusLine().toString();
if(status1.contains("200")){
flag2 = true;
logger.info("get the iris1_item preference from web server: response 200 ok, value is: "+EntityUtils.toString(response1.getEntity()));
}else {flag2 = false;
logger.info("can not get the iris1_item from the web server");}
break;
}
}
}
catch (Exception e){
e.printStackTrace();
}
}
private void setNewValue(String[] values, String[] name, String file_typ){
if(file_typ.equals("activity"))
{
int i = 0;
for(String value : values){
String uri = ERASER_ITEM_URI + name[i] + "/state";
try {
HttpResponse httpResponse = Request.Put(uri)
.bodyString(value, ContentType.TEXT_PLAIN)
.execute().returnResponse();
String status=httpResponse.getStatusLine().toString();
if(status.contains("200")){
logger.info("put activity input name: "+name[i]+", value: "+value+"to web server: response 200 ok");
}else{
logger.info("can not put activity inputs to rest server");
}
}catch (Exception e){
e.printStackTrace();
}
i+=1;
}
}else{
String uri= ERASER_ITEM_URI + "w_brightness" +"/state";
try {
HttpResponse httpResponse = Request.Put(uri)
.bodyString(values[1], ContentType.TEXT_PLAIN)
.execute().returnResponse();
String put_response=httpResponse.getStatusLine().toString();
if (put_response.contains("200")){logger.info("put w_brightness to web server: response 200 ok");}else{
logger.info("can not put w_brightness to rest server");
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}
\ No newline at end of file
Loading