formatting changes
This commit is contained in:
163
daq/taglogger.py
163
daq/taglogger.py
@@ -18,61 +18,59 @@ from pycomm.cip.cip_base import CommError
|
|||||||
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
|
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
|
||||||
|
|
||||||
# DEFAULTS
|
# DEFAULTS
|
||||||
db_address = "web_db"
|
DB_ADDRESS = "web_db"
|
||||||
db_url = "https://{}:5000".format(db_address)
|
DB_URL = "https://{}:5000".format(DB_ADDRESS)
|
||||||
scan_rate = 30 # seconds
|
SCAN_RATE = 30 # seconds
|
||||||
save_all = "test" # use True, False, or any string
|
SAVE_ALL = "test" # use True, False, or any string
|
||||||
plc_handshake_tags = {}
|
PLC_HANDSHAKE_TAGS = {}
|
||||||
last_handshake_time = 0
|
LAST_HANDSHAKE_TIME = 0
|
||||||
|
|
||||||
tag_store = {}
|
TAG_STORE = {}
|
||||||
tag_list = []
|
TAG_LIST = []
|
||||||
handshake_list = []
|
HANDSHAKE_LIST = []
|
||||||
device_types = {}
|
DEVICE_TYPES = {}
|
||||||
|
|
||||||
|
|
||||||
def read_from_plc(addr, tag):
|
def read_from_plc(addr, tag):
|
||||||
"""Read a value from a PLC."""
|
"""Read a value from a PLC."""
|
||||||
addr = str(addr)
|
addr = str(addr)
|
||||||
tag = str(tag)
|
tag = str(tag)
|
||||||
c = ClxDriver()
|
clx = ClxDriver()
|
||||||
if c.open(addr):
|
if clx.open(addr):
|
||||||
try:
|
try:
|
||||||
v = c.read_tag(tag)
|
return clx.read_tag(tag)
|
||||||
return v
|
|
||||||
except Exception:
|
except Exception:
|
||||||
print("ERROR RETRIEVING TAG: {} at {}".format(tag, addr))
|
print("ERROR RETRIEVING TAG: {} at {}".format(tag, addr))
|
||||||
err = c.get_status()
|
err = clx.get_status()
|
||||||
c.close()
|
clx.close()
|
||||||
print(err)
|
print(err)
|
||||||
pass
|
clx.close()
|
||||||
c.close()
|
|
||||||
|
|
||||||
|
|
||||||
def write_to_plc(addr, tag, value):
|
def write_to_plc(addr, tag, value):
|
||||||
"""Write a value to a tag in the PLC at the specified address."""
|
"""Write a value to a tag in the PLC at the specified address."""
|
||||||
pv = read_from_plc(addr, tag)
|
present_value = read_from_plc(addr, tag)
|
||||||
if pv:
|
if present_value:
|
||||||
c = ClxDriver()
|
clx = ClxDriver()
|
||||||
if c.open(addr):
|
if clx.open(addr):
|
||||||
try:
|
try:
|
||||||
v = c.write_tag(tag, value, pv[1])
|
clx.write_tag(tag, value, present_value[1])
|
||||||
c.close()
|
clx.close()
|
||||||
return True
|
return True
|
||||||
except Exception:
|
except Exception:
|
||||||
print("ERROR WRITING TAG: {} at {}".format(tag, addr))
|
print("ERROR WRITING TAG: {} at {}".format(tag, addr))
|
||||||
err = c.get_status()
|
err = clx.get_status()
|
||||||
c.close()
|
clx.close()
|
||||||
print(err)
|
print(err)
|
||||||
return False
|
return False
|
||||||
c.close()
|
clx.close()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def store_tag(tag):
|
def store_tag(tag):
|
||||||
"""Store the value of a tag in the web_db."""
|
"""Store the value of a tag in the web_db."""
|
||||||
global db_url
|
# global DB_URL
|
||||||
url = "{}/api/tag_vals".format(db_url)
|
url = "{}/api/tag_vals".format(DB_URL)
|
||||||
tag_val_obj = {
|
tag_val_obj = {
|
||||||
"tag_id": tag['id'],
|
"tag_id": tag['id'],
|
||||||
"value": tag['last_stored']
|
"value": tag['last_stored']
|
||||||
@@ -84,10 +82,11 @@ def store_tag(tag):
|
|||||||
|
|
||||||
def load_data():
|
def load_data():
|
||||||
"""Load configuration data from the web server."""
|
"""Load configuration data from the web server."""
|
||||||
global db_url, scan_rate, save_all, tag_list, handshake_list
|
global DB_URL, SCAN_RATE, SAVE_ALL, TAG_LIST, HANDSHAKE_LIST
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Get tags stored in database
|
# Get tags stored in database
|
||||||
url = '{}/api/tags'.format(db_url)
|
url = '{}/api/tags'.format(DB_URL)
|
||||||
get_tag_request = requests.get(url, verify=False)
|
get_tag_request = requests.get(url, verify=False)
|
||||||
tags = json.loads(get_tag_request.text)
|
tags = json.loads(get_tag_request.text)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -97,59 +96,59 @@ def load_data():
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# Get device types stored in database
|
# Get device types stored in database
|
||||||
get_device_type_request = requests.get('{}/api/device_types'.format(db_url), verify=False)
|
get_device_type_request = requests.get('{}/api/device_types'.format(DB_URL), verify=False)
|
||||||
device_types_json = json.loads(get_device_type_request.text)
|
device_types_json = json.loads(get_device_type_request.text)
|
||||||
for t in device_types_json['objects']:
|
for dev_type in device_types_json['objects']:
|
||||||
device_types[t['id']] = t['device_type']
|
DEVICE_TYPES[dev_type['id']] = dev_type['device_type']
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Error getting tags: {}".format(e))
|
print("Error getting tags: {}".format(e))
|
||||||
time.sleep(10)
|
time.sleep(10)
|
||||||
main()
|
main()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
config_req = requests.get('{}/api/configs'.format(db_url), verify=False)
|
config_req = requests.get('{}/api/configs'.format(DB_URL), verify=False)
|
||||||
config_json = json.loads(config_req.text)
|
config_json = json.loads(config_req.text)
|
||||||
config_list = config_json['objects']
|
config_list = config_json['objects']
|
||||||
|
|
||||||
if len(config_list) > 0:
|
if len(config_list) > 0:
|
||||||
for c in config_list:
|
for c in config_list:
|
||||||
if c['parameter'] == "scan_rate":
|
if c['parameter'] == "scan_rate":
|
||||||
scan_rate = float(c['val'])
|
SCAN_RATE = float(c['val'])
|
||||||
elif c['parameter'] == "save_all":
|
elif c['parameter'] == "save_all":
|
||||||
save_all = c['val']
|
SAVE_ALL = c['val']
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Error getting configs: {}".format(e))
|
print("Error getting configs: {}".format(e))
|
||||||
|
|
||||||
new_tags = [t['name'] for t in tags['objects']]
|
new_tags = [dev_type['name'] for dev_type in tags['objects']]
|
||||||
existing_tags = [t['name'] for t in tag_list]
|
existing_tags = [dev_type['name'] for dev_type in TAG_LIST]
|
||||||
existing_handshakes = [h['name'] for h in handshake_list]
|
existing_handshakes = [h['name'] for h in HANDSHAKE_LIST]
|
||||||
|
|
||||||
tags_to_add = []
|
tags_to_add = []
|
||||||
handshakes_to_add = []
|
handshakes_to_add = []
|
||||||
tags_to_copy = []
|
tags_to_copy = []
|
||||||
handshakes_to_copy = []
|
handshakes_to_copy = []
|
||||||
for t in new_tags:
|
for dev_type in new_tags:
|
||||||
this_tag = {}
|
this_tag = {}
|
||||||
for n_t in tags['objects']:
|
for n_t in tags['objects']:
|
||||||
if n_t['name'] == t:
|
if n_t['name'] == dev_type:
|
||||||
this_tag["tag"] = n_t['tag']
|
this_tag["tag"] = n_t['tag']
|
||||||
this_tag["id"] = n_t["id"]
|
this_tag["id"] = n_t["id"]
|
||||||
this_tag["name"] = n_t['name']
|
this_tag["name"] = n_t['name']
|
||||||
this_tag["change_threshold"] = n_t['change_threshold']
|
this_tag["change_threshold"] = n_t['change_threshold']
|
||||||
this_tag["guarantee_sec"] = n_t['guarantee_sec']
|
this_tag["guarantee_sec"] = n_t['guarantee_sec']
|
||||||
this_tag["ip_address"] = n_t['device']['address']
|
this_tag["ip_address"] = n_t['device']['address']
|
||||||
this_tag["device_type"] = device_types[n_t['device']['device_type_id']]
|
this_tag["device_type"] = DEVICE_TYPES[n_t['device']['device_type_id']]
|
||||||
this_tag["last_stored"] = 0.0
|
this_tag["last_stored"] = 0.0
|
||||||
this_tag["last_store_time"] = 0
|
this_tag["last_store_time"] = 0
|
||||||
if t in existing_tags:
|
if dev_type in existing_tags:
|
||||||
for e_t in tag_list:
|
for e_t in TAG_LIST:
|
||||||
if e_t['name'] == t:
|
if e_t['name'] == dev_type:
|
||||||
this_tag['last_stored'] = e_t['last_stored']
|
this_tag['last_stored'] = e_t['last_stored']
|
||||||
this_tag['last_store_time'] = e_t['last_store_time']
|
this_tag['last_store_time'] = e_t['last_store_time']
|
||||||
tags_to_copy.append(this_tag)
|
tags_to_copy.append(this_tag)
|
||||||
elif t in existing_handshakes:
|
elif dev_type in existing_handshakes:
|
||||||
for e_h in handshake_list:
|
for e_h in HANDSHAKE_LIST:
|
||||||
if e_h['name'] == t:
|
if e_h['name'] == dev_type:
|
||||||
this_tag['last_stored'] = e_h['last_stored']
|
this_tag['last_stored'] = e_h['last_stored']
|
||||||
this_tag['last_store_time'] = e_h['last_store_time']
|
this_tag['last_store_time'] = e_h['last_store_time']
|
||||||
handshakes_to_copy.append(this_tag)
|
handshakes_to_copy.append(this_tag)
|
||||||
@@ -158,69 +157,69 @@ def load_data():
|
|||||||
tags_to_add.append(this_tag)
|
tags_to_add.append(this_tag)
|
||||||
elif n_t['tag_class_id'] == 6:
|
elif n_t['tag_class_id'] == 6:
|
||||||
handshakes_to_add.append(this_tag)
|
handshakes_to_add.append(this_tag)
|
||||||
tag_list = tags_to_add + tags_to_copy
|
TAG_LIST = tags_to_add + tags_to_copy
|
||||||
handshake_list = handshakes_to_add + handshakes_to_copy
|
HANDSHAKE_LIST = handshakes_to_add + handshakes_to_copy
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Run the main routine."""
|
"""Run the main routine."""
|
||||||
global scan_rate, tag_store, device_types, tag_list, handshake_list, save_all
|
global SCAN_RATE, TAG_STORE, DEVICE_TYPES, TAG_LIST, HANDSHAKE_LIST, SAVE_ALL
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
load_data()
|
load_data()
|
||||||
# print(tag_list)
|
# print(tag_list)
|
||||||
if len(tag_list + handshake_list) == 0:
|
if len(TAG_LIST + HANDSHAKE_LIST) == 0:
|
||||||
print("No tags configured. Trying again in 10 seconds.")
|
print("No tags configured. Trying again in 10 seconds.")
|
||||||
time.sleep(10)
|
time.sleep(10)
|
||||||
main()
|
main()
|
||||||
|
|
||||||
if len(tag_list) > 0:
|
if len(TAG_LIST) > 0:
|
||||||
for i in range(0, len(tag_list)):
|
for i in range(0, len(TAG_LIST)):
|
||||||
try:
|
try:
|
||||||
|
|
||||||
val = read_from_plc(tag_list[i]['ip_address'], tag_list[i]['tag'])[0]
|
val = read_from_plc(TAG_LIST[i]['ip_address'], TAG_LIST[i]['tag'])[0]
|
||||||
now = time.time()
|
now = time.time()
|
||||||
|
|
||||||
store_value = abs(val - tag_list[i]['last_stored']) > tag_list[i]['change_threshold']
|
store_value = abs(val - TAG_LIST[i]['last_stored']) > TAG_LIST[i]['change_threshold']
|
||||||
store_time = (now - tag_list[i]['last_store_time']) > tag_list[i]['guarantee_sec']
|
store_time = (now - TAG_LIST[i]['last_store_time']) > TAG_LIST[i]['guarantee_sec']
|
||||||
|
|
||||||
if store_value or store_time or (save_all == "true"):
|
if store_value or store_time or (SAVE_ALL == "true"):
|
||||||
store_reason = ""
|
store_reason = ""
|
||||||
if store_time:
|
if store_time:
|
||||||
store_reason = "time delta = {} > {}".format(now - tag_list[i]['last_store_time'],
|
store_reason = "time delta = {} > {}".format(now - TAG_LIST[i]['last_store_time'],
|
||||||
tag_list[i]['guarantee_sec'])
|
TAG_LIST[i]['guarantee_sec'])
|
||||||
elif store_value:
|
elif store_value:
|
||||||
store_reason = "value delta = {} > {}".format(abs(val - tag_list[i]['last_stored']),
|
store_reason = "value delta = {} > {}".format(abs(val - TAG_LIST[i]['last_stored']),
|
||||||
tag_list[i]['change_threshold'])
|
TAG_LIST[i]['change_threshold'])
|
||||||
elif save_all == "true":
|
elif SAVE_ALL == "true":
|
||||||
store_reason = "save all parameter"
|
store_reason = "save all parameter"
|
||||||
|
|
||||||
tag_list[i]['last_stored'] = val
|
TAG_LIST[i]['last_stored'] = val
|
||||||
tag_list[i]['last_store_time'] = now
|
TAG_LIST[i]['last_store_time'] = now
|
||||||
store_tag(tag_list[i])
|
store_tag(TAG_LIST[i])
|
||||||
|
|
||||||
print("Stored {} for {} at {} due to {}".format(val, tag_list[i]['name'], now, store_reason))
|
print("Stored {} for {} at {} due to {}".format(val, TAG_LIST[i]['name'], now, store_reason))
|
||||||
except CommError:
|
except CommError:
|
||||||
print("CommError: Error connecting to {} for {}".format(tag_list[i]['ip_address'],
|
print("CommError: Error connecting to {} for {}".format(TAG_LIST[i]['ip_address'],
|
||||||
tag_list[i]['name']))
|
TAG_LIST[i]['name']))
|
||||||
except TypeError:
|
except TypeError:
|
||||||
print("Error reading {}".format(tag_list[i]['name']))
|
print("Error reading {}".format(TAG_LIST[i]['name']))
|
||||||
|
|
||||||
if len(handshake_list) > 0:
|
if len(HANDSHAKE_LIST) > 0:
|
||||||
for h in range(0, len(handshake_list)):
|
for h in range(0, len(HANDSHAKE_LIST)):
|
||||||
now = time.time()
|
now = time.time()
|
||||||
if (now - handshake_list[h]['last_store_time']) > handshake_list[h]['guarantee_sec']:
|
if (now - HANDSHAKE_LIST[h]['last_store_time']) > HANDSHAKE_LIST[h]['guarantee_sec']:
|
||||||
try:
|
try:
|
||||||
write_to_plc(str(handshake_list[h]['ip_address']), str(handshake_list[h]['tag']), 1)
|
write_to_plc(str(HANDSHAKE_LIST[h]['ip_address']), str(HANDSHAKE_LIST[h]['tag']), 1)
|
||||||
handshake_list[h]['last_store_time'] = now
|
HANDSHAKE_LIST[h]['last_store_time'] = now
|
||||||
print("Handshake with {} - {} at {}".format(handshake_list[h]['ip_address'],
|
print("Handshake with {} - {} at {}".format(HANDSHAKE_LIST[h]['ip_address'],
|
||||||
handshake_list[h]['tag'], now))
|
HANDSHAKE_LIST[h]['tag'], now))
|
||||||
except CommError:
|
except CommError:
|
||||||
print("CommError: Error connecting to {} for {}".format(handshake_list[h]['ip_address'],
|
print("CommError: Error connecting to {} for {}".format(HANDSHAKE_LIST[h]['ip_address'],
|
||||||
handshake_list[h]['name']))
|
HANDSHAKE_LIST[h]['name']))
|
||||||
except TypeError:
|
except TypeError:
|
||||||
print("Error writing {}".format(tag_list[i]['name']))
|
print("Error writing {}".format(TAG_LIST[i]['name']))
|
||||||
time.sleep(scan_rate)
|
time.sleep(SCAN_RATE)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
3
web_db/javascript/server/.jshintrc
Normal file
3
web_db/javascript/server/.jshintrc
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"esversion": 6
|
||||||
|
}
|
||||||
15
web_db/javascript/server/index.js
Normal file
15
web_db/javascript/server/index.js
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
// Lumberjack -- More than just a logger
|
||||||
|
|
||||||
|
// Imports
|
||||||
|
import express from 'express';
|
||||||
|
import morgan from 'morgan';
|
||||||
|
|
||||||
|
let app = express();
|
||||||
|
app.use(morgan('dev'));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
app.listen(3000, function () {
|
||||||
|
console.log('Listening on port 3000!');
|
||||||
|
});
|
||||||
|
|
||||||
3052
web_db/javascript/server/package-lock.json
generated
Normal file
3052
web_db/javascript/server/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
30
web_db/javascript/server/package.json
Normal file
30
web_db/javascript/server/package.json
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
{
|
||||||
|
"name": "lumberjack",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "PLC Data Logger",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://github.com/Henry-Pump/DataLogger-Generic.git"
|
||||||
|
},
|
||||||
|
"author": "Patrick McDonagh",
|
||||||
|
"license": "ISC",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/Henry-Pump/DataLogger-Generic/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/Henry-Pump/DataLogger-Generic#readme",
|
||||||
|
"devDependencies": {
|
||||||
|
"babel-cli": "^6.26.0",
|
||||||
|
"babel-preset-env": "^1.6.0",
|
||||||
|
"babel-preset-es2015": "^6.24.1",
|
||||||
|
"mocha": "^3.5.3",
|
||||||
|
"rimraf": "^2.6.2"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"express": "^4.15.5",
|
||||||
|
"morgan": "^1.8.2"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user