254 lines
7.4 KiB
Python
254 lines
7.4 KiB
Python
import requests
|
|
from pattern import PatternMeta
|
|
from status import Status
|
|
|
|
|
|
class Flickerstrip:
|
|
def __init__(self, ip_address):
|
|
"""The initializer for the Flickerstrip class.
|
|
|
|
Args:
|
|
ip_address (string): The ip address of the flickerstrip.
|
|
"""
|
|
self.ip_address = ip_address
|
|
self.status = None
|
|
|
|
def __checkResponse(self, response):
|
|
"""Check if the request was succesful.
|
|
|
|
Args:
|
|
response (requests.Response): The response from the request.
|
|
|
|
Returns:
|
|
boolean: If the request was successful
|
|
"""
|
|
json = response.json()
|
|
respType = json["type"]
|
|
if respType == "error":
|
|
message = json["message"]
|
|
print(f"ERROR: Request failed with message \"{message}\".")
|
|
return False
|
|
elif respType == "OK":
|
|
return True
|
|
elif respType == "status":
|
|
self.status = Status.fromJSON(json)
|
|
return True
|
|
else:
|
|
print(f"ERROR: Unrecognized response type: {respType}.")
|
|
return False
|
|
|
|
def __getRequest(self, path, params=None):
|
|
"""Send a GET request to the strip.
|
|
|
|
Args:
|
|
path (string): The path for the URL to request.
|
|
params (dictionary, optional): The query string params for the
|
|
request. Defaults to None.
|
|
|
|
Returns:
|
|
boolean: If the request was successful
|
|
"""
|
|
resp = requests.get(f"http://{self.ip_address}/{path}", params=params)
|
|
return self.__checkResponse(resp)
|
|
|
|
def __postRequest(self, path, json=None, data=None):
|
|
"""Send a POST request to the strip.
|
|
|
|
Args:
|
|
path (string): The path for the URL to request.
|
|
json (dictionary, optional): The JSON encodable body for the
|
|
request. Defaults to None.
|
|
data (string, optional): The raw data body. Defaults to None.
|
|
|
|
Returns:
|
|
boolean: If the request was successful
|
|
"""
|
|
resp = requests.post(f"http://{self.ip_address}/{path}",
|
|
json=json, data=data)
|
|
return self.__checkResponse(resp)
|
|
|
|
def forceUpdate(self):
|
|
"""Force updates the status of the strip.
|
|
|
|
Returns:
|
|
boolean: If the request was succesful
|
|
"""
|
|
return self.__getRequest("status")
|
|
|
|
def powerOn(self):
|
|
"""Trigger the strip to turn the lights on.
|
|
|
|
Returns:
|
|
boolean: If the request was succesful
|
|
"""
|
|
return self.__getRequest("power/on")
|
|
|
|
def powerOff(self):
|
|
"""Trigger the strip to turn the lights off.
|
|
|
|
Returns:
|
|
boolean: If the request was succesful
|
|
"""
|
|
return self.__getRequest("power/off")
|
|
|
|
def powerToggle(self):
|
|
"""Trigger the strip to toggle the power status.
|
|
|
|
Returns:
|
|
boolean: If the request was succesful
|
|
"""
|
|
return self.__getRequest("power/toggle")
|
|
|
|
def nextPattern(self):
|
|
"""Trigger the strip to switch to the next pattern in memory.
|
|
|
|
Returns:
|
|
boolean: If the request was succesful
|
|
"""
|
|
return self.__getRequest("pattern/next")
|
|
|
|
def freezeFrame(self, frame):
|
|
"""Freeze the animation at the specified frame.
|
|
|
|
Args:
|
|
frame (int): The frame number to freeze on.
|
|
|
|
Returns:
|
|
boolean: If the request was succesful
|
|
"""
|
|
return self.__getRequest("pattern/frame", {"value": frame})
|
|
|
|
def setPattern(self, index):
|
|
"""Change the current pattern to the pattern at the specified index.
|
|
|
|
Args:
|
|
index (int): The index of the pattern.
|
|
|
|
Returns:
|
|
boolean: If the request was succesful
|
|
"""
|
|
return self.__getRequest("pattern/select", {"index": index})
|
|
|
|
def deletePattern(self, pattern=None, index=None, id=None):
|
|
if pattern is not None:
|
|
if not isinstance(pattern, PatternMeta):
|
|
raise TypeError(
|
|
"Expected a PatternMeta object for the pattern arg.")
|
|
return self.deletePattern(id=pattern.id)
|
|
elif index is not None:
|
|
return self.__getRequest("pattern/forget", {"index": index})
|
|
elif id is not None:
|
|
return self.__getRequest("pattern/forget", {"id": id})
|
|
else:
|
|
raise TypeError(
|
|
"Deleting a pattern requires one of the three args.")
|
|
|
|
def setBrightness(self, value):
|
|
"""Set the brightness of the flickerstrip.
|
|
|
|
Args:
|
|
value (int): The brightness level as an int from 0 to 100.
|
|
|
|
Returns:
|
|
boolean: If the request was succesful
|
|
"""
|
|
return self.__getRequest("brightness", {"value": value})
|
|
|
|
def setName(self, value):
|
|
"""Set the name of the flickerstrip.
|
|
|
|
Args:
|
|
value (string): The new name.
|
|
|
|
Returns:
|
|
boolean: If the request was succesful
|
|
"""
|
|
return self.__postRequest("config/name", {"name": value})
|
|
|
|
def setGroup(self, value):
|
|
"""Set the group of the flickerstrip.
|
|
|
|
Args:
|
|
value (string): The new group.
|
|
|
|
Returns:
|
|
boolean: If the request was succesful
|
|
"""
|
|
return self.__postRequest("config/group", {"name": value})
|
|
|
|
def setCycle(self, value):
|
|
"""Set the cycle timer of the flickerstrip.
|
|
|
|
Args:
|
|
value (int): How long to cycle to next pattern.
|
|
Value is handled in seconds.
|
|
|
|
Returns:
|
|
boolean: If the request was succesful
|
|
"""
|
|
return self.__getRequest("config/cycle", {"value": value})
|
|
|
|
def setFadeDuration(self, value):
|
|
"""Set the fade timer of the flickerstrip.
|
|
|
|
Args:
|
|
value (int): How long to fade to next pattern.
|
|
Value is handled in seconds.
|
|
|
|
Returns:
|
|
boolean: If the request was succesful
|
|
"""
|
|
return self.__getRequest("config/fade", {"value": value})
|
|
|
|
def setStripLength(self, value):
|
|
"""Set the length of the flickerstrip.
|
|
|
|
Args:
|
|
value (int): The length of the strip.
|
|
|
|
Returns:
|
|
boolean: If the request was succesful
|
|
"""
|
|
return self.__getRequest("config/length", {"value": value})
|
|
|
|
def setStripStart(self, value):
|
|
"""Set the start pixel of the strip.
|
|
|
|
This will make the flickerstrip ignore all pixels before
|
|
the given number, set to 0 for the first pixel.
|
|
|
|
Args:
|
|
value (int): The new first pixel of the strip.
|
|
|
|
Returns:
|
|
boolean: If the request was successful.
|
|
"""
|
|
return self.__getRequest("config/start", {"value": value})
|
|
|
|
def setStripEnd(self, value):
|
|
"""Set the end pixel of the strip.
|
|
|
|
This will make the flickerstrip ignore all pixels after
|
|
the given number, set to -1 for the last pixel.
|
|
|
|
Args:
|
|
value (int): The new last pixel of the strip.
|
|
|
|
Returns:
|
|
boolean: If the request was successful.
|
|
"""
|
|
return self.__getRequest("config/end", {"value": value})
|
|
|
|
def setReversed(self, value):
|
|
"""Set the reversed state of the flickerstrip.
|
|
|
|
Args:
|
|
value (boolean): If the flickerstrip should animate
|
|
patterns in reverse.
|
|
|
|
Returns:
|
|
boolean: If the request was succesful
|
|
"""
|
|
return self.__getRequest("config/reversed",
|
|
{"value": 1 if value else 0})
|