Make use sharedprefrences

This commit is contained in:
Astatin3
2024-09-20 15:59:35 -06:00
parent 9f5ebd4215
commit 07c6e9a2aa
26 changed files with 315 additions and 580 deletions
@@ -1,6 +1,5 @@
package com.ridgebotics.ridgescout.utility;
import com.ridgebotics.ridgescout.SettingsVersionStack.latestSettings;
import com.ridgebotics.ridgescout.scoutingData.fields;
import com.ridgebotics.ridgescout.scoutingData.transfer.transferType;
import com.ridgebotics.ridgescout.types.frcEvent;
@@ -15,7 +14,7 @@ public class DataManager {
}
public static String getevcode() {
return latestSettings.settings.get_evcode();
return settingsManager.getEVCode();
}
public static inputType[][] match_values;
@@ -5,8 +5,6 @@ import android.content.Context;
import com.ridgebotics.ridgescout.types.frcEvent;
import com.ridgebotics.ridgescout.types.frcTeam;
import com.ridgebotics.ridgescout.SettingsVersionStack.latestSettings;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
@@ -268,8 +266,8 @@ public final class fileEditor {
public static boolean setEvent(frcEvent event){
final String filename = (event.eventCode + ".eventdata");
if(latestSettings.settings.get_evcode().equals("unset")){
latestSettings.settings.set_evcode(event.eventCode);
if(settingsManager.getEVCode().equals("unset")){
settingsManager.setEVCode(event.eventCode);
}
return writeFile(filename, event.encode());
@@ -3,16 +3,12 @@ package com.ridgebotics.ridgescout.utility.ollama;
import androidx.annotation.NonNull;
import com.ridgebotics.ridgescout.utility.AlertManager;
import com.ridgebotics.ridgescout.utility.ollama.types.ChatMessage;
import com.ridgebotics.ridgescout.utility.ollama.types.ChatRequest;
import com.ridgebotics.ridgescout.utility.ollama.types.ChatResponse;
import com.ridgebotics.ridgescout.utility.ollama.types.Messages;
//import com.ridgebotics.ridgescout.utility.ollama.types.Messages;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
@@ -23,7 +19,7 @@ import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.OkHttpClient;
public class OllamaTest {
public class OllamaClient {
private static final String MODEL_KEY = "llama3";
private static final String OLLAMA_URL_KEY = "http://199.204.135.71:11434";
@@ -48,8 +44,6 @@ public class OllamaTest {
}
public static void run(String prompt, ollamaListener listener){
final Messages llamaMessages = new Messages();
llamaMessages.messages.add(new Messages.Message("user", "Test!"));
// ChatRequest chatRequest = new ChatRequest(MODEL_KEY, llamaMessages.messages,true);
RequestBody body = RequestBody.create(promptToJson(prompt), MediaType.parse("application/json"));
@@ -0,0 +1,135 @@
package com.ridgebotics.ridgescout.utility.ollama;
import static com.ridgebotics.ridgescout.utility.DataManager.evcode;
import static com.ridgebotics.ridgescout.utility.DataManager.event;
import static com.ridgebotics.ridgescout.utility.DataManager.match_latest_values;
import static com.ridgebotics.ridgescout.utility.DataManager.pit_latest_values;
import com.ridgebotics.ridgescout.scoutingData.ScoutingDataWriter;
import com.ridgebotics.ridgescout.types.data.dataType;
import com.ridgebotics.ridgescout.types.frcMatch;
import com.ridgebotics.ridgescout.types.input.dropdownType;
import com.ridgebotics.ridgescout.types.input.inputType;
import com.ridgebotics.ridgescout.types.input.sliderType;
import com.ridgebotics.ridgescout.utility.DataManager;
import com.ridgebotics.ridgescout.utility.fileEditor;
public class PromptCreator {
private static String fieldSummary(inputType field){
String summary = field.name + ": ";
switch (field.getInputType()){
case DROPDOWN:
summary += "A the index of a dropdown with the possible options: [" +String.join(", ", ((dropdownType) field).text_options) + "]";
break;
case SLIDER:
sliderType slider = (sliderType) field;
summary += "A slider with the range ["+slider.min+","+slider.max+"]";
break;
case TALLY:
summary += "A tally counter";
break;
case NOTES_INPUT:
summary += "Raw text input";
break;
}
return summary;
}
public static String genMatchPrompt(int matchIndex){
String prompt = "Below is a list of data collected from an FRC match. Generate a qualitative and concise summary of the teams listed in the data collected.\n\n";
frcMatch curmatch = event.matches.get(matchIndex);
prompt += "## Pit scouting\n";
prompt += "This is a list of the different fields that are present in the pit scouting data:\n";
prompt += "1) Team number\n";
for(int i = 0; i < pit_latest_values.length; i++){
prompt += (i+2) + ") " + fieldSummary(pit_latest_values[i]) + "\n";
}
prompt += ("\nData:\n");
for(int a = 0; a < 6; a++){
int teamNum = 0;
if(a < 3)
teamNum = curmatch.redAlliance[a];
else
teamNum = curmatch.blueAlliance[a-3];
prompt += teamNum + ",";
String filename = evcode+"-"+teamNum+".pitscoutdata";
if(!fileEditor.fileExist(filename)){
prompt += ("null,".repeat(pit_latest_values.length));
}else{
ScoutingDataWriter.ParsedScoutingDataResult psdr = ScoutingDataWriter.load(filename, DataManager.pit_values, DataManager.pit_transferValues);
dataType[] types = psdr.data.array;
for(int i = 0; i < types.length; i++) {
prompt += (types[i].get() + ",");
}
}
prompt += "\n";
}
prompt += "\n## Match scouting\n";
prompt += "This is a list of the different fields that are present in the match scouting data:\n";
prompt += "1) Match number\n";
for(int i = 0; i < match_latest_values.length; i++){
prompt += (i+2) + ") " + fieldSummary(match_latest_values[i]) + "\n";
}
prompt += ("\nData:\n");
for(int a = 0; a < 6; a++){
int teamNum = 0;
if(a < 3)
teamNum = curmatch.redAlliance[a];
else
teamNum = curmatch.blueAlliance[a-3];
prompt += "Team " + teamNum + " Match scout data:\n";
frcMatch[] matchNums = event.getTeamMatches(teamNum);
for(int b = 0; b < matchNums.length; b++) {
frcMatch match = matchNums[b];
String alliance = "";
int alliancePos = 0;
for(int c = 0; c < 6; c++) {
if(c<3){
if(match.redAlliance[c] != teamNum) continue;
alliance = "red";
alliancePos = c+1;
break;
}else{
if(match.blueAlliance[c-3] != teamNum) continue;
alliance = "blue";
alliancePos = c-2;
break;
}
}
String filename = evcode + "-" + match.matchIndex + "-" + alliance + "-" + alliancePos + "-" + teamNum + ".matchscoutdata";
if (!fileEditor.fileExist(filename)) continue;
ScoutingDataWriter.ParsedScoutingDataResult psdr = ScoutingDataWriter.load(filename, DataManager.match_values, DataManager.match_transferValues);
dataType[] types = psdr.data.array;
prompt += match.matchIndex + ",";
for (int i = 0; i < types.length; i++) {
prompt += (types[i].get() + ",");
}
prompt += "\n";
}
}
return prompt;
}
}
@@ -1,26 +0,0 @@
package com.ridgebotics.ridgescout.utility.ollama.types;
// Credit to https://github.com/DataDropp/OllamaDroid
public class ChatMessage {
private final int profileImage;
private final String profileName;
private final String messageContent;
public ChatMessage(int profileImage, String profileName, String messageContent) {
this.profileImage = profileImage;
this.profileName = profileName;
this.messageContent = messageContent;
}
public int getProfileImage() {
return profileImage;
}
public String getProfileName() {
return profileName;
}
public String getMessageContent() {
return messageContent;
}
}
@@ -1,23 +0,0 @@
package com.ridgebotics.ridgescout.utility.ollama.types;
// Credit to https://github.com/DataDropp/OllamaDroid
import java.util.ArrayList;
public class ChatRequest {
public String model;
public ArrayList<Messages.Message> messages;
public boolean stream;
public ChatRequest(String model, ArrayList<Messages.Message> message, boolean stream){
this.model = model;
this.messages = message;
this.stream = stream;
}
public String toJson(){
return "{\n" +
" \"model\": \"llama3\",\n" +
" \"prompt\": \"Why is the sky blue?\"\n" +
"}";
}
}
@@ -1,17 +0,0 @@
package com.ridgebotics.ridgescout.utility.ollama.types;
// Credit to https://github.com/DataDropp/OllamaDroid
import java.util.Date;
public class ChatResponse {
public String model;
public Date created_at;
public Messages.Message message;
public boolean done;
public long total_duration;
public int load_duration;
public int prompt_eval_count;
public int prompt_eval_duration;
public int eval_count;
public long eval_duration;
}
@@ -1,17 +0,0 @@
package com.ridgebotics.ridgescout.utility.ollama.types;
// Credit to https://github.com/DataDropp/OllamaDroid
import java.util.ArrayList;
public class Messages {
public ArrayList<Message> messages = new ArrayList<>();
public static class Message {
public String role;
public String content;
public Message(String role, String string) {
this.role = role;
this.content = string;
}
}
}
@@ -1,24 +0,0 @@
package com.ridgebotics.ridgescout.utility.ollama.types;
// Credit to https://github.com/DataDropp/OllamaDroid
import java.util.ArrayList;
public class OllamaModels {
public ArrayList<OllamaModel> models;
public class OllamaModel {
public String name;
public String modified_at;
public Object size;
public String digest;
public Details details;
private class Details {
public String format;
public String family;
public Object families;
public String parameter_size;
public String quantization_level;
}
}
}
@@ -0,0 +1,69 @@
package com.ridgebotics.ridgescout.utility;
import android.content.SharedPreferences;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class settingsManager {
public static SharedPreferences prefs;
public static SharedPreferences.Editor editor;
public static final String UnameKey = "username";
public static final String SelEVCodeKey = "selected_event_code";
public static final String WifiModeKey = "wifi_mode";
public static final String TeamNumKey = "team_num";
public static final String MatchNumKey = "match_num";
public static final String AllyPosKey = "alliance_pos";
public static final String DataModeKey = "data_view_mode";
public static final String BtUUIDKey = "bt_uuid";
public static Map defaults = getDefaults();
private static Map getDefaults(){
Map<String, Object> hm = new HashMap<>();
hm.put(UnameKey, "Username");
hm.put(SelEVCodeKey, "unset");
hm.put(WifiModeKey, false);
hm.put(TeamNumKey, 4388);
hm.put(MatchNumKey, 0);
hm.put(AllyPosKey, "red-1");
hm.put(DataModeKey, 0);
hm.put(BtUUIDKey, UUID.randomUUID().toString());
return hm;
}
private static SharedPreferences.Editor getEditor(){
if(editor == null) editor = prefs.edit();
return editor;
}
// IDK why I decided to format these functions like this. It looks cool though.
public static String getUsername(){return prefs.getString( UnameKey, (String) defaults.get(UnameKey));}
public static void setUsername(String str){ getEditor().putString( UnameKey,str).apply();}
public static String getEVCode(){return prefs.getString( SelEVCodeKey, (String) defaults.get(SelEVCodeKey));}
public static void setEVCode(String str){ getEditor().putString( SelEVCodeKey,str).apply();}
public static boolean getWifiMode(){return prefs.getBoolean( WifiModeKey, (boolean) defaults.get(WifiModeKey));}
public static void setWifiMode(boolean bool){getEditor().putBoolean( WifiModeKey,bool).apply();}
public static int getTeamNum(){return prefs.getInt( TeamNumKey, (int) defaults.get(TeamNumKey));}
public static void setTeamNum(int num){ getEditor().putInt( TeamNumKey,num).apply();}
public static int getMatchNum(){return prefs.getInt( MatchNumKey, (int) defaults.get(MatchNumKey));}
public static void setMatchNum(int num){ getEditor().putInt( MatchNumKey,num).apply();}
public static String getAllyPos(){return prefs.getString( AllyPosKey, (String) defaults.get(AllyPosKey));}
public static void setAllyPos(String str){ getEditor().putString( AllyPosKey,str).apply();}
public static int getDataMode(){return prefs.getInt( DataModeKey, (int) defaults.get(DataModeKey));}
public static void setDataMode(int num){ getEditor().putInt( DataModeKey,num).apply();}
public static String getBtUUID(){return prefs.getString( BtUUIDKey, (String) defaults.get(BtUUIDKey));}
public static void setBtUUID(String str){ getEditor().putString( BtUUIDKey,str).apply();}
}