keep the driver up and running if something fails
This commit is contained in:
@@ -267,167 +267,194 @@ class start(threading.Thread, deviceBase):
|
||||
time.sleep(sleep_timer)
|
||||
|
||||
def checkBackup(self):
|
||||
backupList = json.loads(requests.get(self.device_address + "/json/backups").text)
|
||||
file = backupList["backups"][0]
|
||||
data = json.loads(requests.get(self.device_address + "/json/backups/" + file).text)
|
||||
timestamp = time.time()
|
||||
if data != self.wellSetup or self.forceSend:
|
||||
self.sendtodbJSON("well_setup", json.dumps(data), timestamp)
|
||||
self.wellSetup = data
|
||||
with open('wellSetup.p', 'wb') as handle:
|
||||
pickle.dump(self.wellSetup, handle)
|
||||
try:
|
||||
backupList = json.loads(requests.get(self.device_address + "/json/backups").text)
|
||||
file = backupList["backups"][0]
|
||||
data = json.loads(requests.get(self.device_address + "/json/backups/" + file).text)
|
||||
timestamp = time.time()
|
||||
if data != self.wellSetup or self.forceSend:
|
||||
self.sendtodbJSON("well_setup", json.dumps(data), timestamp)
|
||||
self.wellSetup = data
|
||||
with open('wellSetup.p', 'wb') as handle:
|
||||
pickle.dump(self.wellSetup, handle)
|
||||
except Exception, e:
|
||||
print("checkBackup Error: {}".format(e))
|
||||
|
||||
def checkEvents(self):
|
||||
data = json.loads(requests.get(self.device_address + "/json/event_list").text)
|
||||
events = data["events"]
|
||||
for event in events:
|
||||
if int(event["id"]) not in self.eventIds:
|
||||
timestamp = event["datetime"]
|
||||
# we have a new event
|
||||
self.sendtodbJSON("events", json.dumps(event), timestamp)
|
||||
self.eventIds.append(int(event["id"]))
|
||||
if len(self.eventIds) > 50:
|
||||
del self.eventIds[0]
|
||||
with open('eventIds.p', 'wb') as handle:
|
||||
pickle.dump(self.eventIds, handle)
|
||||
try:
|
||||
data = json.loads(requests.get(self.device_address + "/json/event_list").text)
|
||||
events = data["events"]
|
||||
for event in events:
|
||||
if int(event["id"]) not in self.eventIds:
|
||||
timestamp = event["datetime"]
|
||||
# we have a new event
|
||||
self.sendtodbJSON("events", json.dumps(event), timestamp)
|
||||
self.eventIds.append(int(event["id"]))
|
||||
if len(self.eventIds) > 50:
|
||||
del self.eventIds[0]
|
||||
with open('eventIds.p', 'wb') as handle:
|
||||
pickle.dump(self.eventIds, handle)
|
||||
except Exception, e:
|
||||
print("checkEvents Error: {}".format(e))
|
||||
|
||||
def checkNotes(self):
|
||||
data = json.loads(requests.get(self.device_address + "/json/notes/get").text)
|
||||
notes = data["notes"]
|
||||
for note in notes:
|
||||
if int(note["id"]) not in self.noteIDs:
|
||||
timestamp = calendar.timegm(time.strptime(note["date_time"], '%Y-%m-%d %H:%M:%S'))
|
||||
# we have a new note
|
||||
self.sendtodbJSON("notes", json.dumps(note), timestamp)
|
||||
self.noteIDs.append(int(note["id"]))
|
||||
if len(self.noteIDs) > 50:
|
||||
del self.noteIDs[0]
|
||||
with open('noteIDs.p', 'wb') as handle:
|
||||
pickle.dump(self.noteIDs, handle)
|
||||
try:
|
||||
data = json.loads(requests.get(self.device_address + "/json/notes/get").text)
|
||||
notes = data["notes"]
|
||||
for note in notes:
|
||||
if int(note["id"]) not in self.noteIDs:
|
||||
timestamp = calendar.timegm(time.strptime(note["date_time"], '%Y-%m-%d %H:%M:%S'))
|
||||
# we have a new note
|
||||
self.sendtodbJSON("notes", json.dumps(note), timestamp)
|
||||
self.noteIDs.append(int(note["id"]))
|
||||
if len(self.noteIDs) > 50:
|
||||
del self.noteIDs[0]
|
||||
with open('noteIDs.p', 'wb') as handle:
|
||||
pickle.dump(self.noteIDs, handle)
|
||||
except Exception, e:
|
||||
print("checkNotes Error: {}".format(e))
|
||||
|
||||
def checkFluidShots(self):
|
||||
data = json.loads(requests.get(self.device_address + "/json/fluid_shot/get").text)
|
||||
fluid_shots = data["fluid_shots"]
|
||||
for shot in fluid_shots:
|
||||
if int(shot["id"]) not in self.fluidshotIDs:
|
||||
timestamp = calendar.timegm(time.strptime(shot["shot_datetime"], '%Y-%m-%d %H:%M:%S'))
|
||||
# we have a new note
|
||||
self.sendtodbJSON("fluidshots", json.dumps(shot), timestamp)
|
||||
self.fluidshotIDs.append(int(shot["id"]))
|
||||
if len(self.fluidshotIDs) > 50:
|
||||
del self.fluidshotIDs[0]
|
||||
with open('fluidshotIDs.p', 'wb') as handle:
|
||||
pickle.dump(self.fluidshotIDs, handle)
|
||||
try:
|
||||
data = json.loads(requests.get(self.device_address + "/json/fluid_shot/get").text)
|
||||
fluid_shots = data["fluid_shots"]
|
||||
for shot in fluid_shots:
|
||||
if int(shot["id"]) not in self.fluidshotIDs:
|
||||
timestamp = calendar.timegm(time.strptime(shot["shot_datetime"], '%Y-%m-%d %H:%M:%S'))
|
||||
# we have a new note
|
||||
self.sendtodbJSON("fluidshots", json.dumps(shot), timestamp)
|
||||
self.fluidshotIDs.append(int(shot["id"]))
|
||||
if len(self.fluidshotIDs) > 50:
|
||||
del self.fluidshotIDs[0]
|
||||
with open('fluidshotIDs.p', 'wb') as handle:
|
||||
pickle.dump(self.fluidshotIDs, handle)
|
||||
except Exception, e:
|
||||
print("checkFluidShots Error: {}".format(e))
|
||||
|
||||
def checkWellTests(self):
|
||||
data = json.loads(requests.get(self.device_address + "/json/well_test/get").text)
|
||||
well_tests = data["well_tests"]
|
||||
for test in well_tests:
|
||||
if int(test["id"]) not in self.welltestIDs:
|
||||
timestamp = calendar.timegm(time.strptime(test["test_date"], '%Y-%m-%d %H:%M:%S'))
|
||||
# we have a new note
|
||||
self.sendtodbJSON("welltests", json.dumps(test), timestamp)
|
||||
self.welltestIDs.append(int(test["id"]))
|
||||
if len(self.welltestIDs) > 50:
|
||||
del self.welltestIDs[0]
|
||||
with open('welltestIDs.p', 'wb') as handle:
|
||||
pickle.dump(self.welltestIDs, handle)
|
||||
try:
|
||||
data = json.loads(requests.get(self.device_address + "/json/well_test/get").text)
|
||||
well_tests = data["well_tests"]
|
||||
for test in well_tests:
|
||||
if int(test["id"]) not in self.welltestIDs:
|
||||
timestamp = calendar.timegm(time.strptime(test["test_date"], '%Y-%m-%d %H:%M:%S'))
|
||||
# we have a new note
|
||||
self.sendtodbJSON("welltests", json.dumps(test), timestamp)
|
||||
self.welltestIDs.append(int(test["id"]))
|
||||
if len(self.welltestIDs) > 50:
|
||||
del self.welltestIDs[0]
|
||||
with open('welltestIDs.p', 'wb') as handle:
|
||||
pickle.dump(self.welltestIDs, handle)
|
||||
except Exception, e:
|
||||
print("checkWellTests Error: {}".format(e))
|
||||
|
||||
def checkStatus(self):
|
||||
global status
|
||||
statusMap = {
|
||||
0: 'Stopped',
|
||||
1: 'Running',
|
||||
2: 'Pumped Off',
|
||||
3: 'Faulted',
|
||||
4: 'Starting',
|
||||
5: 'Recovering',
|
||||
100: 'Read Error',
|
||||
1000: 'PLC Error',
|
||||
9999: 'No Response'
|
||||
}
|
||||
st_response = requests.get(self.device_address + "/json/status")
|
||||
if st_response.status_code == 200:
|
||||
data = json.loads(st_response.text)
|
||||
# date = data["ISOdate"]
|
||||
status_read = data["run_status"]
|
||||
try:
|
||||
global status
|
||||
statusMap = {
|
||||
0: 'Stopped',
|
||||
1: 'Running',
|
||||
2: 'Pumped Off',
|
||||
3: 'Faulted',
|
||||
4: 'Starting',
|
||||
5: 'Recovering',
|
||||
100: 'Read Error',
|
||||
1000: 'PLC Error',
|
||||
9999: 'No Response'
|
||||
}
|
||||
st_response = requests.get(self.device_address + "/json/status")
|
||||
if st_response.status_code == 200:
|
||||
data = json.loads(st_response.text)
|
||||
# date = data["ISOdate"]
|
||||
status_read = data["run_status"]
|
||||
|
||||
if status.last_value != status_read:
|
||||
self.statusChanged = True
|
||||
print "Status has changed from {0} to {1} @ {2}".format(status.last_value, status_read, time.time())
|
||||
else:
|
||||
self.statusChanged = False
|
||||
if status.last_value != status_read:
|
||||
self.statusChanged = True
|
||||
print "Status has changed from {0} to {1} @ {2}".format(status.last_value, status_read, time.time())
|
||||
else:
|
||||
self.statusChanged = False
|
||||
|
||||
if self.statusChanged or self.forceSend:
|
||||
self.status = status_read
|
||||
# reg = "(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).(\d{3})Z"
|
||||
# fd = re.search(reg, date)
|
||||
# dt = datetime(int(fd.group(1)), int(fd.group(2)), int(fd.group(3)), int(fd.group(4)), int(fd.group(5)), int(fd.group(6)), int(fd.group(7)))
|
||||
# # timestamp = int(time.mktime(time.strptime(date, '%Y-%m-%dT%H:%M:%S.%fZ')))
|
||||
# timestamp = calendar.timegm(dt.timetuple())
|
||||
self.sendtodb("status", status_read, 0)
|
||||
status.last_value = status_read
|
||||
if self.statusChanged or self.forceSend:
|
||||
self.status = status_read
|
||||
# reg = "(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).(\d{3})Z"
|
||||
# fd = re.search(reg, date)
|
||||
# dt = datetime(int(fd.group(1)), int(fd.group(2)), int(fd.group(3)), int(fd.group(4)), int(fd.group(5)), int(fd.group(6)), int(fd.group(7)))
|
||||
# # timestamp = int(time.mktime(time.strptime(date, '%Y-%m-%dT%H:%M:%S.%fZ')))
|
||||
# timestamp = calendar.timegm(dt.timetuple())
|
||||
self.sendtodb("status", status_read, 0)
|
||||
status.last_value = status_read
|
||||
except Exception, e:
|
||||
print("checkStatus Error: {}".format(e))
|
||||
|
||||
def checkDailyTotals(self):
|
||||
data = json.loads(requests.get(self.device_address + "/json/totals").text)
|
||||
if data['status'] == "OK":
|
||||
totals = data["totals"]
|
||||
timestamp = 0
|
||||
for val in totals:
|
||||
if val['name'] in dt_channels:
|
||||
if dt_channels[val['name']].checkSend(val['value'], False):
|
||||
self.sendtodb(dt_channels[val['name']].mesh_name, dt_channels[val['name']].value, timestamp)
|
||||
else:
|
||||
print("checkDailyTotalsError: {0}".format(data.message))
|
||||
try:
|
||||
data = json.loads(requests.get(self.device_address + "/json/totals").text)
|
||||
if data['status'] == "OK":
|
||||
totals = data["totals"]
|
||||
timestamp = 0
|
||||
for val in totals:
|
||||
if val['name'] in dt_channels:
|
||||
if dt_channels[val['name']].checkSend(val['value'], False):
|
||||
self.sendtodb(dt_channels[val['name']].mesh_name, dt_channels[val['name']].value, timestamp)
|
||||
else:
|
||||
print("checkDailyTotalsError: {0}".format(data.message))
|
||||
except Exception, e:
|
||||
print("checkDailyTotals Error: {}".format(e))
|
||||
|
||||
def checkGaugeOffData(self):
|
||||
data = json.loads(requests.get(self.device_address + "/json/history").text)
|
||||
day = data["hist"]
|
||||
date = day['gauge_date']
|
||||
try:
|
||||
data = json.loads(requests.get(self.device_address + "/json/history").text)
|
||||
day = data["hist"]
|
||||
date = day['gauge_date']
|
||||
|
||||
reg = "(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})"
|
||||
fd = re.search(reg, date)
|
||||
dt = datetime(int(fd.group(1)), int(fd.group(2)), int(fd.group(3)), int(fd.group(4)), int(fd.group(5)), int(fd.group(6)))
|
||||
# timestamp = int(time.mktime(time.strptime(date, '%Y-%m-%dT%H:%M:%S.%fZ')))
|
||||
timestamp = calendar.timegm(dt.timetuple())
|
||||
for entry in day:
|
||||
if entry in go_channels:
|
||||
if go_channels[entry].checkSend(day[entry], False):
|
||||
self.sendtodb(go_channels[entry].mesh_name, day[entry], timestamp)
|
||||
reg = "(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})"
|
||||
fd = re.search(reg, date)
|
||||
dt = datetime(int(fd.group(1)), int(fd.group(2)), int(fd.group(3)), int(fd.group(4)), int(fd.group(5)), int(fd.group(6)))
|
||||
# timestamp = int(time.mktime(time.strptime(date, '%Y-%m-%dT%H:%M:%S.%fZ')))
|
||||
timestamp = calendar.timegm(dt.timetuple())
|
||||
for entry in day:
|
||||
if entry in go_channels:
|
||||
if go_channels[entry].checkSend(day[entry], False):
|
||||
self.sendtodb(go_channels[entry].mesh_name, day[entry], timestamp)
|
||||
except Exception, e:
|
||||
print("checkGaugeOffData Error: {}".format(e))
|
||||
|
||||
def checkLatestCard(self, numCards = 1):
|
||||
latest = ""
|
||||
if numCards == 1:
|
||||
latest = json.loads(requests.get(self.device_address + "/json/latestcard").text)
|
||||
else:
|
||||
latest = json.loads(requests.get(self.device_address + "/json/latestcard/{}".format(numCards)).text)
|
||||
# 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
|
||||
for i in range(0, len(latest['card_data'])):
|
||||
card = latest['card_data'][i]
|
||||
if card_channels['card_id'].checkSend(card['Card_ID'], self.forceSend):
|
||||
dateTime = str(card["Stroke_Time"])
|
||||
try:
|
||||
latest = ""
|
||||
if numCards == 1:
|
||||
latest = json.loads(requests.get(self.device_address + "/json/latestcard").text)
|
||||
else:
|
||||
latest = json.loads(requests.get(self.device_address + "/json/latestcard/{}".format(numCards)).text)
|
||||
# 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
|
||||
for i in range(0, len(latest['card_data'])):
|
||||
card = latest['card_data'][i]
|
||||
if card_channels['card_id'].checkSend(card['Card_ID'], self.forceSend):
|
||||
dateTime = str(card["Stroke_Time"])
|
||||
|
||||
reg = "(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})"
|
||||
fd = re.search(reg, dateTime)
|
||||
dt = datetime(int(fd.group(1)), int(fd.group(2)), int(fd.group(3)), int(fd.group(4)), int(fd.group(5)), int(fd.group(6)))
|
||||
timestamp = calendar.timegm(dt.timetuple())
|
||||
card_timestamp = int(time.mktime(dt.timetuple()))
|
||||
reg = "(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})"
|
||||
fd = re.search(reg, dateTime)
|
||||
dt = datetime(int(fd.group(1)), int(fd.group(2)), int(fd.group(3)), int(fd.group(4)), int(fd.group(5)), int(fd.group(6)))
|
||||
timestamp = calendar.timegm(dt.timetuple())
|
||||
card_timestamp = int(time.mktime(dt.timetuple()))
|
||||
|
||||
print "New card detected @ {0}".format(datetime.strftime(datetime.fromtimestamp(timestamp), "%Y-%m-%d %H:%M:%S.%f"))
|
||||
# set the last value = to current value and upload your data
|
||||
self.sendtodb("card_history", card_channels['card_id'].value, timestamp)
|
||||
print "New card detected @ {0}".format(datetime.strftime(datetime.fromtimestamp(timestamp), "%Y-%m-%d %H:%M:%S.%f"))
|
||||
# set the last value = to current value and upload your data
|
||||
self.sendtodb("card_history", card_channels['card_id'].value, timestamp)
|
||||
|
||||
# check the last time the card was updated
|
||||
if (time.time() - card_channels['card'].last_send_time) > self.cardLoopTimer or self.statusChanged or self.forceSend:
|
||||
# its been 10 minutes, send the full upload
|
||||
print "Either status has changed or last stored card is too old."
|
||||
card_channels["card"].last_send_time = time.time()
|
||||
self.process_card(card, timestamp, card_timestamp, sendCards=True)
|
||||
return
|
||||
else:
|
||||
self.process_card(card, timestamp, card_timestamp, sendCards=False)
|
||||
# check the last time the card was updated
|
||||
if (time.time() - card_channels['card'].last_send_time) > self.cardLoopTimer or self.statusChanged or self.forceSend:
|
||||
# its been 10 minutes, send the full upload
|
||||
print "Either status has changed or last stored card is too old."
|
||||
card_channels["card"].last_send_time = time.time()
|
||||
self.process_card(card, timestamp, card_timestamp, sendCards=True)
|
||||
return
|
||||
else:
|
||||
self.process_card(card, timestamp, card_timestamp, sendCards=False)
|
||||
except Exception, e:
|
||||
print("checkLatestCard Error: {}".format(e))
|
||||
|
||||
def process_card(self, data, data_timestamp, card_timestamp, sendCards=False):
|
||||
|
||||
@@ -474,46 +501,55 @@ class start(threading.Thread, deviceBase):
|
||||
self.sendtodb("dc", newDc, card_timestamp)
|
||||
|
||||
def checkStoredValues(self, forceSend):
|
||||
data = json.loads(requests.get(self.device_address + "/json/tagvalues").text)
|
||||
if data['status'] == "OK":
|
||||
vals = data['vals']
|
||||
for val in vals:
|
||||
if val['name'] in tag_channels:
|
||||
if tag_channels[val['name']].checkSend(val['val'], forceSend):
|
||||
self.sendtodbJSON(tag_channels[val['name']].mesh_name, tag_channels[val['name']].value, 0)
|
||||
try:
|
||||
data = json.loads(requests.get(self.device_address + "/json/tagvalues").text)
|
||||
if data['status'] == "OK":
|
||||
vals = data['vals']
|
||||
for val in vals:
|
||||
if val['name'] in tag_channels:
|
||||
if tag_channels[val['name']].checkSend(val['val'], forceSend):
|
||||
self.sendtodbJSON(tag_channels[val['name']].mesh_name, tag_channels[val['name']].value, 0)
|
||||
except Exception, e:
|
||||
print("checkStoredValues Error: {}".format(e))
|
||||
|
||||
def getLatestXCards(self, numCards):
|
||||
data = json.loads(requests.get(self.device_address + "/json/latest/" + str(int(numCards))).text)
|
||||
for card in data['cards']:
|
||||
card_data = json.loads(requests.get(self.device_address + "/json/" + data['folder'] + "/" + card).text)
|
||||
dateTime = str(card_data["card_data"]["Stroke_Time"])
|
||||
# timestamp = time.mktime(time.strptime(dateTime,'%Y-%m-%dT%H:%M:%S.%fZ'))
|
||||
reg = "(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})"
|
||||
fd = re.search(reg, dateTime)
|
||||
dt = datetime(int(fd.group(1)), int(fd.group(2)), int(fd.group(3)), int(fd.group(4)), int(fd.group(5)), int(fd.group(6)))
|
||||
timestamp = calendar.timegm(dt.timetuple())
|
||||
card_timestamp = int(time.mktime(dt.timetuple()))
|
||||
try:
|
||||
data = json.loads(requests.get(self.device_address + "/json/latest/" + str(int(numCards))).text)
|
||||
for card in data['cards']:
|
||||
card_data = json.loads(requests.get(self.device_address + "/json/" + data['folder'] + "/" + card).text)
|
||||
dateTime = str(card_data["card_data"]["Stroke_Time"])
|
||||
# timestamp = time.mktime(time.strptime(dateTime,'%Y-%m-%dT%H:%M:%S.%fZ'))
|
||||
reg = "(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})"
|
||||
fd = re.search(reg, dateTime)
|
||||
dt = datetime(int(fd.group(1)), int(fd.group(2)), int(fd.group(3)), int(fd.group(4)), int(fd.group(5)), int(fd.group(6)))
|
||||
timestamp = calendar.timegm(dt.timetuple())
|
||||
card_timestamp = int(time.mktime(dt.timetuple()))
|
||||
|
||||
channels["card_history"]["last_value"] = (data['folder'] + "/" + card)
|
||||
self.sendtodb("card_history", (data['folder'] + "/" + card), card_timestamp)
|
||||
self.process_card(card_data, timestamp, card_timestamp, sendCards=True)
|
||||
channels["card_history"]["last_value"] = (data['folder'] + "/" + card)
|
||||
self.sendtodb("card_history", (data['folder'] + "/" + card), card_timestamp)
|
||||
self.process_card(card_data, timestamp, card_timestamp, sendCards=True)
|
||||
except Exception, e:
|
||||
print("getLatestXCards Error: {}".format(e))
|
||||
|
||||
def getDataLoggerStatus(self):
|
||||
data = json.loads(requests.get(self.device_address + "/json/pythonstatus/").text)
|
||||
al_status = "Not OK"
|
||||
if data['status']['alarmLogger']:
|
||||
al_status = "OK"
|
||||
try:
|
||||
data = json.loads(requests.get(self.device_address + "/json/pythonstatus/").text)
|
||||
al_status = "Not OK"
|
||||
if data['status']['alarmLogger']:
|
||||
al_status = "OK"
|
||||
|
||||
if al_status != self.al_status_last:
|
||||
self.sendtodb("alarmlogger_status", al_status, 0)
|
||||
self.al_status_last = al_status
|
||||
if al_status != self.al_status_last:
|
||||
self.sendtodb("alarmlogger_status", al_status, 0)
|
||||
self.al_status_last = al_status
|
||||
|
||||
dl_status = "Not OK"
|
||||
if data['status']['dataLogger']:
|
||||
dl_status = "OK"
|
||||
if al_status != self.dl_status_last:
|
||||
self.sendtodb("datalogger_status", dl_status, 0)
|
||||
self.dl_status_last = dl_status
|
||||
dl_status = "Not OK"
|
||||
if data['status']['dataLogger']:
|
||||
dl_status = "OK"
|
||||
if al_status != self.dl_status_last:
|
||||
self.sendtodb("datalogger_status", dl_status, 0)
|
||||
self.dl_status_last = dl_status
|
||||
except Exception, e:
|
||||
print("getDataLoggerStatus Error: {}".format(e))
|
||||
|
||||
def poc_get_card(self, name, value):
|
||||
self.getcard(value)
|
||||
|
||||
Reference in New Issue
Block a user