How to parse a string parameter as JSON with a PVM plugin?

Example of using a plugin with PVM code to extract string parameters from GPS tracker message and save them as a part of flespi device message JSON.

Data sent by GPS trackers can contain custom string values that cannot be parsed directly following the standard protocol specification. In such cases, you may use plugins to set up custom parsing and add the extra parameters into the JSON message. Here’s how to do it.

Raw packet

Here’s what a packet with string parameters can look like (this one is from Queclink device):

+RESP:GTDAT,270A01,867162027101111,,1,,,{"fleet_id":4,"fleet_owner":"octopus llc"},0,57.3,34,150.8,-72.450719,-35.387515,20220210165845,,,,,,,,,,20220210165847,C17B$

Channel message

The above packet will be handled by flespi and converted into the following channel message (note the payload.text parameter with the target string values):

{
  "channel.id": 8,
  "ident": "867162027101111",
  "message.buffered.status": false,
  "payload.text": "{\"fleet_id\":4,\"fleet_owner\":\"octopus llc\"}",
  "peer": "127.0.0.1:42040",
  "position.altitude": 150.8,
  "position.direction": 34,
  "position.latitude": -35.387515,
  "position.longitude": -72.450719,
  "position.speed": 57.3,
  "position.timestamp": 1644512325,
  "protocol.id": 33,
  "record.seqnum": 49531,
  "report.code": "GTDAT",
  "server.timestamp": 1645536827.034753,
  "timestamp": 1644512327,
  "timestamp.key": 1644512327.49531
}

PVM plugin configuration

To arrange custom parsing of the payload.text parameter, we need a “msg-pvm-code” plugin with the following script:

create pvm code plugin

Here’s the PVM code in plain text in case you decide to copy it:

optional .payload.text ==> %text ==> json[object]:
.fleet_id ==> #corporate.fleet.id
.fleet_owner ==> #corporate.fleet.owner
unset .payload.text

You can learn more about PVM syntax in a comprehensive article.

Assign the plugin to a device

Remember, plugins apply on device level only. So we need to assign the plugin to the device(s):

Device message

Here’s the resulting flespi device message with the parameters carefully parsed in injected:

{
  "channel.id": 8,
  "corporate.fleet.id": 4,
  "corporate.fleet.owner": "octopus llc",

  "device.id": 1542,
  "device.name": "Queclink GV300W",
  "device.type.id": 150,
  "ident": "867162027101111",
  "message.buffered.status": false,
  "peer": "127.0.0.1:42040",
  "position.altitude": 150.8,
  "position.direction": 34,
  "position.latitude": -35.387515,
  "position.longitude": -72.450719,
  "position.speed": 57.3,
  "position.timestamp": 1644512325,
  "protocol.id": 33,
  "record.seqnum": 49531,
  "report.code": "GTDAT",
  "server.timestamp": 1645536827.034753,
  "timestamp": 1644512327,
  "timestamp.key": 1644512327.49531
}

Now, you can consume these messages using the devices REST API.


See also
Using a webhook plugin to make requests to the script on your custom server to modify messages.