import requests
import time
import json

# Initialization

# Dummy data for motors
# Currently stored as an array corresponding to [motor 1, motor 2]
motor_values = []

# Robot from database to listen to
this_robot_id = 0
# Firebase database parameters
api_key = ""
URL = "https://emar-database.firebaseio.com/"
AUTH_URL = "https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=" + api_key;
headers = {'Content-type': 'application/json'}
auth_req_params = {"returnSecureToken":"true"}

# Start connection to Firebase and get anonymous authentication
connection = requests.Session()
connection.headers.update(headers)
auth_request = connection.post(url=AUTH_URL, params=auth_req_params)
auth_info = auth_request.json()
auth_params = {'auth': auth_info["idToken"]}

# Main loop

while(True):

	# Sending get request and obtaining the response
	get_request = connection.get(url = URL + "robots/0/state.json")
	# Extracting data in json format 
	robot_state = get_request.json()
	
	##############
	# Check if there is a new motor value in the database
	##############
	new_motor_values = robot_state["motors"]
	if (motor_values != new_motor_values):
		print("New motor values: " + str(new_motor_values))
		# TODO: Do something with the new motor values
		motor_values = new_motor_values
	
	time.sleep(0.1)

import requests
import time
import json

# Initialization

# Dummy data for motors
# Currently stored as an array corresponding to [motor 1, motor 2]
motor_values = []

# Robot from database to listen to
this_robot_id = 0
# Firebase database parameters
api_key = ""
URL = "https://emar-database.firebaseio.com/"
AUTH_URL = "https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=" + api_key;
headers = {'Content-type': 'application/json'}
auth_req_params = {"returnSecureToken":"true"}

# Start connection to Firebase and get anonymous authentication
connection = requests.Session()
connection.headers.update(headers)
auth_request = connection.post(url=AUTH_URL, params=auth_req_params)
auth_info = auth_request.json()
auth_params = {'auth': auth_info["idToken"]}

# Main loop

while(True):

	# Sending get request and obtaining the response
	get_request = connection.get(url = URL + "robots/0/state.json")
	# Extracting data in json format 
	robot_state = get_request.json()
	
	##############
	# Check if there is a new motor value in the database
	##############
	new_motor_values = robot_state["motors"]
	if (motor_values != new_motor_values):
		print("New motor values: " + str(new_motor_values))
		# TODO: Do something with the new motor values
		motor_values = new_motor_values
	
	time.sleep(0.1)

# Initialization # Dummy data for motors # Currently stored as an array corresponding to [motor 1, motor 2] # Robot from database to listen to # Firebase database parameters # Start connection to Firebase and get anonymous authentication # Main loop # Sending get request and obtaining the response # Extracting data in json format ############## # Check if there is a new motor value in the database ############## # TODO: Do something with the new motor values