|
| 1 | +package org.sofwerx.swe.example; |
| 2 | + |
| 3 | +import android.annotation.SuppressLint; |
| 4 | +import android.content.SharedPreferences; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.preference.PreferenceManager; |
| 7 | +import android.text.Editable; |
| 8 | +import android.text.TextWatcher; |
| 9 | +import android.text.method.ScrollingMovementMethod; |
| 10 | +import android.util.Log; |
| 11 | +import android.util.Pair; |
| 12 | +import android.view.View; |
| 13 | +import android.widget.Button; |
| 14 | +import android.widget.CheckBox; |
| 15 | +import android.widget.ImageButton; |
| 16 | +import android.widget.SeekBar; |
| 17 | +import android.widget.TextView; |
| 18 | + |
| 19 | +import androidx.appcompat.app.AppCompatActivity; |
| 20 | + |
| 21 | +import com.google.android.material.textfield.TextInputEditText; |
| 22 | + |
| 23 | +import org.json.JSONException; |
| 24 | +import org.json.JSONObject; |
| 25 | +import org.sofwerx.ogc.sos.AbstractSosOperation; |
| 26 | +import org.sofwerx.ogc.sos.HttpHelper; |
| 27 | +import org.sofwerx.ogc.sos.OperationGetCapabilities; |
| 28 | +import org.sofwerx.ogc.sos.OperationGetCapabilitiesResponse; |
| 29 | +import org.sofwerx.ogc.sos.SensorMeasurement; |
| 30 | +import org.sofwerx.ogc.sos.SensorMeasurementTime; |
| 31 | +import org.sofwerx.ogc.sos.SensorResultTemplateField; |
| 32 | +import org.sofwerx.ogc.sos.SosIpcTransceiver; |
| 33 | +import org.sofwerx.ogc.sos.SosSensor; |
| 34 | +import org.sofwerx.ogc.sos.SosService; |
| 35 | +import org.sofwerx.ogc.sos.SosMessageListener; |
| 36 | + |
| 37 | +import java.io.IOException; |
| 38 | +import java.io.StringWriter; |
| 39 | +import java.util.ArrayList; |
| 40 | +import java.util.Random; |
| 41 | + |
| 42 | +public class PullExampleActivity extends AppCompatActivity implements SosMessageListener { |
| 43 | + private SosService sosService; |
| 44 | + private ArrayList<SosSensor> sensors; |
| 45 | + |
| 46 | + @Override |
| 47 | + public void onDestroy() { |
| 48 | + if (sosService != null) |
| 49 | + sosService.shutdown(); //for proper memory management, remember to shutdown the SOS service so it can do its cleanup as well |
| 50 | + saveValuesToPreferences(); |
| 51 | + super.onDestroy(); |
| 52 | + } |
| 53 | + /** |
| 54 | + * Below is all the GUI and support coded needed to display and alter the sensor info |
| 55 | + */ |
| 56 | + |
| 57 | + private TextInputEditText editSosServerUrl, editSosUsername, editSosPassword; |
| 58 | + private CheckBox checkSendIpc, checkSendNet; |
| 59 | + private TextView textCannotSendWarning, textResult; |
| 60 | + private Button sendButton; |
| 61 | + private View viewMeasurements; |
| 62 | + |
| 63 | + @Override |
| 64 | + protected void onCreate(Bundle savedInstanceState) { |
| 65 | + super.onCreate(savedInstanceState); |
| 66 | + setContentView(R.layout.activity_pull); |
| 67 | + editSosServerUrl = findViewById(R.id.editUrl); |
| 68 | + editSosUsername = findViewById(R.id.editUsername); |
| 69 | + editSosPassword = findViewById(R.id.editPassword); |
| 70 | + checkSendIpc = findViewById(R.id.checkSendIpc); |
| 71 | + checkSendNet = findViewById(R.id.checkSendNet); |
| 72 | + textCannotSendWarning = findViewById(R.id.textSendWarning); |
| 73 | + textResult = findViewById(R.id.result); |
| 74 | + sendButton = findViewById(R.id.send); |
| 75 | + viewMeasurements = findViewById(R.id.viewMeasurements); |
| 76 | + sendButton.setOnClickListener(v -> sendQuery()); |
| 77 | + checkSendIpc.setOnCheckedChangeListener((buttonView, isChecked) -> { |
| 78 | + updateVisibility(); |
| 79 | + if (sosService != null) |
| 80 | + sosService.setIpcBroadcast(isChecked); |
| 81 | + }); |
| 82 | + checkSendNet.setOnCheckedChangeListener((buttonView, isChecked) -> { |
| 83 | + updateVisibility(); |
| 84 | + if (sosService != null) |
| 85 | + sosService.setSosServerUrl(isChecked?editSosServerUrl.getText().toString():null); |
| 86 | + }); |
| 87 | + textResult.setMovementMethod(new ScrollingMovementMethod()); |
| 88 | + loadValuesFromPreferences(); |
| 89 | + } |
| 90 | + |
| 91 | + private void sendQuery() { |
| 92 | + if (sosService == null) { |
| 93 | + String url; |
| 94 | + if (checkSendNet.isChecked()) |
| 95 | + url = editSosServerUrl.getText().toString(); |
| 96 | + else |
| 97 | + url = null; |
| 98 | + String username = editSosUsername.getText().toString(); |
| 99 | + String sosPassword = editSosPassword.getText().toString(); |
| 100 | + log("Querying server..."); |
| 101 | + sosService = new SosService(PullExampleActivity.this, null, url, username, sosPassword, true, checkSendIpc.isChecked()); |
| 102 | + } |
| 103 | + sosService.broadcast(new OperationGetCapabilities()); |
| 104 | + } |
| 105 | + |
| 106 | + private final static String PREFS_SEND_IPC = "ipc"; |
| 107 | + private final static String PREFS_SEND_NET = "net"; |
| 108 | + private final static String PREFS_SOS_URL = "url"; |
| 109 | + private final static String PREFS_SOS_USERNAME = "usr"; |
| 110 | + private final static String PREFS_SOS_PASSWORD = "pwd"; |
| 111 | + |
| 112 | + private void loadValuesFromPreferences() { |
| 113 | + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); |
| 114 | + editSosServerUrl.setText(prefs.getString(PREFS_SOS_URL,null)); |
| 115 | + editSosUsername.setText(prefs.getString(PREFS_SOS_USERNAME,null)); |
| 116 | + editSosPassword.setText(prefs.getString(PREFS_SOS_PASSWORD,null)); |
| 117 | + checkSendIpc.setChecked(prefs.getBoolean(PREFS_SEND_IPC,true)); |
| 118 | + checkSendNet.setChecked(prefs.getBoolean(PREFS_SEND_NET,false)); |
| 119 | + } |
| 120 | + |
| 121 | + private void saveValuesToPreferences() { |
| 122 | + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); |
| 123 | + SharedPreferences.Editor edit = prefs.edit(); |
| 124 | + String url = editSosServerUrl.getText().toString(); |
| 125 | + if ((url != null) && (url.length() > 0)) |
| 126 | + edit.putString(PREFS_SOS_URL,url); |
| 127 | + else |
| 128 | + edit.remove(PREFS_SOS_URL); |
| 129 | + String usr = editSosUsername.getText().toString(); |
| 130 | + if ((usr != null) && (usr.length() > 0)) |
| 131 | + edit.putString(PREFS_SOS_USERNAME,usr); |
| 132 | + else |
| 133 | + edit.remove(PREFS_SOS_USERNAME); |
| 134 | + String pwd = editSosPassword.getText().toString(); |
| 135 | + if ((pwd != null) && (pwd.length() > 0)) |
| 136 | + edit.putString(PREFS_SOS_PASSWORD,pwd); |
| 137 | + else |
| 138 | + edit.remove(PREFS_SOS_PASSWORD); |
| 139 | + edit.putBoolean(PREFS_SEND_IPC,checkSendIpc.isChecked()); |
| 140 | + edit.putBoolean(PREFS_SEND_NET,checkSendNet.isChecked()); |
| 141 | + edit.apply(); |
| 142 | + } |
| 143 | + |
| 144 | + private void updateVisibility() { |
| 145 | + if (checkSendNet.isChecked()) { |
| 146 | + editSosServerUrl.setVisibility(View.VISIBLE); |
| 147 | + editSosUsername.setVisibility(View.VISIBLE); |
| 148 | + editSosPassword.setVisibility(View.VISIBLE); |
| 149 | + } else { |
| 150 | + editSosServerUrl.setVisibility(View.GONE); |
| 151 | + editSosUsername.setVisibility(View.GONE); |
| 152 | + editSosPassword.setVisibility(View.GONE); |
| 153 | + } |
| 154 | + checkSendingPrereqs(); |
| 155 | + } |
| 156 | + |
| 157 | + @SuppressLint("SetTextI18n") |
| 158 | + private void checkSendingPrereqs() { |
| 159 | + boolean isEnabled = checkSendIpc.isChecked() || checkSendNet.isChecked(); |
| 160 | + if (isEnabled) { |
| 161 | + String failures = getMissingPrereqs(); |
| 162 | + |
| 163 | + if (failures == null) { |
| 164 | + viewMeasurements.setVisibility(View.VISIBLE); |
| 165 | + sendButton.setEnabled(true); |
| 166 | + textCannotSendWarning.setVisibility(View.GONE); |
| 167 | + } else { |
| 168 | + viewMeasurements.setVisibility(View.GONE); |
| 169 | + sendButton.setEnabled(true); |
| 170 | + textCannotSendWarning.setText(failures); |
| 171 | + textCannotSendWarning.setVisibility(View.VISIBLE); |
| 172 | + } |
| 173 | + } else { |
| 174 | + sendButton.setEnabled(false); |
| 175 | + textCannotSendWarning.setText("Send via IPC or Internet must be checked"); |
| 176 | + textCannotSendWarning.setVisibility(View.VISIBLE); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void onResume() { |
| 182 | + super.onResume(); |
| 183 | + updateVisibility(); |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public void onPause() { |
| 188 | + super.onPause(); |
| 189 | + } |
| 190 | + |
| 191 | + private final static String LINE_SEP = System.getProperty("line.separator"); |
| 192 | + private boolean firstLogLine = true; |
| 193 | + private void log(String text) { |
| 194 | + if (text == null) |
| 195 | + return; |
| 196 | + if (textResult.getText().length() > 2000) |
| 197 | + firstLogLine = true; |
| 198 | + if (firstLogLine) { |
| 199 | + firstLogLine = false; |
| 200 | + textResult.setText(text); |
| 201 | + } else { |
| 202 | + textResult.append(LINE_SEP); |
| 203 | + textResult.append(text); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Gets a message describing anything this sensor is missing that would be required |
| 209 | + * to register with an SOS-T server |
| 210 | + * @return null == all prereqs are complete |
| 211 | + */ |
| 212 | + private String getMissingPrereqs() { |
| 213 | + StringWriter out = null; |
| 214 | + if (checkSendNet.isChecked()) { |
| 215 | + String url = editSosServerUrl.getText().toString(); |
| 216 | + if ((url == null) || (url.length() == 0)) { |
| 217 | + if (out == null) |
| 218 | + out = new StringWriter(); |
| 219 | + else |
| 220 | + out.append(", "); |
| 221 | + out.append("Missing Server URL"); |
| 222 | + } |
| 223 | + } |
| 224 | + if (out == null) |
| 225 | + return null; |
| 226 | + return out.toString(); |
| 227 | + } |
| 228 | + |
| 229 | + @Override |
| 230 | + public void onSosOperationReceived(final AbstractSosOperation operation) { |
| 231 | + if (operation != null) { |
| 232 | + runOnUiThread(() -> { |
| 233 | + log(operation.getClass().getSimpleName() + " received"); |
| 234 | + updateVisibility(); |
| 235 | + }); |
| 236 | + if (operation instanceof OperationGetCapabilitiesResponse) { |
| 237 | + ArrayList<SosSensor> opSensors = ((OperationGetCapabilitiesResponse) operation).getSensors(); |
| 238 | + if ((opSensors != null) && !opSensors.isEmpty()) { |
| 239 | + Log.d(SosIpcTransceiver.TAG,opSensors.size()+" sensors described by server"); |
| 240 | + for (SosSensor sensor:opSensors) { |
| 241 | + //TODO testing |
| 242 | + if ((sensor.getId() != null) && sensor.getId().contains("405-TORGI")) { |
| 243 | + Log.d(SosIpcTransceiver.TAG,"... to include a TORGI sensor. Requesting most recent readings..."); |
| 244 | + sosService.getSensorResultFromServer(sensor); |
| 245 | + SosSensor current = findSensor(sensor); |
| 246 | + if (sensors == null) |
| 247 | + sensors = new ArrayList<>(); |
| 248 | + if (current == null) |
| 249 | + sensors.add(sensor); |
| 250 | + else |
| 251 | + current.update(sensor); |
| 252 | + } |
| 253 | + } |
| 254 | + runOnUiThread(new Runnable() { |
| 255 | + @Override |
| 256 | + public void run() { |
| 257 | + if ((sensors != null) && !sensors.isEmpty()) { |
| 258 | + StringWriter out = new StringWriter(); |
| 259 | + boolean first = true; |
| 260 | + for (SosSensor a:sensors) { |
| 261 | + if (first) |
| 262 | + first = false; |
| 263 | + else |
| 264 | + out.append("\r\n"); |
| 265 | + out.append(a.toString()); |
| 266 | + } |
| 267 | + log(out.toString()); |
| 268 | + } else |
| 269 | + log("No sensor data received"); |
| 270 | + } |
| 271 | + }); |
| 272 | + } |
| 273 | + } |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + private SosSensor findSensor(SosSensor sensor) { |
| 278 | + if ((sensor != null) && (sensors != null) && !sensors.isEmpty()) { |
| 279 | + for (SosSensor current:sensors) { |
| 280 | + if (sensor.isSame(current)) |
| 281 | + return current; |
| 282 | + } |
| 283 | + } |
| 284 | + return null; |
| 285 | + } |
| 286 | + |
| 287 | + @Override |
| 288 | + public void onSosError(final String message) { |
| 289 | + runOnUiThread(() -> { |
| 290 | + log(message); |
| 291 | + }); |
| 292 | + } |
| 293 | + |
| 294 | + @Override |
| 295 | + public void onSosConfigurationSuccess() { |
| 296 | + runOnUiThread(() -> { |
| 297 | + log("SOS configuration was successful"); |
| 298 | + }); |
| 299 | + } |
| 300 | +} |
0 commit comments