The Traccar server component has rather good support for notifying a user of a particular event. For example when a device enters or leaves a geofence or, if the device has support for it, notifying that ignition has been switched. These events can be configured to be issued to Web (meaning the Traccar Web interface where they slide in from below), Mail, or SMS (which in the case of Traccar means a configured SMPP server).

mail

What does a guy do who wants to manipulate notifications and do other things with them? Basically there are two choices:

  • Use an SMPP server which obtains the payload and does something clever with it
  • Configure position and/or event forwarding in Traccar

The former works, and we’ve had that working for the better part of a year, but the latter is a more solid approach.

Traccar can be instructed to submit an HTTP POST whenever it receives a position report from a device and whenever it would otherwise notify one of the built-in methods (mail, Web, SMS). So what I’m going to is to tell Traccar to give me all this data.

traccar to http to mqtt

Whenever Traccar notifies of an event or receives a position, it bundles up some data as JSON and POSTs this to our configured endpoint. An example for an enter event (called geofenceEnter in Traccar-speak) is (slightly shortened):

{
	"geofence": {
		"id": 7,
		"name": "blub9",
		"description": "",
		"area": "CIRCLE (49.133867934876974 8.166520803303387, 33112.6)"
	},
	"position": {
		"id": 18336,
		"attributes": {
			"t": "i",
			"ignition": true,
			"distance": 449672.22
		},
		"deviceId": 7,
		"protocol": "owntracks",
		"deviceTime": "2018-09-14T15:34:17.000+0000",
		"fixTime": "2018-09-14T15:34:17.000+0000",
		"latitude": 49.0156556,
		"longitude": 8.3975169,
		"network": null
	},
	"event": {
		"id": 1216,
		"deviceId": 7,
		"type": "geofenceEnter",
		"serverTime": "2018-09-14T15:34:17.906+0000",
		"positionId": 18336
	},
	"device": {
		"id": 7,
		"attributes": {
			"aaa": "AAAA",
			"mm": "1"
		},
		"name": "Vehicle-54",
		"uniqueId": "q54",
		"status": "online",
		"lastUpdate": "2018-09-14T15:34:17.881+0000",
		"positionId": 18335,
		"geofenceIds": [
			7
		],
		"category": "boat"
	},
	"users": [
		{
			"id": 1,
			"name": "jjolie",
			"login": "",
			"phone": "+49123456",
			"readonly": false,
			"twelveHourFormat": false
		}
	]
}

We then create an HTTP endpoint to which Traccar will transmit the POST requests containing our notification, as it fires. By the way: did you notice that the position was reported via OwnTracks? We submitted an OwnTracks protocol decoder to the Traccar project a year ago, and it can be used directly from the OwnTracks apps in HTTP mode.

enter

The Traccar configuration for this is done in conf/traccar.xml in which I can configure position forwarding and/or event forwarding.

<!-- position forwarding -->
<entry key='forward.enable'>true</entry>
<entry key='forward.json'>true</entry>
<entry key='forward.url'>http://127.0.0.1:8840/evpos</entry>

<!-- event forwarding -->
<entry key="event.forward.enable">true</entry>
<entry key='event.forward.url'>http://127.0.0.1:8840/evpos</entry>

(Until Traccar 4.0 I could add additional parameters to the HTTP POST using event.forward.paramMode.additionalParams, but that feature was silently removed.)

If you prefer, Traccar can forward positions using query parameters: we can configure this by a disabling forward.json and specifying the parameters we’re interested in.

<entry key='forward.enable'>true</entry>
<entry key='forward.url'>http://127.0.0.1:8840/positions?id={uniqueId}&amp;lat={latitude}&amp;lon={longitude}</entry>
<entry key='forward.json'>false</entry>         

(And because I hear you asking: the &amp; are actually required as we’re adding an ampersand between each query parameter and an ampersand is formatted as &amp; in XML.)

The list of possible query parameter values which can be interpolated I’ve taken from the source:

  • {name} is the name of a device
  • {uniqueId} its unique identifier
  • {protocol} the protocol through which a position was reported, e.g. "owntracks"
  • {fixTime} the time of fix
  • {latitude} and {longitude} the latitude, and longitude respectively
  • {altitude}, {speed}, {course}, and {accuracy} should be self-explanatory
  • {address} the reverse-geo-coded address if available

If you configure forward.json to be true, the query-string GET parameters are not substituded; instead a body containing a JSON payload is POSTed to the forward.url.

We have a small utility named from-traccar which implements an HTTP server which republishes incoming positions and events to an MQTT broker.

Why MQTT? Well, because we do lots of good things with MQTT.

GPS, OwnTracks, and Traccar :: 14 Sep 2018 :: e-mail