
function update_synoptic(data){

    var green   = "#198754"
    var yellow  = "#ffc107"
    var red     = "#dc3545"
    var gray    = "#efefef"//"#343a40"
    var normal  = gray
        
    var light_on = "stroke:#ff6600;fill:none;stroke-width:5;"
    var light_off = "stroke:#cccccc;fill:none;stroke-width:5;"

    var success = green
    var warning = yellow
    var danger  = red

    $.each(data, function(key,status){

        ////////////////////////////////
        // Positions and named positions
        ////////////////////////////////

        var elem_encoder = $('#value-'+key)
        if (status["encoder"]) {
            elem_encoder.text(status["encoder"])
        } else if (status["value"]) { // for dm, rtc, sasha
            elem_encoder.text(status["value"])
        }

        var elem_named = $('#named-'+key)

        if (status["in_limit"]) {
            elem_named.attr("fill", danger)
            elem_named.text("in limit!")
            elem_encoder.attr("fill", danger)
            elem_encoder.text(status["encoder"])
        } else {
            elem_named.attr("fill", normal)
            elem_encoder.text(status["encoder"])
        }

        if ((status["is_moving"]) | (status["named"] == "not defined")) {
            elem_named.attr("fill", warning)
            elem_encoder.attr("fill", warning)
            if (status["is_moving"])  {
                elem_named.text("moving...")
                elem_encoder.text(status["encoder"])
            } else {
                if (status["encoder"] == 0) {
                    elem_named.attr("fill", success)
                    elem_named.text("in home!")
                    elem_encoder.attr("fill", success)
                    elem_encoder.text(status["encoder"])
                } else {
                    elem_named.text("not defined")
                    elem_encoder.text(status["encoder"])
                }
            }
        } else {
            elem_named.attr("fill", normal)
            elem_named.text(status["named"])
            elem_encoder.attr("fill", normal)
            elem_encoder.text(status["encoder"])
        }

        ////////
        // Icons
        ////////
        
        if (data.INBEAM_DEP.named == "OUT" ) {
            $("#in-INBEAM_DEP-0").attr("style", "display:inline")
            $("#in-INBEAM_DEP-1").attr("style", "display:none")
        } else if (data.INBEAM_DEP.named == "IN" ) {
            $("#in-INBEAM_DEP-0").attr("style", "display:none")
            $("#in-INBEAM_DEP-1").attr("style", "display:inline")
        } else {
            $("#in-INBEAM_DEP-0").attr("style", "display:none")
            $("#in-INBEAM_DEP-1").attr("style", "display:inline")
        }

        if (data.SHUTTER.named == "OUT" ) {
            $("#in-SHUTTER-0").attr("style", "display:inline")
            $("#in-SHUTTER-1").attr("style", "display:none")
        } else if (data.SHUTTER.named == "IN" ) {
            $("#in-SHUTTER-0").attr("style", "display:none")
            $("#in-SHUTTER-1").attr("style", "display:inline")
        } else {
            $("#in-SHUTTER-0").attr("style", "display:none")
            $("#in-SHUTTER-1").attr("style", "display:inline")
        }

        if (data.CAL_MIRROR_DEP.named == "OUT" ) {
            $("#CAL_MIRROR_DEP-0").attr("style", "display:inline")
            $("#CAL_MIRROR_DEP-1").attr("style", "display:none")
        } else if (data.CAL_MIRROR_DEP.named == "IN" ) {
            $("#CAL_MIRROR_DEP-0").attr("style", "display:none")
            $("#CAL_MIRROR_DEP-1").attr("style", "display:inline")
        } else {
            $("#CAL_MIRROR_DEP-0").attr("style", "display:none")
            $("#CAL_MIRROR_DEP-1").attr("style", "display:none")
        }

        if (data.CAL_FIBER_DEP.named == "NO_FIBERS" ) {
            $("#CAL_FIBER_DEP-0").attr("style", "display:inline")
            $("#CAL_FIBER_DEP-1").attr("style", "display:none")
            $("#CAL_FIBER_DEP-2").attr("style", "display:none")
        } else if (data.CAL_FIBER_DEP.named == "FOCUS_IN" ) {
            $("#CAL_FIBER_DEP-0").attr("style", "display:none")
            $("#CAL_FIBER_DEP-1").attr("style", "display:inline")
            $("#CAL_FIBER_DEP-2").attr("style", "display:none")
        } else if (data.CAL_FIBER_DEP.named == "DEFOCUS_IN" ) {
            $("#CAL_FIBER_DEP-0").attr("style", "display:none")
            $("#CAL_FIBER_DEP-1").attr("style", "display:none")
            $("#CAL_FIBER_DEP-2").attr("style", "display:inline")
        } else {
            $("#CAL_FIBER_DEP-0").attr("style", "display:none")
            $("#CAL_FIBER_DEP-1").attr("style", "display:none")
            $("#CAL_FIBER_DEP-2").attr("style", "display:none")
        }

        if (data.CAL_FILTER_DEP.named == "OUT" ) {
            $("#CAL_FILTER_DEP-0").attr("style", "display:inline")
            $("#CAL_FILTER_DEP-1").attr("style", "display:none")
        } else if (data.CAL_FILTER_DEP.named == "IN" ) {
            $("#CAL_FILTER_DEP-0").attr("style", "display:none")
            $("#CAL_FILTER_DEP-1").attr("style", "display:inline")
        } else {
            $("#CAL_FILTER_DEP-0").attr("style", "display:none")
            $("#CAL_FILTER_DEP-1").attr("style", "display:none")
        }

        if (data.CAL_FIBER_FOCUS_LAMP.named == "OFF" ) {
            $("#CAL_FIBER_FOCUS_LAMP-0").attr("style", "display:inline")
            $("#CAL_FIBER_FOCUS_LAMP-1").attr("style", "display:none")
        } else if (data.CAL_FIBER_FOCUS_LAMP.named == "ON" ) {
            $("#CAL_FIBER_FOCUS_LAMP-0").attr("style", "display:none")
            $("#CAL_FIBER_FOCUS_LAMP-1").attr("style", "display:inline")
        } else {
            $("#CAL_FIBER_FOCUS_LAMP-0").attr("style", "display:none")
            $("#CAL_FIBER_FOCUS_LAMP-1").attr("style", "display:none")
        }

        if (data.CAL_FIBER_DEFOCUS_LAMP.named == "OFF" ) {
            $("#CAL_FIBER_DEFOCUS_LAMP-0").attr("style", "display:inline")
            $("#CAL_FIBER_DEFOCUS_LAMP-1").attr("style", "display:none")
        } else if (data.CAL_FIBER_DEFOCUS_LAMP.named == "ON" ) {
            $("#CAL_FIBER_DEFOCUS_LAMP-0").attr("style", "display:none")
            $("#CAL_FIBER_DEFOCUS_LAMP-1").attr("style", "display:inline")
        } else {
            $("#CAL_FIBER_DEFOCUS_LAMP-0").attr("style", "display:none")
            $("#CAL_FIBER_DEFOCUS_LAMP-1").attr("style", "display:none")
        }

        if (data.CAL_FF_LAMP.named == "OFF" ) {
            $("#CAL_FF_LAMP-0").attr("style", "display:inline")
            $("#CAL_FF_LAMP-1").attr("style", "display:none")
        } else if (data.CAL_FF_LAMP.named == "ON" ) {
            $("#CAL_FF_LAMP-0").attr("style", "display:none")
            $("#CAL_FF_LAMP-1").attr("style", "display:inline")
        } else {
            $("#CAL_FF_LAMP-0").attr("style", "display:none")
            $("#CAL_FF_LAMP-1").attr("style", "display:none")
        }

        if (data.PUPIL_LENS_DEP.named == "OUT" ) {
            $("#PUPIL_LENS_DEP-0").attr("style", "display:inline")
            $("#PUPIL_LENS_DEP-1").attr("style", "display:none")
        } else if (data.PUPIL_LENS_DEP.named == "IN" ) {
            $("#PUPIL_LENS_DEP-0").attr("style", "display:none")
            $("#PUPIL_LENS_DEP-1").attr("style", "display:inline")
        } else {
            $("#PUPIL_LENS_DEP-0").attr("style", "display:none")
            $("#PUPIL_LENS_DEP-1").attr("style", "display:none")
        }

        /////////////
        // Light path
        /////////////

        if (data.INBEAM_DEP.named != "IN" ) {
            $("#light-ext").attr("style", light_on)
            $("#light-in").attr("style", light_off)
            $("#light-SHUTTER").attr("style", light_off)
            $("#light-general").attr("style", light_off)
            $("#light-bs").attr("style", light_off)
        } else {
            $("#light-ext").attr("style", light_off)
            $("#light-in").attr("style", light_on)

            if (data.SHUTTER.named == "OUT" ) {
                $("#light-SHUTTER").attr("style", light_on)

                if (data.CAL_MIRROR_DEP.named == "OUT" ) {
                    $("#light-general").attr("style", light_on)
                    $("#light-bs").attr("style", light_on)
                } else {
                    $("#light-general").attr("style", light_off)
                    $("#light-bs").attr("style", light_off)
                }

            } else {//if (data.SHUTTER.named == "IN" ) {
                $("#light-SHUTTER").attr("style", light_off)
                $("#light-general").attr("style", light_off)
                $("#light-bs").attr("style", light_off)
            }
        }

        if (data.CAL_FF_LAMP.named == "ON" ) {
            $("#light-CAL_FF_LAMP").attr("style", light_on)
            if (data.CAL_MIRROR_DEP.named == "IN" ) {
                $("#light-general").attr("style", light_on)
                $("#light-bs").attr("style", light_on)
            } else {
                $("#light-general").attr("style", light_off)
                $("#light-bs").attr("style", light_off)
            }
        } else {
            $("#light-CAL_FF_LAMP").attr("style", light_off)
        }

        if (data.CAL_FIBER_FOCUS_LAMP.named == "ON" ) {
            $("#light-CAL_FIBER_FOCUS_LAMP").attr("style", light_on)
            if (data.CAL_FIBER_DEP.named == "FOCUS_IN" ) {
                $("#light-general").attr("style", light_on)
                $("#light-bs").attr("style", light_on)
            } else {
                $("#light-general").attr("style", light_off)
                $("#light-bs").attr("style", light_off)
            }
        } else {
            $("#light-CAL_FIBER_FOCUS_LAMP").attr("style", light_off)
        }

        if (data.CAL_FIBER_DEFOCUS_LAMP.named == "ON" ) {
            $("#light-CAL_FIBER_DEFOCUS_LAMP").attr("style", light_on)
            if (data.CAL_FIBER_DEP.named == "DEFOCUS_IN" ) {
                $("#light-general").attr("style", light_on)
                $("#light-bs").attr("style", light_on)
            } else {
                $("#light-general").attr("style", light_off)
                $("#light-bs").attr("style", light_off)
            }
        } else {
            $("#light-CAL_FIBER_DEFOCUS_LAMP").attr("style", light_off)
        }

    });
    

}
