Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions pkg/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,10 @@ func (p *Pod) getResponse(cmd command.Command) response.Response {
var rsp response.Response

getStatus, isStatusRequest := cmd.(*command.GetStatus)
if !isStatusRequest || getStatus.RequestType == 0 {
// Not a get status command or a type 0 get status
if !isStatusRequest || getStatus.RequestType == 0 || getStatus.RequestType == 7 {
// Not a get status command or a type 0 or type 7 get status
if p.state.FaultEvent == 0 {
// Pod is not faulted, return a general status response
// Pod is not faulted, return a general status response for the type 0 or 7 get status
rsp = p.makeGeneralStatusResponse()
} else {
// Pod is faulted, return a detailed status response
Expand All @@ -435,7 +435,7 @@ func (p *Pod) getResponse(cmd command.Command) response.Response {
rsp = p.makeType5StatusResponse()
default:
// Includes 0x46, 0x50, 0x51 and the nack responses that are all hardcoded
log.Fatal("pkg pod; getStatus: unexpected type 0x%x", getStatus.RequestType)
log.Fatalf("pkg pod; getStatus: unexpected type 0x%x", getStatus.RequestType)
}
}

Expand All @@ -454,6 +454,23 @@ func (p *Pod) clearAlerts(alertMask uint8) {
}

func (p *Pod) handleCommand(cmd command.Command) {

// this progress advancement happens in the pump control logic in a real pod
if p.state.PodProgress == response.PodProgressPriming {
// if enough time has passed for priming to finish, advance PodProgress
if p.state.BolusEnd.Before(time.Now()) {
log.Infof("*** Advancing progress to PodProgressPrimingCompleted as prime bolus has ended")
p.state.PodProgress = response.PodProgressPrimingCompleted
}
}
if p.state.PodProgress == response.PodProgressInsertingCannula {
// if enough time has passed for cannula insert bolus to finish, advance PodProgress
if p.state.BolusEnd.Before(time.Now()) {
log.Infof("*** Advancing progress to PodProgressRunningAbove50U as cannula insert bolus has ended")
p.state.PodProgress = response.PodProgressRunningAbove50U
}
}

if crashBeforeProcessingCommand && cmd.DoesMutatePodState() {
log.Fatalf("pkg pod; Crashing before processing command with sequence %d", cmd.GetSeq())
}
Expand All @@ -466,21 +483,7 @@ func (p *Pod) handleCommand(cmd command.Command) {
p.state.PodProgress = response.PodProgressPairingCompleted

case *command.GetStatus: // 0x0E
now := time.Now()
if p.state.PodProgress == response.PodProgressPriming {
// if enough time has passed for priming to finish, advance PodProgress
if p.state.BolusEnd.Before(now) {
log.Infof("*** Advancing progress to PodProgressPrimingCompleted as prime bolus has ended")
p.state.PodProgress = response.PodProgressPrimingCompleted
}
}
if p.state.PodProgress == response.PodProgressInsertingCannula && !p.state.BolusEnd.After(now) {
// if enough time has passed for cannula insert bolus to finish, advance PodProgress
if p.state.BolusEnd.Before(now) {
log.Infof("*** Advancing progress to PodProgressRunningAbove50U as cannula insert bolus has ended")
p.state.PodProgress = response.PodProgressRunningAbove50U
}
}
break

case *command.SilenceAlerts: // 0x11
// clears the ActiveAlertSlots bits and Trigger Times for the specified alerts
Expand All @@ -503,8 +506,6 @@ func (p *Pod) handleCommand(cmd command.Command) {
} else if p.state.PodProgress < response.PodProgressInsertingCannula {
// this must be the insert cannula command
p.state.PodProgress = response.PodProgressInsertingCannula
} else if p.state.PodProgress < response.PodProgressRunningAbove50U {
p.state.PodProgress = response.PodProgressRunningAbove50U
}

// Programming basal schedule
Expand All @@ -522,7 +523,7 @@ func (p *Pod) handleCommand(cmd command.Command) {
if c.TableNum == 2 {
p.state.Delivered += c.Pulses
p.state.Reservoir -= c.Pulses
if p.state.PodProgress > response.PodProgressInsertingCannula {
if p.state.PodProgress >= response.PodProgressRunningAbove50U {
p.state.BolusEnd = time.Now().Add(time.Duration(c.Pulses) * time.Second * 2)
} else {
p.state.BolusEnd = time.Now().Add(time.Duration(c.Pulses) * time.Second) // one sec/pulse during pod setup
Expand Down
9 changes: 7 additions & 2 deletions pkg/pod/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,19 @@ func (p *PODState) MinutesActive() uint16 {
return uint16(time.Now().Sub(p.ActivationTime).Round(time.Minute).Minutes())
}

// NOTE: only handles immediate boluses; any extended bolus is not accounted for
func (p *PODState) BolusRemaining() uint16 {
now := time.Now()
var secondsPerPulse uint16
if p.BolusEnd.After(now) {
// Add one so the response for a bolus command has a bolus remaining value that matches the bolus size
bolusSecondsRemaining := uint16(p.BolusEnd.Sub(now).Seconds() + 1)
if p.PodProgress > response.PodProgressInsertingCannula {
return uint16(p.BolusEnd.Sub(now).Seconds()) / 2
secondsPerPulse = 2 // normal immediate bolus rate
} else {
return uint16(p.BolusEnd.Sub(now).Seconds()) // one sec/pulse during pod setup
secondsPerPulse = 1 // pod setup bolus rate
}
return bolusSecondsRemaining / secondsPerPulse
} else {
return 0
}
Expand Down