How to use geolocation

0. Introduction

This easy tutorial helps you to determine if a device is outside the established coordinates.
In this project you will learn how to:

  • Update devices coordinates from a data stream.
  • Determine if a device is outside the established coordinates.

Difficulty:

Music to listen: Justice - Civilization

TOP

1. Create device coordinates

In your device you can configure latitude and longitude:

  • config latitude and longitude

Now you can view device position on the map:

  • device position on the map

TOP

2. Create listener

A listener in Carriots can be associated to any hierarchy entity, from Project to Device. If you associate a listener to a project, all devices below the services of that project will be affected. If you associate a listener to service, all devices below the groups of that services will be affected. And so on.

In this example, we create a listener associated with our device. This listener waits for an event to occur in the context of the device and then evaluates the content.

To create a listener go to the control panel, then to “Rules” → “Listeners” and click on the new button. Select "Device" as "Entity type" and locate your device in the "Id" field.

  • Example listener UpdateGeo

Fill the fields with the following values and save the new listener

  • Name: The name that you prefer
  • Description: The description that you prefer
  • Entity type: Device
  • Entity id: Your id device
  • Event to listen: data_persisted
  • If expression:
    true
  • Then expression:
    import com.carriots.sdk.Device;
    import com.carriots.sdk.utils.Email

    // Update coordinates in device
    def device = Device.find("your_id_developer_device_here")
    device.lat=context.data.lat.toFloat()
    device.lon=context.data.lon.toFloat()
    device.update();

    // check device
    device_in = device.inside(-3.693142, 40.4158134, 1000)


    // send mail if not inside
    if(!device_in) {
    def email = new Email()
    email.to="your_mail_here"
    email.subject="Device outside"
    email.message="Device outside."
    email.send()
    }
  • Then rule: leave empty
  • Else expression: leave empty
  • Else rule: leave empty
  • Be sure to have your listener enabled if you want it to be executed

As you can see here we use the method 'inside' of Device instance. This method is used to determine if a point is within coordinates.

  • Dynamic method: boolean device.inside(float longitude, float latitude, float distance)
  • Static method: boolean Device.inside(float longitude, float latitude, float distance, String id_developer_device)

Its time to test our listener. Send a data stream to this device.

TOP

3. Send data stream with coordinates

Now you can try to send a stream with coordinates.
In your data: {'lat': 40.4158134, 'lon': -3.693142}

Now your listener will be executed and you can view the new position on the map.

  • device updated with new location

Marker color

You can see that the marker on the map changes color depending on device status.

  • Green: OK
  • Orange: Disconnected
  • Grey: No status or data

TOP