def AvgCalc():
	## Code to calc avg latency.
	whid = system.tag.readBlocking(["Configuration/FC"])[0].value
	
	# Read tag value
	tag_values = system.tag.readBlocking(['[%s_SCADA_TAG_PROVIDER]Latency/rolling_latency'% whid])[0].value
	
	# Convert string timestamps to milliseconds
	timestamps = [int(value) for value in tag_values]
	
	if 0 in timestamps:
		return
	else:
		# Calculate the average latency in milliseconds
		avg_latency = round(sum(timestamps) / len(tag_values), 3)
		zero_list = [0]*30
		circular_buffer = '[%s_SCADA_TAG_PROVIDER]Latency/rolling_latency.value'% whid
		avg_latency_path ='[%s_SCADA_TAG_PROVIDER]Latency/avg_latency'% whid
		
		# clear array to all 0's so we can wait to trigger until 30 new records have happened and write new avg
		system.tag.writeBlocking([avg_latency_path,circular_buffer], [avg_latency,zero_list]) # writing the avg_latency back to tag on purpose for visual.
		
		# Call Dynamodb write script, passing in site and latency to be written. 
		Latency.WriteToDynamo.DynamoWriter(whid,avg_latency)