diff --git a/pkg/command/deactivate.go b/pkg/command/deactivate.go index dd1751e..d0d50c9 100644 --- a/pkg/command/deactivate.go +++ b/pkg/command/deactivate.go @@ -21,7 +21,7 @@ func (g *Deactivate) GetSeq() uint8 { } func (g *Deactivate) IsResponseHardcoded() bool { - return true + return false } func (g *Deactivate) DoesMutatePodState() bool { diff --git a/pkg/pod/pod.go b/pkg/pod/pod.go index 85c3de4..be63321 100644 --- a/pkg/pod/pod.go +++ b/pkg/pod/pod.go @@ -674,7 +674,11 @@ func (p *Pod) getResponse(cmd command.Command) response.Response { getStatus, isStatusRequest := cmd.(*command.GetStatus) if p.state.MinutesActive() >= 80*60 && p.state.FaultEvent == 0 { - p.state.FaultEvent = 0x1C // 0x1C = exceeded maximum pod life of 80 hours + // 0x1C = exceeded maximum pod life of 80 hours. SetFault stops any + // bolus, and the fault happened at 80 hours even if the pod isn't + // asked until later. + p.state.SetFault(0x1C) + p.state.FaultTime = 80 * 60 } if !isStatusRequest || getStatus.RequestType == 0 || getStatus.RequestType == 7 { // Not a get status command or a type 0 or type 7 get status @@ -812,7 +816,10 @@ func (p *Pod) handleCommand(cmd command.Command) { p.state.BasalActive = false } - default: // includes 0x08, 0x1C, 0x1E + case *command.Deactivate: // 0x1C + p.state.Deactivate() + + default: // includes 0x08, 0x1E // No action } diff --git a/pkg/pod/state.go b/pkg/pod/state.go index 4b7756d..1dc5f1a 100644 --- a/pkg/pod/state.go +++ b/pkg/pod/state.go @@ -136,6 +136,22 @@ func (p *PODState) SetFault(code uint8) { p.FaultTime = p.MinutesActive() } +// Deactivate stops all delivery and leaves the pod inactive. A bolus still +// running is cut short, so what it left undelivered goes back to the reservoir. +// A faulted pod already stopped its bolus in SetFault. +func (p *PODState) Deactivate() { + if p.FaultEvent == 0 { + remaining := p.BolusRemaining() + p.Delivered -= remaining + p.Reservoir += remaining + } + p.BolusEnd = time.Time{} + p.ExtendedBolusActive = false + p.TempBasalEnd = time.Time{} + p.BasalActive = false + p.PodProgress = response.PodProgressPodInactive +} + // NOTE: only handles immediate boluses; any extended bolus is not accounted for func (p *PODState) BolusRemaining() uint16 { if p.FaultEvent != 0 { diff --git a/pkg/pod/state_test.go b/pkg/pod/state_test.go index ca88874..42da6c1 100644 --- a/pkg/pod/state_test.go +++ b/pkg/pod/state_test.go @@ -4,6 +4,7 @@ import ( "testing" "time" + "github.com/avereha/pod/pkg/command" "github.com/avereha/pod/pkg/response" ) @@ -55,3 +56,53 @@ func TestAFaultWithNoBolusLeavesNothingUndelivered(t *testing.T) { t.Fatalf("remaining %d, delivered %d; want 0 and 1080", got, s.Delivered) } } + +func TestDeactivateReportsTheRealTimeAndReservoir(t *testing.T) { + // Two hours in, 120 U left, a 4 U bolus 20 s in with 70 pulses to go + s := &PODState{ + PodProgress: response.PodProgressRunningAbove50U, + ActivationTime: time.Now().Add(-2 * time.Hour), + Delivered: 1080, + Reservoir: 2400, + BasalActive: true, + BolusEnd: time.Now().Add(140 * time.Second), + } + p := &Pod{state: s} + + p.handleCommand(&command.Deactivate{}) + got := p.getResponse(&command.Deactivate{}) + rsp, ok := got.(*response.GeneralStatusResponse) + if !ok { + t.Fatalf("deactivate answered with %T, want a general status", got) + } + + if rsp.MinutesActive != 120 { + t.Fatalf("minutes active = %d, want 120", rsp.MinutesActive) + } + if rsp.Reservoir != 2470 || rsp.Delivered != 1010 { + t.Fatalf("reservoir %d, delivered %d; want 2470 and 1010", rsp.Reservoir, rsp.Delivered) + } + if rsp.PodProgress != response.PodProgressPodInactive { + t.Fatalf("pod progress = %d, want %d", rsp.PodProgress, response.PodProgressPodInactive) + } + if rsp.BasalActive || rsp.BolusActive || rsp.TempBasalActive || rsp.ExtendedBolusActive { + t.Fatalf("delivery still running after deactivation: %+v", rsp) + } +} + +func TestTheEightyHourFaultHappensAtEightyHours(t *testing.T) { + s := &PODState{ + PodProgress: response.PodProgressRunningBelow50U, + ActivationTime: time.Now().Add(-81 * time.Hour), + Reservoir: 400, + } + p := &Pod{state: s} + + got := p.getResponse(&command.GetStatus{}) + if _, ok := got.(*response.DetailedStatusResponse); !ok { + t.Fatalf("a pod past 80 hours answered with %T, want a detailed status", got) + } + if s.FaultEvent != 0x1C || s.FaultTime != 80*60 { + t.Fatalf("fault 0x%02X at %d minutes, want 0x1C at %d", s.FaultEvent, s.FaultTime, 80*60) + } +}