Skip to content

Commit 66cc914

Browse files
committed
- initial method added to poll server for sensors and retrieve results
1 parent 6fc8e82 commit 66cc914

6 files changed

Lines changed: 110 additions & 8 deletions

File tree

Example/app/debug/app-debug.apk

756 Bytes
Binary file not shown.
1.02 KB
Binary file not shown.

Example/app/src/main/java/org/sofwerx/swe/example/PullExampleActivity.java

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import android.widget.ArrayAdapter;
1515
import android.widget.Button;
1616
import android.widget.CheckBox;
17+
import android.widget.CompoundButton;
1718
import android.widget.ImageButton;
1819
import android.widget.ProgressBar;
1920
import android.widget.SeekBar;
@@ -48,7 +49,6 @@
4849
public class PullExampleActivity extends AppCompatActivity implements SosMessageListener {
4950
private SosService sosService;
5051
private ArrayList<SosSensor> sensors;
51-
private SosSensor currentSensor = null;
5252

5353
@Override
5454
public void onDestroy() {
@@ -62,14 +62,15 @@ public void onDestroy() {
6262
*/
6363

6464
private TextInputEditText editSosServerUrl, editSosUsername, editSosPassword;
65-
private CheckBox checkSendIpc, checkSendNet;
65+
private CheckBox checkSendIpc, checkSendNet, checkPoll;
6666
private TextView textCannotSendWarning, textResult;
6767
private Button sendButton;
6868
private View viewMeasurements;
69-
private ProgressBar progressBar;
69+
private ProgressBar progressBar, progressBarPolling;
7070
private Spinner spinner;
7171
private ArrayAdapter spinnerArrayAdapter = null;
7272
private boolean isWaiting = false;
73+
private boolean systemChangingCheckPoll = false;
7374

7475
@Override
7576
protected void onCreate(Bundle savedInstanceState) {
@@ -86,11 +87,16 @@ protected void onCreate(Bundle savedInstanceState) {
8687
viewMeasurements = findViewById(R.id.viewMeasurements);
8788
sendButton.setOnClickListener(v -> sendQuery());
8889
spinner = findViewById(R.id.spinner);
90+
checkPoll = findViewById(R.id.checkPoll);
91+
progressBarPolling = findViewById(R.id.progressBarPolling);
8992
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
9093
@Override
9194
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
92-
if ((sensors != null) && (position < sensors.size()))
93-
currentSensor = sensors.get(position);
95+
if ((sensors != null) && (position < sensors.size()) && (sosService != null)) {
96+
sosService.setSensor(sensors.get(position));
97+
if (checkPoll.isChecked())
98+
sosService.startPolling();
99+
}
94100
updateVisibility();
95101
}
96102

@@ -108,6 +114,15 @@ public void onNothingSelected(AdapterView<?> parent) { }
108114
if (sosService != null)
109115
sosService.setSosServerUrl(isChecked?editSosServerUrl.getText().toString():null);
110116
});
117+
checkPoll.setOnCheckedChangeListener((buttonView, isChecked) -> {
118+
if ((!systemChangingCheckPoll) && (sosService != null)) {
119+
if (isChecked)
120+
sosService.startPolling();
121+
else
122+
sosService.stopPolling();
123+
updateVisibility();
124+
}
125+
});
111126
textResult.setMovementMethod(new ScrollingMovementMethod());
112127
loadValuesFromPreferences();
113128
}
@@ -129,7 +144,7 @@ private void sendQuery() {
129144
if ((sensors == null) || sensors.isEmpty())
130145
sosService.broadcast(new OperationGetCapabilities());
131146
else
132-
sosService.broadcast(new OperationGetResults(currentSensor));
147+
sosService.broadcast(new OperationGetResults(sosService.getSosSensor()));
133148
}
134149

135150
private final static String PREFS_SEND_IPC = "ipc";
@@ -184,6 +199,8 @@ private void updateVisibility() {
184199
progressBar.setVisibility(isWaiting?View.VISIBLE:View.GONE);
185200
if ((sensors == null) || sensors.isEmpty()) {
186201
spinner.setVisibility(View.INVISIBLE);
202+
checkPoll.setVisibility(View.INVISIBLE);
203+
progressBarPolling.setVisibility(View.INVISIBLE);
187204
sendButton.setText("Request List of Sensors");
188205
} else {
189206
if (spinnerArrayAdapter == null) {
@@ -199,9 +216,26 @@ private void updateVisibility() {
199216
values);
200217
spinner.setAdapter(spinnerArrayAdapter);
201218
spinner.setVisibility(View.VISIBLE);
219+
checkPoll.setVisibility(View.VISIBLE);
202220
spinner.setSelection(0);
203221
}
204-
sendButton.setText("Request Sensor Results");
222+
if (sosService != null) {
223+
if (sosService.isPollingServer()) {
224+
sendButton.setText("Periodically getting results...");
225+
sendButton.setEnabled(false);
226+
progressBarPolling.setVisibility(View.VISIBLE);
227+
} else {
228+
sendButton.setText("Request Sensor Results");
229+
sendButton.setEnabled(true);
230+
progressBarPolling.setVisibility(View.INVISIBLE);
231+
}
232+
} else
233+
sendButton.setEnabled(false);
234+
}
235+
if (sosService != null) {
236+
systemChangingCheckPoll = true;
237+
checkPoll.setChecked(sosService.isPollingServer());
238+
systemChangingCheckPoll = false;
205239
}
206240
}
207241

@@ -284,6 +318,8 @@ public void onSosOperationReceived(final AbstractSosOperation operation) {
284318
runOnUiThread(() -> {
285319
updateVisibility();
286320
});
321+
if (checkPoll.isChecked() && (sosService != null))
322+
sosService.startPolling();
287323
if (operation instanceof OperationGetCapabilitiesResponse) {
288324
ArrayList<SosSensor> opSensors = ((OperationGetCapabilitiesResponse) operation).getSensors();
289325
if ((opSensors != null) && !opSensors.isEmpty()) {
@@ -321,8 +357,10 @@ public void onSosOperationReceived(final AbstractSosOperation operation) {
321357
runOnUiThread(() -> {
322358
if (received == null)
323359
log("GetResults failed to get any sensor information");
324-
else
360+
else {
361+
Toast.makeText(PullExampleActivity.this,received.getId()+" updated",Toast.LENGTH_SHORT).show();
325362
log(received.toString());
363+
}
326364
});
327365
}
328366
runOnUiThread(() -> {

Example/app/src/main/res/layout/activity_pull.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,19 @@
138138
android:layout_width="match_parent"
139139
android:layout_height="wrap_content" />
140140

141+
<CheckBox
142+
android:id="@+id/checkPoll"
143+
android:layout_width="match_parent"
144+
android:layout_height="wrap_content"
145+
android:text="Periodically poll server for results" />
146+
147+
<ProgressBar
148+
android:id="@+id/progressBarPolling"
149+
style="?android:attr/progressBarStyleHorizontal"
150+
android:layout_width="match_parent"
151+
android:layout_height="wrap_content"
152+
android:indeterminate="true" />
153+
141154
<Button
142155
android:id="@+id/send"
143156
android:layout_width="match_parent"

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ A quick way to send sensor data to an OGC Sensor Observation Service - Transacti
1111
1. [What is SOS-T](#what-is)
1212
1. [How SOS-T Works](#brief-sos)
1313
1. [How to Send Sensor Data](#how-to)
14+
1. [How to Get Sensor Data](#how-to-receive)
1415

1516
<h2 id="quick-start">Quick Start</h2>
1617

@@ -120,3 +121,8 @@ When you are finally done communicating with the server, clean up the server con
120121
```java
121122
sosService.shutdown();
122123
```
124+
125+
<h2 id="how-to-receive">How to Get Sensor Data</h2>
126+
127+
Although outside of the original scope of this library, you can also use swe-android to pull data from an **SOS 2.0** compatible server. swe-library will retrieve a list of sensors and will allow you to get the latest sensor data for a particular sensor to include automatically polling the server to regularly get sensor data. To see how this is implemented, check out the PullExampleActivity in the Example app.<br/>
128+
swe-android will also except a limited number of **SOS-T** commands to allow one app running swe-android to share OGS SOS data with another app running swe-android. This is mostly a temporary work around to provide interim connectivity while more robust solutions like **Open Sensor Hub** become fully integrated.

SweLib/src/main/java/org/sofwerx/ogc/sos/SosService.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public class SosService implements SosMessageListener {
3838
private boolean ipcBroadcast;
3939
private boolean sosHttpBroadcast;
4040
private boolean sensorMode = true;
41+
private AtomicBoolean shouldPollServer = new AtomicBoolean(false);
42+
private long pollInterval = 1000l * 15l; //default server polling interval
4143

4244
/**
4345
* Creates a new SosService
@@ -81,6 +83,45 @@ public SosService(Context context, SosSensor sosSensor, String sosServerURL, fin
8183
this(context, sosSensor, sosServerURL, null, null, turnOn, enableIpcBroadcast);
8284
}
8385

86+
public boolean startPolling() {
87+
if (!shouldPollServer.get()) {
88+
if (handler == null) {
89+
Log.d(SosIpcTransceiver.TAG,"cannot setup regular server polling as handler is not yet assigned");
90+
return false;
91+
}
92+
shouldPollServer.set(true);
93+
handler.post(repeatPollServer);
94+
return true;
95+
}
96+
return true;
97+
}
98+
99+
public void stopPolling() {
100+
shouldPollServer.set(false);
101+
}
102+
103+
/**
104+
* TIme between polling of the SOS server for sensor data
105+
* @param interval time (in ms)
106+
*/
107+
public void setPollingInterval(long interval) { pollInterval = interval; }
108+
public boolean isPollingServer() { return shouldPollServer.get(); }
109+
110+
private Runnable repeatPollServer = new Runnable() {
111+
@Override
112+
public void run() {
113+
if (shouldPollServer.get()) {
114+
if (sosSensor != null) {
115+
OperationGetResults op = new OperationGetResults(sosSensor);
116+
if (op.isValid())
117+
broadcast(op);
118+
}
119+
if (handler != null)
120+
handler.postDelayed(repeatPollServer, pollInterval);
121+
}
122+
}
123+
};
124+
84125
/**
85126
* Toggles between on (active/running/transmitting and receiving) and off (paused)
86127
* @param on true = on/active/transmitting and receiving
@@ -95,6 +136,8 @@ public void setOn(boolean on) {
95136
context.registerReceiver(transceiver, intentFilter);
96137
if (sensorMode)
97138
broadcastSensorReadings();
139+
else
140+
startPolling();
98141
}
99142
} else {
100143
Log.i(SosIpcTransceiver.TAG,"SosService turned OFF");
@@ -209,6 +252,7 @@ public void broadcast(AbstractSosOperation operation) {
209252

210253
public void shutdown() {
211254
Log.i(SosIpcTransceiver.TAG,"Shutting down SosServer");
255+
stopPolling();
212256
setOn(false);
213257
if (sosThread != null) {
214258
if (handler != null)
@@ -251,6 +295,7 @@ public void registerSensor() {
251295
public SosMessageListener getListener() { return listener; }
252296
public void setListener(SosMessageListener listener) { this.listener = listener; }
253297
public SosSensor getSosSensor() { return sosSensor; }
298+
public void setSensor(SosSensor sensor) { this.sosSensor = sensor; }
254299
public void setSosServerUrl(String serverUrl) { this.serverURL = serverUrl; }
255300
public String getSosServerUrl() { return serverURL; }
256301
public void setSosServerUsername(String username) {

0 commit comments

Comments
 (0)