I don't understand what the timer > 5 hours is doing here. Timer counter should never get bigger than 30 minutes (resets to zero after reprime completes), so would never get to 5 hours anyway. I think we just do reprime if 30 min interval OR 5 minutes remaining in treatment. And once we do the reprime for 5 min before end of treatment, we need some way to not do it again (i.e. if we just say time remaining <= 5 minutes, we will keep doing reprimes over and over again for the last 5 minutes).
Switching valves to dialyzer with DPo stopped initially will be a shock and temporarily send a lot of dialysate to the patient while DPo ramps up. So I think DPi should be hard stopped first and then start both DPi and DPo at fast purge rate together.
I think it would make more sense to set DPi rate to zero, set re-prime in progress flag to FALSE, and set state to dialysate pumps off which will then handle the ramp down and transition to reprime state.
I don't think we can just shift into reverse here. I think I had last SRS written in a way that we would go tack to the dialysate pumps off state to ramp down again before moving to reprime state.