1 document.getElementById("test").innerHTML = "WebSocket is not connected"; |
|
2 |
|
3 var websocket = new WebSocket('ws://'+location.hostname+'/ws'); |
|
4 var slider = document.getElementById("myRange"); |
|
5 |
|
6 |
|
7 var BBScreen={ |
|
8 |
|
9 MLT_pv: function(s) { |
|
10 $("#mlt_pv").text(s); |
|
11 }, |
|
12 MLT_sp: function(s) { |
|
13 $("#mlt_sp").text(s); |
|
14 }, |
|
15 MLT_power: function(s) { |
|
16 $("#mlt_power").text(s); |
|
17 }, |
|
18 MLT_led: function(s) { |
|
19 if (s == 0) |
|
20 $("#mlt_led").css("background-color", "#270"); |
|
21 else |
|
22 $("#mlt_led").css("background-color", "#5E0"); |
|
23 }, |
|
24 Pump_led: function(s) { |
|
25 if (s == 0) |
|
26 $("#pump_led").css("background-color", "#820"); |
|
27 else |
|
28 $("#pump_led").css("background-color", "#F40"); |
|
29 }, |
|
30 HLT_pv: function(s) { |
|
31 $("#hlt_pv").text(s); |
|
32 }, |
|
33 HLT_sp: function(s) { |
|
34 $("#hlt_sp").text(s); |
|
35 }, |
|
36 HLT_power: function(s) { |
|
37 $("#hlt_power").text(s); |
|
38 }, |
|
39 HLT_led: function(s) { |
|
40 if (s == 0) |
|
41 $("#hlt_led").css("background-color", "#270"); |
|
42 else |
|
43 $("#hlt_led").css("background-color", "#5E0"); |
|
44 }, |
|
45 Topmessage: function(s) { |
|
46 $("#top_msg").text(s); |
|
47 }, |
|
48 Timer: function(s) { |
|
49 $("#timer").text(s); |
|
50 }, |
|
51 }; |
|
52 |
|
53 var BrewBoard = { |
|
54 |
|
55 p_msg:function(e){ |
|
56 var b=this; |
|
57 var a=JSON.parse(e); |
|
58 var c={ |
|
59 mlt_pv:function(v){ |
|
60 BBScreen.MLT_pv(v) |
|
61 }, |
|
62 mlt_sp:function(v){ |
|
63 BBScreen.MLT_sp(v) |
|
64 }, |
|
65 mlt_power:function(v){ |
|
66 BBScreen.MLT_power(v) |
|
67 }, |
|
68 mlt_led:function(v){ |
|
69 BBScreen.MLT_led(v) |
|
70 }, |
|
71 pump_led:function(v){ |
|
72 BBScreen.Pump_led(v) |
|
73 }, |
|
74 hlt_pv:function(v){ |
|
75 BBScreen.HLT_pv(v) |
|
76 }, |
|
77 hlt_sp:function(v){ |
|
78 BBScreen.HLT_sp(v) |
|
79 }, |
|
80 hlt_power:function(v){ |
|
81 BBScreen.HLT_power(v) |
|
82 }, |
|
83 hlt_led:function(v){ |
|
84 BBScreen.HLT_led(v) |
|
85 }, |
|
86 top_msg:function(v){ |
|
87 BBScreen.Topmessage(v) |
|
88 }, |
|
89 timer:function(v){ |
|
90 BBScreen.Timer(v) |
|
91 }, |
|
92 }; |
|
93 console.log(e); |
|
94 $.each(a,function(k,v){ |
|
95 if (typeof(c[k])!="undefined"){ |
|
96 c[k](v) |
|
97 } |
|
98 }); |
|
99 } |
|
100 }; |
|
101 |
|
102 |
|
103 function sendMsg() { |
|
104 //websocket.send("sup playa!"); |
|
105 websocket.send('L50'); |
|
106 console.log('Sent message to websocket'); |
|
107 } |
|
108 |
|
109 |
|
110 websocket.onopen = function(evt) { |
|
111 console.log('WebSocket connection opened'); |
|
112 // websocket.send("It's open! Hooray!!!"); |
|
113 document.getElementById("test").innerHTML = "WebSocket is connected!"; |
|
114 } |
|
115 |
|
116 /* |
|
117 * Process received websocket messages. |
|
118 */ |
|
119 websocket.onmessage = function(evt) { |
|
120 var msg = evt.data; |
|
121 var value; |
|
122 switch (msg.charAt(0)) { |
|
123 case '{': |
|
124 BrewBoard.p_msg(evt.data); |
|
125 break; |
|
126 default: |
|
127 document.getElementById("output").innerHTML = evt.data; |
|
128 break; |
|
129 } |
|
130 } |
|
131 |
|
132 websocket.onclose = function(evt) { |
|
133 console.log('Websocket connection closed'); |
|
134 document.getElementById("test").innerHTML = "WebSocket closed"; |
|
135 } |
|
136 |
|
137 websocket.onerror = function(evt) { |
|
138 console.log('Websocket error: ' + evt); |
|
139 document.getElementById("test").innerHTML = "WebSocket error????!!!1!!"; |
|
140 } |
|
141 |
|
142 |
|