Skip to content

Commit

Permalink
JSON response Updated
Browse files Browse the repository at this point in the history
timestamp and accuracy added in JSON response
  • Loading branch information
umer0586 committed Jan 17, 2022
1 parent 7901461 commit 9e1a55b
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -30,6 +31,7 @@ public class SensorWebSocketServer extends WebSocketServer implements SensorEven

private int sensorDelay = SensorManager.SENSOR_DELAY_FASTEST;
private static final String CONNECTION_PATH = "/sensor/connect";
private static final HashMap<String,Object> response = new HashMap<>();

private SensorManager sensorManager;
private SensorUtil sensorUtil;
Expand Down Expand Up @@ -267,14 +269,23 @@ public void run()
@Override
public void onSensorChanged(SensorEvent sensorEvent)
{
response.clear();

// Loop through each connected client
for( WebSocket webSocket : getConnections())
{

// Send data as per sensor type requested by client
if( ((Sensor) webSocket.getAttachment()) !=null )
if( ((Sensor) webSocket.getAttachment()).getType() == sensorEvent.sensor.getType() && !webSocket.isClosing())
webSocket.send(JsonUtil.toJSON(sensorEvent.values));
{
response.put("values", sensorEvent.values);
response.put("timestamp",sensorEvent.timestamp);
response.put("accuracy",sensorEvent.accuracy);

webSocket.send(JsonUtil.toJSON(response));
}

}
}

Expand Down

0 comments on commit 9e1a55b

Please sign in to comment.