Blame view

src/misc/static/js/command_control_backup.js 6.89 KB
d3faf327   Quentin Durand   Command control D...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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');
      }

    });
  }
});