|
| 1 | +package tech.plugs.torgilistener; |
| 2 | + |
| 3 | +import android.content.BroadcastReceiver; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.Intent; |
| 6 | +import android.util.Log; |
| 7 | + |
| 8 | +import java.io.StringWriter; |
| 9 | +import java.text.ParseException; |
| 10 | +import java.text.SimpleDateFormat; |
| 11 | +import java.util.Date; |
| 12 | + |
| 13 | +/** |
| 14 | + * Implements the OGC Sensor Observation Service and provides a means for other processes |
| 15 | + * to communicate with the service. To use, extend this class then add: |
| 16 | + * |
| 17 | + * <receiver android:name=".YourBroadcastTransceiver"> |
| 18 | + * <intent-filter> |
| 19 | + * <action android:name="org.sofwerx.torgi.ogc.ACTION_SOS"/> |
| 20 | + * </intent-filter> |
| 21 | + * </receiver> |
| 22 | + * |
| 23 | + * to <application> in your manifest. You can send requests to the SOS by using the |
| 24 | + * broadcast message and you will receive responses back through the onMessageReceived method. |
| 25 | + * See the TorgiSOSBroadcastTransceiver for an example of how to use this |
| 26 | + * |
| 27 | + */ |
| 28 | +public abstract class AbstractSOSBroadcastTransceiver extends BroadcastReceiver { |
| 29 | + private final static SimpleDateFormat dateFormatISO8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); |
| 30 | + protected final static String TAG = "OGC.SOS"; |
| 31 | + public static final String ACTION_SOS = "org.sofwerx.torgi.ogc.ACTION_SOS"; |
| 32 | + private static final String EXTRA_PAYLOAD = "SOS"; |
| 33 | + private static final String EXTRA_ORIGIN = "src"; |
| 34 | + |
| 35 | + @Override |
| 36 | + public void onReceive(Context context, Intent intent) { |
| 37 | + if ((context != null) && (intent != null)) { |
| 38 | + if (ACTION_SOS.equals(intent.getAction())) |
| 39 | + onMessageReceived(context,intent.getStringExtra(EXTRA_ORIGIN),intent.getStringExtra(EXTRA_PAYLOAD)); |
| 40 | + else |
| 41 | + Log.e(TAG, "Unexpected action message received: " + intent.getAction()); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Broadcast an SOS Operation |
| 47 | + * @param context |
| 48 | + * @param sosOperation |
| 49 | + */ |
| 50 | + public static void broadcast(Context context, String sosOperation) { |
| 51 | + if (context == null) { |
| 52 | + Log.d(TAG,"Context needed to broadcast an SOS Operation"); |
| 53 | + return; |
| 54 | + } |
| 55 | + if (sosOperation == null) { |
| 56 | + Log.e(TAG, "Cannot broadcast an empty SOS Operation"); |
| 57 | + return; |
| 58 | + } |
| 59 | + Intent intent = new Intent(ACTION_SOS); |
| 60 | + intent.putExtra(EXTRA_ORIGIN, BuildConfig.APPLICATION_ID); |
| 61 | + intent.putExtra(EXTRA_PAYLOAD,sosOperation); |
| 62 | + |
| 63 | + context.sendBroadcast(intent); |
| 64 | + Log.d(TAG,"Broadcast: "+sosOperation); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Handles the message received from the SOS Broadcast; extend this method to handle the input |
| 69 | + * @param source the application that sent the request; mostly to prevent circular reporting |
| 70 | + * @param input the SOS operation received |
| 71 | + */ |
| 72 | + public void onMessageReceived(Context context,String source,String input) { |
| 73 | + if (source == null) { |
| 74 | + Log.e(TAG,"SOS broadcasts from anonymous senders is not supported"); |
| 75 | + return; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public final static String getOperationDescribeSensor() { |
| 80 | + return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><DescribeSensor version=\"1.0.0\" service=\"SOS\" mobileEnabled=\"true\" xmlns=\"http://www.opengis.net/sos/1.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.opengis.net/sos/1.0 http://schemas.opengis.net/sos/1.0.0/sosDescribeSensor.xsd\" outputFormat=\"text/xml;subtype="sensorML/1.0.1"\"><procedure>urn:ogc:object:feature:Sensor:IFGI:ifgi-sensor-1</procedure></DescribeSensor>"; |
| 81 | + } |
| 82 | + |
| 83 | + public final static String getOperationGetCapabilities() { |
| 84 | + return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><GetCapabilities xmlns=\"http://www.opengis.net/sos/1.0\" xmlns:ows=\"http://www.opengis.net/ows/1.1\" xmlns:ogc=\"http://www.opengis.net/ogc\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.opengis.net/sos/1.0http://schemas.opengis.net/sos/1.0.0/sosGetCapabilities.xsd\" service=\"SOS\" updateSequence=\"\"><ows:AcceptVersions><ows:Version>1.0.0</ows:Version></ows:AcceptVersions><ows:Sections><ows:Section>OperationsMetadata</ows:Section><ows:Section>ServiceIdentification</ows:Section><ows:Section>Filter_Capabilities</ows:Section><ows:Section>Contents</ows:Section></ows:Sections></GetCapabilities>"; |
| 85 | + } |
| 86 | + |
| 87 | + public static long parseTime(String time) { |
| 88 | + if (time != null) { |
| 89 | + try { |
| 90 | + Date date = dateFormatISO8601.parse(time); |
| 91 | + return date.getTime(); |
| 92 | + } catch (ParseException e) { |
| 93 | + e.printStackTrace(); |
| 94 | + } |
| 95 | + } |
| 96 | + return Long.MIN_VALUE; |
| 97 | + } |
| 98 | + |
| 99 | + public static String formatTime(long time) { |
| 100 | + return dateFormatISO8601.format(time); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Gets the recent observations |
| 105 | + * @return |
| 106 | + */ |
| 107 | + public static String getOperationGetObservations() { |
| 108 | + return getOperationGetObservations(Long.MIN_VALUE, Long.MIN_VALUE); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Gets a range of observations |
| 113 | + * @param start start time |
| 114 | + * @param end end time |
| 115 | + * @return |
| 116 | + */ |
| 117 | + public static String getOperationGetObservations(long start, long end) { |
| 118 | + StringWriter writer = new StringWriter(); |
| 119 | + writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + |
| 120 | + "<GetObservation xmlns=\"http://www.opengis.net/sos/1.0\" xmlns:ows=\"http://www.opengis.net/ows/1.1\" xmlns:gml=\"http://www.opengis.net/gml\" xmlns:ogc=\"http://www.opengis.net/ogc\" xmlns:om=\"http://www.opengis.net/om/1.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.opengis.net/sos/1.0\n" + |
| 121 | + "http://schemas.opengis.net/sos/1.0.0/sosGetObservation.xsd\" service=\"SOS\" version=\"1.0.0\" srsName=\"urn:ogc:def:crs:EPSG:4326\">\n" + |
| 122 | + " <offering>TORGI</offering>\n"); //TODO change this type of offering |
| 123 | + if (end > Long.MIN_VALUE) { |
| 124 | + writer.append(" <eventTime>\n" + |
| 125 | + " <ogc:TM_During>\n" + |
| 126 | + " <ogc:PropertyName>urn:ogc:data:time:iso8601</ogc:PropertyName>\n" + |
| 127 | + " <gml:TimePeriod>\n" + |
| 128 | + " <gml:beginPosition>"); |
| 129 | + writer.append(formatTime(start)); |
| 130 | + writer.append("</gml:beginPosition>\n" + |
| 131 | + " <gml:endPosition>"); |
| 132 | + writer.append(formatTime(end)); |
| 133 | + writer.append("</gml:endPosition>\n" + |
| 134 | + " </gml:TimePeriod>\n" + |
| 135 | + " </ogc:TM_During>\n" + |
| 136 | + " </eventTime>\n"); |
| 137 | + } |
| 138 | + //TODO need to implement " <procedure>urn:ogc:object:feature:Sensor:IFGI:ifgi-sensor-1</procedure>\n" + |
| 139 | + //TODO need to implement " <observedProperty>urn:ogc:def:phenomenon:OGC:1.0.30:waterlevel</observedProperty>\n" + |
| 140 | + writer.append(" <responseFormat>text/xml;subtype="om/1.0.0"</responseFormat>\n" + |
| 141 | + "</GetObservation>"); |
| 142 | + |
| 143 | + return writer.toString(); |
| 144 | + } |
| 145 | +} |
0 commit comments