forked from wangeshen/MiCOKit_test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
57f737bd.html
168 lines (150 loc) · 5.49 KB
/
57f737bd.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
<!--
product id: 57f737bd
product name: MiCOKit-3165-demo_wechat_rgbled
-->
<!DOCTYPE html>
<html ng-app="ionicApp">
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width" />
<title>MiCOKit</title>
<link href="//code.ionicframework.com/1.0.0-beta.13/css/ionic.css" rel="stylesheet">
<style>
#debug {
margin-top:44px;
background-color: white;
height: 300px;
overflow: scroll;
color: black;
font-size: 12px;
}
#debug p {
padding: 3px 5px;
word-wrap: break-word;
}
.gray {
background-color: #ddd;
}
#debug span {
display: block;
word-wrap: break-word;
margin-bottom: 2px;
}
</style>
</head>
<body>
<div class="bar bar-header bar-assertive">
<h1 class="title" id="device_id"></h1>
</div>
<div id="debug"></div>
<div class="bar-footer" style="bottom:0;position:absolute;width:100%">
<div class="button-bar">
<a class="button" style="background:red; color:white" id="led_red">Red</a>
<a class="button" style="background:green; color:white" id="led_green">Green</a>
<a class="button" style="background:blue; color:white" id="led_blue">Blue</a>
</div>
<div class="button-bar">
<a class="button" style="background:white; color:black" id="led_on">On</a>
<a class="button" style="background:black; color:white" id="led_off">Off</a>
</div>
</div>
<script src="http://api.easylink.io/assets/js/mqttws31.js"></script>
<script>
function getParameterByName(name) {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
var device_id = getParameterByName('device_id');
if ( device_id !== null ){
ez_connect(device_id);
}
function dump_obj(myObject) {
var s = '';
for (var property in myObject) {
s += '<span>' + property +": " + myObject[property] + '</span>';
}
return s;
}
var i = 0;
console.log = (function(old_funct, div_log) {
return function(text) {
old_funct(text);
var p = '';
if (i%2 == 0)
p = '<p>';
else
p = '<p class=\'gray\'>';
if (typeof text === "object")
div_log.innerHTML += p + JSON.stringify(text) + '</p>';
else
div_log.innerHTML += p + text + '</p>';
div_log.scrollTop = div_log.scrollHeight;
i += 1;
};
} (console.log.bind(console), document.getElementById("debug")));
console.error = console.debug = console.info = console.log
function ez_connect(device_id) {
var access_token = getParameterByName('access_token');
document.getElementById('device_id').innerHTML = device_id;
var wsbroker = "api.easylink.io"; //mqtt websocket enabled broker
var wsport = 1983 // port for above
var client = new Paho.MQTT.Client(wsbroker, wsport, "v1-web-" + parseInt(Math.random() * 1000000, 10));
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.connect({onSuccess:onConnect});
function onConnect() {
// Once a connection has been made, make a subscription and send a message.
client.publish = function(topic, payload) {
message = new Paho.MQTT.Message(payload);
message.destinationName = topic;
client.send(message);
}
console.log("App connected!");
client.subscribe(device_id+'/out/#', {qos: 0});
}
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0)
console.log("App connection lost:"+responseObject.errorMessage);
}
function onMessageArrived(message) {
if (message.destinationName == device_id + '/out/_online') {
if (message.payloadString == '1') {
console.log("Device online.");
}
else {
console.log("Device offline.");
}
}
else {
console.log(message.payloadString);
}
}
function led_on() {
client.publish(device_id+'/in', '{"rgbled_switch":true}');
console.log(device_id+'/in: ' + '{"rgbled_switch":true}');
}
document.querySelector('#led_on').addEventListener('click', led_on);
function led_off() {
client.publish(device_id+'/in', '{"rgbled_switch":false}');
console.log(device_id+'/in: ' + '{"rgbled_switch":false}');
}
document.querySelector('#led_off').addEventListener('click', led_off);
function led_red() {
client.publish(device_id+'/in', '{"rgbled_switch":true,"rgbled_hues":0, "rgbled_saturation":100, "rgbled_brightness":100}');
console.log(device_id+'/in: ' + '{"rgbled_switch":true,"rgbled_hues":0, "rgbled_saturation":100, "rgbled_brightness":100}');
}
document.querySelector('#led_red').addEventListener('click', led_red);
function led_green() {
client.publish(device_id+'/in', '{"rgbled_switch":true,"rgbled_hues":120, "rgbled_saturation":100, "rgbled_brightness":100}');
console.log(device_id+'/in: ' + '{"rgbled_switch":true,"rgbled_hues":120, "rgbled_saturation":100, "rgbled_brightness":100}');
}
document.querySelector('#led_green').addEventListener('click', led_green);
function led_blue() {
client.publish(device_id+'/in', '{"rgbled_switch":true,"rgbled_hues":240, "rgbled_saturation":100, "rgbled_brightness":100}');
console.log(device_id+'/in: ' + '{"rgbled_switch":true,"rgbled_hues":240, "rgbled_saturation":100, "rgbled_brightness":100}');
}
document.querySelector('#led_blue').addEventListener('click', led_blue);
}
</script>
</body>
</html>