Almost everything is working on a new driver. Need to fix card parsing

This commit is contained in:
Patrick McDonagh
2016-11-22 21:27:25 -06:00
parent fe4f07846c
commit e5fac283a5

View File

@@ -8,10 +8,12 @@ from device_base import deviceBase
from datetime import datetime
import requests
import json
import calendar
import pickle
API_HTTP_TYPE = "https"
API_DEVICE_ADDRESS = "192.168.1.30"
API_DEVICE_PORT = 5000
API_DEVICE_ADDRESS = "casselman16-8.callon.poconsole.net"
API_DEVICE_PORT = 8082
API_BASE_URL = "{}://{}:{}".format(API_HTTP_TYPE, API_DEVICE_ADDRESS, API_DEVICE_PORT)
@@ -37,27 +39,27 @@ go_channels = {
}
tag_channels = {
'Polished Rod HP': {'channel': 'polished_rod_hp', 'last_value_sent': None}
'Peak Downhole Load': {'channel': 'downhole_peak_load', 'last_value_sent': None} #TODO: ADD
'Gross Stroke Length': {'channel': 'downhole_gross_stroke', 'last_value_sent': None}
'Stroke Speed': {'channel': 'SPM', 'last_value_sent': None}
'Tubing Head Pressure': {'channel': 'tubing_head_pressure', 'last_value_sent': None}
'Minimum Polished Rod Load': {'channel': 'surface_min_load', 'last_value_sent': None}
'Fluid Load': {'channel': 'downhole_fluid_load', 'last_value_sent': None}
'Downhole Max. Position': {'channel': 'downhole_max_position', 'last_value_sent': None}
'Downhole Net Stroke': {'channel': 'downhole_net_stroke', 'last_value_sent': None}
'Pump Fill Percent': {'channel': 'fillage_percent', 'last_value_sent': None}
'Downhole Pump HP': {'channel': 'pump_hp', 'last_value_sent': None}
'Surface Min. Position': {'channel': 'surface_min_position', 'last_value_sent': None} #TODO: ADD
'Pump Intake Pressure': {'channel': 'pump_intake_pressure', 'last_value_sent': None}
'Surface Max. Position': {'channel': 'surface_max_position', 'last_value_sent': None} #TODO: ADD
'Tubing Movement': {'channel': 'tubing_movement', 'last_value_sent': None}
'Downhole Min. Position': {'channel': 'downhole_min_position', 'last_value_sent': None}
'Peak Polished Rod Load': {'channel': 'surface_max_load', 'last_value_sent': None}
'Minimum Downhole Load': {'channel': 'downhole_min_load', 'last_value_sent': None} #TODO: ADD
'Surface Stroke Length': {'channel': 'surface_stroke_length', 'last_value_sent': None}
'Downhole Adjusted Gross Stroke': {'channel': 'downhole_adjusted_gross_stroke', 'last_value_sent': None}
'Fluid Level': {'channel': 'fluid_above_pump', 'last_value_sent': None}
'Polished Rod HP': {'channel': 'polished_rod_hp', 'last_value_sent': None},
'Peak Downhole Load': {'channel': 'downhole_peak_load', 'last_value_sent': None}, #TODO: ADD
'Gross Stroke Length': {'channel': 'downhole_gross_stroke', 'last_value_sent': None},
'Stroke Speed': {'channel': 'SPM', 'last_value_sent': None},
'Tubing Head Pressure': {'channel': 'tubing_head_pressure', 'last_value_sent': None},
'Minimum Polished Rod Load': {'channel': 'surface_min_load', 'last_value_sent': None},
'Fluid Load': {'channel': 'downhole_fluid_load', 'last_value_sent': None},
'Downhole Max. Position': {'channel': 'downhole_max_position', 'last_value_sent': None},
'Downhole Net Stroke': {'channel': 'downhole_net_stroke', 'last_value_sent': None},
'Pump Fill Percent': {'channel': 'fillage_percent', 'last_value_sent': None},
'Downhole Pump HP': {'channel': 'pump_hp', 'last_value_sent': None},
'Surface Min. Position': {'channel': 'surface_min_position', 'last_value_sent': None}, #TODO: ADD
'Pump Intake Pressure': {'channel': 'pump_intake_pressure', 'last_value_sent': None},
'Surface Max. Position': {'channel': 'surface_max_position', 'last_value_sent': None}, #TODO: ADD
'Tubing Movement': {'channel': 'tubing_movement', 'last_value_sent': None},
'Downhole Min. Position': {'channel': 'downhole_min_position', 'last_value_sent': None},
'Peak Polished Rod Load': {'channel': 'surface_max_load', 'last_value_sent': None},
'Minimum Downhole Load': {'channel': 'downhole_min_load', 'last_value_sent': None}, #TODO: ADD
'Surface Stroke Length': {'channel': 'surface_stroke_length', 'last_value_sent': None},
'Downhole Adjusted Gross Stroke': {'channel': 'downhole_adjusted_gross_stroke', 'last_value_sent': None},
'Fluid Level': {'channel': 'fluid_above_pump', 'last_value_sent': None},
'Stroke Production': {'channel': 'stroke_production', 'last_value_sent': None}
}
@@ -242,14 +244,15 @@ class start(threading.Thread, deviceBase):
def checkStatus(self, last_status):
global API_BASE_URL
try:
api_req = requests.get( '{}/api/run_status_log/?q={"order_by":[{"field":"created_on","direction":"desc"}]}'.format(API_BASE_URL))
url = API_BASE_URL + '/api/run_status_log?q={"order_by":[{"field":"created_on","direction":"desc"}]}'
api_req = requests.get(url, verify=False)
if api_req.status_code == 200:
req_data = json.loads(api_req.text)
req_data['objects'].reverse()
for i in range(0,len(req_data['objects'])):
if int(req_data['objects'][i]['_id']) not in self.runstatusIds:
new_status = req_data['objects'][i]["run_status"]
timestamp = req_data['objects'][i]["created_on"]
timestamp = calendar.timegm(datetime.strptime(req_data['objects'][i]['created_on'], '%Y-%m-%dT%H:%M:%S.%f').timetuple())
self.sendtodb('status', new_status, timestamp)
self.runstatusIds.append(int(req_data['objects'][i]['_id']))
if len(self.runstatusIds) > 20:
@@ -259,7 +262,7 @@ class start(threading.Thread, deviceBase):
if req_data['objects'][-1:][0]['run_status'] != last_status:
print "Status has changed from {0} to {1} @ {2}".format(last_status, req_data['objects'][-1:][0]['run_status'], req_data['objects'][-1:][0]['created_on'])
return ['objects'][-1:][0]['run_status']
return req_data['objects'][-1:][0]['run_status']
except Exception as e:
print "Error during checkStatus..."
print("++++++== TRACEBACK ==++++++")
@@ -270,13 +273,14 @@ class start(threading.Thread, deviceBase):
def checkEvents(self):
global API_BASE_URL
try:
api_req = requests.get( '{}/api/events/?q={"order_by":[{"field":"created_on","direction":"desc"}]}'.format(API_BASE_URL))
url = API_BASE_URL + '/api/events?q={"order_by":[{"field":"created_on","direction":"desc"}]}'
api_req = requests.get( url, verify=False)
if api_req.status_code == 200:
req_data = json.loads(api_req.text)
req_data['objects'].reverse()
for i in range(0,len(req_data['objects'])):
if int(req_data['objects'][i]['_id']) not in self.eventIds:
timestamp = req_data['objects'][i]["created_on"]
timestamp = calendar.timegm(datetime.strptime(req_data['objects'][i]['created_on'], '%Y-%m-%dT%H:%M:%S.%f').timetuple())
self.sendtodbJSON('events', json.dumps(req_data['objects'][i]), timestamp)
self.eventIds.append(int(req_data['objects'][i]['_id']))
if len(self.eventIds) > 20:
@@ -294,13 +298,14 @@ class start(threading.Thread, deviceBase):
def checkNotes(self):
global API_BASE_URL
try:
api_req = requests.get( '{}/api/notes/?q={"order_by":[{"field":"created_on","direction":"desc"}]}'.format(API_BASE_URL))
url = API_BASE_URL + '/api/notes?q={"order_by":[{"field":"created_on","direction":"desc"}]}'
api_req = requests.get( url, verify=False)
if api_req.status_code == 200:
req_data = json.loads(api_req.text)
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 = req_data['objects'][i]["created_on"]
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)
self.noteIDs.append(int(req_data['objects'][i]['_id']))
if len(self.noteIDs) > 20:
@@ -318,13 +323,14 @@ class start(threading.Thread, deviceBase):
def checkFluidShots(self):
global API_BASE_URL
try:
api_req = requests.get( '{}/api/fluid_shots/?q={"order_by":[{"field":"created_on","direction":"desc"}]}'.format(API_BASE_URL))
url = API_BASE_URL + '/api/fluid_shots?q={"order_by":[{"field":"created_on","direction":"desc"}]}'
api_req = requests.get(url, verify=False)
if api_req.status_code == 200:
req_data = json.loads(api_req.text)
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 = req_data['objects'][i]["created_on"]
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)
self.fluidshotIds.append(int(req_data['objects'][i]['_id']))
if len(self.fluidshotIds) > 20:
@@ -342,13 +348,14 @@ class start(threading.Thread, deviceBase):
def checkWellTests(self):
global API_BASE_URL
try:
api_req = requests.get( '{}/api/well_test/?q={"order_by":[{"field":"created_on","direction":"desc"}]}'.format(API_BASE_URL))
url = API_BASE_URL + '/api/well_test?q={"order_by":[{"field":"created_on","direction":"desc"}]}'
api_req = requests.get(url, verify=False)
if api_req.status_code == 200:
req_data = json.loads(api_req.text)
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 = req_data['objects'][i]["created_on"]
timestamp = calendar.timegm(datetime.strptime(req_data['objects'][i]['created_on'], '%Y-%m-%dT%H:%M:%S.%f').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:
@@ -366,7 +373,7 @@ class start(threading.Thread, deviceBase):
def checkDailyTotals(self):
global API_BASE_URL, dt_channels
try:
api_req = requests.get("{}/api/today_totals".format(API_BASE_URL)).text
api_req = requests.get("{}/api/today_totals".format(API_BASE_URL), verify=False)
if api_req.status_code == 200:
req_data = json.loads(api_req.text)
for i in range(0, len(req_data)):
@@ -392,13 +399,14 @@ class start(threading.Thread, deviceBase):
def checkGaugeOffData(self):
global API_BASE_URL, go_channels
try:
api_req = requests.get( '{}/api/gauge_off/?q={"order_by":[{"field":"created_on","direction":"desc"}]}'.format(API_BASE_URL))
url = API_BASE_URL + '/api/gauge_off?q={"order_by":[{"field":"created_on","direction":"desc"}]}'
api_req = requests.get(url , verify=False)
if api_req.status_code == 200:
req_data = json.loads(api_req.text)
req_data['objects'].reverse()
for i in range(0,len(req_data['objects'])):
if int(req_data['objects'][i]['_id']) not in self.gaugeoffIds:
timestamp = req_data['objects'][i]['created_on']
timestamp = calendar.timegm(datetime.strptime(req_data['objects'][i]['created_on'], '%Y-%m-%dT%H:%M:%S.%f').timetuple())
for col_name in req_data['objects'][i]:
if col_name in go_channels:
self.sendtodb(go_channels[col_name]['channel'], req_data['objects'][i][col_name], timestamp)
@@ -419,17 +427,17 @@ class start(threading.Thread, deviceBase):
def checkStoredValues(self):
global API_BASE_URL, tag_channels
try:
api_req = requests.get( '{}/api/latest'.format(API_BASE_URL))
api_req = requests.get( '{}/api/latest'.format(API_BASE_URL), verify=False)
if api_req.status_code == 200:
req_data = json.loads(api_req.text)
for i in range(0, len(req_data)):
if req_data[i]['tag_name'] in tag_channels:
if tag_channels['tag_name']['last_value_sent'] is None:
self.sendtodb(tag_channels['tag_name']['channel'], req_data[i]['value'], req_data[i]['created_on'])
tag_channels['tag_name']['last_value_sent'] = req_data[i]['value']
elif req_data[i]['value'] != tag_channels['tag_name']['last_value_sent']:
self.sendtodb(tag_channels['tag_name']['channel'], req_data[i]['value'], req_data[i]['created_on'])
tag_channels['tag_name']['last_value_sent'] = req_data[i]['value']
if tag_channels[req_data[i]['tag_name']]['last_value_sent'] is None:
self.sendtodb(tag_channels[req_data[i]['tag_name']]['channel'], req_data[i]['value'], calendar.timegm(datetime.strptime(req_data[i]['datetime'], '%Y-%m-%d %H:%M:%S.%f').timetuple()))
tag_channels[req_data[i]['tag_name']]['last_value_sent'] = req_data[i]['value']
elif req_data[i]['value'] != tag_channels[req_data[i]['tag_name']]['last_value_sent']:
self.sendtodb(tag_channels[req_data[i]['tag_name']]['channel'], req_data[i]['value'], calendar.timegm(datetime.strptime(req_data[i]['datetime'], '%Y-%m-%d %H:%M:%S.%f').timetuple()))
tag_channels[req_data[i]['tag_name']]['last_value_sent'] = req_data[i]['value']
except Exception as e:
print "Error during checkStoredValues..."
print("++++++== TRACEBACK ==++++++")
@@ -440,7 +448,9 @@ class start(threading.Thread, deviceBase):
def checkLatestCard(self, numCards = 1):
global API_BASE_URL
try:
api_req = requests.get('{}/api/cards?q={"order_by":[{"field":"created_on","direction":"desc"}], "limit":{}}'.format(API_BASE_URL, numCards))
url = API_BASE_URL + '/api/cards?q={"order_by":[{"field":"created_on","direction":"desc"}], "limit":' + str(numCards) + "}"
print(url)
api_req = requests.get(url, verify=False)
req_data = json.loads(api_req.text)['objects']
# check the card to see if its new
@@ -450,11 +460,11 @@ class start(threading.Thread, deviceBase):
current_card = req_data[i]
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')
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()))
print "New card detected @ {0}".format(datetime.strftime(datetime.fromtimestamp(timestamp), "%Y-%m-%d %H:%M:%S.%f"))
print "New card detected @ {0}".format(datetime.strftime(datetime.fromtimestamp(timestamp), "%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.last_card_id = current_card['_id']
@@ -465,12 +475,13 @@ 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(data_timestamp))
self.sendtodb("cardtype", current_card['stroke_type'], int(timestamp))
s_p = data["surf_pos"]
s_l = data["surf_lod"]
d_p = data["down_pos"]
d_l = data["down_lod"]
# TODO: FIX CARD PARSING
s_p = current_card["surf_pos"]
s_l = current_card["surf_lod"]
d_p = current_card["down_pos"]
d_l = current_card["down_lod"]
newSc = "["
newDc = "["
@@ -505,7 +516,7 @@ class start(threading.Thread, deviceBase):
# def getDataLoggerStatus(self):
# try:
# data = json.loads(requests.get(self.device_address + "/json/pythonstatus/").text)
# data = json.loads(requests.get(self.device_address + "/json/pythonstatus/", verify=False).text)
# al_status = "Not OK"
# if data['status']['alarmLogger']:
# al_status = "OK"