49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
import socket
|
|
import datetime
|
|
|
|
port=55555
|
|
|
|
outlet_state = ['Off', 'Off', 'Off', 'Off', 'Off', 'Off', 'Off', 'Off']
|
|
|
|
socket_listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
def readAscii(connexion_client : socket):
|
|
message = connexion_client.recv(1024)
|
|
message = message.decode()
|
|
|
|
if message[0] == 'P' :
|
|
outlet_index = ord(message[21]) - ord('0')
|
|
match message[24]:
|
|
case 'n':
|
|
outlet_state[outlet_index] = 'On'
|
|
print("powering on " + str(outlet_index))
|
|
case 'f':
|
|
outlet_state[outlet_index] = 'Off'
|
|
print("powering off " + str(outlet_index))
|
|
case 'e':
|
|
outlet_state[outlet_index] = 'Restart'
|
|
print("restarting " + str(outlet_index))
|
|
if message[0] == 'G':
|
|
print("sending current state report")
|
|
time = datetime.datetime.now().strftime('%d %B %Y %H:%M:%S')
|
|
connexion_client.send(bytes("Hidden Page\n" + time + "\nVersion: 1.5.0.1\n" +
|
|
"M0:O1=" + outlet_state[0] + "\n"+
|
|
"M0:O2=" + outlet_state[1] + "\n"+
|
|
"M0:O3=" + outlet_state[2] + "\n"+
|
|
"M0:O4=" + outlet_state[3] + "\n"+
|
|
"M0:O5=" + outlet_state[4] + "\n"+
|
|
"M0:O6=" + outlet_state[5] + "\n"+
|
|
"M0:O7=" + outlet_state[6] + "\n"+
|
|
"M0:O8=" + outlet_state[7] + "\n", "UTF-8"))
|
|
|
|
|
|
|
|
|
|
|
|
socket_listener.bind(('', port))
|
|
socket_listener.listen()
|
|
|
|
while(True):
|
|
connexion_client, address_client = socket_listener.accept()
|
|
readAscii(connexion_client)
|