<commit_before>$(document).ready(function() {

    jQuery.fn.center = function () {
        this.css("position","absolute");
        this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) + $(window).scrollTop()) + "px");
        this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) + $(window).scrollLeft()) + "px");
        return this;
    };

    var motionAlarmAudio = new Audio('motion_alarm.mp3'); motionAlarmAudio.loop = true;
    var oximeterAlarmAudio = new Audio('oximeter_alarm.mp3'); oximeterAlarmAudio.loop = true;
    var connectionAlarmAudio = new Audio('connection_alarm.mp3'); connectionAlarmAudio.loop = true;

    function stopAllAudio() {
        motionAlarmAudio.pause(); motionAlarmAudio.currentTime = 0;
        oximeterAlarmAudio.pause(); oximeterAlarmAudio.currentTime = 0;
        connectionAlarmAudio.pause(); connectionAlarmAudio.currentTime = 0;
    }

    $('#alarmBanner').center().hide();

    $(document).keypress(function() {
        stopAllAudio();
        $('#alarmBanner').hide();
    });

    function playAlarm(audio, txt) {
        $('#alarmBanner').html(txt).center().show();
        audio.play();
    }

    var needToAlarmAboutOximeterDisconnected = true;
    function playOximeterDisconnectedAlarm(oximeterStatus) {
        if (needToAlarmAboutOximeterDisconnected) {
            playAlarm(connectionAlarmAudio, "Oximeter disconnected!<br/>" + oximeterStatus);
            needToAlarmAboutOximeterDisconnected = false;
        }
    }

    var lastConnectionAlarmTime = 0;
    function playConnectionAlarm() {
        if (Date.now() - lastConnectionAlarmTime > 60*1000) {
            playAlarm(connectionAlarmAudio, "Connection failed!");
            lastConnectionAlarmTime = Date.now();
        }
    }

    var lastMotionAlarmTime = 0;
    function playMotionAlarm(motionReason) {
        if (Date.now() - lastMotionAlarmTime > 60*1000) {
            playAlarm(motionAlarmAudio, "Baby's moving!<br/>" + motionReason);
            lastMotionAlarmTime = Date.now();
        }
    }

    var lastOximeterAlarmTime = 0;
    function playOximeterAlarm() {
        if (Date.now() - lastOximeterAlarmTime > 60*1000) {
            playAlarm(oximeterAlarmAudio, "Oximeter Alarm!");
            lastOximeterAlarmTime = Date.now();
        }
    }

    var lastReadTime = null;
    function refresh() {
        $.ajax({
            url: "/status",
            dataType: "json"
        }).done(function(data) {
            $("#timestamp").html(data.readTime);
            $("#SPO2").html(data.SPO2);
            $("#BPM").html(data.BPM);

            var readTime = Date.parse(data.readTime);

            if (data.SPO2 == -1) {
                playOximeterDisconnectedAlarm(data.oximeterStatus);
            } else {
                // got one good reading. Hence we need to alarm about
                // disconnection again. This way, we only play the oximeter
                // disconnected alarm once per disconnection and not once
                // every so many seconds.
                needToAlarmAboutOximeterDisconnected = true;
            }

            if (data.alarm == 1) {
                playOximeterAlarm();
            }

            if (data.motion == 1) {
                playMotionAlarm(data.motionReason);
            }

        }).error(function() {
            playConnectionAlarm();
        });
    }

    setInterval(function() {
        refresh();
    }, 1000);

    // last ditch attempt to deal with excessive delay in the pipeline,
    // just refresh the stream every 15 seconds to deal with network
    // buffering.
    // var idx = 1;
    // setInterval(function() {
    //     $('#latest').attr('src', '/stream.mjpeg?idx=' + idx);
    //     idx += 1;
    // }, 30000);
});<commit_msg>support alarm priority on the web page<commit_after>$(document).ready(function() {

    jQuery.fn.center = function () {
        this.css("position","absolute");
        this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) + $(window).scrollTop()) + "px");
        this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) + $(window).scrollLeft()) + "px");
        return this;
    };

    var motionAlarmAudio = new Audio('motion_alarm.mp3'); motionAlarmAudio.loop = true;
    var oximeterAlarmAudio = new Audio('oximeter_alarm.mp3'); oximeterAlarmAudio.loop = true;
    var connectionAlarmAudio = new Audio('connection_alarm.mp3'); connectionAlarmAudio.loop = true;

    var currentAlarm = null;

    var motionAlarm = {
        priority: 0,
        audio: motionAlarmAudio,
        lastAlarmTime: 0
    }; 
    var oximeterConnectionAlarm = {
        priority: 1,
        audio: connectionAlarmAudio,
        lastAlarmTime: 0
    };
    var internetConnectionAlarm = {
        priority: 2,
        audio: connectionAlarmAudio,
        lastAlarmTime: 0
    };
    var oximeterAlarm = {
        priority: 3,
        audio: oximeterAlarmAudio,
        lastAlarmTime: 0
    };

    function stopAllAudio() {
        motionAlarmAudio.pause();
        oximeterAlarmAudio.pause();
        connectionAlarmAudio.pause();
    }

    $('#alarmBanner').center().hide();

    $(document).keypress(function() {
        stopAllAudio();
        $('#alarmBanner').hide();
    });

    function playAlarmNow(alarm, txt) {
        // If we are currently showing a high priority alarm, do not hide
        // it with a low priority one. Also prevents us from playing
        // multiple tones.
        if (currentAlarm !== null) {
            if (currentAlarm.priority > alarm.priority) {
                return;
            } else {
                dismissAlarm(currentAlarm);
            }
        }

        $('#alarmBanner').html(txt).center().show();

        alarm.lastAlarmTime = Date.now();
        alarm.audio.currentTime = 0;
        alarm.audio.play();

        currentAlarm = alarm;
    }

    function playAlarm(alarm, txt) {
        if (Date.now() - alarm.lastAlarmTime > 60*1000) {
            playAlarmNow(alarm, txt);
        }
    }

    function dismissAlarm(alarm) {
        if (currentAlarm === alarm) {
            alarm.audio.pause();
            alarm.lastAlarmTime = 0;
            $('#alarmBanner').hide();
            currentAlarm = null;
        }
    }

    // The oximeter alarm is special. See comment below about why we do not
    // use (Date.now() - lastAlarmTime)
	var needToAlarmAboutOximeterDisconnected = true;
	function playOximeterDisconnectedAlarm(oximeterStatus) {
		if (needToAlarmAboutOximeterDisconnected) {
			playAlarmNow(oximeterConnectionAlarm, oximeterStatus);
			needToAlarmAboutOximeterDisconnected = false;
		}
	}

    var lastReadTime = null;
    function refresh() {
        $.ajax({
            url: "/status",
            dataType: "json"
        }).done(function(data) {
            dismissAlarm(internetConnectionAlarm);

            $("#timestamp").html(data.readTime);
            $("#SPO2").html(data.SPO2);
            $("#BPM").html(data.BPM);

            var readTime = Date.parse(data.readTime);

            if (data.SPO2 == -1) {
                playOximeterDisconnectedAlarm(data.oximeterStatus);
            } else {
                // got one good reading. Hence we need to alarm about
                // disconnection again. This way, we only play the oximeter
                // disconnected alarm once per disconnection and not once
                // every so many seconds.
                needToAlarmAboutOximeterDisconnected = true;
                dismissAlarm(oximeterConnectionAlarm);
            }

            if (data.alarm == 1) {
                playAlarm(oximeterAlarm, "Oximeter Alarm!");
            } else {
                dismissAlarm(oximeterAlarm);
            }

            if (data.motion == 1) {
                playAlarm(motionAlarm, "Baby's moving!<br/>" + data.motionReason);
            }

        }).error(function() {
            playAlarm(internetConnectionAlarm, "Connection to Raspberry failed!");
        });
    }

    setInterval(function() {
        refresh();
    }, 1000);
});
{'fraction_non_alphanumeric': 0.1314138058324105, 'fraction_numerical': 0.010212870677986957, 'mean_word_length': 2.137012736395214, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7510827', 'n_tokens_mistral': 2474, 'n_tokens_neox': 2338, 'n_words': 476}