Skip to content

Commit

Permalink
Merge pull request #116 from Empyrean-Capstone/103_persist-obs-form
Browse files Browse the repository at this point in the history
fix(observation form): persist access to form
  • Loading branch information
koseward authored Apr 11, 2023
2 parents 2af1ce2 + 606ffe6 commit 4cf1098
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 20 deletions.
3 changes: 1 addition & 2 deletions api/main/file_writing/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __read_image_file(image_path: str) -> np.ndarray:

def __init_header_data_unit(image: np.ndarray, exposure_data: dict, request_input: dict, filename: str):
def enter_request_input(hdu, input: dict):
hdu.header["OBSERVER"] = input["name"]
hdu.header["OBSERVER"] = input["observer"]

# TODO: whats the difference between this and IMAGETYP?
hdu.header["OBSTYPE"] = {
Expand Down Expand Up @@ -123,7 +123,6 @@ def __update_db_cols(headers, request_input: dict) -> dict:
cols = {
"id": headers["OBSID"],
"observer": headers["OBSERVER"],
"owner_id": request_input["userid"],
"obs_type": headers["OBSTYPE"],
"exp_time": headers["EXPTIME"],
"image_typ": headers["IMAGETYP"],
Expand Down
15 changes: 14 additions & 1 deletion api/main/models/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,18 @@ def serialize(self):
"instrumentID": self.instrumentID,
"statusName": self.statusName,
"statusValue": self.statusValue,
"color": self.color
"color": self.color,
}

def set_attrs(self, attrs: dict):
"""
For all of the attributes, sets their value to the default value given
Parameters:
-----------
attrs: dict
The values to set the user's attributes to
"""

for key, val in attrs.items():
setattr(self, key, val)
37 changes: 25 additions & 12 deletions api/main/observations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from . import observations
from .. import sio
from ..logsheet.views import get_all_log_data
from ..models import Status
from ..models.observation import Observation, get_logs_json_str
from ..status.views import get_current_obsid
import time
from ..status.views import get_current_obsid, get_system_status, set_system_status


def __init_obs_records(request: dict) -> Observation:
Expand Down Expand Up @@ -62,30 +62,35 @@ def post_observation():
Is a two part function with setup_camera
"""
system_status = Status.query.filter_by(statusName="System").first()
system_status.set_attrs({"statusValue": "Busy", "color": "warning"})
db.session.commit()

sio.emit("frontend_update_status", [system_status.serialize()])

set_system_status(True)


obs_instructions: dict = request.get_json()

obs_instructions["owner_id"] = session.get("userid")
obs_instructions["observer"] = session.get("name")

# Have the spectrograph switch to the correct observation type'
# TODO: Ensure that the spectrograph has completed
sio.emit("prepare_observation", data=(obs_instructions["obs_type"], obs_instructions) )
sio.emit("prepare_observation", data=(obs_instructions["obs_type"], obs_instructions))

return {}


@sio.on("spectrograph_changed_ports")
def setup_camera( obs_instructions ):
def setup_camera(obs_instructions):
"""
Is the second part of the post observation function
Takes the observation instructions from the spectrograph and sends this data
to the camera. Also makes observations for the database
"""

print(obs_instructions)
exp_ids: list[int] = __init_obs_requests(obs_instructions)

obs_instructions["userid"] = session.get("userid")
obs_instructions["name"] = session.get("name")

# The request data from the frontend will act like
# instructions for how the camera must populate the
# observations that it has been given in the second
Expand All @@ -111,7 +116,7 @@ def end_observation():
db.session.commit()

get_all_log_data()
sio.emit( "set_obs_type", data=("object") )
sio.emit("set_obs_type", data=("object"))

return {}

Expand All @@ -122,6 +127,14 @@ def conclude_exposure():
Allows the frontend to request another observation, and resets the
spectrograph to its default values to preserve its lamps.
"""

sio.emit("enable_request_form")
sio.emit("set_obs_type", data=("object"))

system_status = Status.query.filter_by(statusName="System").first()
system_status.set_attrs({"statusValue": "Ready", "color": "success"})
db.session.commit()

sio.emit("frontend_update_status", [system_status.serialize()])

set_system_status(False)
25 changes: 25 additions & 0 deletions api/main/status/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""TODO."""

import json
from flask import session

from main import db
from .. import sio
Expand Down Expand Up @@ -67,6 +68,30 @@ def index():
return sorted(serialized_results, key=lambda k: (k["instrumentID"], k["statusName"]))


@status.get("/is_system_busy")
def get_system_status():
"""TODO."""
is_batch_owner: bool = False

system_status_row = Status.query.filter_by(statusName="System").first()
system_status: str = system_status_row.statusValue

if system_status == "Busy":
cur_obsid: int = get_current_obsid()
cur_obs = Observation.query.filter_by(id=cur_obsid).first()
is_batch_owner = str(cur_obs.owner_id) == session.get("userid")

sio.emit("update_request_form", data=(system_status, is_batch_owner))

return "success", 200


def set_system_status(is_batch_owner: bool):
system_status = Status.query.filter_by(statusName="System").first()
sio.emit("update_request_form", data=(system_status.statusValue, is_batch_owner))
return "success", 200


@sio.on("get_instrument_id")
def get_instrument_id(instrument_name) -> int:
"""
Expand Down
39 changes: 34 additions & 5 deletions src/components/Observe/Observe.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,44 @@ function Observe() {

const props = { values, handleFieldChange }

const [isFormEnabled, setFormEnabled] = React.useState(true)
const [isFormEnabled, setFormEnabled] = React.useState()
const [isStopEnabled, setStopEnabled] = React.useState()

const socket = useContext(SocketContext);


const getSystemStatus = async () => {
try {
await axios.get(
`/api/status/is_system_busy`,
null,
{
withCredentials: true
}
)
}
catch (error) {
console.error(error)
}
}

useEffect(() => {
socket.on("enable_request_form", () => {
getSystemStatus()
}, [])

function setForm(status, isBatchOwner) {
if (status === "Ready") {
setFormEnabled(true)
})
}, [socket, setFormEnabled])
}

else if (isBatchOwner) {
setStopEnabled(true)
}
}

socket.on("update_request_form", (systemStatus, isBatchOwner) => {
setForm(systemStatus, isBatchOwner)
})


const fields_row1 = [
Expand Down Expand Up @@ -359,7 +388,7 @@ function Observe() {

loadingPosition="center"
loading={isLoading === "Stop"}
disabled={isFormEnabled}
disabled={isFormEnabled || !isStopEnabled}
>
Stop
</LoadingButton>
Expand Down

0 comments on commit 4cf1098

Please sign in to comment.