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
|
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.Random;
import org.umundo.core.Greeter;
import org.umundo.core.Message;
import org.umundo.core.Node;
import org.umundo.core.Publisher;
public class SpatialMapTicker {
Node node;
Publisher pub;
Greeter greeter;
ArrayList<Sensor> sensors = new ArrayList<Sensor>();
static ArrayList<SensorMessage> messages = new ArrayList<SensorMessage>();
static Random random = new Random(System.currentTimeMillis());
public class SensorMessage {
public String message;
public int severity;
public SensorMessage(String message, int severity) {
this.message = message;
this.severity = severity;
}
public SensorMessage(String message) {
this.message = message;
this.severity = 3;
}
}
public class Sensor {
@Override
public String toString() {
return "Sensor [id=" + id + ", lat=" + lat + ", lon=" + lon
+ ", html=" + getHTML() + "]";
}
public String id = "";
public Double lat = new Double(0);
public Double lon = new Double(0);
LinkedList<SensorMessage> messages = new LinkedList<SensorMessage>();
public void addMessage(SensorMessage message) {
if (messages.size() > 15)
messages.removeLast();
messages.addFirst(message);
}
public String getHTML() {
StringBuilder sb = new StringBuilder();
for (SensorMessage message : messages) {
sb.append(message.severity);
sb.append(": ");
sb.append(message.message);
sb.append("<br />");
}
return sb.toString();
}
}
public class SensorGreeter extends Greeter {
public void welcome(Publisher publisher, String nodeId, String subId) {
// send all sensors to new subscribers
for (Sensor sensor : sensors) {
Message msg = new Message(); //Message.toSubscriber(subId);
msg.putMeta("id", sensor.id);
msg.putMeta("lat", sensor.lat.toString());
msg.putMeta("lon", sensor.lon.toString());
msg.putMeta("html", sensor.getHTML());
pub.send(msg);
}
}
@Override
public void farewell(Publisher arg0, String nodeId, String subId) {
}
}
public SpatialMapTicker() {
node = new Node();
pub = new Publisher("map/tick");
greeter = new SensorGreeter();
pub.setGreeter(greeter);
node.addPublisher(pub);
double latCenter = 59.32;
double lonCenter = 18.08;
int nrSensors = 15; //(int) (Math.random() * 20);
for (int i = 0; i < nrSensors; i++) {
Sensor sensor = new Sensor();
double latOffset = (Math.random() - 0.5) * 0.3;
double lonOffset = (Math.random() - 0.5) * 0.3;
sensor.id = "Sensor #" + i;
sensor.lat = latCenter + latOffset;
sensor.lon = lonCenter + lonOffset;
sensors.add(sensor);
}
}
public static void main(String[] args) {
SpatialMapTicker ticker = new SpatialMapTicker();
ticker.run();
}
private void run() {
messages.add(new SensorMessage("Oil pressure threshold exceeded"));
messages.add(new SensorMessage("Error #245 in diagnostics unit"));
messages.add(new SensorMessage("Error #32 in diagnostics unit"));
messages.add(new SensorMessage("Error #81 in diagnostics unit"));
messages.add(new SensorMessage("Error #15 in diagnostics unit"));
messages.add(new SensorMessage("Error #145 in diagnostics unit"));
messages.add(new SensorMessage("Unit was moved out of construction site area"));
messages.add(new SensorMessage("Hydraulic pressure exceeding safety limits"));
messages.add(new SensorMessage("Drivers seat belts are not fastened!"));
messages.add(new SensorMessage("Battery recharge cycles exceeded"));
messages.add(new SensorMessage("Unit operated outside recommended paramters"));
while (true) {
try {
Thread.sleep((long) (Math.random() * 1000) + 200);
} catch (InterruptedException e) {
e.printStackTrace();
}
Sensor sensor = sensors.get(random.nextInt(sensors.size()));
SensorMessage fault = messages.get(random.nextInt(messages.size()));
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss z");
String nowString = sdf.format(now);
sensor.addMessage(fault);
// System.out.println("Publishing " + sensor);
Message msg = new Message();
msg.putMeta("id", sensor.id);
msg.putMeta("lat", sensor.lat.toString());
msg.putMeta("lon", sensor.lon.toString());
msg.putMeta("html", sensor.getHTML());
msg.putMeta("time", nowString);
msg.putMeta("timeStamp", Long.toString(now.getTime()));
msg.putMeta("message", sensor.messages.getFirst().message);
msg.putMeta("severity", Integer.toString(random.nextInt(10)));
pub.send(msg);
}
}
}
|