-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathLedSMQ.java
More file actions
248 lines (223 loc) · 7.37 KB
/
LedSMQ.java
File metadata and controls
248 lines (223 loc) · 7.37 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import RTL.SMQ.*;
import org.json.*;
import eu.hansolo.steelseries.extras.Led;
import eu.hansolo.steelseries.tools.LedColor;
interface SetLedIntf {
public void state(boolean on);
}
interface DevIntf {
public void setLed(int ledId, boolean on);
public void removeDev();
}
public class LedSMQ extends JFrame implements IntfOnClose {
public LedSMQ()
{
initUI();
try {
_macAddr = NetworkInterface.getByInetAddress(
InetAddress.getLocalHost()).getHardwareAddress();
connect();
}
catch (UnknownHostException e) {}
catch (SocketException e) {}
}
//For connecting and reconnecting to the broker.
private boolean connect()
{
try {
URL url=null;
try {url = new URL("https://simplemq.com/smq.lsp"); }
catch(MalformedURLException e) {System.exit(2);}
_smq = new SwingSMQ(url,
null, //TrustAny.cert(),
null, //TrustAny.hostName(),
null,
this);
_smq.connect(_macAddr, null, null);
IntfOnMsg onDevInfoCB = new IntfOnMsg() {
public void smqOnMsg(Msg msg) { newDevice(msg); }
};
//When a new device broadcasts to all connected "display" units.
_smq.subscribe("/m2m/led/device", "devinfo", onDevInfoCB, null);
//When a device responds to our "/m2m/led/display" published message.
_smq.subscribe("self", "devinfo", onDevInfoCB, null);
//When a device publishes LED state change.
_smq.subscribe("/m2m/led/device", "led", new IntfOnMsg() {
public void smqOnMsg(Msg msg) { ledStateChange(msg); }
}, null);
/* Broadcast to all connected devices.
Device will then send 'info' to our ptid ("self"), sub-tid: "devinfo".
*/
_smq.publish("/m2m/led/display", "Hello");
}
catch(SmqException e) {
System.out.println("Connect err: "+e.toString());
if(e.getReason() == SmqException.SERVER_DISCONNECT)
System.exit(1); //Not allowed to reconnect
return false;
}
return true;
}
private void initUI()
{
JPanel pane = (JPanel)getContentPane();
_tabbedPane.setTabPlacement(SwingConstants.LEFT);
pane.add(_tabbedPane);
setTitle("SMQ LED Demo");
setSize(350, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
//Decode JSON data describing the device and create UI LEDs based on this data
private void newDevice(Msg msg)
{
final Map<Integer, SetLedIntf> devLedM = new HashMap<Integer, SetLedIntf>();
final long devId = msg.getPTid();
final JPanel pane = new JPanel();
BoxLayout layout = new BoxLayout(pane, BoxLayout.PAGE_AXIS);
pane.setLayout(layout);
JSONObject j = new JSONObject(msg.toString());
JSONArray jleds = j.getJSONArray("leds");
for(int i = 0 ; i < jleds.length() ; i++) {
JSONObject jled = jleds.getJSONObject(i);
final int ledId = jled.getInt("id");
final Led led = new Led();
led.setPreferredSize(new Dimension(70, 70));
final JToggleButton button = new JToggleButton(jled.getString("name"));
button.setPreferredSize(new Dimension(100, 25));
if(jled.getBoolean("on")) {
button.setSelected(true);
led.setLedOn(true);
}
button.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ev) {
setLedState(devId,ledId, ev.getStateChange()==ItemEvent.SELECTED);
}
});
JPanel rpane = new JPanel();
rpane.add(button);
switch(jled.getString("color")) {
case "yellow": led.setLedColor(LedColor.YELLOW_LED); break;
case "green": led.setLedColor(LedColor.GREEN_LED); break;
case "blue": led.setLedColor(LedColor.BLUE_LED); break;
default: led.setLedColor(LedColor.RED_LED);
}
rpane.add(led);
pane.add(rpane);
SetLedIntf sli = new SetLedIntf() {
public void state(boolean on) {
led.setLedOn(on);
if(button.isSelected() != on)
button.setSelected(on);
}
};
devLedM.put(ledId,sli);
}
JScrollPane spane = new JScrollPane(pane);
_tabbedPane.addTab(j.getString("ipaddr"), spane);
_tabbedPane.setToolTipTextAt(_tabbedPane.getTabCount()-1,
j.getString("devname"));
DevIntf di = new DevIntf() {
public void setLed(int ledId, boolean on) {
devLedM.get(ledId).state(on);
}
public void removeDev() {
_tabbedPane.remove(pane);
_devicesM.remove(devId);
}
};
_devicesM.put(devId,di);
try {
_smq.observe(msg.getPTid(), new IntfOnChange() {
public void smqOnChange(final long subscribers, final long tid) {
deviceDisconnected(tid);
}
});
}
catch(SmqException e) {}
}
private void deviceDisconnected(long devId)
{
DevIntf di = _devicesM.get(devId);
if(di != null)
di.removeDev();
}
// Send LED state change to ephemeral tid (device tid) when LED button clicked
private void setLedState(long devId,int ledId, boolean on)
{
byte[] data = new byte[2];
data[0] = (byte)ledId;
data[1] = (byte)(on ? 1 : 0);
try {
_smq.publish(devId, 0, data);
}
catch(SmqException e) {}
}
// Device changed LED state: update UI.
private void ledStateChange(Msg msg)
{
byte[] data = msg.getData();
long devId = msg.getPTid();
int ledId = (int)(data[0] & 0xFF);
boolean checked = data[1] == 0 ? false : true;
_devicesM.get(devId).setLed(ledId, checked);
//muhahaha
setAlwaysOnTop(true); toFront(); setAlwaysOnTop(false);
}
@Override //SMQ callback tries to reconnect
public void smqOnClose(SmqException e)
{
System.out.println("Disconnect : " +e);
if(e.getReason() == SmqException.SERVER_DISCONNECT)
System.exit(1); //Not allowed to reconnect
Iterator<Long> it = _devicesM.keySet().iterator();
// Remove all UI components.
while (it.hasNext()) {
DevIntf di = _devicesM.get(it.next());
it.remove();
if(di != null)
di.removeDev();
}
// loop: reconnect. Note this code should be in a separate thread.
for(;;) {
if(connect())
return; // Connected.
try {Thread.sleep(2000); }
catch(InterruptedException ignore) {}
}
}
private static void openURL(String url)
{
try {
java.awt.Desktop.getDesktop().browse(
new URI(url));
}
catch(URISyntaxException e1) {}
catch(IOException e2) {}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
openURL("https://simplemq.com/m2m-led/");
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
openURL("https://makoserver.net/articles/Browser-to-Device-LED-Control-using-SimpleMQ");
openURL("https://realtimelogic.com/products/simplemq/");
}
}));
new LedSMQ();
}
});
}
private JTabbedPane _tabbedPane = new JTabbedPane();
private Map<Long, DevIntf> _devicesM = new HashMap<Long, DevIntf>();
private SwingSMQ _smq;
private byte[] _macAddr;
}