Close

Messaging Formats: Parsing JSON alert message

A project log for Elephant AI

a system to prevent human-elephant conflict by detecting elephants using machine vision, and warning humans and/or repelling elephants

neil-k-sheridanNeil K. Sheridan 09/21/2017 at 18:590 Comments

Here I've got an example elephant detection alert message in JSON format that I import, parse/de-serialise, then it's ready to be sent out to system users via either SMS, or twitter (as is shown here):

#working with elephant alert messages using JSON
#version 1.0 (21/9/2017)
# JSON represents data as nested "lists" and "dictionaries"
# unlike XML which is trees
import json
#open the json file
with open('elephants.json', 'r') as f:
    data = json.load(f)
#printing out all the values from the json for debug purposes
print 'Spotted:',data["spotted"]
print 'Time:',data["time"]
print 'Date:',data["date"]
print 'Location:',data["location"]
print 'Lat:',data["latitude"]
print 'Lon:',data["longitude"]
print 'Lone Male?:',data["details"]["lonemale"]
print 'Herd?:',data["details"]["herd"]
print 'Calves?:',data["details"]["calves"]
print 'Deter done?:',data["deter"]["deterdone"]
print 'Audio deter?:',data["deter"]["deteraudio"]
print 'Physical deter?:',data["deter"]["deterphysical"]
print 'Battery voltage:',data["battery"]
# here we can de-serialize the fields ready  for sending out to system
# users via SMS, twitter, etc  or working with the values for something else
# remember that the deter devices, and the web server/server will be 
# sent a copy of the JSON file we put together earlier. 
#location, time, date, lat/lon
spotted = data["spotted"]
# yes or no for spotted which is maybe a bit redundant since we wouldn't
# get an alert if it was "no"!
location = data["location"]
time = data["time"]
date = data["date"]
latitude = data["latitude"]
longitude = data["longitude"]
# no point in doing if else etc here since it could be all of them!
# elephant details
lone_male = data["details"]["lonemale"]
herd = data["details"]["herd"]
calves = data["details"]["calves"]
#deter details
deter = data["deter"]["deterdone"]
audiodeter = data["deter"]["deteraudio"]
physicaldeter = data["deter"]["deterphysical"]
#battery status
batteryvoltage = data["battery"]
## Sending these out to twitter using Twython
from twython import Twython
# Put the required variables into our twitter message
# i.e. in this example time, location, and if deter occurred
message = "Spotted! Time: %s | Location: %s | Deter? %s |" % (time, location,
deter)
C_KEY = 'xxx'
C_SECRET = 'xxx'
A_TOKEN = 'xxx'
A_SECRET = 'xxx'
api = Twython(C_KEY, C_SECRET, A_TOKEN, A_SECRET)
api.update_status(status=message)

Here's our tweet that resulted!

We may instead send via DM for security reasons. That's accomplished with the following code:

api.send_direct_message(screen_name="name",text=message)

We can also upload the photo which contains the detected elephant to twitter using Twython too! But that is probably not required, and may constitute a security issue related to giving information on location to poachers. 

Discussions