Exemple #1
0
        public static PdmMessage request_set_basal_schedule(decimal[] schedule, ushort hour, ushort minute, ushort second)
        {
            var halved_schedule = new decimal[48];

            for (int i = 0; i < 47; i++)
            {
                halved_schedule[i] = schedule[i] / 2m;
            }

            int    current_hh      = hour * 2;
            ushort seconds_past_hh = 0;

            if (minute < 30)
            {
                seconds_past_hh = (ushort)(minute * 60 + second);
            }
            else
            {
                seconds_past_hh = (ushort)((minute - 30) * 60 + second);
            }

            var seconds_to_hh  = (ushort)(1800 - seconds_past_hh);
            var seconds_to_hh8 = (ushort)(seconds_to_hh * 8);

            var pulse_list = getPulsesForHalfHours(halved_schedule);
            var ise_list   = getInsulinScheduleTableFromPulses(pulse_list);
            var ise_body   = getBodyFromTable(ise_list);
            var pulse_body = getBodyFromTable(pulse_list);

            var command_body  = new Bytes(0);
            var body_checksum = new Bytes((byte)current_hh);

            var current_hh_pulse_count = pulse_list[current_hh];
            var remaining_pulse_count  = (ushort)(current_hh_pulse_count * seconds_to_hh / 1800);

            body_checksum.Append(seconds_to_hh8);
            body_checksum.Append(remaining_pulse_count);

            command_body.Append(getChecksum(new Bytes(body_checksum, pulse_body)));
            command_body.Append(body_checksum);
            command_body.Append(ise_body);

            var msg = new PdmMessage(PdmRequest.InsulinSchedule, command_body);

            command_body = new Bytes(new byte[] { 0, 0 });

            var pulse_entries = getPulseIntervalEntries(halved_schedule);

            for (int i = 0; i < pulse_entries.Length; i++)
            {
                var pti      = pulse_entries[i];
                var pulses10 = pti.Item1;
                var interval = pti.Item2;
                var indices  = pti.Item3;

                var ii = Array.IndexOf <int>(indices, current_hh);
                if (ii >= 0)
                {
                    command_body.Append((byte)i);
                    var pulses_past_intervals          = (ushort)((uint)ii * (uint)1800000000 / (uint)interval);
                    var pulses_past_this_interval      = (ushort)((uint)seconds_past_hh * (uint)1000000 / (uint)interval + 1);
                    var remaining_pulses_this_interval = (ushort)(pulses10 - pulses_past_this_interval - pulses_past_intervals);
                    var microseconds_to_next_interval  = (uint)interval - ((uint)seconds_past_hh * (uint)1000000 % (uint)interval);

                    command_body.Append(remaining_pulses_this_interval);
                    command_body.Append(microseconds_to_next_interval);
                    break;
                }
            }

            for (int i = 0; i < pulse_entries.Length; i++)
            {
                var pti      = pulse_entries[i];
                var pulses10 = pti.Item1;
                var interval = pti.Item2;

                command_body.Append(pulses10);
                command_body.Append(interval);
            }

            msg.add_part(PdmRequest.BasalSchedule, command_body);
            return(msg);
        }
Exemple #2
0
        public static PdmMessage request_alert_setup(List <AlertConfiguration> alert_configurations)
        {
            var cmd_body = new Bytes();

            foreach (var ac in alert_configurations)
            {
                if (ac.alert_after_minutes == null && ac.alert_after_reservoir == null && ac.activate)
                {
                    throw new PdmException("Either alert_after_minutes or alert_after_reservoir must be set");
                }
                else if (ac.alert_after_minutes != null && ac.alert_after_reservoir != null)
                {
                    throw new PdmException("Only one of alert_after_minutes or alert_after_reservoir must be set");
                }

                if (ac.alert_duration > 0x1FF)
                {
                    throw new PdmException($"Alert duration in minutes cannot be more than {0x1ff:%d}");
                }
                else if (ac.alert_duration < 0)
                {
                    throw new PdmException("Invalid alert duration value");
                }

                if (ac.alert_after_minutes != null && ac.alert_after_minutes > 4800)
                {
                    throw new PdmException("Alert cannot be set beyond 80 hours");
                }
                if (ac.alert_after_minutes != null && ac.alert_after_minutes < 0)
                {
                    throw new PdmException("Invalid value for alert_after_minutes");
                }

                if (ac.alert_after_reservoir != null && ac.alert_after_reservoir > 50)
                {
                    throw new PdmException("Alert cannot be set for more than 50 units");
                }
                if (ac.alert_after_reservoir != null && ac.alert_after_reservoir < 0)
                {
                    throw new PdmException("Invalid value for alert_after_reservoir");
                }

                byte b0 = (byte)(ac.alert_index << 4);
                if (ac.activate)
                {
                    b0 |= 0x08;
                }
                if (ac.alert_after_reservoir != null)
                {
                    b0 |= 0x04;
                }
                if (ac.trigger_auto_off)
                {
                    b0 |= 0x02;
                }

                b0 |= (byte)((ac.alert_duration >> 8) & 0x01);
                byte b1 = (byte)(ac.alert_duration & 0x00ff);
                byte b2 = 0;
                byte b3 = 0;

                if (ac.alert_after_reservoir != null)
                {
                    var reservoir_limit = (int)(ac.alert_after_reservoir * 10);
                    b2 = (byte)(reservoir_limit >> 8);
                    b3 = (byte)(reservoir_limit & 0xff);
                }
                if (ac.alert_after_minutes != null)
                {
                    b2 = (byte)(ac.alert_after_minutes >> 8);
                    b3 = (byte)(ac.alert_after_minutes & 0xff);
                }

                cmd_body.Append(new byte[] { b0, b1, b2, b3, (byte)ac.beep_repeat_type, (byte)ac.beep_type });
            }
            return(new PdmMessage(PdmRequest.ConfigureAlerts, cmd_body));
        }