Skip to content

Commit

Permalink
saving rocks
Browse files Browse the repository at this point in the history
  • Loading branch information
adenjonah committed May 22, 2024
1 parent 36eabc3 commit a81e8f4
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
7 changes: 7 additions & 0 deletions server/initdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
MESSAGES_FILE = './server/json_databases/messages.json'
GOLDEN_ER_FILE = './server/json_databases/golden_er_procedure.json'

SAVED_ROCKS_FILE = './server/json_databases/saved_rocks.json'

# Default JSON data
DEFAULT_CONFIG_DATA = {
"TSS_IP": "localhost:14141",
Expand Down Expand Up @@ -369,6 +371,10 @@

}

DEFAULT_SAVED_ROCKS = {
"saved rocks":[]
}

def initialize_file(file_path, default_data, overwrite=True):
"""Initialize a JSON file with default data, optionally overwriting it if it exists."""
if overwrite or not os.path.exists(file_path):
Expand All @@ -386,6 +392,7 @@ def initialize_database_files():
initialize_file(GOLDEN_ER_FILE, DEFAULT_GOLDEN_ER)
initialize_file(USER_PINS_FILE, DEFAULT_USER_PINS)
initialize_file(NAV_PATH_FILE, DEFAULT_NAV_PATH)
initialize_file(SAVED_ROCKS_FILE, DEFAULT_SAVED_ROCKS)

if __name__ == "__main__":
initialize_database_files()
2 changes: 1 addition & 1 deletion server/json_databases/DCUUIA.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"timestamp": "2024-05-22T02:59:43.095802", "dcu": {"eva1": {"batt": false, "oxy": false, "comm": false, "fan": false, "pump": false, "co2": false}, "eva2": {"batt": false, "oxy": false, "comm": false, "fan": false, "pump": false, "co2": false}}, "uia": {"eva1_power": false, "eva1_oxy": false, "eva1_water_supply": false, "eva1_water_waste": true, "eva2_power": false, "eva2_oxy": false, "eva2_water_supply": true, "eva2_water_waste": false, "oxy_vent": false, "depress": true}}
{"timestamp": "2024-05-22T03:13:22.774582", "dcu": {"eva1": {"batt": false, "oxy": false, "comm": false, "fan": false, "pump": false, "co2": false}, "eva2": {"batt": false, "oxy": false, "comm": false, "fan": false, "pump": false, "co2": false}}, "uia": {"eva1_power": false, "eva1_oxy": false, "eva1_water_supply": false, "eva1_water_waste": true, "eva2_power": false, "eva2_oxy": false, "eva2_water_supply": true, "eva2_water_waste": false, "oxy_vent": false, "depress": true}}
36 changes: 36 additions & 0 deletions server/json_databases/saved_rocks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"saved rocks": [
{
"name": "default_rock",
"id": 0,
"data": {
"SiO2": 1,
"TiO2": 1,
"Al2O3": 1,
"FeO": 1,
"MnO": 1,
"MgO": 1,
"CaO": 1,
"K2O": 1,
"P2O3": 1,
"other": 91
}
},
{
"name": "olivine_basalt_2",
"id": 6,
"data": {
"SiO2": 40.36,
"TiO2": 0.99,
"Al2O3": 2.32,
"FeO": 25.71,
"MnO": 0.58,
"MgO": 12.81,
"CaO": 5.95,
"K2O": 0.2,
"P2O3": 0.28,
"other": 10.8
}
}
]
}
36 changes: 36 additions & 0 deletions server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
ALERTS_FILE = './server/json_databases/alerts.json'
MESSAGES_FILE = './server/json_databases/messages.json'
GOLDEN_ER_FILE = './server/json_databases/golden_er_procedure.json'
SAVED_ROCKS_FILE = './server/json_databases/saved_rocks.json'

# Initialize variables for configuration data
tss_ip = 'localhost:14141'
Expand Down Expand Up @@ -592,8 +593,43 @@ async def drop_pin_here(eva_num: int):
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))

@app.post("/save_spec")
async def save_spec():
async with httpx.AsyncClient() as client:
response = await client.get('http://localhost:8000/spec_scans')

if response.status_code == 200:
data = response.json()
eva1_data = data['spec']['eva1']

# Load existing data
with open(SAVED_ROCKS_FILE, 'r') as file:
saved_rocks_data = json.load(file)

# Append new data to the "saved rocks" array
saved_rocks_data["saved rocks"].append(eva1_data)

# Save updated data
with open(SAVED_ROCKS_FILE, 'w') as file:
json.dump(saved_rocks_data, file, indent=4)

return {"message": "Data saved successfully"}
else:
raise HTTPException(status_code=response.status_code, detail="Failed to retrieve data")


@app.get("/get_spec")
async def get_spec():
try:
if os.path.exists(SAVED_ROCKS_FILE):
with open(SAVED_ROCKS_FILE, 'r') as file:
data = json.load(file)
else:
raise FileNotFoundError(f"{SAVED_ROCKS_FILE} not found")
return data
except FileNotFoundError as e:
raise HTTPException(status_code=404, detail=str(e))

if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='0.0.0.0', port=8000)
Expand Down

0 comments on commit a81e8f4

Please sign in to comment.