-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d76fb85
commit 1b76cf3
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
''' | ||
Richard Stanley | ||
TitanRover 2017 | ||
audstanley@gmail.com | ||
''' | ||
import requests | ||
import json | ||
import commands | ||
|
||
|
||
roverIp = "192.168.1.2" # This ip will change periodically, | ||
# for now, this is the ip of the rover on openvpn | ||
|
||
|
||
try: | ||
if "titan" in commands.getoutput("iwgetid -r"): | ||
roverIp = "192.168.1.2" | ||
#print('Your Deepstream IP address is : ' + str(roverIp)) | ||
elif "00:24:B2:CA:8B:86" in commands.getoutput("nmap -sP 192.168.1.1"): | ||
roverIp = "192.168.1.2" | ||
#print('Your Deepstream IP address is : ' + str(roverIp)) | ||
else: | ||
roverIp = "127.0.0.1" | ||
#print('Your Deepstream IP address is : ' + str(roverIp)) | ||
except: | ||
roverIp = "127.0.0.1" | ||
|
||
|
||
def get(recordName): | ||
''' | ||
The get function will get the entire record "rover/" + recordName | ||
and returns the record as an object. | ||
Possible return Errors: | ||
If the record does not exists in DeepStream, returns "NO_RECORD" | ||
Else there is no connection to DeepStream, returns "NO_DEEPSTREAM" | ||
''' | ||
response = None | ||
if type(recordName) is not str: | ||
raise "Your argument needs to be a string when getting from deepstream" | ||
payload = {"body":[{"topic": "record", "action":"read", "recordName": "rover/" + recordName}]} | ||
request = requests.post('http://' + roverIp + ':4080', json=payload) | ||
if type(request.content) is bytes: | ||
response = json.loads(request.content.decode('utf-8')) | ||
elif type(request.content) is str: | ||
response = json.loads(request.content) | ||
|
||
if response["result"] == "SUCCESS": | ||
return response["body"][0]["data"] | ||
elif response["result"] == "FAILURE": | ||
return "NO_RECORD" | ||
else: | ||
return "NO_DEEPSTREAM" | ||
|
||
|
||
def post(obj, recordName): | ||
''' | ||
This function will post the object sen to the deepstream server. | ||
Arguments: | ||
obj: an object that you want to post to deepstream | ||
recordName: the name of the record that you want to post to | ||
''' | ||
if type(recordName) is not str: | ||
raise "Your second argument needs to be a string when setting data to deepstream" | ||
if type(obj) is not dict: | ||
raise "Your first argument needs to be a dict setting data to deepstream" | ||
payload = {"body":[{"topic": "record", "action":"write", "recordName": "rover/" + recordName, "data": obj}]} | ||
request = requests.post('http://' + roverIp + ':4080', json=payload) | ||
if request is not None: | ||
if type(request) is bytes: | ||
request = request.decode('utf-8') | ||
response = request.json() | ||
return response["result"] | ||
else: | ||
return "NO_DEEPSTREAM" |