Skip to content

Commit

Permalink
latest pushes
Browse files Browse the repository at this point in the history
  • Loading branch information
RonaldsonBellande committed Jul 10, 2024
1 parent a78f3c0 commit 9a2e5db
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,45 @@
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.bellande_api.bellande_cpu_usage.bellande_cpu_usage_base_service;
import com.bellande_api.bellande_cpu_usage.bellande_cpu_usage_base_api;
import com.google.firebase.crashlytics.buildtools.reloc.com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.reflect.*;

import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.Map;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class bellande_cpu_usage_base_activity extends AppCompatActivity {
private bellande_cpu_usage_base_service bellande_cpu_usage_base_service;
protected bellande_cpu_usage_base_service cpuUsageService;
protected String connectivityPasscode;

public bellande_cpu_usage_base_activity(Context context) {
Map<String, Object> config = loadConfigFromFile(context);
String apiUrl = (String) config.get("url");
String endpointPath = (String) ((Map<String, Object>) config.get("endpoint_path")).get("base");
Map<String, String> endpointPaths = (Map<String, String>) config.get("endpoint_path");
String inputEndpoint = endpointPaths.get("input_data");
String outputEndpoint = endpointPaths.get("output_data");
String apiAccessKey = (String) config.get("Bellande_Framework_Access_Key");
this.connectivityPasscode = (String) config.get("connectivity_passcode");

bellande_cpu_usage_base_api bellande_cpu_usage_base_api = new Retrofit.Builder()
.baseUrl(apiUrl + endpointPath)
bellande_cpu_usage_base_api cpuUsageApi = new Retrofit.Builder()
.baseUrl(apiUrl)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(bellande_cpu_usage_base_api.class);

bellande_cpu_usage_base_service = new bellande_cpu_usage_base_service(apiUrl, endpointPath, apiAccessKey, bellande_cpu_usage_base_api);
cpuUsageService = new bellande_cpu_usage_base_service(apiUrl, inputEndpoint, outputEndpoint, apiAccessKey, cpuUsageApi);
}

@SuppressLint("LongLogTag")
private Map<String, Object> loadConfigFromFile(Context context) {
try {
InputStream inputStream = context.getAssets().open("configs.json");
InputStreamReader reader = new InputStreamReader(inputStream);
Type type = new TypeToken<Map<String, Object>>() {}.getType();
Type type = new TypeToken<Map<String, Object>>(){}.getType();
return new Gson().fromJson(reader, type);
} catch (IOException e) {
Log.e("bellande_cpu_usage_base_activity", "Error reading config file: " + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,41 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
**/

*/
package com.bellande_api.bellande_cpu_usage;

import com.google.gson.annotations.SerializedName;

import okhttp3.MultipartBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Response;

public interface bellande_cpu_usage_base_api {
@Multipart
@POST("base")
Call<BellandeResponse> getBellandeResponse(@Body RequestBody requestBody);
@POST
Call<BellandeResponse> getBellandeResponse(@Url String url, @Body RequestBody body, @Header("Bellande-Framework-Access-Key") String apiKey);

class RequestBody {
private final String inputText;
@POST
Call<BellandeResponse> sendBellandeResponse(@Url String url, @Body RequestBody body, @Header("Bellande-Framework-Access-Key") String apiKey);

public RequestBody(String inputText) { this.inputText = inputText; }
class RequestBody {
private final String input;
private final String connectivityPasscode;

public String getString() { return inputText; }
public RequestBody(String input, String connectivityPasscode) {
this.input = input;
this.connectivityPasscode = connectivityPasscode;
}
}

class BellandeResponse {
@SerializedName("response")
public String response;
private String cpuUsage;
private String status;

public String getCpuUsage() {
return cpuUsage;
}

public String getStatus() {
return status;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,45 @@
import retrofit2.converter.gson.GsonConverterFactory;

public class bellande_cpu_usage_base_service {
private final bellande_cpu_usage_base_api bellande_cpu_usage_base_api;
private final bellande_cpu_usage_base_api cpuUsageApi;
private final String apiAccessKey;
private final String inputEndpoint;
private final String outputEndpoint;

public bellande_cpu_usage_base_service(String apiUrl, String endpointPath, String apiAccessKey, bellande_cpu_usage_base_api bellande_cpu_usage_base_api) {
this.bellande_cpu_usage_base_api = bellande_cpu_usage_base_api;
public bellande_cpu_usage_base_service(String apiUrl, String inputEndpoint, String outputEndpoint, String apiAccessKey, bellande_cpu_usage_base_api cpuUsageApi) {
this.cpuUsageApi = cpuUsageApi;
this.apiAccessKey = apiAccessKey;
this.inputEndpoint = inputEndpoint;
this.outputEndpoint = outputEndpoint;
}

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(apiUrl + endpointPath)
.addConverterFactory(GsonConverterFactory.create())
.build();
public String getCpuUsage(String connectivityPasscode) {
bellande_cpu_usage_base_api.RequestBody apiRequestBody = new bellande_cpu_usage_base_api.RequestBody("get_cpu_usage", connectivityPasscode);

retrofit.create(bellande_cpu_usage_base_api.class);
try {
Response<bellande_cpu_usage_base_api.BellandeResponse> response = cpuUsageApi.getBellandeResponse(inputEndpoint, apiRequestBody, apiAccessKey).execute();
if (response.isSuccessful() && response.body() != null) {
return response.body().getCpuUsage();
} else {
throw new RuntimeException("Error getting CPU usage: " + response.code() + " - " + response.message());
}
} catch (IOException e) {
throw new RuntimeException("Error getting CPU usage: " + e.getMessage());
}
}

public bellande_cpu_usage_base_api.BellandeResponse getPrediction(String inputText) {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), inputText);
bellande_cpu_usage_base_api.RequestBody apiRequestBody = new bellande_cpu_usage_base_api.RequestBody(
inputText
);
public String sendCpuUsage(String cpuUsage, String connectivityPasscode) {
bellande_cpu_usage_base_api.RequestBody apiRequestBody = new bellande_cpu_usage_base_api.RequestBody(cpuUsage, connectivityPasscode);

try {
Response<bellande_cpu_usage_base_api.BellandeResponse> response = bellande_cpu_usage_base_api.getBellandeResponse(apiRequestBody).execute();
Response<bellande_cpu_usage_base_api.BellandeResponse> response = cpuUsageApi.sendBellandeResponse(outputEndpoint, apiRequestBody, apiAccessKey).execute();
if (response.isSuccessful() && response.body() != null) {
return response.body();
return response.body().getStatus();
} else {
throw new RuntimeException("Error getting prediction: " + response.code() + " - " + response.message());
throw new RuntimeException("Error sending CPU usage: " + response.code() + " - " + response.message());
}
} catch (IOException e) {
throw new RuntimeException("Error getting prediction: " + e.getMessage());
throw new RuntimeException("Error sending CPU usage: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (C) 2024 Bellande Application UI UX Research Innovation Center, Ronaldson Bellande
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
**/

package com.bellande_api.bellande_storage_usage;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.bellande_api.bellande_storage_usage", appContext.getPackageName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Copyright (C) 2024 Bellande Application UI UX Research Innovation Center, Ronaldson Bellande
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
**/
package com.bellande_api.bellande_storage_usage;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.bellande_api.bellande_storage_usage.bellande_storage_usage_usage_service;
import com.bellande_api.bellande_storage_usage.bellande_storage_usage_usage_api;
import com.google.firebase.crashlytics.buildtools.reloc.com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.reflect.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.Map;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class bellande_storage_usage_usage_activity extends AppCompatActivity {
private bellande_storage_usage_usage_service bellande_storage_usage_usage_service;

public bellande_storage_usage_usage_activity(Context context) {
Map<String, Object> config = loadConfigFromFile(context);
String apiUrl = (String) config.get("url");
String endpointPath = (String) ((Map<String, Object>) config.get("endpoint_path")).get("prediction");
String apiAccessKey = (String) config.get("Bellande_Framework_Access_Key");

bellande_storage_usage_usage_api bellande_storage_usage_usage_api = new Retrofit.Builder()
.baseUrl(apiUrl + endpointPath)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(bellande_storage_usage_usage_api.class);

bellande_storage_usage_usage_service = new bellande_storage_usage_usage_service(apiUrl, endpointPath, apiAccessKey, bellande_storage_usage_usage_api);
}

@SuppressLint("LongLogTag")
private Map<String, Object> loadConfigFromFile(Context context) {
try {
InputStream inputStream = context.getAssets().open("configs.json");
InputStreamReader reader = new InputStreamReader(inputStream);
Type type = new TypeToken<Map<String, Object>>() {}.getType();
return new Gson().fromJson(reader, type);
} catch (IOException e) {
Log.e("bellande_network_usage_activity", "Error reading config file: " + e.getMessage());
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright (C) 2024 Bellande Application UI UX Research Innovation Center, Ronaldson Bellande
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
**/

package com.bellande_api.bellande_storage_usage;

import com.google.gson.annotations.SerializedName;

import okhttp3.MultipartBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;

public interface bellande_storage_usage_usage_api {
@Multipart
@POST("prediction")
Call<BellandeResponse> getPrediction(@Part MultipartBody.Part pointCloud);

class RequestBody {
private final MultipartBody.Part pointCloud;

public RequestBody(MultipartBody.Part pointCloud) {
this.pointCloud = pointCloud;
}

public MultipartBody.Part getPointCloud() {
return pointCloud;
}
}

class BellandeResponse {
@SerializedName("prediction")
public String prediction;

@SerializedName("confidence")
public double confidence;
}
}
Loading

0 comments on commit 9a2e5db

Please sign in to comment.