Lots of fixes, docker optimization
- fixes status check for logger - adds ability to test sample data - adds PLC Handshaking capability - adds portainer as container manager - exposes mysql port for reading database (as 6603)
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
FROM python:latest
|
||||
|
||||
# Copy source files
|
||||
RUN mkdir /root/tag-logger
|
||||
COPY taglogger.py /root/tag-logger/taglogger.py
|
||||
|
||||
# Install some python packages
|
||||
RUN pip install requests
|
||||
RUN pip install git+https://github.com/Henry-Pump/Pycomm-Helper.git
|
||||
RUN pip install git+https://github.com/ruscito/pycomm.git
|
||||
|
||||
CMD ["python", "/root/tag-logger/taglogger.py"]
|
||||
# Copy source files
|
||||
RUN mkdir /root/tag-logger
|
||||
COPY taglogger.py /root/tag-logger/taglogger.py
|
||||
|
||||
CMD ["python", "-u", "/root/tag-logger/taglogger.py"]
|
||||
|
||||
@@ -2,7 +2,6 @@ FROM resin/rpi-raspbian:jessie
|
||||
|
||||
# Copy source files
|
||||
RUN mkdir /root/tag-logger
|
||||
COPY taglogger.py /root/tag-logger/taglogger.py
|
||||
RUN apt-get update && apt-get install -y python python-pip git
|
||||
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
|
||||
|
||||
@@ -12,4 +11,6 @@ RUN pip install requests
|
||||
RUN pip install git+https://github.com/Henry-Pump/Pycomm-Helper.git
|
||||
RUN pip install git+https://github.com/ruscito/pycomm.git
|
||||
|
||||
CMD ["python", "/root/tag-logger/taglogger.py"]
|
||||
COPY taglogger.py /root/tag-logger/taglogger.py
|
||||
|
||||
CMD ["python", "-u", "/root/tag-logger/taglogger.py"]
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
FROM python:latest
|
||||
|
||||
# Copy source files
|
||||
RUN mkdir /root/tag-logger
|
||||
COPY taglogger.py /root/tag-logger/taglogger.py
|
||||
|
||||
# Install some python packages
|
||||
RUN pip install requests
|
||||
RUN pip install git+https://github.com/Henry-Pump/Pycomm-Helper.git
|
||||
RUN pip install git+https://github.com/ruscito/pycomm.git
|
||||
|
||||
CMD ["python", "/root/tag-logger/taglogger.py"]
|
||||
# Copy source files
|
||||
RUN mkdir /root/tag-logger
|
||||
COPY taglogger.py /root/tag-logger/taglogger.py
|
||||
|
||||
CMD ["python", "-u", "/root/tag-logger/taglogger.py"]
|
||||
|
||||
@@ -11,7 +11,6 @@ import time
|
||||
import json
|
||||
import requests
|
||||
from requests.packages.urllib3.exceptions import InsecureRequestWarning
|
||||
# from pycomm_helper.tag import Tag
|
||||
from pycomm.ab_comm.clx import Driver as ClxDriver
|
||||
from pycomm.cip.cip_base import CommError
|
||||
|
||||
@@ -19,8 +18,8 @@ from pycomm.cip.cip_base import CommError
|
||||
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
|
||||
|
||||
# DEFAULTS
|
||||
db_address = "10.0.0.103"
|
||||
db_url = "https://{}".format(db_address)
|
||||
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 = {}
|
||||
@@ -32,7 +31,7 @@ handshake_list = []
|
||||
device_types = {}
|
||||
|
||||
|
||||
def readFromPLC(addr, tag):
|
||||
def read_from_plc(addr, tag):
|
||||
"""Read a value from a PLC."""
|
||||
addr = str(addr)
|
||||
tag = str(tag)
|
||||
@@ -50,6 +49,26 @@ def readFromPLC(addr, tag):
|
||||
c.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):
|
||||
try:
|
||||
v = c.write_tag(tag, value, pv[1])
|
||||
c.close()
|
||||
return True
|
||||
except Exception:
|
||||
print("ERROR WRITING TAG: {} at {}".format(tag, addr))
|
||||
err = c.get_status()
|
||||
c.close()
|
||||
print(err)
|
||||
return False
|
||||
c.close()
|
||||
return False
|
||||
|
||||
|
||||
def store_tag(tag):
|
||||
"""Store the value of a tag in the web_db."""
|
||||
global db_url
|
||||
@@ -103,7 +122,7 @@ def load_data():
|
||||
|
||||
new_tags = [t['name'] for t in tags['objects']]
|
||||
existing_tags = [t['name'] for t in tag_list]
|
||||
existing_handshakes = [t['name'] for h in handshake_list]
|
||||
existing_handshakes = [h['name'] for h in handshake_list]
|
||||
|
||||
tags_to_add = []
|
||||
handshakes_to_add = []
|
||||
@@ -130,7 +149,7 @@ def load_data():
|
||||
tags_to_copy.append(this_tag)
|
||||
elif t in existing_handshakes:
|
||||
for e_h in handshake_list:
|
||||
if e_h['name'] == h:
|
||||
if e_h['name'] == t:
|
||||
this_tag['last_stored'] = e_h['last_stored']
|
||||
this_tag['last_store_time'] = e_h['last_store_time']
|
||||
handshakes_to_copy.append(this_tag)
|
||||
@@ -150,11 +169,16 @@ def main():
|
||||
while True:
|
||||
load_data()
|
||||
# print(tag_list)
|
||||
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)):
|
||||
try:
|
||||
|
||||
val = readFromPLC(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']
|
||||
@@ -163,9 +187,11 @@ def main():
|
||||
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'])
|
||||
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"
|
||||
|
||||
@@ -175,21 +201,25 @@ def main():
|
||||
|
||||
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']))
|
||||
traceback.print_exc()
|
||||
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']))
|
||||
else:
|
||||
print("No tags in tag_list. Trying again in 10 seconds.")
|
||||
time.sleep(10)
|
||||
main()
|
||||
|
||||
# if plc_handshake_tags:
|
||||
# if time.time() - last_handshake_time > 30.0:
|
||||
# for hs_tag in plc_handshake_tags:
|
||||
# plc_handshake_tags[hs_tag].write(1)
|
||||
# print("Handshake with {} - {}".format(plc_handshake_tags[hs_tag].address, hs_tag))
|
||||
# last_handshake_time = time.time()
|
||||
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']:
|
||||
try:
|
||||
write_to_plc(handshake_list[h]['ip_address'], 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']))
|
||||
except TypeError:
|
||||
print("Error writing {}".format(tag_list[i]['name']))
|
||||
time.sleep(scan_rate)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user