Update EthernetIP.py

This commit is contained in:
Nico Melone
2021-09-09 09:12:51 -05:00
parent 4d91fd9797
commit 305710105c

View File

@@ -56,7 +56,7 @@ def sendData(data,property_alias):
time_in_nanos = TimeInNanos(
time_in_seconds=calendar.timegm(time.gmtime()) - random.randint(0, 60), offset_in_nanos=random.randint(0, 10000)
)
variant = Variant(double_value=data.value)
variant = Variant(double_value=data)
asset = [AssetPropertyValue(value=variant, quality=Quality.GOOD, timestamp=time_in_nanos)]
valueEntry = PutAssetPropertyValueEntry(entry_id=str(uuid.uuid4()), property_alias=property_alias, property_values=asset)
client.append_message(stream_name, Util.validate_and_serialize_to_json_bytes(valueEntry))
@@ -66,11 +66,11 @@ def sendData(data,property_alias):
return False
def sendCheck(data, device):
last_send_time = config["devices"][device]['tag_data'][data.tag]['last_send_time']
guaranteed_send_time = config["devices"][device]['tag_data'][data.tag]['guaranteed_send_seconds']
last_send_value = config["devices"][device]['tag_data'][data.tag]['last_send_value']
change_threshold = config["devices"][device]['tag_data'][data.tag]['change_threshold']
def sendCheckDevice(data, group, device):
last_send_time = config[group]["devices"][device]['tag_data'][data.tag]['last_send_time']
guaranteed_send_time = config[group]["devices"][device]['tag_data'][data.tag]['guaranteed_send_seconds']
last_send_value = config[group]["devices"][device]['tag_data'][data.tag]['last_send_value']
change_threshold = config[group]["devices"][device]['tag_data'][data.tag]['change_threshold']
if last_send_time == None:
return True
@@ -83,6 +83,23 @@ def sendCheck(data, device):
else:
return False
def sendCheckFunction(data,group, func):
last_send_time = config[group]["functions"][func]['last_send_time']
guaranteed_send_time = config[group]["functions"][func]['guaranteed_send_seconds']
last_send_value = config[group]["functions"][func]['last_send_value']
change_threshold = config[group]["functions"][func]['change_threshold']
if last_send_time == None:
return True
elif time.time() - last_send_time >= guaranteed_send_time:
return True
elif last_send_value == None:
return True
elif abs(data - last_send_value) >= change_threshold:
return True
else:
return False
def sum(operands:list):
return math.fsum(operands)
@@ -131,50 +148,53 @@ except ConnectionError or asyncio.TimeoutError:
#Main Polling Loop
while(True):
values = {}
for device in config["devices"].keys():
plc_address = config["devices"][device]['source_address']
tag_data = list(config["devices"][device]['tag_data'].keys())
if len(tag_data) > 1:
try:
response = read_multiple(plc_address,tag_data)
logging.info(f"Got back: {response}")
for tag in response:
if tag.tag in values.keys():
values[tag.tag].append(tag.value)
for group in config.keys():
values = {}
for device in config[group]["devices"].keys():
plc_address = config[group]["devices"][device]['source_address']
tag_data = list(config[group]["devices"][device]['tag_data'].keys())
if len(tag_data) > 1:
try:
response = read_multiple(plc_address,tag_data)
logging.info(f"Got back: {response}")
for tag in response:
#logging.info(f"Processing: {tag.tag}")
if tag.tag in values.keys():
values[tag.tag].append(tag.value)
else:
values[tag.tag] = [tag.value]
#logging.info(f"Values: {values}")
if sendCheckDevice(tag, group, device):
sendDataResp = sendData(tag.value, config[group]["devices"][device]['tag_data'][tag.tag]['property_alias'])
if sendDataResp:
config[group]["devices"][device]['tag_data'][tag.tag]['last_send_value'] = tag.value
config[group]["devices"][device]['tag_data'][tag.tag]['last_send_time'] = sendDataResp
except Exception as e:
logging.error(e)
else:
try:
response = read_single(plc_address,tag_data[0])
logging.info(f"Got back: {response}")
if response.tag in values.keys():
values[response.tag].append(response.value)
else:
values[tag.tag] = [tag.value]
if sendCheck(tag, device):
sendDataResp = sendData(tag, config["devices"][device]['tag_data'][response.tag]['property_alias'])
values[response.tag] = [response.value]
if sendCheckDevice(response, group, device):
sendDataResp = sendData(response.value, config[group]["devices"][device]['tag_data'][response.tag]['property_alias'])
if sendDataResp:
config["devices"][device]['tag_data'][response.tag]['last_send_value'] = response.value
config["devices"][device]['tag_data'][response.tag]['last_send_time'] = sendDataResp
except Exception as e:
logging.error(e)
else:
try:
response = read_single(plc_address,tag_data[0])
logging.info(f"Got back: {response}")
if response.tag in values.keys():
values[response.tag].append(response.value)
else:
values[response.tag] = [response.value]
if sendCheck(response, device):
sendDataResp = sendData(response, config["devices"][device]['tag_data'][response.tag]['property_alias'])
if sendDataResp:
config["devices"][device]['tag_data'][response.tag]['last_send_value'] = response.value
config["devices"][device]['tag_data'][response.tag]['last_send_time'] = sendDataResp
except Exception as e:
logging.error(e)
for func in config["functions"].keys():
operands = []
for x in func["operands"]:
operands = operands + values[x]
value = operations[func["operation"]](operands)
#if sendCheck(response, device):
sendDataResp = sendData(value, config["functions"][func]["property_alias"])
if sendDataResp:
config["functions"][func]["last_send_value"] = value
config["fucntions"][func]["last_send_time"] = sendDataResp
config[group]["devices"][device]['tag_data'][response.tag]['last_send_value'] = response.value
config[group]["devices"][device]['tag_data'][response.tag]['last_send_time'] = sendDataResp
except Exception as e:
logging.error(e)
for func in config[group]["functions"].keys():
operands = []
for x in config[group]["functions"][func]["operands"]:
operands = operands + values[x]
value = operations[config[group]["functions"][func]["operation"]](operands)
if sendCheckFunction(value, group, func):
sendDataResp = sendData(value, config[group]["functions"][func]["property_alias"])
if sendDataResp:
config[group]["functions"][func]["last_send_value"] = value
config[group]["functions"][func]["last_send_time"] = sendDataResp
time.sleep(5)