Skip to content

Commit

Permalink
Reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
badaix committed Feb 17, 2024
1 parent 48b2840 commit baf09b3
Show file tree
Hide file tree
Showing 6 changed files with 437 additions and 308 deletions.
91 changes: 51 additions & 40 deletions control/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,72 @@
import json

if len(sys.argv) < 3:
print("usage: control.py <SERVER HOST> [setVolume|setName]")
sys.exit(0)
print("usage: control.py <SERVER HOST> [setVolume|setName]")
sys.exit(0)

telnet = telnetlib.Telnet(sys.argv[1], 1705)
requestId = 1

def doRequest( j, requestId ):
print("send: " + j)
telnet.write(j + "\r\n")
while (True):
response = telnet.read_until("\r\n", 2)
jResponse = json.loads(response)
if 'id' in jResponse:
if jResponse['id'] == requestId:
print("recv: " + response)
return jResponse;

def doRequest(j, requestId):
print("send: " + j)
telnet.write(j + "\r\n")
while (True):
response = telnet.read_until("\r\n", 2)
jResponse = json.loads(response)
if 'id' in jResponse:
if jResponse['id'] == requestId:
print("recv: " + response)
return jResponse


def setVolume(client, volume):
global requestId
doRequest(json.dumps({'jsonrpc': '2.0', 'method': 'Client.SetVolume', 'params': {'client': client, 'volume': volume}, 'id': requestId}), requestId)
requestId = requestId + 1
global requestId
doRequest(json.dumps({'jsonrpc': '2.0', 'method': 'Client.SetVolume', 'params': {
'client': client, 'volume': volume}, 'id': requestId}), requestId)
requestId = requestId + 1


def setName(client, name):
global requestId
doRequest(json.dumps({'jsonrpc': '2.0', 'method': 'Client.SetName', 'params': {'client': client, 'name': name}, 'id': requestId}), requestId)
requestId = requestId + 1
global requestId
doRequest(json.dumps({'jsonrpc': '2.0', 'method': 'Client.SetName', 'params': {
'client': client, 'name': name}, 'id': requestId}), requestId)
requestId = requestId + 1


if sys.argv[2] == "setVolume":
if len(sys.argv) < 5:
print("usage: control.py <SERVER HOST> setVolume <HOSTNAME> [+/-]<VOLUME>")
exit(0)
volstr = sys.argv[4]
j = doRequest(json.dumps({'jsonrpc': '2.0', 'method': 'Server.GetStatus', 'id': 1}), 1)
for client in j["result"]["clients"]:
if(sys.argv[3] == client['host']['name'] or sys.argv[3] == 'all'):
if(volstr[0] == '+'):
volume = int(client['config']['volume']['percent']) + int(volstr[1:])
elif(volstr[0] == '-'):
volume = int(client['config']['volume']['percent']) - int(volstr[1:])
else:
volume = int(volstr)
setVolume(client['host']['mac'], volume)
if len(sys.argv) < 5:
print(
"usage: control.py <SERVER HOST> setVolume <HOSTNAME> [+/-]<VOLUME>")
exit(0)
volstr = sys.argv[4]
j = doRequest(json.dumps(
{'jsonrpc': '2.0', 'method': 'Server.GetStatus', 'id': 1}), 1)
for client in j["result"]["clients"]:
if (sys.argv[3] == client['host']['name'] or sys.argv[3] == 'all'):
if (volstr[0] == '+'):
volume = int(client['config']['volume']
['percent']) + int(volstr[1:])
elif (volstr[0] == '-'):
volume = int(client['config']['volume']
['percent']) - int(volstr[1:])
else:
volume = int(volstr)
setVolume(client['host']['mac'], volume)

elif sys.argv[2] == "setName":
if len(sys.argv) < 5:
print("usage: control.py <SERVER HOST> setName <MAC> <NAME>")
exit(0)
setName(sys.argv[3], sys.argv[4])
if len(sys.argv) < 5:
print("usage: control.py <SERVER HOST> setName <MAC> <NAME>")
exit(0)
setName(sys.argv[3], sys.argv[4])

else:
print("unknown command \"" + sys.argv[2] + "\"")
print("unknown command \"" + sys.argv[2] + "\"")

j = doRequest(json.dumps({'jsonrpc': '2.0', 'method': 'Server.GetStatus', 'id': 1}), 1)
j = doRequest(json.dumps(
{'jsonrpc': '2.0', 'method': 'Server.GetStatus', 'id': 1}), 1)
for client in j["result"]["clients"]:
print("MAC: " + client['host']['mac'] + ", connect: " + str(client['connected']) + ", volume: " + str(client['config']['volume']['percent']) + ", name: " + client['host']['name'] + ", host: " + client['host']['ip'])
print("MAC: " + client['host']['mac'] + ", connect: " + str(client['connected']) + ", volume: " + str(
client['config']['volume']['percent']) + ", name: " + client['host']['name'] + ", host: " + client['host']['ip'])

telnet.close

42 changes: 24 additions & 18 deletions control/setVolume.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,38 @@
telnet = telnetlib.Telnet(sys.argv[1], 1705)
requestId = 1

def doRequest( j, requestId ):
print("send: " + j)
telnet.write(j + "\r\n")
while (True):
response = telnet.read_until("\r\n", 2)
jResponse = json.loads(response)
if 'id' in jResponse:
if jResponse['id'] == requestId:
# print("recv: " + response)
return jResponse;

def doRequest(j, requestId):
print("send: " + j)
telnet.write(j + "\r\n")
while (True):
response = telnet.read_until("\r\n", 2)
jResponse = json.loads(response)
if 'id' in jResponse:
if jResponse['id'] == requestId:
# print("recv: " + response)
return jResponse


def setVolume(client, volume):
global requestId
doRequest(json.dumps({'jsonrpc': '2.0', 'method': 'Client.SetVolume', 'params': {'id': client, 'volume': {'muted': False, 'percent': volume}}, 'id': requestId}), requestId)
requestId = requestId + 1
global requestId
doRequest(json.dumps({'jsonrpc': '2.0', 'method': 'Client.SetVolume', 'params': {
'id': client, 'volume': {'muted': False, 'percent': volume}}, 'id': requestId}), requestId)
requestId = requestId + 1


volume = int(sys.argv[2])
j = doRequest(json.dumps({'jsonrpc': '2.0', 'method': 'Server.GetStatus', 'id': 1}), 1)
j = doRequest(json.dumps(
{'jsonrpc': '2.0', 'method': 'Server.GetStatus', 'id': 1}), 1)
for group in j["result"]["server"]["groups"]:
for client in group["clients"]:
setVolume(client['id'], volume)
setVolume(client['id'], volume)

j = doRequest(json.dumps({'jsonrpc': '2.0', 'method': 'Server.GetStatus', 'id': 1}), 1)
j = doRequest(json.dumps(
{'jsonrpc': '2.0', 'method': 'Server.GetStatus', 'id': 1}), 1)
for group in j["result"]["server"]["groups"]:
for client in group["clients"]:
print("MAC: " + client['host']['mac'] + ", name: " + client['config']['name'] + ", conntect: " + str(client['connected']) + ", volume: " + str(client['config']['volume']['percent']))
print("MAC: " + client['host']['mac'] + ", name: " + client['config']['name'] + ", conntect: " + str(
client['connected']) + ", volume: " + str(client['config']['volume']['percent']))

telnet.close

44 changes: 22 additions & 22 deletions control/testClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,36 @@
import time

if len(sys.argv) < 2:
print("usage: testClient.py <SERVER HOST>")
sys.exit(0)
print("usage: testClient.py <SERVER HOST>")
sys.exit(0)

telnet = telnetlib.Telnet(sys.argv[1], 1705)


class ReaderThread(threading.Thread):
def __init__(self, tn, stop_event):
super(ReaderThread, self).__init__()
self.tn = tn
self.stop_event = stop_event
def __init__(self, tn, stop_event):
super(ReaderThread, self).__init__()
self.tn = tn
self.stop_event = stop_event

def run(self):
while (not self.stop_event.is_set()):
response = self.tn.read_until("\r\n", 2)
if response:
print("received: " + response)
jresponse = json.loads(response)
print(json.dumps(jresponse, indent=2))
print("\r\n")
def run(self):
while (not self.stop_event.is_set()):
response = self.tn.read_until("\r\n", 2)
if response:
print("received: " + response)
jresponse = json.loads(response)
print(json.dumps(jresponse, indent=2))
print("\r\n")


def doRequest( str ):
print("send: " + str)
telnet.write(str)
time.sleep(1)
return;
def doRequest(str):
print("send: " + str)
telnet.write(str)
time.sleep(1)
return


t_stop= threading.Event()
t_stop = threading.Event()
t = ReaderThread(telnet, t_stop)
t.start()

Expand All @@ -58,7 +58,7 @@ def doRequest( str ):
time.sleep(5)


#doRequest("{\"jsonrpc\": \"2.0\", \"method\": \"Server.GetStatus\", \"params\": {\"client\": \"80:1f:02:ed:fd:e0\"}, \"id\": 2}\r\n")
# doRequest("{\"jsonrpc\": \"2.0\", \"method\": \"Server.GetStatus\", \"params\": {\"client\": \"80:1f:02:ed:fd:e0\"}, \"id\": 2}\r\n")
'''
doRequest("{\"jsonrpc\": \"2.0\", \"method\": \"Client.SetVolume\", \"params\": {\"client\": \"80:1f:02:ed:fd:e0\", \"volume\": 10}, \"id\": 3}\r\n")
doRequest("{\"jsonrpc\": \"2.0\", \"method\": \"Client.SetVolume\", \"params\": {\"client\": \"80:1f:02:ed:fd:e0\", \"volume\": 30}, \"id\": 4}\r\n")
Expand All @@ -83,6 +83,6 @@ def doRequest( str ):
'''
s = raw_input("")
print(s)
t_stop.set();
t_stop.set()
t.join()
telnet.close
11 changes: 7 additions & 4 deletions extras/package/debian/changelog_md2deb.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
with open(sys.argv[1], 'r') as file:
data = file.read()

data = re.sub('^\s*# Snapcast changelog *\n*', '', data, flags=re.MULTILINE)
data = re.sub('^\s*### ([a-zA-Z]+) *\n', r'\n * \1\n', data, flags=re.MULTILINE)
data = re.sub('^\s*## Version\s+(\S*) *\n', r'snapcast (\1-1) unstable; urgency=medium\n', data, flags=re.MULTILINE)
data = re.sub('^\s*# Snapcast changelog *\n*',
'', data, flags=re.MULTILINE)
data = re.sub('^\s*### ([a-zA-Z]+) *\n',
r'\n * \1\n', data, flags=re.MULTILINE)
data = re.sub('^\s*## Version\s+(\S*) *\n',
r'snapcast (\1-1) unstable; urgency=medium\n', data, flags=re.MULTILINE)
data = re.sub('^\s*-\s*(.*) *\n', r' -\1\n', data, flags=re.MULTILINE)
data = re.sub('^_(.*)_ *\n', r' -- \1\n\n', data, flags=re.MULTILINE)

print(data)
print(data)
Loading

0 comments on commit baf09b3

Please sign in to comment.