summaryrefslogtreecommitdiff
path: root/templates/admin/testexp.html
blob: 39291fa2b5f299af0c114b1f39bd79426c881a36 (plain)
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
{% extends "layout.html" %}
{% load staticfiles %}

{% block content %}
<style type="text/css">
    .highlight {
        background-color: lightblue;
    }
    #test-device, #monitor-logs {
        position: fixed;
        right: 10%;
        border: 1px slategray solid;
        padding: 15px;
        display: none;
    }
</style>
<div class="container">
    <div class="row">
        {% include "account/sub_nav.html" %}
        <div style="margin-left: 30px">
            {% include "admin/sub_nav.html" %}
            <h1>Board status</h1>
        </div>
        <div class="span7">
            <table class="table table-bordered" id="tableId">
                <thead>
                    <tr>
                        <th>Board MID</th>
                        <th>Status</th>
                        <th>Occupied</th>
                    </tr>
                </thead>
                <tbody>
                    {% for b in boards %}
                    <tr>
                        <td>{{ b.mid }}</td>
                        <td><span class="label label-{% if b.online %}success{% else %}important{% endif %}">{% if b.online %}Online{% else %}Offline{% endif %}</span></td>
                        <td><span class="label label-{% if b.mid in mids %}important{% else %}success{% endif %}">{% if b.mid in mids %}Ongoing{% else %}Vacant{% endif %}</span></td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        <div id="test-device" class="span5">    
            <div>
                <input type="text" id="temp" disabled="true"><br/>
                <button class="btn btn-primary" onclick="getTemp()">Get Temperature</button>
            </div>
            <br><br>

            <div>
                Heater Input: <input type="number" size="3" min="0" max="250" id="heater_val"><br/>
                Fan Input:<input type="number" size="3" min="0" max="250" id="fan_val" style="margin-left: 20px"><br/>
                <button class="btn btn-primary" onclick="setParams()">Set Parameters</button>
                <br><br>
            </div>
            <br><br>

            <div>
                <button class="btn btn-primary" onclick="resetParams()">Reset Parameters</button>
            </div>           

        </div>

        <div class="span5" id="monitor-logs">
            <span>chal raha hain bhai</span>
        </div>

    </div>
</div>
<script>
    var BASE_URL = window.location.origin;
    $("#tableId").on("click", "tr", function(e) {
        $("#tableId").find("tr.highlight").removeClass("highlight");
        $(this).addClass("highlight");
        
        columns = e.currentTarget.getElementsByTagName("td");
        isSelectedMachineVacant = columns[columns.length-1].childNodes[0].className.indexOf("label-success") > -1
        
        if (isSelectedMachineVacant) {
            $("#test-device").show();
            $("#monitor-logs").hide();
        }
        else {
            $("#monitor-logs").show();
            $("#test-device").hide();
        }
    });
    
    function resetParams() {
        var selected_machine = document.getElementsByClassName("highlight");
        if (selected_machine.length == 0) {
            alert("Please select a machine first");
            return;
        }
        var selected_mid = selected_machine[0].getElementsByTagName('td')[0].innerHTML;
        var request = $.ajax({
            url : BASE_URL + '/admin/resetdevice',
            method : 'POST',
            data : {
                'mid' : selected_mid
            }
        });

        request.done(function(data){
            if (data.status_code == 200) {
                document.getElementById("temp").value = data.message;
                document.getElementById("fan_val").value = 100;
                document.getElementById("heater_val").value = 0;
            }
            else {
                alert(data.message);
            }
        });
    }

    function setParams() {
        var MAX_VALUE = 100;
        var selected_machine = document.getElementsByClassName("highlight");
        if (selected_machine.length == 0) {
            alert("Please select a machine first");
            return;
        }
        var selected_mid = selected_machine[0].getElementsByTagName('td')[0].innerHTML;
        var fan_value = document.getElementById("fan_val").value;
        var heater_value = document.getElementById("heater_val").value;

        var isInputOK = !isNaN(fan_value) && fan_value >=0 && fan_value <= MAX_VALUE && !isNaN(heater_value) && heater_value >=0 && heater_value <= MAX_VALUE;
        if (!isInputOK) {
            alert("Please enter a value between 0 and " + MAX_VALUE);
            return;
        }

        var request = $.ajax({
            url : BASE_URL + '/admin/setdevice',
            method : 'POST',
            data : {
                'mid' : selected_mid,
                'fan' : fan_value,
                'heat' : heater_value
            }
        });

        request.done(function(data){
            if (data.status_code == 200) {
                document.getElementById("temp").value = data.message;
            }
            else {
                alert(data.message);
            }
        });
    }
    var mydata;
    function getTemp() {
        var selected_machine = document.getElementsByClassName("highlight");
        if (selected_machine.length == 0) {
            alert("Please select a machine first");
            return;
        }
        var selected_mid = selected_machine[0].getElementsByTagName('td')[0].innerHTML;
        var request = $.ajax({
            url : BASE_URL + '/admin/gettemp',
            method : 'POST',
            data : {
                'mid' : selected_mid
            }
        });

        request.done(function(data){
            mydata = data;
            if (data.status_code == 200) {
                document.getElementById("temp").value = data.message;
            }
            else {
                alert(data.message);
            }
        });
    }
</script>

{% endblock %}