def api_controlleroff(): # Check if an ID was provided as part of the URL. # If ID is provided, assign it to a variable. # If no ID is provided, display an error in the browser. jsondata = request.get_json() if 'id' in jsondata: id = int(jsondata['id']) else: return "Error: No id field provided. Please specify an id." conn = dbc.dbconnect() curselect = conn.cursor(buffered=False) dbquery = "SELECT MacAddress, IP FROM Riverside WHERE ID={0}".format(id) curselect.execute(dbquery) results = curselect.fetchone() curselect.close() conn.close() control_interface = xled.ControlInterface(results[1], results[0]) hicontrol = xled.HighControlInterface(results[1]) control_interface.set_mode('off') # Use the jsonify function from Flask to convert our list of # Python dictionaries to the JSON format. return 200
def api_add(): jsondata = request.get_json() if 'timeout' in jsondata: timeout = int(jsondata['timeout']) else: return "Need timeout value" controllerlist = [] controllers = xled.discover.xdiscover(None, None, timeout) with suppress(xled.exceptions.DiscoverTimeout): for controller in controllers: controllerlist.append(controller) print(controllerlist) try: conn = dbc.dbconnect() for con in controllerlist: curinsert = conn.cursor(buffered=False) curselect = conn.cursor(buffered=False) curselect.execute( "SELECT StartChannel, StartUniverse, NumLEDS, ChannelsPerLED FROM Riverside ORDER BY id DESC LIMIT 1;" ) sel_results = curselect.fetchone() curselect.close() control_interface = xled.ControlInterface(con.ip_address, con.hw_address) device_info = control_interface.get_device_info() if not sel_results: curinsert.execute( "INSERT INTO rxtnet.Riverside(Name, MacAddress, IP, NumLEDS, ChannelsPerLED, StartChannel, StartUniverse) VALUES (?, ?, ?, ?, ?, ?, ?)", (con.id, con.hw_address, con.ip_address, device_info["number_of_led"], len(device_info["led_profile"]), '1', '0')) conn.commit() curinsert.close() conn.close() else: startchannel = sel_results[0] startuniverse = sel_results[1] usedchannels = sel_results[2] * sel_results[3] burnedchannels = ((usedchannels // 512) - 1) + ( (512 - startchannel + 1) % sel_results[3]) nextaddr = [ startuniverse + (usedchannels // 512), startchannel + (usedchannels % 512) + burnedchannels ] curinsert.execute( "INSERT INTO rxtnet.Riverside(Name, MacAddress, IP, NumLEDS, ChannelsPerLED, StartChannel, StartUniverse) VALUES (?, ?, ?, ?, ?, ?, ?)", (con.id, con.hw_address, con.ip_address, device_info["number_of_led"], len(device_info["led_profile"]), nextaddr[1], nextaddr[0])) conn.commit() curinsert.close() conn.close() except mariadb.Error as e: print(f"Error: {e}") return api_all()
def api_staticcolor(): # Check if an ID was provided as part of the URL. # If ID is provided, assign it to a variable. # If no ID is provided, display an error in the browser. jsondata = request.get_json() if 'id' in jsondata: id = int(jsondata['id']) else: return "Error: No id field provided. Please specify an id." if 'red' in jsondata: red = int(jsondata['red']) else: return "Error: No red field provided. Please specify an red." if 'green' in jsondata: green = int(jsondata['green']) else: return "Error: No green field provided. Please specify an green." if 'blue' in jsondata: blue = int(jsondata['blue']) else: return "Error: No blue field provided. Please specify an blue." conn = dbc.dbconnect() dbquery = "SELECT MacAddress, IP FROM Riverside WHERE ID={0}".format(id) curselect.execute(dbquery) results = curselect.fetchone() curselect.close() conn.close() control_interface = xled.ControlInterface(results[1], results[0]) hicontrol = xled.HighControlInterface(results[1]) control_interface.set_mode('movie') hicontrol.set_static_color(red, green, blue) # Use the jsonify function from Flask to convert our list of # Python dictionaries to the JSON format. return 200
with suppress(xled.exceptions.DiscoverTimeout): for controller in controllers: controllerlist.append(controller) print(controllerlist) try: for con in controllerlist: curinsert = conn.cursor(buffered=False) curselect = conn.cursor(buffered=False) curselect.execute( "SELECT StartChannel, StartUniverse, NumLEDS, ChannelsPerLED FROM Riverside ORDER BY id DESC LIMIT 1;" ) sel_results = curselect.fetchone() curselect.close() control_interface = xled.ControlInterface(con.ip_address, con.hw_address) device_info = control_interface.get_device_info() if not sel_results: curinsert.execute( "INSERT INTO rxtnet.Riverside(Name, MacAddress, IP, NumLEDS, ChannelsPerLED, StartChannel, StartUniverse) VALUES (?, ?, ?, ?, ?, ?, ?)", (con.id, con.hw_address, con.ip_address, device_info["number_of_led"], len( device_info["led_profile"]), '1', '0')) conn.commit() curinsert.close() else: startchannel = sel_results[0] startuniverse = sel_results[1] usedchannels = sel_results[2] * sel_results[3] burnedchannels = ((usedchannels // 512) - 1) + ( (512 - startchannel + 1) % sel_results[3])
import sys import socket import os import time import mmap sys.path.append("/home/server/git/RXTNet/xled") import xled filename = '/tmp/fd0' fd = os.open(filename, os.O_RDONLY) buf = mmap.mmap(fd, 0, mmap.MAP_SHARED, mmap.PROT_READ) #discovered_device = xled.discover.discover() #print(discovered_device) control_interface = xled.ControlInterface('192.168.3.218', '98:f4:ab:36:ae:39') hicontrol = xled.HighControlInterface('192.168.3.218') control_interface.set_mode('movie') hicontrol.set_static_color(255, 255, 255)
import sys sys.path.append("/home/server/git/RXTNet/xled") import xled import mariadb from contextlib import suppress # Connect to MariaDB Platform try: conn = mariadb.connect( user="******", password="******", host="127.0.0.1", port=3306, database="rxtnet" ) except mariadb.Error as e: print(f"Error connecting to MariaDB Platform: {e}") sys.exit(1) curselect = conn.cursor(buffered=False) conID = 43 curselect.execute("SELECT MacAddress, IP FROM Riverside WHERE ID=conID") results = curselect.fetchone() control_interface = xled.ControlInterface(results[1], results[0]) hicontrol = xled.HighControlInterface(results[1]) control_interface.set_mode('movie') hicontrol.set_static_color(255,255,255)
import xled from requests.compat import urljoin testcontroller = xled.ControlInterface('192.168.3.218', '98:f4:ab:36:ae:39') testcontroller.set_mode('movie')