Silences HTTPS errors, fixes fluid shots, fixes a bunch of time stamping issues

This commit is contained in:
Patrick McDonagh
2016-11-30 18:15:40 -06:00
parent b1c39e3ee6
commit f9a7bed354

View File

@@ -10,10 +10,16 @@ import requests
import json
import calendar
import pickle
from dateutil import tz
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from requests.packages.urllib3.exceptions import InsecurePlatformWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
requests.packages.urllib3.disable_warnings(InsecurePlatformWarning)
API_HTTP_TYPE = "https"
API_DEVICE_ADDRESS = "casselman16-8.callon.poconsole.net"
API_DEVICE_PORT = 8082
API_DEVICE_PORT = 8081
API_BASE_URL = "{}://{}:{}".format(API_HTTP_TYPE, API_DEVICE_ADDRESS, API_DEVICE_PORT)
@@ -101,7 +107,7 @@ class start(threading.Thread, deviceBase):
self.finished = threading.Event()
threading.Thread.start(self)
self.status_changed = False
self.last_status = ""
self.last_card_send_time = 0
self.al_status_last = False
self.dl_status_last = False
@@ -148,13 +154,13 @@ class start(threading.Thread, deviceBase):
# load stored Fluid Shot ID's
try:
with open('fluidshotIDs.p', 'rb') as handle:
self.fluidshotIDs = pickle.load(handle)
with open('fluidshotIds.p', 'rb') as handle:
self.fluidshotIds = pickle.load(handle)
print "found pickled fluidshotIDs dictionary: {0}".format(self.fluidshotIDs)
print "found pickled fluidshotIDs dictionary: {0}".format(self.fluidshotIds)
except:
print "couldn't load fluid shot ID's from pickle"
self.fluidshotIDs = []
self.fluidshotIds = []
# load stored note ID's
try:
@@ -182,6 +188,7 @@ class start(threading.Thread, deviceBase):
channels["status"]["last_value"] = ""
def run(self):
self.last_status = ""
runLoopStatus = "Startup"
while True:
try:
@@ -305,7 +312,7 @@ class start(threading.Thread, deviceBase):
req_data['objects'].reverse()
for i in range(0,len(req_data['objects'])):
if int(req_data['objects'][i]['_id']) not in self.noteIDs:
timestamp = calendar.timegm(datetime.strptime(req_data['objects'][i]['created_on'], '%Y-%m-%dT%H:%M:%S.%f').timetuple())
timestamp = calendar.timegm(datetime.strptime(req_data['objects'][i]['created_on'], '%Y-%m-%dT%H:%M:%S').timetuple())
self.sendtodbJSON('notes', json.dumps(req_data['objects'][i]), timestamp)
self.noteIDs.append(int(req_data['objects'][i]['_id']))
if len(self.noteIDs) > 20:
@@ -330,8 +337,8 @@ class start(threading.Thread, deviceBase):
req_data['objects'].reverse()
for i in range(0,len(req_data['objects'])):
if int(req_data['objects'][i]['_id']) not in self.fluidshotIds:
timestamp = calendar.timegm(datetime.strptime(req_data['objects'][i]['created_on'], '%Y-%m-%dT%H:%M:%S.%f').timetuple())
self.sendtodbJSON('notes', json.dumps(req_data['objects'][i]), timestamp)
timestamp = calendar.timegm(datetime.strptime(req_data['objects'][i]['created_on'], '%Y-%m-%dT%H:%M:%S').timetuple())
self.sendtodbJSON('fluidshots', json.dumps(req_data['objects'][i]), timestamp)
self.fluidshotIds.append(int(req_data['objects'][i]['_id']))
if len(self.fluidshotIds) > 20:
del self.fluidshotIds[0]
@@ -355,7 +362,7 @@ class start(threading.Thread, deviceBase):
req_data['objects'].reverse()
for i in range(0,len(req_data['objects'])):
if int(req_data['objects'][i]['_id']) not in self.welltestIDs:
timestamp = calendar.timegm(datetime.strptime(req_data['objects'][i]['created_on'], '%Y-%m-%dT%H:%M:%S.%f').timetuple())
timestamp = calendar.timegm(datetime.strptime(req_data['objects'][i]['created_on'], '%Y-%m-%dT%H:%M:%S').timetuple())
self.sendtodbJSON('welltests', json.dumps(req_data['objects'][i]), timestamp)
self.welltestIDs.append(int(req_data['objects'][i]['_id']))
if len(self.welltestIDs) > 20:
@@ -452,6 +459,9 @@ class start(threading.Thread, deviceBase):
api_req = requests.get(url, verify=False)
req_data = json.loads(api_req.text)['objects']
utc_tz = tz.tzutc()
local_tz = tz.tzlocal()
# check the card to see if its new
# 1. if its new send the folder/file_name to the card_history channel
# 2. if its new and its been 10 minutes since you last sent an entire card, then send up all of the data
@@ -460,12 +470,15 @@ class start(threading.Thread, deviceBase):
if current_card['_id'] > self.last_card_id:
#2016-11-23T00:37:02.806026
dt = datetime.strptime(current_card['created_on'], '%Y-%m-%dT%H:%M:%S.%f')
timestamp = calendar.timegm(dt.timetuple())
card_timestamp = int(time.mktime(dt.timetuple()))
dt_utc = dt.replace(tzinfo=utc_tz)
dt_local = dt_utc.astimezone(tz.tzlocal())
print "New card detected @ {0}".format(datetime.strftime(datetime.fromtimestamp(timestamp), "%Y-%m-%dT%H:%M:%S.%f"))
timestamp_utc = calendar.timegm(dt_utc.timetuple())
timestamp_local = calendar.timegm(dt_local.timetuple())
print "New card detected @ {0}".format(datetime.strftime(dt_local, "%Y-%m-%dT%H:%M:%S.%f"))
# set the last value = to current value and upload your data
self.sendtodb("card_history", current_card['_id'], timestamp)
self.sendtodb("card_history", current_card['_id'], timestamp_utc)
self.last_card_id = current_card['_id']
with open('last_card_id.p', 'wb') as handle:
pickle.dump(self.last_card_id, handle)
@@ -474,7 +487,7 @@ class start(threading.Thread, deviceBase):
if (time.time() - self.last_card_send_time) > self.cardLoopTimer or self.status_changed or self.forceSend:
# its been 10 minutes, send the full upload
print "Either status has changed or last stored card is too old."
self.sendtodb("cardtype", current_card['stroke_type'], int(timestamp))
self.sendtodb("cardtype", current_card['stroke_type'], int(timestamp_utc))
# TODO: FIX CARD PARSING
s_p = current_card["surf_pos"].replace("[", "").replace("]", "").split(", ")
@@ -489,26 +502,27 @@ class start(threading.Thread, deviceBase):
if s_p[i] is None:
continue
if s_p[i] != 0.0 and s_l[i] != 0.0:
newSc += "[" + str(round(float(s_p[i]), 3)) + ", " + str(round(float(s_l[i]), 3)) + "], "
newSc += "[" + str(round(float(s_p[i]), 3)) + "," + str(round(float(s_l[i]), 3)) + "],"
except:
pass
newSc += "]"
newSc = newSc[:-1] + "]"
for i in range(len(d_p)):
try:
if d_p[i] is None:
continue
if d_p[i] != 0.0 and d_l[i] != 0.0:
newDc += "[" + str(round(float(d_p[i]), 3)) + ", " + str(round(float(d_l[i]), 3)) + "], "
newDc += "[" + str(round(float(d_p[i]), 3)) + "," + str(round(float(d_l[i]), 3)) + "],"
except:
pass
newDc += "]"
self.sendtodb("surf_pos", current_card["surf_pos"], card_timestamp)
self.sendtodb("surf_lod", current_card["surf_lod"], card_timestamp)
self.sendtodb("down_pos", current_card["down_pos"], card_timestamp)
self.sendtodb("down_lod", current_card["down_lod"], card_timestamp)
self.sendtodb("sc", newSc, card_timestamp)
self.sendtodb("dc", newDc, card_timestamp)
newDc = newDc[:-1] + "]"
self.sendtodb("surf_pos", current_card["surf_pos"], timestamp_utc)
self.sendtodb("surf_lod", current_card["surf_lod"], timestamp_utc)
self.sendtodb("down_pos", current_card["down_pos"], timestamp_utc)
self.sendtodb("down_lod", current_card["down_lod"], timestamp_utc)
self.sendtodb("sc", newSc, timestamp_utc)
self.sendtodb("dc", newDc, timestamp_utc)
except Exception as e:
print "Error during checkLatestCard..."