formatting changes

This commit is contained in:
Patrick McDonagh
2018-02-01 11:57:33 -06:00
parent 3e37d88d6e
commit 61f90ec04a
5 changed files with 3181 additions and 82 deletions

View File

@@ -18,61 +18,59 @@ from pycomm.cip.cip_base import CommError
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
# DEFAULTS
db_address = "web_db"
db_url = "https://{}:5000".format(db_address)
scan_rate = 30 # seconds
save_all = "test" # use True, False, or any string
plc_handshake_tags = {}
last_handshake_time = 0
DB_ADDRESS = "web_db"
DB_URL = "https://{}:5000".format(DB_ADDRESS)
SCAN_RATE = 30 # seconds
SAVE_ALL = "test" # use True, False, or any string
PLC_HANDSHAKE_TAGS = {}
LAST_HANDSHAKE_TIME = 0
tag_store = {}
tag_list = []
handshake_list = []
device_types = {}
TAG_STORE = {}
TAG_LIST = []
HANDSHAKE_LIST = []
DEVICE_TYPES = {}
def read_from_plc(addr, tag):
"""Read a value from a PLC."""
addr = str(addr)
tag = str(tag)
c = ClxDriver()
if c.open(addr):
clx = ClxDriver()
if clx.open(addr):
try:
v = c.read_tag(tag)
return v
return clx.read_tag(tag)
except Exception:
print("ERROR RETRIEVING TAG: {} at {}".format(tag, addr))
err = c.get_status()
c.close()
err = clx.get_status()
clx.close()
print(err)
pass
c.close()
clx.close()
def write_to_plc(addr, tag, value):
"""Write a value to a tag in the PLC at the specified address."""
pv = read_from_plc(addr, tag)
if pv:
c = ClxDriver()
if c.open(addr):
present_value = read_from_plc(addr, tag)
if present_value:
clx = ClxDriver()
if clx.open(addr):
try:
v = c.write_tag(tag, value, pv[1])
c.close()
clx.write_tag(tag, value, present_value[1])
clx.close()
return True
except Exception:
print("ERROR WRITING TAG: {} at {}".format(tag, addr))
err = c.get_status()
c.close()
err = clx.get_status()
clx.close()
print(err)
return False
c.close()
clx.close()
return False
def store_tag(tag):
"""Store the value of a tag in the web_db."""
global db_url
url = "{}/api/tag_vals".format(db_url)
# global DB_URL
url = "{}/api/tag_vals".format(DB_URL)
tag_val_obj = {
"tag_id": tag['id'],
"value": tag['last_stored']
@@ -84,10 +82,11 @@ def store_tag(tag):
def load_data():
"""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:
# 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)
tags = json.loads(get_tag_request.text)
except Exception as e:
@@ -97,59 +96,59 @@ def load_data():
try:
# 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)
for t in device_types_json['objects']:
device_types[t['id']] = t['device_type']
for dev_type in device_types_json['objects']:
DEVICE_TYPES[dev_type['id']] = dev_type['device_type']
except Exception as e:
print("Error getting tags: {}".format(e))
time.sleep(10)
main()
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_list = config_json['objects']
if len(config_list) > 0:
for c in config_list:
if c['parameter'] == "scan_rate":
scan_rate = float(c['val'])
SCAN_RATE = float(c['val'])
elif c['parameter'] == "save_all":
save_all = c['val']
SAVE_ALL = c['val']
except Exception as e:
print("Error getting configs: {}".format(e))
new_tags = [t['name'] for t in tags['objects']]
existing_tags = [t['name'] for t in tag_list]
existing_handshakes = [h['name'] for h in handshake_list]
new_tags = [dev_type['name'] for dev_type in tags['objects']]
existing_tags = [dev_type['name'] for dev_type in TAG_LIST]
existing_handshakes = [h['name'] for h in HANDSHAKE_LIST]
tags_to_add = []
handshakes_to_add = []
tags_to_copy = []
handshakes_to_copy = []
for t in new_tags:
for dev_type in new_tags:
this_tag = {}
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["id"] = n_t["id"]
this_tag["name"] = n_t['name']
this_tag["change_threshold"] = n_t['change_threshold']
this_tag["guarantee_sec"] = n_t['guarantee_sec']
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_store_time"] = 0
if t in existing_tags:
for e_t in tag_list:
if e_t['name'] == t:
if dev_type in existing_tags:
for e_t in TAG_LIST:
if e_t['name'] == dev_type:
this_tag['last_stored'] = e_t['last_stored']
this_tag['last_store_time'] = e_t['last_store_time']
tags_to_copy.append(this_tag)
elif t in existing_handshakes:
for e_h in handshake_list:
if e_h['name'] == t:
elif dev_type in existing_handshakes:
for e_h in HANDSHAKE_LIST:
if e_h['name'] == dev_type:
this_tag['last_stored'] = e_h['last_stored']
this_tag['last_store_time'] = e_h['last_store_time']
handshakes_to_copy.append(this_tag)
@@ -158,69 +157,69 @@ def load_data():
tags_to_add.append(this_tag)
elif n_t['tag_class_id'] == 6:
handshakes_to_add.append(this_tag)
tag_list = tags_to_add + tags_to_copy
handshake_list = handshakes_to_add + handshakes_to_copy
TAG_LIST = tags_to_add + tags_to_copy
HANDSHAKE_LIST = handshakes_to_add + handshakes_to_copy
def main():
"""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:
load_data()
# 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.")
time.sleep(10)
main()
if len(tag_list) > 0:
for i in range(0, len(tag_list)):
if len(TAG_LIST) > 0:
for i in range(0, len(TAG_LIST)):
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()
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_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']
if store_value or store_time or (save_all == "true"):
if store_value or store_time or (SAVE_ALL == "true"):
store_reason = ""
if store_time:
store_reason = "time delta = {} > {}".format(now - tag_list[i]['last_store_time'],
tag_list[i]['guarantee_sec'])
store_reason = "time delta = {} > {}".format(now - TAG_LIST[i]['last_store_time'],
TAG_LIST[i]['guarantee_sec'])
elif store_value:
store_reason = "value delta = {} > {}".format(abs(val - tag_list[i]['last_stored']),
tag_list[i]['change_threshold'])
elif save_all == "true":
store_reason = "value delta = {} > {}".format(abs(val - TAG_LIST[i]['last_stored']),
TAG_LIST[i]['change_threshold'])
elif SAVE_ALL == "true":
store_reason = "save all parameter"
tag_list[i]['last_stored'] = val
tag_list[i]['last_store_time'] = now
store_tag(tag_list[i])
TAG_LIST[i]['last_stored'] = val
TAG_LIST[i]['last_store_time'] = now
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:
print("CommError: Error connecting to {} for {}".format(tag_list[i]['ip_address'],
tag_list[i]['name']))
print("CommError: Error connecting to {} for {}".format(TAG_LIST[i]['ip_address'],
TAG_LIST[i]['name']))
except TypeError:
print("Error reading {}".format(tag_list[i]['name']))
print("Error reading {}".format(TAG_LIST[i]['name']))
if len(handshake_list) > 0:
for h in range(0, len(handshake_list)):
if len(HANDSHAKE_LIST) > 0:
for h in range(0, len(HANDSHAKE_LIST)):
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:
write_to_plc(str(handshake_list[h]['ip_address']), str(handshake_list[h]['tag']), 1)
handshake_list[h]['last_store_time'] = now
print("Handshake with {} - {} at {}".format(handshake_list[h]['ip_address'],
handshake_list[h]['tag'], now))
write_to_plc(str(HANDSHAKE_LIST[h]['ip_address']), str(HANDSHAKE_LIST[h]['tag']), 1)
HANDSHAKE_LIST[h]['last_store_time'] = now
print("Handshake with {} - {} at {}".format(HANDSHAKE_LIST[h]['ip_address'],
HANDSHAKE_LIST[h]['tag'], now))
except CommError:
print("CommError: Error connecting to {} for {}".format(handshake_list[h]['ip_address'],
handshake_list[h]['name']))
print("CommError: Error connecting to {} for {}".format(HANDSHAKE_LIST[h]['ip_address'],
HANDSHAKE_LIST[h]['name']))
except TypeError:
print("Error writing {}".format(tag_list[i]['name']))
time.sleep(scan_rate)
print("Error writing {}".format(TAG_LIST[i]['name']))
time.sleep(SCAN_RATE)
if __name__ == '__main__':

View File

@@ -0,0 +1,3 @@
{
"esversion": 6
}

View 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

File diff suppressed because it is too large Load Diff

View 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"
}
}