Lab_interaccio/2023/Particle_2_0/server_mini.py

145 lines
7.1 KiB
Python
Raw Normal View History

2025-02-25 21:29:42 +01:00
from PIL import Image
import socket
import time
import math
Particle_mini = False
localIP = "192.168.3.2"
#localIP = "192.168.1.138"
localPort = 6454
bufferSize = 530
CAP_MINI_01 =b'\x42\x4D\xB2\x4D\x00\x00\x00\x00\x00\x00\x36\x00\x00\x00\x28\x00\x00\x00\x39\x00\x00\x00\x74\x00\x00\x00\x01\x00\x18\x00\x00\x00\x00\x00\x7C\x4D\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
mode = 1
convert_jpeg = False
if Particle_mini==True:
LEDS_TOTAL_1 = 57
LEDS_TOTAL_2 = 43
LEDS_TOTAL_3 = 29
LEDS_TOTAL_4 = 15
else:
LEDS_TOTAL_1 = 113
LEDS_TOTAL_2 = 85
LEDS_TOTAL_3 = 57
LEDS_TOTAL_4 = 29
PARTS_1 = math.ceil(LEDS_TOTAL_1*2/4)*4
PARTS_2 = math.ceil(LEDS_TOTAL_2*2/4)*4
PARTS_3 = math.ceil(LEDS_TOTAL_3*2/4)*4
PARTS_4 = math.ceil(LEDS_TOTAL_4*2/4)*4
print("{f} {g} {h} {i}".format(f=PARTS_1, g=PARTS_2, h=PARTS_3, i=PARTS_4))
NUM_UNIVERSES_1 = math.ceil(LEDS_TOTAL_1*3*PARTS_1/510)
NUM_UNIVERSES_2 = math.ceil(LEDS_TOTAL_2*3*PARTS_2/510)
NUM_UNIVERSES_3 = math.ceil(LEDS_TOTAL_3*3*PARTS_3/510)
NUM_UNIVERSES_4 = math.ceil(LEDS_TOTAL_4*3*PARTS_4/510)
print("{f} {g} {h} {i}".format(f=NUM_UNIVERSES_1, g=NUM_UNIVERSES_2, h=NUM_UNIVERSES_3, i=NUM_UNIVERSES_4))
INI_UNIVERSE_1 = 0
INI_UNIVERSE_2 = NUM_UNIVERSES_1
INI_UNIVERSE_3 = NUM_UNIVERSES_1 + NUM_UNIVERSES_2
INI_UNIVERSE_4 = NUM_UNIVERSES_1 + NUM_UNIVERSES_2 + NUM_UNIVERSES_3
END_UNIVERSE = INI_UNIVERSE_4 + NUM_UNIVERSES_4
ART_DMX = 0X5000
ART_NET_ID = "Art-Net"
ART_DMX_START = 18
P01_numbers = [ [0 for i in range(530)] for j in range(NUM_UNIVERSES_1) ]
P02_numbers = [ [0 for i in range(530)] for j in range(NUM_UNIVERSES_2) ]
P03_numbers = [ [0 for i in range(530)] for j in range(NUM_UNIVERSES_3) ]
P04_numbers = [ [0 for i in range(530)] for j in range(NUM_UNIVERSES_4) ]
#P01_BIG_DMX = [[0] for j in range(18+NUM_UNIVERSES_1*510*3)]
P01_SIZE = 1400#18+NUM_UNIVERSES_1*510*3
P01_BIG_DMX = bytearray(P01_SIZE)
count_big_dmx_01 = 0
#print("{f}".format(f=P01_BIG_DMX))
mini01 = ("192.168.3.10", 6454)
mini02 = ("192.168.3.11", 6454)
mini03 = ("192.168.3.12", 6454)
mini04 = ("192.168.3.13", 6454)
##msgFromServer = "Hello UDP Client"
##bytesToSend = str.encode(msgFromServer)
# Create a datagram socket
UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
# Bind to address and ip
UDPServerSocket.bind((localIP, localPort))
print("UDP server up and listening")
# Listen for incoming datagrams
while(True):
bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)
message = bytesAddressPair[0]
address = bytesAddressPair[1]
clientMsg = "Message from Client:{}".format(message)
msg = "{}".format(message)
result = msg.find(ART_NET_ID)
if (result>=0):
opcode = message[8] | message[9] << 8
if (opcode == ART_DMX):
incomingUniverse = message[14] | message[15] << 8
dmxDataLength = message[17] | message[16] << 8
#print("{f} found with universe: {u}, DataLength: {n}.".format(f=ART_NET_ID, u= incomingUniverse, n=dmxDataLength))
if mode==0: #echo
for i in range(dmxDataLength):
if message[ART_DMX_START+i]!=P01_numbers[incomingUniverse][i]:
P01_numbers[incomingUniverse] = message[ART_DMX_START:(dmxDataLength+ART_DMX_START)] ##Copia los 512 valores del ARRAY
UDPServerSocket.sendto(message, mini01)
#print("Sended")
break
elif mode==1: #delay entre paquetes
if incomingUniverse<INI_UNIVERSE_2:P01_numbers[incomingUniverse] = message[:] ##Copia todos los valores del ARRAY
elif incomingUniverse<(INI_UNIVERSE_3):P02_numbers[incomingUniverse-INI_UNIVERSE_2] = message[:] ##Copia todos los valores del ARRAY
elif incomingUniverse<(INI_UNIVERSE_4):P03_numbers[incomingUniverse-(INI_UNIVERSE_3)] = message[:] ##Copia todos los valores del ARRAY
elif incomingUniverse<(END_UNIVERSE):P04_numbers[incomingUniverse-(INI_UNIVERSE_4)] = message[:] ##Copia todos los valores del ARRAY
##if incomingUniverse==(END_UNIVERSE-1):
if incomingUniverse==(INI_UNIVERSE_2-1):
if convert_jpeg==True:
with open('mini01.bmp', 'wb') as fp:
in_x = 0
fp.write(CAP_MINI_01)
for h in range(NUM_UNIVERSES_1):
#fp.write(P01_numbers[h][ART_DMX_START:528])
for j in range(ART_DMX_START,528):
in_x=in_x+1
fp.write(P01_numbers[h][j:j+1])
if in_x==171:
fp.write(b'\x00')
in_x = 0
for h in range(NUM_UNIVERSES_1):
if h%3==0: fp.write(b'\x00')
image = Image.open('mini01.bmp')
# Specifying the RGB mode to the image
image.save("mini01.jpg")
for h in range(NUM_UNIVERSES_1):
UDPServerSocket.sendto(P01_numbers[h], mini01)
## if h<(NUM_UNIVERSES_2-1):
## UDPServerSocket.sendto(P02_numbers[h], mini02)
## if h<(NUM_UNIVERSES_3-1):
## UDPServerSocket.sendto(P03_numbers[h], mini03)
## if h<(NUM_UNIVERSES_4-1):
## UDPServerSocket.sendto(P04_numbers[h], mini04)
if h%4==0:
time.sleep(0.0011) # Sleep for 3 seconds
elif mode==2: #agrupacion
if incomingUniverse<INI_UNIVERSE_2:
#print("{f}".format(f=incomingUniverse))
if incomingUniverse == INI_UNIVERSE_1:
#print("{f}".format(f=message))
for h in range(530):
P01_BIG_DMX[h] = message[h] ##Copia todos los valores del ARRAY
P01_BIG_DMX[14] = 0x00FF & 0
P01_BIG_DMX[15] = 0x00FF&(0 >> 8)
P01_BIG_DMX[17] = 0x00FF & 18+P01_SIZE
P01_BIG_DMX[16] = 0x00FF&((18+P01_SIZE) >> 8)
#else:
#for h in range(510):
#P01_BIG_DMX[incomingUniverse*510 +h] = message[18+h] ##Copia todos los valores del ARRAY
if incomingUniverse==(END_UNIVERSE-1):
UDPServerSocket.sendto(P01_BIG_DMX, mini01)
incomingUniverse = P01_BIG_DMX[14] | P01_BIG_DMX[15] << 8
dmxDataLength = P01_BIG_DMX[17] | P01_BIG_DMX[16] << 8
##print("{f} {g}".format(f=incomingUniverse, g=dmxDataLength))
##print("{f}".format(f=P01_BIG_DMX))