import cards.common
import controller
import time
import utils


import matplotlib.pyplot as plt


util = utils.utils()

# Initialize controller object
control = controller.vme_controller()
control.init_routing_table()

# initialize cards
my_card1 = cards.common.common_card(control, 55, 118)
# my_card2 = cards.common.common_card(control, 55, 117)

# print my_card1.util_free_ram()
time.sleep(1)
# Run experiment

op1 = [0x00] + util.int_to_bytes(0) + util.int_to_bytes(64) + util.int_to_bytes(1) + util.int_to_bytes(1)
op2 = [0xFF]


my_card1.exp_load_op(op1)
time.sleep(0.1)
my_card1.exp_load_op(op2)
time.sleep(0.1)
from flask import Flask
from controller import vme_controller
ctrlr = vme_controller()


app = Flask(__name__)


@app.route('/')
def hello_world():
	return 'You have reached the Open Controls Server'

@app.route('/init')
def init():
	ctrlr.init_routing_table()
	return("<p>Subrack ihitialized with " + str(ctrlr.num_devices()) + " devices!</p>")

@app.route('/list')
def list():
	address_array = ctrlr.poll_table()
	print("number of devices = " + str(len(address_array)) )
	html = ""
	html = html + "performing live address poll on subrack" + "</br>"
	for address in address_array:
		html = html + "found device with address:" + str(address) + "</br>"
	return html



#@app.route('/api', methods = ['POST','GET'])
#def api_handle():