-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
701 additions
and
282 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
...e/fraunhofer/fokus/OpenMobileNetworkToolkit/InfluxDB2x/Worker/InfluxDB2xUploadWorker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2023 Peter Hasse <[email protected]> | ||
* SPDX-FileCopyrightText: 2023 Johann Hackler <[email protected]> | ||
* SPDX-FileCopyrightText: 2023 Fraunhofer FOKUS | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause-Clear | ||
*/ | ||
|
||
package de.fraunhofer.fokus.OpenMobileNetworkToolkit.InfluxDB2x.Worker; | ||
|
||
import android.content.Context; | ||
import android.util.Log; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.work.Data; | ||
import androidx.work.Worker; | ||
import androidx.work.WorkerParameters; | ||
|
||
import com.google.gson.Gson; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.InfluxDB2x.InfluxdbConnection; | ||
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.InfluxDB2x.InfluxdbConnections; | ||
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Inputs.Inputs; | ||
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Inputs.Iperf3Input; | ||
|
||
public class InfluxDB2xUploadWorker extends Worker { | ||
private static final String TAG = "InfDB2xUploadWorker"; | ||
InfluxdbConnection influx; | ||
private Inputs input; | ||
public static final String UPLOAD = "influxdb2x_upload"; | ||
|
||
public InfluxDB2xUploadWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { | ||
super(context, workerParams); | ||
Gson gson = new Gson(); | ||
String inputString = getInputData().getString(Inputs.INPUT); | ||
input = gson.fromJson(inputString, Inputs.class); | ||
} | ||
private void setup(){ | ||
influx = InfluxdbConnections.getRicInstance(getApplicationContext()); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Result doWork() { | ||
setup(); | ||
Data output = new Data.Builder().putBoolean(UPLOAD, false).build(); | ||
if(influx == null){ | ||
return Result.failure(output); | ||
} | ||
if(influx.getWriteApi() == null){ | ||
influx.open_write_api(); | ||
if(influx.getWriteApi() == null) | ||
return Result.failure(output); | ||
} | ||
|
||
if(!influx.ping()){ | ||
return Result.failure(output); | ||
} | ||
BufferedReader br; | ||
try { | ||
br = new BufferedReader(new FileReader(input.getLineProtocolFile())); | ||
} catch (FileNotFoundException | NullPointerException e) { | ||
Log.d(TAG,e.toString()); | ||
return Result.failure(output); | ||
} | ||
List<String> points = br.lines().collect(Collectors.toList()); | ||
try { | ||
Log.d(TAG, String.format("doWork: uploading %s", input.getLineProtocolFile())); | ||
influx.writeRecords(points); | ||
} catch (IOException e) { | ||
Log.d(TAG, String.format("doWork: upload of %s failed!", input.getLineProtocolFile())); | ||
return Result.failure(output); | ||
} | ||
|
||
|
||
influx.flush(); | ||
|
||
output = new Data.Builder().putBoolean(UPLOAD, true).build(); | ||
return Result.success(output); | ||
} | ||
} |
150 changes: 150 additions & 0 deletions
150
app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Inputs/Inputs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package de.fraunhofer.fokus.OpenMobileNetworkToolkit.Inputs; | ||
|
||
import android.os.Environment; | ||
import android.os.Parcel; | ||
import android.os.Parcelable; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.work.Data; | ||
import androidx.work.OneTimeWorkRequest; | ||
|
||
import java.sql.Timestamp; | ||
|
||
public abstract class Inputs implements Parcelable { | ||
|
||
public static final String rootPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath(); | ||
public static final String INPUT = "input"; | ||
public static final String TESTUUID = "testUUID"; | ||
public static final String SEQUENCEUUID = "sequenceUUID"; | ||
public static final String MEASUREMENTUUID = "measurementUUID"; | ||
public static final String CAMPAIGNUUID = "campaignUUID"; | ||
private String rawFile; | ||
private String testUUID; | ||
private String logFileName; | ||
private String measurementName; | ||
private String lineProtocolFile; | ||
private Timestamp timestamp; | ||
private String campaignUUID; | ||
private String sequenceUUID; | ||
private String measurementUUID; | ||
|
||
public String getRawFile() { | ||
return rawFile; | ||
} | ||
|
||
public void setRawFile(String rawFile) { | ||
this.rawFile = rawFile; | ||
} | ||
|
||
public String getTestUUID() { | ||
return testUUID; | ||
} | ||
|
||
public void setTestUUID(String testUUID) { | ||
this.testUUID = testUUID; | ||
} | ||
|
||
public String getLogFileName() { | ||
return logFileName; | ||
} | ||
|
||
public void setLogFileName(String logFileName) { | ||
this.logFileName = logFileName; | ||
} | ||
|
||
public String getMeasurementName() { | ||
return measurementName; | ||
} | ||
|
||
public void setMeasurementName(String measurementName) { | ||
this.measurementName = measurementName; | ||
} | ||
|
||
public String getLineProtocolFile() { | ||
return lineProtocolFile; | ||
} | ||
|
||
public void setLineProtocolFile(String lineProtocolFile) { | ||
this.lineProtocolFile = lineProtocolFile; | ||
} | ||
|
||
public Timestamp getTimestamp() { | ||
return timestamp; | ||
} | ||
|
||
public void setTimestamp(Timestamp timestamp) { | ||
this.timestamp = timestamp; | ||
} | ||
|
||
public String getCampaignUUID() { | ||
return campaignUUID; | ||
} | ||
|
||
public String getSequenceUUID() { | ||
return sequenceUUID; | ||
} | ||
|
||
public String getMeasurementUUID() { | ||
return measurementUUID; | ||
} | ||
protected Inputs(Parcel in) { | ||
rawFile = in.readString(); | ||
logFileName = in.readString(); | ||
measurementName = in.readString(); | ||
lineProtocolFile = in.readString(); | ||
timestamp = (Timestamp) in.readSerializable(); | ||
campaignUUID = in.readString(); | ||
sequenceUUID = in.readString(); | ||
measurementUUID = in.readString(); | ||
testUUID = in.readString(); | ||
} | ||
public Inputs(String rawFile, String logFileName, String measurementName, String lineProtocolFile, String campaignUUID, String sequenceUUID, String measurementUUID, String testUUID) { | ||
|
||
this.rawFile = rawFile; | ||
this.logFileName = logFileName; | ||
this.measurementName = measurementName; | ||
this.lineProtocolFile = lineProtocolFile; | ||
this.timestamp = new Timestamp(System.currentTimeMillis()); | ||
|
||
this.campaignUUID = campaignUUID; | ||
this.sequenceUUID = sequenceUUID; | ||
this.measurementUUID = measurementUUID; | ||
this.testUUID = testUUID; | ||
} | ||
|
||
public Inputs(String campaignUUID, String sequenceUUID, String measurementUUID, String testUUID) { | ||
this.rawFile = ""; | ||
this.logFileName = ""; | ||
this.measurementName = ""; | ||
this.lineProtocolFile = ""; | ||
this.timestamp = new Timestamp(System.currentTimeMillis()); | ||
|
||
this.campaignUUID = campaignUUID; | ||
this.sequenceUUID = sequenceUUID; | ||
this.measurementUUID = measurementUUID; | ||
this.testUUID = testUUID; | ||
} | ||
|
||
@Override | ||
public int describeContents() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public void writeToParcel(@NonNull Parcel dest, int flags) { | ||
dest.writeString(rawFile); | ||
dest.writeString(logFileName); | ||
dest.writeString(measurementName); | ||
dest.writeString(lineProtocolFile); | ||
dest.writeSerializable(timestamp); | ||
dest.writeString(campaignUUID); | ||
dest.writeString(sequenceUUID); | ||
dest.writeString(measurementUUID); | ||
dest.writeString(testUUID); | ||
} | ||
public abstract Data.Builder getInputAsDataBuilder(int i, String packageName); | ||
public abstract OneTimeWorkRequest getWorkRequestExecutor(int i, String packageName); | ||
public abstract OneTimeWorkRequest getWorkRequestLineProtocol(int i, String packageName); | ||
public abstract OneTimeWorkRequest getWorkRequestUpload(int i, String packageName); | ||
|
||
} |
143 changes: 143 additions & 0 deletions
143
app/src/main/java/de/fraunhofer/fokus/OpenMobileNetworkToolkit/Inputs/Iperf3Input.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package de.fraunhofer.fokus.OpenMobileNetworkToolkit.Inputs; | ||
|
||
import static androidx.work.multiprocess.RemoteListenableWorker.ARGUMENT_CLASS_NAME; | ||
import static androidx.work.multiprocess.RemoteListenableWorker.ARGUMENT_PACKAGE_NAME; | ||
|
||
import android.content.ComponentName; | ||
import android.os.Parcel; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.work.Data; | ||
import androidx.work.OneTimeWorkRequest; | ||
|
||
|
||
import com.google.gson.GsonBuilder; | ||
|
||
|
||
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Parameter.Iperf3Parameter; | ||
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Iperf3.Service.Iperf3ServiceWorkerFour; | ||
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Iperf3.Service.Iperf3ServiceWorkerThree; | ||
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Iperf3.Service.Iperf3ServiceWorkerTwo; | ||
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Iperf3.Worker.Iperf3ExecutorWorker; | ||
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Iperf3.Service.Iperf3ServiceWorkerOne; | ||
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Iperf3.Worker.Iperf3ToLineProtocolWorker; | ||
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Iperf3.Worker.Iperf3UploadWorker; | ||
|
||
|
||
public class Iperf3Input extends Inputs { | ||
public static final String rawDirPath = rootPath+"/omnt/iperf3/raw/"; | ||
public static final String lineProtocolDirPath = rootPath+"/omnt/iperf3/lineprotocol/"; | ||
private static final String TAG = "Iperf3Input"; | ||
public static final String IPERF3UUID = "iPerf3UUID"; | ||
|
||
private Iperf3Parameter iperf3Parameter; | ||
protected Iperf3Input(Parcel in) { | ||
super(in); | ||
iperf3Parameter = in.readParcelable(Iperf3Parameter.class.getClassLoader()); | ||
} | ||
|
||
public static final Creator<Iperf3Input> CREATOR = new Creator<>() { | ||
@Override | ||
public Iperf3Input createFromParcel(Parcel in) { | ||
return new Iperf3Input(in); | ||
} | ||
|
||
@Override | ||
public Iperf3Input[] newArray(int size) { | ||
return new Iperf3Input[size]; | ||
} | ||
}; | ||
|
||
@Override | ||
public int describeContents() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public void writeToParcel(@NonNull Parcel parcel, int i) { | ||
super.writeToParcel(parcel, i); | ||
parcel.writeParcelable(iperf3Parameter, i); | ||
} | ||
|
||
public Iperf3Input(Iperf3Parameter iperf3Parameter, | ||
String testUUID, | ||
String sequenceUUID, | ||
String measurementUUID, | ||
String campaignUUID) { | ||
super(testUUID, sequenceUUID, measurementUUID, campaignUUID); | ||
super.setRawFile(rawDirPath +testUUID+".json"); | ||
super.setLineProtocolFile(lineProtocolDirPath+testUUID+".txt"); | ||
this.iperf3Parameter = iperf3Parameter; | ||
} | ||
|
||
public Iperf3Parameter getIperf3Parameter() { | ||
return iperf3Parameter; | ||
} | ||
|
||
public Data.Builder getInputAsDataBuilder(int i, String packageName) { | ||
Data.Builder data = new Data.Builder(); | ||
String serviceName = ""; | ||
switch (i){ | ||
case 0: | ||
serviceName = Iperf3ServiceWorkerOne.class.getName(); | ||
break; | ||
case 1: | ||
serviceName = Iperf3ServiceWorkerTwo.class.getName(); | ||
break; | ||
case 2: | ||
serviceName = Iperf3ServiceWorkerThree.class.getName(); | ||
break; | ||
case 3: | ||
serviceName = Iperf3ServiceWorkerFour.class.getName(); | ||
break; | ||
default: | ||
break; | ||
|
||
} | ||
ComponentName componentName = new ComponentName(packageName, serviceName); | ||
|
||
data.putString(ARGUMENT_PACKAGE_NAME, componentName.getPackageName()); | ||
data.putString(ARGUMENT_CLASS_NAME, componentName.getClassName()); | ||
data.putInt("notificationNumber", i); | ||
data.putString(Inputs.INPUT, new GsonBuilder().create().toJson(this, Iperf3Input.class)); | ||
return data; | ||
} | ||
|
||
@Override | ||
public OneTimeWorkRequest getWorkRequestExecutor(int i, String packageName) { | ||
OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(Iperf3ExecutorWorker.class) | ||
.addTag(super.getTestUUID()) | ||
.addTag(super.getMeasurementUUID()) | ||
.addTag(super.getSequenceUUID()) | ||
.addTag(super.getCampaignUUID()) | ||
.addTag(iperf3Parameter.getiPerf3UUID()) | ||
.setInputData(getInputAsDataBuilder(i, packageName).build()) | ||
.build(); | ||
return workRequest; | ||
} | ||
@Override | ||
public OneTimeWorkRequest getWorkRequestLineProtocol(int i, String packageName) { | ||
OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(Iperf3ToLineProtocolWorker.class) | ||
.addTag(super.getTestUUID()) | ||
.addTag(super.getMeasurementUUID()) | ||
.addTag(super.getSequenceUUID()) | ||
.addTag(super.getCampaignUUID()) | ||
.addTag(iperf3Parameter.getiPerf3UUID()) | ||
.setInputData(getInputAsDataBuilder(i, packageName).build()) | ||
.build(); | ||
return workRequest; | ||
} | ||
|
||
@Override | ||
public OneTimeWorkRequest getWorkRequestUpload(int i, String packageName) { | ||
OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(Iperf3UploadWorker.class) | ||
.addTag(super.getTestUUID()) | ||
.addTag(super.getMeasurementUUID()) | ||
.addTag(super.getSequenceUUID()) | ||
.addTag(super.getCampaignUUID()) | ||
.addTag(iperf3Parameter.getiPerf3UUID()) | ||
.setInputData(getInputAsDataBuilder(i, packageName).build()) | ||
.build(); | ||
return workRequest; | ||
} | ||
} |
Oops, something went wrong.