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
2 changes: 1 addition & 1 deletion pkg/command/deactivate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (g *Deactivate) GetSeq() uint8 {
}

func (g *Deactivate) IsResponseHardcoded() bool {
return true
return false
}

func (g *Deactivate) DoesMutatePodState() bool {
Expand Down
11 changes: 9 additions & 2 deletions pkg/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/pod/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
51 changes: 51 additions & 0 deletions pkg/pod/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

"github.com/avereha/pod/pkg/command"
"github.com/avereha/pod/pkg/response"
)

Expand Down Expand Up @@ -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)
}
}
Loading