command_control_backup.js 6.89 KB
var LOGS_REFRESH_FREQUENCE = 2000; //in milliseconds
var expert_mode = false;

jQuery(document).ready(function(){

  var t = data["Telescope"]["set"][0]["input_label"][0];

  //console.log(t);
  for (var i in t)
  {
    console.log(i);
    //console.log(t[i][2]);
  }

  function command_set()
  {
    var form = $("#command_form");
    var arr = [
      {val : "COORDS_FRAME", text: 'COORDS_FRAME'},
      {val : 'FOCUS', text: 'FOCUS'},
      {val : 'ROTATOR', text: 'ROTATOR'},
      {val : 'MOVE_MODE', text: 'MOVE_MODE'}
    ];

    var sel = $('<select>', {name: "param1", id:"param1"}).appendTo('#command_form');
    $(arr).each(function() {
      sel.append($("<option>").attr('value',this.val).text(this.text));
      sel.css("font-size", "25px");
    });

    var arr = [
      {val: "Hadec", text: 'Hadec'},
      {val: 'Radec', text: 'Radec'},
      {val: 'Altaz', text: 'Altaz'}
    ];
    var type = "<select>";

    var sel = $(type, {id:"param2", name:"param2"}).appendTo('#command_form');
    $(arr).each(function() {
      sel.append($("<option>").attr('value',this.val).text(this.text));
      sel.css("font-size", "25px");
    });

    $("#param1").change(function () {
      var length = form.children().length;
      for (var i = 4; i < length; i++) {
        $("#command_form").children().last().remove();
      }

      if ($("#param1").find(":selected").text() === "ROTATOR") {
        var arr = [
          {val: "NONE", text: 'NONE'},
          {val: 'SYNCHRO', text: 'SYNCHRO'},
          {val: 'TRACKING', text: 'TRACKING'}
        ];
      }

      else if ($("#param1").find(":selected").text() === "MOVE_MODE") {
        var arr = [
          {val: "Goto", text: 'Goto'},
          {val: 'Track', text: 'Track'},
          {val: 'GotoTrack', text: 'GotoTrack'}
        ];
      }
      else if ($("#param1").find(":selected").text() === "COORDS_FRAME") {
        var arr = [
          {val: "Hadec", text: 'Hadec'},
          {val: 'Radec', text: 'Radec'},
          {val: 'Altaz', text: 'Altaz'}
        ];
      }

      var type = '<select name=param2">';
      if ($("#param1").find(":selected").text()  === "FOCUS")
        type = "<input name='param2' type='number' step='any' />";

      var sel = $(type, {id:"param2"}).appendTo('#command_form');
      $(arr).each(function() {
        sel.append($("<option>").attr('value',this.val).text(this.text));
        sel.css("font-size", "25px");
      });
      $('<br><br>').appendTo("#command_form");
      $('<input>', {id:"submit_button", type:"submit", value:"SEND"}).appendTo('#command_form');

    });
  }


  function command_get()
  {
    var arr = [
      {val: "POSITION", text: 'POSITION'},
      {val: 'STATUS', text: 'STATUS'},
      {val: 'SETUP', text: 'SETUP'}
    ];
    var sel = $('<select>', {id:"param1", name:"param1"}).appendTo('#command_form');
    $(arr).each(function() {
      sel.append($("<option>").attr('value',this.val).text(this.text));
      sel.css("font-size", "25px");
    });
  }

  function command_do()
  {
    var arr = [
      {val: "START", text: 'START'},
      {val: 'ABORT', text: 'ABORT'},
      {val: 'HOMING', text: 'HOMING'},
      {val: 'DOORS', text: 'DOORS'},
        {val: 'GOTO', text: 'GOTO'},
      {val: 'STOP', text: 'STOP'},
        {val: 'OPEN DOME SHUTTER', text: 'OPEN DOME SHUTTER'},
        {val: 'CLOSE DOME SHUTTER', text: 'CLOSE DOME SHUTTER'}
    ];

    var sel = $('<select>', {id:"param1", name:"param1"}).appendTo('#command_form');
    $(arr).each(function() {
      sel.append($("<option>").attr('value',this.val).text(this.text));
      sel.css("font-size", "25px");
    });

    $("#param1").change(function () {
      var length = $("#command_form").children().length;
      for (var i = 4; i < length; i++) {
        $("#command_form").children().last().remove();
      }
      if ($("#param1").find(":selected").text()  === "DOORS")
      {
        var selected = $('<select>', {id:"param2", name:"param2"}).appendTo('#command_form');
        selected.append($("<option>").attr('value','OPEN').text('OPEN'));
        selected.append($("<option>").attr('value','CLOSE').text('CLOSE'));
        selected.css("font-size", "25px");
      }
      if ($("#param1").find(":selected").text()  === "GOTO")
      {
        var selected = $('<input size="10">', {id:"param2", name:"param2", placeholder:"Coords"}).appendTo('#command_form');
        selected.css("font-size", "25px");
      }


      $('<br><br>').appendTo("#command_form");
      $('<input>', {id:"submit_button", type:"submit", value:"SEND"}).appendTo('#command_form');

    });

  }

  $("#commande").change(function() {

    var form = $("#command_form");
    var length = form.children().length;

    for (var i = 3; i < length; i++) {
      form.children().last().remove();
    }

    var selected_command = $("#commande").find(":selected").val();
    if ( selected_command === "SET")
      command_set();
    else if (selected_command === "GET")
      command_get();
    else if (selected_command === "DO")
      command_do();
    $('<br><br>').appendTo("#command_form");
    $('<input>', {id:"submit_button", type:"submit", value:"SEND"}).appendTo('#command_form');

  });
  //$("#command_form").append(param1);

$("#expert_mode").click(function(){
  if (!expert_mode)
  {
    expert_mode = true;
    $("#command_form_expert").append("<input id='input_command_expert' type='text' name='commande_expert'>");
    $('<input>', {id:"submit_button_expert", type:"submit", value:"SEND"}).appendTo('#command_form_expert');
  }
  else {
    $("#input_command_expert").remove();
    $("#submit_button_expert").remove();
    expert_mode = false;
  }
  //$("#command_form").remove();
});

$("#command_form_expert").submit(function(event){

  event.preventDefault();
  $.post('/dashboard/observation_status/send_command_to_telescope/submit_expert', $(this).serialize(), function(data){
      var obj = JSON.parse(data);
      console.log(obj.message);
      console.log(obj.response);
      if (obj.response === "KO: Unknown command")
        alert("Unknown command");
    })
       .fail(function() {
         alert( "An error occured" );
       })
    });




  interval_telescope = setInterval(function() {ajax_request("telescope", "telescope_list");}, LOGS_REFRESH_FREQUENCE);

  function ajax_request(){
    var telescope_url ="/devices/update_telescope_logs";
    $.ajax({
      url: telescope_url,
      type: 'get',
      dataType: 'text',
      success: function(data)
      {
        var obj = JSON.parse(data);
        if (obj) {
          var listDiv = document.getElementById('telescope_logs');
          listDiv.innerHTML = '';
          for (var tmp in obj) {
            var li = document.createElement('li');
            li.innerHTML = obj[tmp]['fields']['created'] + ":     " + obj[tmp]['fields']['message'];   // Use innerHTML to set the text
            listDiv.appendChild(li);
          }
        }
      },
      error: function()
      {
        console.log('Ajax error: GET request failed\n');
      }

    });
  }
});