Files
HenryPump-Drivers/plcfreshwater/reset lifetime/update_values.py
2024-07-31 13:56:47 -05:00

174 lines
5.6 KiB
Python

from pycomm.ab_comm.clx import Driver as ClxDriver
from pycomm.cip.cip_base import CommError, DataError
import time
import sys
import json
TAG_DATAERROR_SLEEPTIME = 5
def read_tag(addr, tag, plc_type="CLX"):
#Read a tag from the PLC.
direct = plc_type == "Micro800"
clx = ClxDriver()
try:
if clx.open(addr, direct_connection=direct):
try:
val = clx.read_tag(tag)
clx.close()
return val
except DataError as err:
clx.close()
time.sleep(TAG_DATAERROR_SLEEPTIME)
print("Data Error during readTag({}, {}): {}".format(addr, tag, err))
except CommError:
# err = c.get_status()
#clx.close()
print("Could not connect during readTag({}, {})".format(addr, tag))
except AttributeError as err:
clx.close()
print("AttributeError during readTag({}, {}): \n{}".format(addr, tag, err))
clx.close()
return False
def write_tag(addr, tag, val, plc_type="CLX"):
#Write a tag value to the PLC.
direct = plc_type == "Micro800"
clx = ClxDriver()
try:
if clx.open(addr, direct_connection=direct):
try:
initial_val = clx.read_tag(tag)
write_status = clx.write_tag(tag, val, initial_val[1])
clx.close()
return write_status
except DataError as err:
clx_err = clx.get_status()
clx.close()
print("--\nDataError during writeTag({}, {}, {}, plc_type={}) -- {}\n{}\n".format(addr, tag, val, plc_type, err, clx_err))
except CommError as err:
clx_err = clx.get_status()
print("--\nCommError during write_tag({}, {}, {}, plc_type={})\n{}\n--".format(addr, tag, val, plc_type, err))
#clx.close()
return False
ips = {
"000001000016": "192.168.1.216",
"000001000005": "192.168.1.205",
"000001000004": "192.168.1.204",
"000001000007": "192.168.1.207",
"000001000006": "192.168.1.206",
"000001000001": "192.168.1.201",
"000001000003": "192.168.1.203",
"000001000002": "192.168.1.202",
"000001000012": "192.168.1.212",
"000001000013": "192.168.1.213",
"000001000010": "192.168.1.210",
"000001000011": "192.168.1.211",
"000001000009": "192.168.1.209",
"000001000008": "192.168.1.208",
"000001000014": "192.168.1.214",
"000001000015": "192.168.1.215",
"000006000001": "192.168.1.217",
"000006000002": "192.168.1.218",
"000006000003": "192.168.1.219",
"000006000004": "192.168.1.221",
"000006000005": "192.168.1.220",
"000001000023": "192.168.1.223",
"000001000024": "192.168.1.224",
"000001000025": "192.168.1.225"
}
totals_to_fix = [
{
"total_fm_last_month_gal": 585526.8125,
"total_fm_last_month_bbls": 13941.114583333334,
"name": "Windham 108-1",
"mac": "000001000005"
},
{
"total_fm_last_month_gal": 432819.125,
"total_fm_last_month_bbls": 10305.217261904761,
"name": "Windham 108-6",
"mac": "000001000002"
},
{
"total_fm_last_month_gal": 569190.8111801123,
"total_fm_last_month_bbls": 13552.162170955055,
"name": "Windham 108-5",
"mac": "000001000003"
},
{
"total_fm_last_month_gal": 349503.8125,
"total_fm_last_month_bbls": 8321.519345238095,
"name": "Windham 107-1",
"mac": "000001000004"
},
{
"total_fm_last_month_gal": 835251.983612061,
"total_fm_last_month_bbls": 19886.95199076336,
"name": "Windham 107-2",
"mac": "000001000008"
},
{
"total_fm_last_month_gal": 455446.20678711,
"total_fm_last_month_bbls": 10843.957304455,
"name": "Windham 108-8",
"mac": "000001000009"
},
{
"total_fm_last_month_gal": 589450.9773197174,
"total_fm_last_month_bbls": 14034.54707904089,
"name": "Windham 108-10",
"mac": "000001000010"
},
{
"total_fm_last_month_gal": 1272923.0,
"total_fm_last_month_bbls": 30307.690476190477,
"name": "Windham 108-2",
"mac": "000001000011"
},
{
"total_fm_last_month_gal": 642752.0,
"total_fm_last_month_bbls": 15303.619047619048,
"name": "Caden WW #3",
"mac": "000000000006"
},
{
"total_fm_last_month_gal": 3431191.5,
"total_fm_last_month_bbls": 81695.03571428571,
"name": "Kate A2",
"mac": "000001000015"
},
{
"total_fm_last_month_gal": 1197581.8099999998,
"total_fm_last_month_bbls": 28513.852619047615,
"name": "Barnett 24-1 PLC Fresh Water",
"mac": "000000000031"
},
{
"total_fm_last_month_gal": 603957.6875,
"total_fm_last_month_bbls": 14379.94494047619,
"name": "Kelly #1 PLC Fresh Water",
"mac": "000001000023"
},
{
"total_fm_last_month_gal": 1089152.125,
"total_fm_last_month_bbls": 25932.193452380954,
"name": "Kelly #2 PLC Fresh Water",
"mac": "000001000024"
},
{
"total_fm_last_month_gal": 493207.21875,
"total_fm_last_month_bbls": 11743.029017857143,
"name": "Kelly #3 PLC Fresh Water",
"mac": "000001000025"
}
]
for x in totals_to_fix:
addr = ips.get(x['mac'], None)
if addr:
write_gal = write_tag(addr, "Totalizer_FM_Last_Month_Gal", x["total_fm_last_month_gal"], "Micro800" )
write_bbl = write_tag(addr, "Totalizer_FM_Last_Month_BBLs", x["total_fm_last_month_bbls"], "Micro800" )
print(x["name"], write_gal, write_bbl)