-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
239 lines (205 loc) · 8.31 KB
/
Main.java
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
package cz.ondrejbilek.viblog;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.ToggleButton;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main extends AppCompatActivity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private Sensor mGravity;
private Sensor mAcceleration;
private TextView vibX, vibY, vibZ, accX, accY, accZ, graX, graY, graZ;
private ToggleButton tgl;
private RadioButton save, mqtt;
private OutputStreamWriter ostream;
private FileOutputStream fostream;
private MqttClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prepare();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
vibX.setText(Float.toString(sensorEvent.values[0]));
vibY.setText(Float.toString(sensorEvent.values[1]));
vibZ.setText(Float.toString(sensorEvent.values[2]));
} else if (sensorEvent.sensor.getType() == Sensor.TYPE_GRAVITY) {
graX.setText(Float.toString(sensorEvent.values[0]));
graY.setText(Float.toString(sensorEvent.values[1]));
graZ.setText(Float.toString(sensorEvent.values[2]));
} else if (sensorEvent.sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION) {
accX.setText(Float.toString(sensorEvent.values[0]));
accY.setText(Float.toString(sensorEvent.values[1]));
accZ.setText(Float.toString(sensorEvent.values[2]));
}
if (save.isChecked()) {
writeFile(sensorEvent);
} else if (mqtt.isChecked()) {
writeStream(sensorEvent);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
public void prepare() {
prepareLayout();
prepareSensors();
}
public void writeStream(SensorEvent sensor) {
String x = Float.toString(sensor.values[0]);
String y = Float.toString(sensor.values[1]);
String z = Float.toString(sensor.values[2]);
String topic = "Viblog";
String content = (x + ";" + y + ";" + z + System.getProperty("line.separator"));
int qos = 2;
System.out.println("Publishing message: " + content);
MqttMessage message = new MqttMessage(content.getBytes());
message.setQos(qos);
try {
client.publish(topic, message);
System.out.println("Message published");
} catch (MqttException me) {
System.out.println("reason " + me.getReasonCode());
System.out.println("msg " + me.getMessage());
System.out.println("loc " + me.getLocalizedMessage());
System.out.println("cause " + me.getCause());
System.out.println("excep " + me);
me.printStackTrace();
}
}
public void prepareStream() {
String broker = "tcp://iot.eclipse.org:1883";
String clientId = "Android#1";
MemoryPersistence persistence = new MemoryPersistence();
try {
client = new MqttClient(broker, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
client.connect(connOpts);
System.out.println("Connected");
} catch (MqttException me) {
System.out.println("reason " + me.getReasonCode());
System.out.println("msg " + me.getMessage());
System.out.println("loc " + me.getLocalizedMessage());
System.out.println("cause " + me.getCause());
System.out.println("excep " + me);
me.printStackTrace();
}
}
public void closeStream() {
try {
client.disconnect();
} catch (MqttException me) {
System.out.println("reason " + me.getReasonCode());
System.out.println("msg " + me.getMessage());
System.out.println("loc " + me.getLocalizedMessage());
System.out.println("cause " + me.getCause());
System.out.println("excep " + me);
me.printStackTrace();
}
}
public void writeFile(SensorEvent sensor) {
String x = Float.toString(sensor.values[0]);
String y = Float.toString(sensor.values[1]);
String z = Float.toString(sensor.values[2]);
try {
ostream.write(x + ";" + y + ";" + z + System.getProperty("line.separator"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void prepareFile() {
long timestamp = System.currentTimeMillis();
try {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), Long.toString(timestamp) + ".txt");
file.createNewFile();
fostream = new FileOutputStream(file);
ostream = new OutputStreamWriter(fostream);
} catch (IOException e) {
e.printStackTrace();
}
}
public void closeFile() {
try {
ostream.close();
fostream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void prepareSensors() {
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mGravity = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
mAcceleration = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
}
public void prepareLayout() {
vibX = (TextView) findViewById(R.id.vibX);
vibY = (TextView) findViewById(R.id.vibY);
vibZ = (TextView) findViewById(R.id.vibZ);
graX = (TextView) findViewById(R.id.graX);
graY = (TextView) findViewById(R.id.graY);
graZ = (TextView) findViewById(R.id.graZ);
accX = (TextView) findViewById(R.id.accX);
accY = (TextView) findViewById(R.id.accY);
accZ = (TextView) findViewById(R.id.accZ);
tgl = (ToggleButton) findViewById(R.id.start);
save = (RadioButton) findViewById(R.id.log);
mqtt = (RadioButton) findViewById(R.id.mqtt);
}
public void tglClick(View v) {
if (tgl.isChecked()) {
if (save.isChecked()) {
prepareFile();
} else if (mqtt.isChecked()) {
prepareStream();
}
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this, mGravity, SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this, mAcceleration, SensorManager.SENSOR_DELAY_FASTEST);
save.setEnabled(false);
mqtt.setEnabled(false);
} else {
if (save.isChecked()) {
closeFile();
} else if (mqtt.isChecked()) {
closeStream();
}
mSensorManager.unregisterListener(this, mAccelerometer);
mSensorManager.unregisterListener(this, mGravity);
mSensorManager.unregisterListener(this, mAcceleration);
save.setEnabled(true);
mqtt.setEnabled(true);
}
}
}