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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Fake pod implementation

* This version also mimics a behavior we see in some DASH pods where the pod disconnects every 3 minutes; this can be used with iOS hooks to make a heartbeat to run Loop in situations where a BLE CGM is not available.

* It serves a control panel at http://<host>:8080/ (`-api` changes the address): the pod's reservoir, delivery, age, alerts and fault, with controls to set the reservoir and pod age, raise alerts, trigger or clear a fault, and crash on the next command to test unacknowledged commands. The panel drives a JSON API: `GET /status`, and `POST /command` with `{"command": "changeReservoir", "value": 45}` (also `setActiveTime` in minutes, `setAlerts` as a slot mask, `setFault` with a fault code, 0 to clear, `crashNextCommand` with `"beforeProcessing": true|false`, `ignoreNextMessages` with a count, which keeps the link up but leaves that many messages unanswered, as a pod that didn't hear them would, `hangUpOnNextMessages` with a count, which hangs up without answering when each of that many messages arrives, as a pod that drops the link during a command does, or with `"afterAnswering": true` once the phone has acknowledged the answer, and `failNextConnects` with a count and an optional `hangUpAfter` in seconds, 7 by default, which leaves service discovery unanswered on that many connects and then hangs up with CoreBluetooth's "peripheral disconnected" error, as some pods do in the field; this needs `-transport impossible` and an ImpossiBLE that passes `errorDomain` and `errorCode` through on a disconnect). A fault stops a bolus in progress, and the pod reports the part left undelivered however late it is read, as a real pod does. A reset fault (0x0D, 0x0E, 0x0F, 0x10, 0x12, 0x1E, 0x22, 0x34, 0x36, 0x3B, 0x4F, 0xD6 or 0xD7) reports what a reset pod does instead: no fault time (0x0000 on a DASH, 0xFFFF on an Omnipod 5, which has been seen reporting both), nothing delivered or left undelivered, and minutes active counted from the reset.
* It serves a control panel at http://<host>:8080/ (`-api` changes the address): the pod's reservoir, delivery, age, alerts and fault, with controls to set the reservoir and pod age, raise alerts, trigger or clear a fault, and crash on the next command to test unacknowledged commands. The panel drives a JSON API: `GET /status`, and `POST /command` with `{"command": "changeReservoir", "value": 45}` (also `setActiveTime` in minutes, `setAlerts` as a slot mask, `setFault` with a fault code, 0 to clear, `crashNextCommand` with `"beforeProcessing": true|false`, `ignoreNextMessages` with a count, which keeps the link up but leaves that many messages unanswered, as a pod that didn't hear them would, `hangUpOnNextMessages` with a count, which hangs up without answering when each of that many messages arrives, as a pod that drops the link during a command does, or with `"afterAnswering": true` once the phone has acknowledged the answer, and `failNextConnects` with a count and an optional `hangUpAfter` in seconds, 7 by default, which leaves service discovery unanswered on that many connects and then hangs up with CoreBluetooth's "peripheral disconnected" error, as some pods do in the field; this needs `-transport impossible` and an ImpossiBLE that passes `errorDomain` and `errorCode` through on a disconnect). A fault stops a bolus in progress, and the pod reports the part left undelivered however late it is read, as a real pod does. A reset fault (0x0D, 0x0E, 0x0F, 0x10, 0x12, 0x1E, 0x22, 0x34, 0x36, 0x3B, 0x4F, 0xD6 or 0xD7) reports what a reset pod does instead: no fault time (0x0000 on a DASH, 0xFFFF on an Omnipod 5, which has been seen reporting both), nothing delivered or left undelivered, and minutes active counted from the reset. A bolus sent while a bolus is running faults the pod with 0x31, as a real pod does; the pod takes neither the new bolus nor its sequence number.

* The websocket API at `/ws`, used by the separate [NodeJS/React frontend](https://github.com/ps2/pod_simulator_frontend), is still there.

Expand Down
8 changes: 8 additions & 0 deletions pkg/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,14 @@ func (p *Pod) handleCommand(cmd command.Command) {
case *command.ProgramInsulin: // 0x1A
log.Debugf("pkg pod; ProgramInsulin: PodProgress = %d", p.state.PodProgress)

if c.TableNum == 2 && p.state.BolusEnd.After(time.Now()) {
// A bolus while one is running faults the pod with 0x31. The pod
// takes neither the new bolus nor its sequence number.
log.Infof("pkg pod; bolus of %d pulses while a bolus is running: fault 0x31", c.Pulses)
p.state.SetFault(0x31)
return
}

if p.state.PodProgress < response.PodProgressPriming {
// this must be the prime command
p.state.PodProgress = response.PodProgressPriming
Expand Down
47 changes: 47 additions & 0 deletions pkg/pod/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,53 @@ func TestDeactivateReportsTheRealTimeAndReservoir(t *testing.T) {
}
}

func TestABolusWhileABolusIsRunningFaults0x31(t *testing.T) {
// A second prime sent 15 s into a 2.6 U cannula insertion bolus, as in a
// field report: 37 pulses still to go, the last program had sequence 15
s := &PODState{
PodProgress: response.PodProgressInsertingCannula,
ActivationTime: time.Now().Add(-4 * time.Minute),
Delivered: 104,
Reservoir: 2896,
BolusEnd: time.Now().Add(36500 * time.Millisecond),
LastProgSeqNum: 15,
}
p := &Pod{state: s}

p.handleCommand(&command.ProgramInsulin{Seq: 4, TableNum: 2, Pulses: 52})

if s.FaultEvent != 0x31 {
t.Fatalf("fault 0x%02X, want 0x31", s.FaultEvent)
}
if s.LastProgSeqNum != 15 || s.PodProgress != response.PodProgressInsertingCannula {
t.Fatalf("last program %d, progress %d; want 15 and %d", s.LastProgSeqNum, s.PodProgress, response.PodProgressInsertingCannula)
}
if s.Delivered != 67 || s.BolusRemaining() != 37 {
t.Fatalf("delivered %d, bolus remaining %d; want 67 and 37", s.Delivered, s.BolusRemaining())
}
rsp, ok := p.getResponse(&command.ProgramInsulin{Seq: 4, TableNum: 2, Pulses: 52}).(*response.DetailedStatusResponse)
if !ok || rsp.FaultEvent != 0x31 || rsp.LastProgSeqNum != 15 {
t.Fatalf("answered %+v, want a detailed status with fault 0x31 and sequence 15", rsp)
}
}

func TestABolusAfterTheLastOneEndedIsTaken(t *testing.T) {
s := &PODState{
PodProgress: response.PodProgressRunningAbove50U,
ActivationTime: time.Now().Add(-2 * time.Hour),
Delivered: 1080,
Reservoir: 2400,
BolusEnd: time.Now().Add(-time.Second),
}
p := &Pod{state: s}

p.handleCommand(&command.ProgramInsulin{Seq: 9, TableNum: 2, Pulses: 20})

if s.FaultEvent != 0 || s.LastProgSeqNum != 9 || s.Delivered != 1100 {
t.Fatalf("fault 0x%02X, last program %d, delivered %d; want none, 9 and 1100", s.FaultEvent, s.LastProgSeqNum, s.Delivered)
}
}

func TestTheEightyHourFaultHappensAtEightyHours(t *testing.T) {
s := &PODState{
PodProgress: response.PodProgressRunningBelow50U,
Expand Down
Loading