<script>
    let check_signal_lost = 999;
    let lost_signal_count = 0;
    let signal_lost_sound = true;
    let alarm_signal_lost = false;
    var audio_signal_lost = new Audio("{{ domainname('config/sound/capture/signal_lost.m4a') }}");
    setInterval(() => {
        socket.emit('obs', `{"event":"check_signal"}`);
    }, 7000);

    socket.on('obs', function(msg) {
        let obj = JSON.parse(msg);
        if (obj.event == 'vdo_size') {
            alarm_signal_lost = true;
        }
    });

    socket.on('filename', function(msg) {
        alarm_signal_lost = true;
    });


    socket.on('obs', function(msg) {
        let obj = JSON.parse(msg);
        if (obj.event == 'signal_detected') {
            if (obj.size == check_signal_lost && alarm_signal_lost) {
                lost_signal_count++;
                if (lost_signal_count == 3) {
                    socket.emit('obs', `{"event":"reload"}`);
                }
                if (lost_signal_count > 3) {
                    if (lost_signal_count % 3 == 0) {
                        console.log("สัญญาณขาดหาย");
                        $('#modal_signal_lost').modal('show');
                        if (signal_lost_sound) {
                            audio_signal_lost.play();
                        }
                    }
                }
            } else {
                check_signal_lost = obj.size;
                lost_signal_count = 0;
            }
        }
    });
</script>

@if(@$configscope->readocr)
<script>
    setInterval(() => {
        socket.emit('obs', `{"event":"serial_ocr"}`);
    }, 15000);
    socket.on('obs', function(msg) {
        let obj = JSON.parse(msg);
        if (obj.event == 'ocr_result') {
            if (obj.status == 'success') {
                let scope_options = $("#source1_obs option");
                let serialno = $("#source1_obs").val();
                scope_options.each(function() {
                    if($(this).data('serialno') == obj.serial && $(this).data('id') != serialno) {
                        $("#source1_obs").select2().val($(this).val()).trigger('change');
                    }
                });
            }
        }
    });
</script>
@endif
