Skip to content

Commit

Permalink
added threading, elapsed time to switch out of manual to autonomanual…
Browse files Browse the repository at this point in the history
… mode
  • Loading branch information
Richard Stanley committed Jan 6, 2018
1 parent 425bfc0 commit cfa134d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 27 deletions.
98 changes: 71 additions & 27 deletions rover/core/servers/iftop/iftop.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,85 @@
#!/usr/bin/python
from time import sleep
from time import sleep, time
from threading import Thread
from subprocess import call, Popen
from deepstream import post
import subprocess, re

recordName = "speed"
interface = "wlp3s0"

# Number in seconds to switch to autonomous/manual Mode
elapsedTimout = 16

obj = {}
post(obj, recordName)

while True:
success = ""
while success == "":
try:
p = Popen([ "iftop", "-o", "10s", "-t", "-s", "10", "-L", "2", "-i", interface ], stdout=subprocess.PIPE)
out, err = p.communicate()
uploadArr = re.findall(r"Total send rate:\s+(\d{1,}\.{0,1}\d{0,})(\w+)", out)
downloadArr = re.findall(r"Total receive rate:\s+(\d{1,}\.{0,1}\d{0,})(\w+)", out)
ipAddress = re.findall(r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", out)[0]
upload = 0
download = 0
if uploadArr is not [] and downloadArr is not []:
upload = float(uploadArr[0][0])
download = float(downloadArr[0][0])
if uploadArr[0][1] == "Kb":
upload = upload * 1000
if downloadArr[0][1] == "Kb":
download = download * 1000
if uploadArr[0][1] == "Mb":
upload = upload * 1000000
if downloadArr[0][1] == "Mb":
download = download * 1000000
print "Upload: {} {} Download: {} {} IP: {}".format(upload, uploadArr[0][1], download , downloadArr[0][1], ipAddress)
post({"upload": upload, "download": download, "ip": ipAddress}, recordName)
success = post(obj, recordName)
success = post({"mode": "manual"}, "mode")
except:
print("Not connected to deepstream")

ipAddress = " NOREC"
upload = 0
download = 0
elapsedTime = 0


def getUpDownData():
global obj, ipAddress, upload, download, elapsedTime
while True:
try:
post({}, "speed")
print("No data from interface: " + interface)
sleep(1)
# command: sudo iftop -o 10s -t -s 10 -L 1 -i wlp3s0
elapsedTime = 0
p = Popen([ "iftop", "-o", "10s", "-t", "-s", "10", "-L", "1", "-i", interface ], stdout=subprocess.PIPE)
out, err = p.communicate()
print(elapsedTime)

uploadArr = re.findall(r"Total send rate:\s+(\d{1,}\.{0,1}\d{0,})(\w+)", out)
downloadArr = re.findall(r"Total receive rate:\s+(\d{1,}\.{0,1}\d{0,})(\w+)", out)
ipAddressArr = re.findall(r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", out)
if ipAddressArr is not []:
ipAddress = ipAddressArr[0]

if uploadArr is not [] and downloadArr is not []:
upload = float(uploadArr[0][0])
download = float(downloadArr[0][0])
if uploadArr[0][1] == "Kb":
upload = upload * 1000
if downloadArr[0][1] == "Kb":
download = download * 1000
if uploadArr[0][1] == "Mb":
upload = upload * 1000000
if downloadArr[0][1] == "Mb":
download = download * 1000000
print "upload: {} {} download: {} {} ip: {}".format(upload, uploadArr[0][1], download , downloadArr[0][1], ipAddress)
post({"upload": upload, "download": download, "ip": ipAddress, "elapsed": elapsedTime}, recordName)
except:
print("cannot connect to deepstream.")
try:
post({}, "speed")
print("No data from interface: " + interface)
sleep(1)
except:
print("cannot connect to deepstream.")

def checkElapsedTime():
global elapsedTime
while True:
elapsedTime = elapsedTime + 1
if(elapsedTime > 16):
try:
post({ "mode": "autonomanual" }, "mode")
upload = 0
download = 0
post({"upload": upload, "download": download, "ip": ipAddress, "elapsed": elapsedTime}, recordName)
except:
print("cannot post to deepstream")
sleep(1)

t1 = Thread(target=getUpDownData)
t2 = Thread(target=checkElapsedTime)

t1.start()
t2.start()
13 changes: 13 additions & 0 deletions rover/core/servers/iftop/listenToSpeedAndModeRecords.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from deepstream import get, post
from time import sleep

while True:
try:
iftop = get("speed")
sleep(.1)
mode = get("mode")
print(iftop)
print(mode)
sleep(1)
except:
print("problem connecting to deepstream")

0 comments on commit cfa134d

Please sign in to comment.