Skip to content

Commit

Permalink
#330: add continuous variable event logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolkenfarmer committed Sep 14, 2024
1 parent 99d008e commit de69740
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
24 changes: 24 additions & 0 deletions backend/dps_training_k/game/channel_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

class ChannelEventTypes:
STATE_CHANGE_EVENT = "state.change.event"
CONTINUOUS_VARIABLE_UPDATE = "continuous.variable.event"
EXERCISE_UPDATE = "send.exercise.event"
EXERCISE_START_EVENT = "exercise.start.event"
EXERCISE_END_EVENT = "exercise.end.event"
Expand Down Expand Up @@ -134,6 +135,12 @@ def dispatch_event(cls, applied_action, changes, is_updated):
channel,
applied_action,
)
if (
applied_action.state_name == models.ActionInstanceStateNames.FINISHED
and applied_action.patient_instance
):
event = {"type": ChannelEventTypes.CONTINUOUS_VARIABLE_UPDATE}
cls._notify_group(channel, event)
if applied_action.template.relocates:
if (
applied_action.state_name
Expand Down Expand Up @@ -333,6 +340,8 @@ def _notify_log_update_event(cls, log_entry):
class MaterialInstanceDispatcher(ChannelNotifier):
@classmethod
def dispatch_event(cls, material, changes, is_updated):
from game.models import PatientInstance

changes_set = set(changes) if changes else set()
assignment_changes = {"patient_instance", "area", "lab"}

Expand All @@ -344,6 +353,18 @@ def dispatch_event(cls, material, changes, is_updated):
event = {"type": ChannelEventTypes.RESOURCE_ASSIGNMENT_EVENT}
cls._notify_group(channel, event)

# in case a necessary material was unassigned, leading to a different future state
if isinstance(material.moved_from, PatientInstance):
channel = cls.get_group_name(material.moved_from)
event = {"type": ChannelEventTypes.CONTINUOUS_VARIABLE_UPDATE}
cls._notify_group(channel, event)

# in case a necessary material was assigned, leading to a different future state
if material.patient_instance:
channel = cls.get_group_name(material.patient_instance)
event = {"type": ChannelEventTypes.CONTINUOUS_VARIABLE_UPDATE}
cls._notify_group(channel, event)

@classmethod
def create_trainer_log(cls, material, changes, is_updated):
changes_set = set(changes) if changes else set()
Expand Down Expand Up @@ -468,6 +489,9 @@ def get_exercise(cls, patient_instance):
@classmethod
def _notify_patient_state_change(cls, patient_instance):
channel = cls.get_group_name(patient_instance)
event = {"type": ChannelEventTypes.CONTINUOUS_VARIABLE_UPDATE}
cls._notify_group(channel, event)

event = {
"type": ChannelEventTypes.STATE_CHANGE_EVENT,
"patient_instance_pk": patient_instance.id,
Expand Down
17 changes: 17 additions & 0 deletions backend/dps_training_k/game/consumers/patient_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
LabActionCheckSerializer,
)
from template.models import Action
from template.serializers.continuous_variable_serializer import (
ContinuousVariableSerializer,
)
from template.serializers.state_serialize import StateSerializer
from .abstract_consumer import AbstractConsumer
from ..channel_notifications import (
Expand Down Expand Up @@ -53,6 +56,7 @@ class PatientOutgoingMessageTypes:
RESOURCE_ASSIGNMENTS = "resource-assignments"
RESPONSE = "response"
STATE = "state"
CONTINUOUS_VARIABLE = "continuous-variable"
PATIENT_BACK = "patient-back"
PATIENT_RELOCATING = "patient-relocating"

Expand Down Expand Up @@ -282,6 +286,19 @@ def state_change_event(self, event=None):
state=serialized_state,
)

def continuous_variable_event(self, event=None):
serialized_continuous_state = ContinuousVariableSerializer(
self.get_patient_instance()
).data
self.send_event(
self.PatientOutgoingMessageTypes.CONTINUOUS_VARIABLE,
continuousState=serialized_continuous_state,
)

def exercise_start_event(self, event=None):
super().exercise_start_event(event)
self.continuous_variable_event()

def action_check_changed_event(self, event=None):
if self.currently_inspected_action:
self.receive_json(
Expand Down
25 changes: 25 additions & 0 deletions frontend/src/sockets/MessageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface MessageData {
materialName?: string
exercise?: Exercise
state?: State
continuousState?: ContinuousState
logEntries?: LogEntry[]
availablePatients: AvailablePatient[]
availableActions: AvailableAction[]
Expand All @@ -31,6 +32,30 @@ interface MessageData {
timeUntilBack: number
}

interface ContinuousState {
timeUntilPhaseChange: number
continuousVariables: ContinuousVariable[]
}

interface ContinuousVariable {
name: ContinuousVariableName
current: number
target: number
function: ContinuousFunctionName
}

enum ContinuousVariableName {
SPO2 = "SpO2",
HEART_RATE = "BPM"
}

enum ContinuousFunctionName {
LINEAR = "linear",
INCREMENT = "increment",
DECREMENT = "decrement",
}


interface Exercise {
exerciseId: string
status: string
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/sockets/SocketPatient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class SocketPatient {
case 'state':
patientStore.loadStatusFromJSON(data.state as State)
break
case 'continuous-variable':
console.log("w received state-continuous data: ", data.continuousState)
break
case 'available-patients':
availablesStore.loadAvailablePatients(data.availablePatients as AvailablePatient[])
patientStore.initializePatientFromAvailablePatients()
Expand Down

0 comments on commit de69740

Please sign in to comment.