-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.py
153 lines (119 loc) · 3.87 KB
/
command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!Encoding:utf-8
# Pixoo64 controller commands
"""Commands for controlling Pixoo64 by Divoom
Written by pf.tiger
"""
import requests
# import json
# import base64
FONT_LIST_URL = "https://app.divoom-gz.com/Device/GetTimeDialFontList"
IMG_UPLOAD_LIST_URL = "https://app.divoom-gz.com/Device/GetImgUploadList"
PIXOO64_URL = "http://10.0.0.61"
PIXOO64_IPV4 = "10.0.0.61"
BRIGHTNESS_MIN = 0
BRIGHTNESS_MAX = 100
COLORBALANCE_MIN = 0
COLORBALANCE_MAX = 100
class Command:
"""Treat pixoo64 commands as objects"""
def __init__(self, name, description, function, type):
self.name = name
self.description = description
self.function = function
self.type = type
def set_function(self, func):
self.function = func
def set_commandType(self, type):
self.type = type
def execute(self, *args, **kwargs):
if self.function:
return self.function(*args, **kwargs)
else:
print(f"No function set for the command '{self.name}'.")
def show_help(self):
print(f"Command: {self.name}")
print(f"Type: {self.type}")
print(self.function.__doc__)
print("Usage: ")
print(f" {self.name} <arguments>")
print("")
# Get
class Get_Command(Command):
def __init__(self, name, description, function, type):
super().__init__(name, description, function, "Get")
# Set
class Set_Command(Command):
def __init__(self, name, description, function, type):
super().__init__(name, description, function, "Set")
# Reset
class Reset_Command(Command):
def __init__(self, name, description, function, type):
super().__init__(name, description, function, "Reset")
# Bulk execution of the command given
def bulkExec():
pass
# individual functions
def httpRequest(ipv4addr, port=80, command = "Channel/GetIndex", **kwargs):
"""
function to send command via http.
Parameters
---
ipv4addr : str
port : int
command : str
**kwargs : str
Returns
----
result : json
parsed json data retrieved from pixoo64
"""
url = f"http://{ipv4addr}:{port}/post"
data = {"Command": command, **kwargs}
try:
response = requests.post(url, json=data)
response.raise_for_status() # Raise an exception for 4xx or 5xx errors
result = response.json()
if "error_code" in result and result["error_code"] == 0:
# Successful response, return parsed JSON
return result
else:
print(f"Error: {result.get('error_code', 'Unknown error')}")
return None
except requests.exceptions.RequestException as e:
print(f"Error making the request: {e}")
return None
# return in text
def get_device_info(ipv4addr):
"""
Gets the Pixoo64's device information. \n
More at Divoom's official documentation:\n
https://doc.divoom-gz.com/web/#/12?page_id=243
Parameters
----
ipv4addr : str
"""
return httpRequest(ipv4addr, 80, "Channel/GetAllConf")
def get_faceId(ipv4addr):
"""
Gets the Pixoo64's working Faces ID. \n
More at Divoom's official documentation:\n
https://doc.divoom-gz.com/web/#/12?page_id=239
Parameters
----
ipv4addr : str
"""
return httpRequest(ipv4addr, 80, "Channel/GetClockInfo")
def get_deviceTime(ipv4addr):
"""
Gets the Pixoo64's system time. \n
More at Divoom's official documentation:\n
https://doc.divoom-gz.com/web/#/12?page_id=337
Parameters
----
ipv4addr : str
"""
return httpRequest(ipv4addr, 80, "Device/GetDeviceTime")
# create command objects
getConfig = Get_Command("Get-Config", get_device_info.__doc__, get_device_info, "Get")
getFaceID = Get_Command("Get-FaceID", get_faceId.__doc__, get_faceId, "Get")
getDeviceTime = Get_Command("Get-DeviceTime", get_deviceTime.__doc__, get_deviceTime, "Get")