Skip to content

Commit

Permalink
#330 frontend: add continuous variables store with function update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolkenfarmer committed Sep 14, 2024
1 parent de69740 commit 8c45c0a
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 51 deletions.
105 changes: 54 additions & 51 deletions frontend/src/sockets/SocketPatient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {useActionOverviewStore} from "@/stores/ActionOverview"
import {useVisibleInjuriesStore} from "@/stores/VisibleInjuries"
import {commonMockEvents} from "./commonMockEvents"
import {useActionCheckStore} from "@/stores/ActionCheck"
import {startContinuousLogic, useContinuousVariablesStore} from "@/stores/ContinuousVariables"


class SocketPatient {
Expand All @@ -23,6 +24,7 @@ class SocketPatient {
connect(): void {
const patientStore = usePatientStore()
const exerciseStore = useExerciseStore()
const patientContinuousStore = useContinuousVariablesStore()
const availablesStore = useAvailablesStore()
const resourceAssignmentsStore = useResourceAssignmentsStore()
const actionOverview = useActionOverviewStore()
Expand Down Expand Up @@ -69,7 +71,7 @@ class SocketPatient {
patientStore.loadStatusFromJSON(data.state as State)
break
case 'continuous-variable':
console.log("w received state-continuous data: ", data.continuousState)
patientContinuousStore.loadContinuousVariablesFromJSON(data.continuousState as ContinuousState)
break
case 'available-patients':
availablesStore.loadAvailablePatients(data.availablePatients as AvailablePatient[])
Expand All @@ -86,6 +88,7 @@ class SocketPatient {
exerciseStore.status = 'running'
setScreen(Screens.STATUS, ScreenPosition.LEFT)
setScreen(Screens.ACTIONS, ScreenPosition.RIGHT)
startContinuousLogic()
break
case 'exercise-pause':
exerciseStore.status = 'paused'
Expand Down Expand Up @@ -288,60 +291,60 @@ export const serverMockEvents = [
id: 'resource-assignments',
data: '{"messageType":"resource-assignments","resourceAssignments":[' +
'{"areaId":1,' +
'"personnel":[' +
'{"personnelId":1,"patientId":"145345"},' +
'{"personnelId":10,"patientId":null}' +
'],' +
'"material":[' +
'{"materialId":1,"patientId":"145345"},' +
'{"materialId":2,"patientId":"256443"}' +
']' +
'"personnel":[' +
'{"personnelId":1,"patientId":"145345"},' +
'{"personnelId":10,"patientId":null}' +
'],' +
'"material":[' +
'{"materialId":1,"patientId":"145345"},' +
'{"materialId":2,"patientId":"256443"}' +
']' +
'},' +
'{"areaId":2,' +
'"personnel":[' +
'{"personnelId":3,"patientId":"123456"},' +
'{"personnelId":8,"patientId":"123456"},' +
'{"personnelId":9,"patientId":null},' +
'{"personnelId":11,"patientId":null},' +
'{"personnelId":2,"patientId":null},' +
'{"personnelId":7,"patientId":null},' +
'{"personnelId":12,"patientId":"105626"},' +
'{"personnelId":13,"patientId":"963733"},' +
'{"personnelId":14,"patientId":"754262"},' +
'{"personnelId":15,"patientId":"754262"}' +
'],' +
'"material":[' +
'{"materialId":3,"patientId":"123456"},' +
'{"materialId":4,"patientId":"123456"},' +
'{"materialId":7,"patientId":null},' +
'{"materialId":8,"patientId":null},' +
'{"materialId":9,"patientId":"126541"},' +
'{"materialId":10,"patientId":null},' +
'{"materialId":11,"patientId":null},' +
'{"materialId":12,"patientId":null},' +
'{"materialId":13,"patientId":null},' +
'{"materialId":14,"patientId":null},' +
'{"materialId":15,"patientId":null},' +
'{"materialId":16,"patientId":null},' +
'{"materialId":17,"patientId":null},' +
'{"materialId":18,"patientId":null},' +
'{"materialId":19,"patientId":null},' +
'{"materialId":20,"patientId":null},' +
'{"materialId":21,"patientId":null},' +
'{"materialId":22,"patientId":null}' +
']' +
'"personnel":[' +
'{"personnelId":3,"patientId":"123456"},' +
'{"personnelId":8,"patientId":"123456"},' +
'{"personnelId":9,"patientId":null},' +
'{"personnelId":11,"patientId":null},' +
'{"personnelId":2,"patientId":null},' +
'{"personnelId":7,"patientId":null},' +
'{"personnelId":12,"patientId":"105626"},' +
'{"personnelId":13,"patientId":"963733"},' +
'{"personnelId":14,"patientId":"754262"},' +
'{"personnelId":15,"patientId":"754262"}' +
'],' +
'"material":[' +
'{"materialId":3,"patientId":"123456"},' +
'{"materialId":4,"patientId":"123456"},' +
'{"materialId":7,"patientId":null},' +
'{"materialId":8,"patientId":null},' +
'{"materialId":9,"patientId":"126541"},' +
'{"materialId":10,"patientId":null},' +
'{"materialId":11,"patientId":null},' +
'{"materialId":12,"patientId":null},' +
'{"materialId":13,"patientId":null},' +
'{"materialId":14,"patientId":null},' +
'{"materialId":15,"patientId":null},' +
'{"materialId":16,"patientId":null},' +
'{"materialId":17,"patientId":null},' +
'{"materialId":18,"patientId":null},' +
'{"materialId":19,"patientId":null},' +
'{"materialId":20,"patientId":null},' +
'{"materialId":21,"patientId":null},' +
'{"materialId":22,"patientId":null}' +
']' +
'},' +
'{"areaId":3,' +
'"personnel":[' +
'{"personnelId":5,"patientId":"126143"},' +
'{"personnelId":6,"patientId":"462455"}' +
'],' +
'"material":[' +
'{"materialId":5,"patientId":"126143"},' +
'{"materialId":6,"patientId":"462455"},' +
'{"materialId":5,"patientId":null},' +
'{"materialId":6,"patientId":null}' +
']' +
'"personnel":[' +
'{"personnelId":5,"patientId":"126143"},' +
'{"personnelId":6,"patientId":"462455"}' +
'],' +
'"material":[' +
'{"materialId":5,"patientId":"126143"},' +
'{"materialId":6,"patientId":"462455"},' +
'{"materialId":5,"patientId":null},' +
'{"materialId":6,"patientId":null}' +
']' +
'}]}'
},
{
Expand Down
90 changes: 90 additions & 0 deletions frontend/src/stores/ContinuousVariables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import {defineStore} from 'pinia'
import {useExerciseStore} from "@/stores/Exercise"
import {watch} from "vue"

let intervalId: number | null = null

export const useContinuousVariablesStore = defineStore('patientContinuous', {
state: (): ContinuousState => ({
timeUntilPhaseChange: Number.NEGATIVE_INFINITY,
continuousVariables: [],
}),
getters: {
getCurrentValueByName: (state) => {
return (variableName: string) => {
const variable = state.continuousVariables.find(v => v.name === variableName)
return variable ? variable.current : null
}
}
},
actions: {
loadContinuousVariablesFromJSON(continuousState: ContinuousState) {
this.timeUntilPhaseChange = continuousState.timeUntilPhaseChange

continuousState.continuousVariables.forEach(variable => {
const existingVariable = this.continuousVariables.find(v => v.name === variable.name)
if (existingVariable) {
existingVariable.target = variable.target
existingVariable.function = variable.function
} else this.continuousVariables.push(variable)
})

if (useExerciseStore().status == "running") startContinuousLogic()
},

startUpdatingContinuousVariables() {
if (intervalId !== null) return

intervalId = setInterval(() => {
if (this.timeUntilPhaseChange <= 0) {
this.stopUpdatingContinuousVariables()
return
}
this.continuousVariables.forEach(variable => {
variable.current = this.calculateVariableIncrement(variable)
})

this.timeUntilPhaseChange--
}, 1000)
},

calculateVariableIncrement(variable: ContinuousVariable) {
switch (variable.function) {
case "linear":
return variable.current + ((variable.target - variable.current) / this.timeUntilPhaseChange)
case "increment":
return variable.current + 1
case "decrement":
return variable.current - 1
default:
console.error("Unrecognized function: " + variable.function)
return variable.current
}
},

stopUpdatingContinuousVariables() {
this.timeUntilPhaseChange = 0
if (intervalId !== null) {
clearInterval(intervalId)
intervalId = null
}
}
}
})

export function startContinuousLogic() {
const patientStore = useContinuousVariablesStore()
const exerciseStore = useExerciseStore()

watch(
() => exerciseStore.status,
(newVal) => {
if (newVal === "running") {
patientStore.startUpdatingContinuousVariables()
} else {
patientStore.stopUpdatingContinuousVariables()
}
},
{immediate: true}
)
}

0 comments on commit 8c45c0a

Please sign in to comment.