-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectStimulation.html
executable file
·196 lines (165 loc) · 6.95 KB
/
directStimulation.html
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
<!DOCTYPE html>
<html>
<head>
<title>Amplitude Configuration</title>
<meta charset="UTF-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js" type="text/javascript"></script>
<style>
html,
body {
/* background:blue; */
text-align: center;
}
form {
margin-top: 200px;
margin-left: 20%;
margin-right: 20%;
}
label {
color: black;
font-weight: bold;
float:left;
}
input {
width: 100%;
height: 30px;
margin: 5px;
}
input[type=submit] {
color: white;
background-color: blue;
border: 2px solid blue;
}
p {
color:blue;
}
</style>
</head>
<body>
<div id="hide_show">
<div class="sub_cat_box" id="cat5">
<ul>
<p><label>Server address</label>
<input id="mqttHost" value="10.1.1.190">
</p>
<p><label>Port</label>
<input id="mqttPort" value="1887">
</p>
<p><label>Device Name</label>
<input id="deviceName" value="6200">
</p>
<p><label>Largura do Pulso</label>
<input id="ton" value="200">
</p>
<p><label>Período</label>
<input id="period" value="20000">
</p>
<button onclick="connectMQTT()">Connect to MQTT</button>
<p><label>Channel 1</label>
<input id="ch1" value="0" type="range"
class="form-control-range" name="canal1">
</p>
<p><label>Channel 2</label>
<input id="ch2" value="0" type="range"
class="form-control-range" name="canal2">
</p>
<p><label>Channel 3</label>
<input id="ch3" value="0" type="range"
class="form-control-range" name="canal3">
</p>
<p><label>Channel 4</label>
<input id="ch4" value="0" type="range"
class="form-control-range" name="canal4">
</p>
<p><label>Log de Comandos:</label></p>
<textarea id="commandlog" rows="15" cols="100"></textarea>
<p><label>Log de Respostas:</label></p>
<textarea id="responselog" rows="15" cols="100"></textarea>
<p><label>Log de Streaming:</label></p>
<textarea id="streaminglog" rows="15" cols="100"></textarea>
<p><label>Log de Dispositivos:</label></p>
<textarea id="deviceLog" rows="15" cols="100"></textarea>
</ul>
</div>
</div>
<script>
let client;
let reconnectTimeout = 2000;
let deviceName = ''
let hostName;
let port = 1887;
let subscribeOptions = {
qos: 0, // QoS
invocationContext: {foo: true}, // Passed to success / failure callback
onFailure: onConnectionLost,
timeout: 10
};
function connectMQTT(){
hostName = document.getElementById("mqttHost").value
port = document.getElementById("mqttPort").value
client = new Paho.MQTT.Client(hostName, Number(port), "", "web" + new Date().getTime());
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
// connect the client
client.connect({onSuccess: Connected,onFailure: onConnectionLost,keepAliveInterval: 10000,timeout: 300});
}
// called when the client connects
function Connected() {
// Once a connection has been made, make a subscription and send a message.
alert("MQTT Connected");
deviceName = document.getElementById("deviceName").value;
client.subscribe("devices");
client.subscribe("dev"+deviceName+"ss",subscribeOptions);
client.subscribe("dev"+deviceName+"ans",subscribeOptions);
client.subscribe("cmd2dev"+deviceName,subscribeOptions);
message = new Paho.MQTT.Message('{"op": 9}');
message.destinationName = "cmd2dev"+deviceName;
client.send(message);
}
// called when the client loses its connection
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
}
}
// called when a message arrives
function onMessageArrived(message) {
console.log("onMessageArrived:"+message.payloadString);
if(message.destinationName == "cmd2dev"+deviceName){
let log = document.getElementById("commandlog");
log.value += "\n"+message.payloadString
} else if(message.destinationName == "dev"+deviceName+"ss"){
let log = document.getElementById("streaminglog");
log.value += "\n"+message.payloadString
}else if(message.destinationName == "dev"+deviceName+"ans"){
let log = document.getElementById("responselog");
log.value += "\n"+message.payloadString
}else if(message.destinationName == "devices"){
let log = document.getElementById("deviceLog");
log.value += "\n"+message.payloadString
}
}
ch1 = document.getElementById("ch1");
ch2 = document.getElementById("ch2");
ch3 = document.getElementById("ch3");
ch4 = document.getElementById("ch4");
ch1.addEventListener("change", sendChannelCangedCommand);
ch2.addEventListener("change", sendChannelCangedCommand);
ch3.addEventListener("change", sendChannelCangedCommand);
ch4.addEventListener("change", sendChannelCangedCommand);
function sendChannelCangedCommand() {
deviceName = document.getElementById("deviceName").value;
ton = document.getElementById("ton").value;
period = document.getElementById("period").value;
ch1 = document.getElementById("ch1").value;
ch2 = document.getElementById("ch2").value;
ch3 = document.getElementById("ch3").value;
ch4 = document.getElementById("ch4").value;
message = new Paho.MQTT.Message('{"op":2,"m":"'+ch1+','+ch2+','+ch3+','+ch4+'","t":"'+ton+'","p":"'+period+'"}');
message.destinationName = "cmd2dev"+deviceName;
client.send(message);
}
</script>
</body>
</html>