Starting model manager for sdAlgNn

This commit is contained in:
Graham Jones
2023-08-21 14:32:54 +01:00
parent bc455a2f7a
commit 08a7aecb4d
2 changed files with 114 additions and 0 deletions

View File

@@ -0,0 +1,112 @@
package uk.org.openseizuredetector;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
// This class manages machine learning models by downloading them from a remote server when necessary.
public class MlModelManager {
protected Context mContext;
protected OsdUtil mUtil;
private String TAG = "MlModelManager";
public boolean mServerConnectionOk = false;
public boolean mModelReady = false;
private final String mUrlBase = "https://openseizuredetector.org.uk/static/MLmodels/";
private final String mModelIndexFname = "MLmodels.json";
RequestQueue mQueue;
public interface JSONObjectCallback {
public void accept(JSONObject retValObj);
}
public MlModelManager(Context context) {
Log.i(TAG,"MlModelManager Constructor");
mContext = context;
mUtil = new OsdUtil(mContext, new Handler());
mQueue = Volley.newRequestQueue(mContext);
}
public void close() {
Log.i(TAG, "close()");
mQueue.stop();
}
/**
* Retrieve the file containing the list of available ML models from the server.
* Calls the specified callback function, passing a JSONObject as a parameter when the data has been received and parsed.
*
* @return true if request sent successfully or else false.
*/
public boolean getMlModelIndex(JSONObjectCallback callback) {
Log.v(TAG, "getMlModelIndex()");
String urlStr = mUrlBase + mModelIndexFname;
Log.v(TAG, "urlStr=" + urlStr);
StringRequest req = new StringRequest(Request.Method.GET, urlStr,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.v(TAG, "getMlModelIndex.onResponse(): Response is: " + response);
mServerConnectionOk = true;
try {
JSONObject retObj = new JSONObject(response);
callback.accept(retObj);
} catch (JSONException e) {
Log.e(TAG, "getMlModelIndex.onRespons(): Error: " + e.getMessage() + "," + e.toString());
callback.accept(null);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mServerConnectionOk = false;
if (error != null) {
Log.e(TAG, "getMlModelIndex.onErrorResponse(): " + error.toString() + ", message:" + error.getMessage());
} else {
Log.e(TAG, "getMlModelIndex.onErrorResponse() - returned null response");
}
callback.accept(null);
}
}) {
// Note, this is overriding part of StringRequest, not one of the sub-classes above!
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json; charset=UTF-8");
//params.put("Authorization", "Token " + getStoredToken());
return params;
}
};
mQueue.add(req);
return (true);
}
}

View File

@@ -32,6 +32,7 @@ public class SdAlgNn {
private String mUrlBase = "https://osdApi.ddns.net";
private InterpreterApi interpreter;
private Context mContext;
private MlModelManager mMm;
RequestQueue mQueue;
private double mSdThresh; // Acceleration Standard Deviation Threshold required to activate analysis (%)
@@ -42,6 +43,7 @@ public class SdAlgNn {
public SdAlgNn(Context context) {
Log.d(TAG, "SdAlgNn Constructor");
mContext = context;
mMm = new MlModelManager(mContext);
SharedPreferences SP = PreferenceManager
.getDefaultSharedPreferences(mContext);