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 8724ea1 commit b0c73b6
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 51 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
@@ -1,4 +1,4 @@
package com.bellande_api.bellande_mobile_android_api;
package com.bellande_api.bellande_storage_usage;

import org.junit.Test;

Expand Down

0 comments on commit b0c73b6

Please sign in to comment.