def send_message(**kwargs):
	fc = system.tag.readBlocking("Configuration/FC")[0].value
	payload = kwargs
	message_type = kwargs.get("message_type")
	scope = kwargs.get("scope")
	source = kwargs.get("source")
	try:
		system.perspective.sendMessage(message_type, payload = payload, scope = scope)
	except:
		system.perspective.print(source, destination="client")
	
def update_device_filters(devices):
	payload= {}
	payload["data"] = devices
	system.perspective.sendMessage("update-device-filters", payload = payload, scope = "page")

def reset_historical_filters(action):
	payload = {}
	payload["data"] = action
	system.perspective.sendMessage("reset-historical-filters", payload = payload, scope = "page")

def update_source_id_filters(source_ids):
	payload= {}
	payload["data"] = source_ids
	system.perspective.sendMessage("update-source_id-filters", payload = payload, scope = "page")

def send_http_response_code(response):
	payload= {}
	payload["response"] = response
	system.perspective.sendMessage("http-response-code", payload = payload, scope = "page")
	
def update_historical(historical_data):
	payload= {}
	payload["data"] = historical_data
	system.perspective.sendMessage("update-historical-data", payload = payload, scope = "page")

def update_historical_first_request(historical_data):
	payload = {}
	payload["data"] = historical_data
	payload["initial_data"] = historical_data
	system.perspective.sendMessage("update-first-request", payload = payload, scope = "page")

def load_initial_data(request):
	#Pass a boolean to load the initial dataset.
	payload = {}
	payload["data"] = request
	system.perspective.sendMessage("load_initial_data", payload = payload, scope = "page")

def update_token_array(token_array):
	payload = {}
	payload["data"] = token_array
	system.perspective.sendMessage("update-token-array", payload = payload, scope = "page")

def show_historical_filters(action):
	payload = {}
	payload["data"] = action
	system.perspective.sendMessage("show-historical-filters", 
		payload = payload, scope = "page")

def update_initial_token(number):
	payload = {}
	payload["data"] = number
	system.perspective.sendMessage("update-initial-tokens", payload = payload, scope = "page")
								
def set_source_filters(self):
	filters = self.props.value
	payload = {}
	payload["data"] = filters
	system.perspective.sendMessage("set-source-filters", payload = payload,
	scope = "page")
		

def set_type_filters(self):
	filters = self.props.value
	payload = {}
	payload["data"] = filters
	system.perspective.sendMessage("set-type-filters", payload = payload,
	scope = "page")

def set_priority_filters(self):
	filters = self.props.value
	payload = {}
	payload["data"] = filters
	system.perspective.sendMessage("set-priority-filters", payload = payload,
	scope = "page")

def set_device_filters(self):
	filters = self.props.value
	payload = {}
	payload["data"] = filters
	system.perspective.sendMessage("set-device-filters", payload = payload,
	scope = "page")

def set_message_filters(self):
	filters = self.props.value
	payload = {}
	payload["data"] = filters
	system.perspective.sendMessage("set-message-filters", payload = payload,
	scope = "page")

def set_time_from_filters(self):
	"This filter sets the time from for history"
	time = self.props.value
	utc_offset = -self.session.props.device.timezone.utcOffset
	#time = system.date.addHours(time, utc_offset)
	epoch = system.date.toMillis(time)
	payload = {}
	payload["data"] = epoch
	system.perspective.sendMessage("set-from-filters", payload = payload,
	scope = "page")

def set_time_to_filters(self):
	"This filter sets the time to for history"
	time = self.props.value
	utc_offset = -self.session.props.device.timezone.utcOffset
	#time = system.date.addHours(time, utc_offset)
	epoch = system.date.toMillis(time)
	payload = {}
	payload["data"] = epoch
	system.perspective.sendMessage("set-to-filters", payload = payload,
	scope = "page")

def set_duration_filters(self):
	filters = self.props.value
	ms = filters * 1000
	payload = {}
	payload["data"] = ms
	system.perspective.sendMessage("set-duration-filters", payload = payload,
	scope = "page")

def update_page_number(page):
	payload = {}
	payload["data"] = page
	system.perspective.sendMessage("update-page-number", payload = payload,
	scope = "page")

def update_client_id(client_id):
	payload = {}
	payload["data"] = client_id
	system.perspective.sendMessage("update-client-id", payload = payload,
	scope = "page")

#Generic message handler scoped at view level
def view_message_handler(message, message_type):
	payload = {}
	payload["data"] = message
	system.perspective.sendMessage(message_type, payload, scope = "view")
