Update EthernetIP.py
This commit is contained in:
@@ -56,7 +56,7 @@ def sendData(data,property_alias):
|
|||||||
time_in_nanos = TimeInNanos(
|
time_in_nanos = TimeInNanos(
|
||||||
time_in_seconds=calendar.timegm(time.gmtime()) - random.randint(0, 60), offset_in_nanos=random.randint(0, 10000)
|
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)]
|
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)
|
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))
|
client.append_message(stream_name, Util.validate_and_serialize_to_json_bytes(valueEntry))
|
||||||
@@ -66,11 +66,11 @@ def sendData(data,property_alias):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def sendCheck(data, device):
|
def sendCheckDevice(data, group, device):
|
||||||
last_send_time = config["devices"][device]['tag_data'][data.tag]['last_send_time']
|
last_send_time = config[group]["devices"][device]['tag_data'][data.tag]['last_send_time']
|
||||||
guaranteed_send_time = config["devices"][device]['tag_data'][data.tag]['guaranteed_send_seconds']
|
guaranteed_send_time = config[group]["devices"][device]['tag_data'][data.tag]['guaranteed_send_seconds']
|
||||||
last_send_value = config["devices"][device]['tag_data'][data.tag]['last_send_value']
|
last_send_value = config[group]["devices"][device]['tag_data'][data.tag]['last_send_value']
|
||||||
change_threshold = config["devices"][device]['tag_data'][data.tag]['change_threshold']
|
change_threshold = config[group]["devices"][device]['tag_data'][data.tag]['change_threshold']
|
||||||
|
|
||||||
if last_send_time == None:
|
if last_send_time == None:
|
||||||
return True
|
return True
|
||||||
@@ -83,6 +83,23 @@ def sendCheck(data, device):
|
|||||||
else:
|
else:
|
||||||
return False
|
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):
|
def sum(operands:list):
|
||||||
return math.fsum(operands)
|
return math.fsum(operands)
|
||||||
|
|
||||||
@@ -131,24 +148,27 @@ except ConnectionError or asyncio.TimeoutError:
|
|||||||
|
|
||||||
#Main Polling Loop
|
#Main Polling Loop
|
||||||
while(True):
|
while(True):
|
||||||
|
for group in config.keys():
|
||||||
values = {}
|
values = {}
|
||||||
for device in config["devices"].keys():
|
for device in config[group]["devices"].keys():
|
||||||
plc_address = config["devices"][device]['source_address']
|
plc_address = config[group]["devices"][device]['source_address']
|
||||||
tag_data = list(config["devices"][device]['tag_data'].keys())
|
tag_data = list(config[group]["devices"][device]['tag_data'].keys())
|
||||||
if len(tag_data) > 1:
|
if len(tag_data) > 1:
|
||||||
try:
|
try:
|
||||||
response = read_multiple(plc_address,tag_data)
|
response = read_multiple(plc_address,tag_data)
|
||||||
logging.info(f"Got back: {response}")
|
logging.info(f"Got back: {response}")
|
||||||
for tag in response:
|
for tag in response:
|
||||||
|
#logging.info(f"Processing: {tag.tag}")
|
||||||
if tag.tag in values.keys():
|
if tag.tag in values.keys():
|
||||||
values[tag.tag].append(tag.value)
|
values[tag.tag].append(tag.value)
|
||||||
else:
|
else:
|
||||||
values[tag.tag] = [tag.value]
|
values[tag.tag] = [tag.value]
|
||||||
if sendCheck(tag, device):
|
#logging.info(f"Values: {values}")
|
||||||
sendDataResp = sendData(tag, config["devices"][device]['tag_data'][response.tag]['property_alias'])
|
if sendCheckDevice(tag, group, device):
|
||||||
|
sendDataResp = sendData(tag.value, config[group]["devices"][device]['tag_data'][tag.tag]['property_alias'])
|
||||||
if sendDataResp:
|
if sendDataResp:
|
||||||
config["devices"][device]['tag_data'][response.tag]['last_send_value'] = response.value
|
config[group]["devices"][device]['tag_data'][tag.tag]['last_send_value'] = tag.value
|
||||||
config["devices"][device]['tag_data'][response.tag]['last_send_time'] = sendDataResp
|
config[group]["devices"][device]['tag_data'][tag.tag]['last_send_time'] = sendDataResp
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(e)
|
logging.error(e)
|
||||||
else:
|
else:
|
||||||
@@ -159,22 +179,22 @@ while(True):
|
|||||||
values[response.tag].append(response.value)
|
values[response.tag].append(response.value)
|
||||||
else:
|
else:
|
||||||
values[response.tag] = [response.value]
|
values[response.tag] = [response.value]
|
||||||
if sendCheck(response, device):
|
if sendCheckDevice(response, group, device):
|
||||||
sendDataResp = sendData(response, config["devices"][device]['tag_data'][response.tag]['property_alias'])
|
sendDataResp = sendData(response.value, config[group]["devices"][device]['tag_data'][response.tag]['property_alias'])
|
||||||
if sendDataResp:
|
if sendDataResp:
|
||||||
config["devices"][device]['tag_data'][response.tag]['last_send_value'] = response.value
|
config[group]["devices"][device]['tag_data'][response.tag]['last_send_value'] = response.value
|
||||||
config["devices"][device]['tag_data'][response.tag]['last_send_time'] = sendDataResp
|
config[group]["devices"][device]['tag_data'][response.tag]['last_send_time'] = sendDataResp
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(e)
|
logging.error(e)
|
||||||
for func in config["functions"].keys():
|
for func in config[group]["functions"].keys():
|
||||||
operands = []
|
operands = []
|
||||||
for x in func["operands"]:
|
for x in config[group]["functions"][func]["operands"]:
|
||||||
operands = operands + values[x]
|
operands = operands + values[x]
|
||||||
value = operations[func["operation"]](operands)
|
value = operations[config[group]["functions"][func]["operation"]](operands)
|
||||||
#if sendCheck(response, device):
|
if sendCheckFunction(value, group, func):
|
||||||
sendDataResp = sendData(value, config["functions"][func]["property_alias"])
|
sendDataResp = sendData(value, config[group]["functions"][func]["property_alias"])
|
||||||
if sendDataResp:
|
if sendDataResp:
|
||||||
config["functions"][func]["last_send_value"] = value
|
config[group]["functions"][func]["last_send_value"] = value
|
||||||
config["fucntions"][func]["last_send_time"] = sendDataResp
|
config[group]["functions"][func]["last_send_time"] = sendDataResp
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user