GeoEvent Websocket

1028
3
06-08-2017 06:09 AM
JohnMcoy
New Contributor III

Hi, so I`m working with GeoEvent task. And I stuck with point simulation. How to specify that, InputStream ins takes geometry points from uri, not from file which is local in device. I have this code now: 

import android.graphics.Color;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;

import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "ChangeViewPoint";
    private WebSocketClient mWebSocketClient;
    private MapView mMapView;
    private SpatialReference spatialReference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        connectWebSocket();

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .commit();
        }

        // inflate MapView from layout
        mMapView = (MapView) findViewById(R.id.mapView);
        // create a map with the BasemapType topographic
        ArcGISMap map = new ArcGISMap(Basemap.Type.OCEANS, 56.075844, -2.681572, 11);
        // set the map to be displayed in this view
        mMapView.setMap(map);

        spatialReference = SpatialReference.create(4326);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
            case R.id.action_settings:
                return true;
        }
        return super.onOptionsItemSelected(item);
    }


    private void connectWebSocket() {
        URI uri;
        try {
            uri = new URI("wss:/*******/StreamServer");
        } catch (URISyntaxException e) {
            e.printStackTrace();
            return;
        }

        mWebSocketClient = new WebSocketClient(uri) {
            @Override
            public void onOpen(ServerHandshake serverHandshake) {
                Log.i("Websocket", "Opened");
                mWebSocketClient.send("Hello from " + Build.MANUFACTURER + " " + Build.MODEL);
            }

            @Override
            public void onMessage(String s) {
                final String message = s;

                InputStream ins = getResources().openRawResource(
                        getResources().getIdentifier("griffithparkjson",
                                "raw", getPackageName()));

                InputStreamReader inputReader = new InputStreamReader(ins);

                BufferedReader bufferReader = new BufferedReader(inputReader);
                String line;
                StringBuilder text = new StringBuilder();

                // read the text file
                try {
                    while (( line = bufferReader.readLine()) != null) {
                        text.append(line);
                    }
                } catch (IOException e) {
                    Log.d(TAG, e.toString());
                }
                String JsonString = text.toString();

                // create Geometry from JSON
                Geometry geometry = Geometry.fromJson(JsonString, spatialReference);
                GraphicsOverlay overlay = new GraphicsOverlay();
                // add graphics overlay on map view
                mMapView.getGraphicsOverlays().add(overlay);
                SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.DIAGONAL_CROSS, Color.GREEN, null);
                // add graphic of Griffith Park
                overlay.getGraphics().add(new Graphic(geometry, fillSymbol));

                // set viewpoint of map view to Geometry - Griffith Park
                mMapView.setViewpointGeometryAsync(geometry);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        TextView textView = (TextView)findViewById(R.id.messages);
                        textView.setText(textView.getText() + "\n" + message);
                    }
                });
            }

            @Override
            public void onClose(int i, String s, boolean b) {
                Log.i("Websocket", "Closed " + s);
            }

            @Override
            public void onError(Exception e) {
                Log.i("Websocket", "Error " + e.getMessage());
            }
        };
        mWebSocketClient.connect();
    }

    public void sendMessage(View view) {
        EditText editText = (EditText)findViewById(R.id.message);
        mWebSocketClient.send(editText.getText().toString());
        editText.setText("");
    }

    @Override
    protected void onPause(){
        super.onPause();
        mMapView.pause();
    }

    @Override
    protected void onResume(){
        super.onResume();
        mMapView.resume();
    }
}

Please, help to solve it. It would be very helpful.

0 Kudos
3 Replies
AlexanderNohe1
Occasional Contributor III

I believe your data is coming in through:

 @Override
            public void onMessage(String s)

So use the s variable to pass that along to geometry.  Ensure that the s variable is properly formatted to be handled by the geometry object otherwise, you will not see the results you expect.

0 Kudos
JohnMcoy
New Contributor III

So I have to work with this part of code ?  

       mWebSocketClient = new WebSocketClient(uri) {
            @Override
            public void onOpen(ServerHandshake serverHandshake) {
                Log.i("Websocket", "Opened");
                mWebSocketClient.send("Hello from " + Build.MANUFACTURER + " " + Build.MODEL);
            } 

            @Override
            public void onMessage(String s) {
                final String message = s;

                InputStream ins = getResources().openRawResource(
                        getResources().getIdentifier("griffithparkjson",
                                "raw", getPackageName()));

                InputStreamReader inputReader = new InputStreamReader(ins);

                BufferedReader bufferReader = new BufferedReader(inputReader);
                String line;
                StringBuilder text = new StringBuilder();

                // read the text file
                try {
                    while (( line = bufferReader.readLine()) != null) {
                        text.append(line);
                    }
                } catch (IOException e) {
                    Log.d(TAG, e.toString());
                }
                String JsonString = text.toString();

                // create Geometry from JSON
                Geometry geometry = Geometry.fromJson(JsonString, spatialReference);
                GraphicsOverlay overlay = new GraphicsOverlay();
                // add graphics overlay on map view
                mMapView.getGraphicsOverlays().add(overlay);
                SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.DIAGONAL_CROSS, Color.GREEN, null);
                // add graphic of Griffith Park
                overlay.getGraphics().add(new Graphic(geometry, fillSymbol));

                // set viewpoint of map view to Geometry - Griffith Park
                mMapView.setViewpointGeometryAsync(geometry);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        TextView textView = (TextView)findViewById(R.id.messages);
                        textView.setText(textView.getText() + "\n" + message);
                    }
                });
            }

            @Override
            public void onClose(int i, String s, boolean b) {
                Log.i("Websocket", "Closed " + s);
            }

            @Override
            public void onError(Exception e) {
                Log.i("Websocket", "Error " + e.getMessage());
            }
        };
        mWebSocketClient.connect();
    }
0 Kudos
AlexanderNohe1
Occasional Contributor III

Correct.  The onMessage should contain the data coming from the service.  Additionally, I have not tested this type of implementation before, so I wish all the best in pursuing this and hope you find some satisfactory results.