Skip to content

Commit

Permalink
#330 frontend: add xStart and tDelta as internal continuous variable …
Browse files Browse the repository at this point in the history
…data
  • Loading branch information
Wolkenfarmer committed Sep 16, 2024
1 parent c855937 commit 3680da1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
12 changes: 12 additions & 0 deletions frontend/src/sockets/MessageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,25 @@ export interface ContinuousState {
continuousVariables: ContinuousVariable[]
}

export interface ContinuousStateInternal {
timeUntilPhaseChange: number
continuousVariables: ContinuousVariableInternal[]
}

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

export interface ContinuousVariableInternal {
name: ContinuousVariableName
xStart: number
xCurrent: number
xTarget: number
tDelta: number
function: ContinuousFunctionName
}


Expand Down
42 changes: 29 additions & 13 deletions frontend/src/stores/ContinuousVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import type {ContinuousState, ContinuousStateInternal, ContinuousVariableInterna
let intervalId: number | null = null

export const useContinuousVariablesStore = defineStore('patientContinuous', {
state: (): ContinuousState => ({
state: (): ContinuousStateInternal => ({
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
return variable ? variable.xCurrent : null
}
}
},
Expand All @@ -26,9 +26,20 @@ export const useContinuousVariablesStore = defineStore('patientContinuous', {
continuousState.continuousVariables.forEach(variable => {
const existingVariable = this.continuousVariables.find(v => v.name === variable.name)
if (existingVariable) {
existingVariable.target = variable.target
existingVariable.xCurrent = variable.current // ToDo: remove
existingVariable.xStart = existingVariable.xCurrent
existingVariable.xTarget = variable.target
existingVariable.tDelta = this.timeUntilPhaseChange
existingVariable.function = variable.function
} else this.continuousVariables.push(variable)
console.log("Continuous Variable overwritten " + existingVariable.name)
} else this.continuousVariables.push({
name: variable.name,
xStart: variable.current,
xCurrent: variable.current,
xTarget: variable.target,
tDelta: this.timeUntilPhaseChange,
function: variable.function,
})
})

if (useExerciseStore().status == "running") startContinuousLogic()
Expand All @@ -43,24 +54,25 @@ export const useContinuousVariablesStore = defineStore('patientContinuous', {
return
}
this.continuousVariables.forEach(variable => {
variable.current = this.calculateVariableIncrement(variable)
variable.xCurrent = this.calculateVariableIncrement(variable)
})

this.timeUntilPhaseChange--
}, 1000)
},

calculateVariableIncrement(variable: ContinuousVariable) {
calculateVariableIncrement(variable: ContinuousVariableInternal) {
console.log("Time until phase change: " + this.timeUntilPhaseChange)
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
case ContinuousFunctionName.LINEAR:
return variable.xCurrent + linear(variable, this.timeUntilPhaseChange)
case ContinuousFunctionName.INCREMENT:
return variable.xCurrent + 1
case ContinuousFunctionName.DECREMENT:
return variable.xCurrent - 1
default:
console.error("Unrecognized function: " + variable.function)
return variable.current
return variable.xCurrent
}
},

Expand Down Expand Up @@ -90,3 +102,7 @@ export function startContinuousLogic() {
{immediate: true}
)
}

function linear(variable: ContinuousVariableInternal, timeUntilPhaseChange: number) {
return ((variable.xTarget - variable.xCurrent) / timeUntilPhaseChange)
}

0 comments on commit 3680da1

Please sign in to comment.