import time

def close_websckt():
	"""
		This function disconnects the web socket and exits any loops.
		Should be called when a project is saved or modified to stop 
		multiple threads running.
	
		Args:
	    	None
	
		Returns:
	    	N/A
	
		Raises:
	    	N/A
		"""
	
	fc = system.tag.readBlocking(["Configuration/FC"])
	fc_value = fc[0].value
	tag_provider = "[%s_SCADA_TAG_PROVIDER]" % (fc_value)
	system.tag.writeBlocking([tag_provider + "System/close_socket"],[0])
	running = system.util.getGlobals().get(fc_value, {}).get("wbsckt_running", 0) 
	if running:
		system.util.getGlobals()[fc_value]["wbsckt_running"] = False
	system.tag.writeBlocking(tag_provider + "System/wbsckt_running", [0])
	time.sleep(2)
	system.tag.writeBlocking([tag_provider + "System/close_socket"],[1])
	logger = system.util.getLogger("%s-WebSocket-Restart" % (fc))
	logger.info("Web-Socket closed due to restart, AWS.wbsckt_abort.close_websckt()")

def check_web_socket():
	"""
		This function checks to see if the "System/close_socket" tag is active
		(boolean on) or inactive (boolean off). If the tag is active the web 
		socket will run, if it is inactive then the web socket will stop running.
		This function is called from the main web socket gateway event. 
		Used to exit the web socket gateway event. 
	
		Args:
	    	None
	
		Returns:
	    	True or False
	
		Raises:
	    	N/A
	"""
	request_to_close = system.tag.readBlocking(["System/close_socket"])
	request_to_close_val = request_to_close[0].value
	if not request_to_close_val:
		return True
	else:
		return False
