Untitled
unknown
plain_text
2 years ago
4.1 kB
22
Indexable
import paho.mqtt.client as paho
import bpy
import json
import random
import threading
global RobotMove, playerMove
client_id = f'python-mqtt-{random.randint(0, 1000)}'
client = paho.Client(paho.CallbackAPIVersion.VERSION1,client_id)
# Define the MQTT connection
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
client.subscribe("PLCupdates")
client.on_connect = on_connect
# Connect to MQTT broker
client.connect("localhost", 1883, 60)
positions = {
'A1': 'WhiteRook1', 'B1': 'WhiteKnight1', 'C1': 'WhiteBishop1', 'D1': 'WhiteQueen',
'E1': 'WhiteKing', 'F1': 'WhiteBishop2', 'G1': 'WhiteKnight2', 'H1': 'WhiteRook2',
'A2': 'WhitePawn1', 'B2': 'WhitePawn2', 'C2': 'WhitePawn3', 'D2': 'WhitePawn4',
'E2': 'WhitePawn5', 'F2': 'WhitePawn6', 'G2': 'WhitePawn7', 'H2': 'WhitePawn8',
'A7': 'BlackPawn1', 'B7': 'BlackPawn2', 'C7': 'BlackPawn3', 'D7': 'BlackPawn4',
'E7': 'BlackPawn5', 'F7': 'BlackPawn6', 'G7': 'BlackPawn7', 'H7': 'BlackPawn8',
'A8': 'BlackRook1', 'B8': 'BlackKnight1', 'C8': 'BlackBishop1', 'D8': 'BlackQueen',
'E8': 'BlackKing', 'F8': 'BlackBishop2', 'G8': 'BlackKnight2', 'H8': 'BlackRook2',
}
for square_name, piece_name in positions.items():
piece = bpy.data.objects.get(piece_name)
square = bpy.data.objects.get(square_name)
if piece and square:
piece.location.x = square.location.x - 0.01
piece.location.y = square.location.y + 0.05
def movement(startPos, endPos):
#extract the name of the piece on the corresponding start square and end square
start_piece_name = positions.get(startPos)
end_piece_name = positions.get(endPos)
#handles captures
if end_piece_name:
end_piece = bpy.data.objects.get(end_piece_name)
end_piece.location.x = 0
if len(endPos) == 2:
#sets a variable that accesses the object in the layout
start_piece = bpy.data.objects.get(start_piece_name)
# sets a variable that access the square object where it will be moved
end_square = bpy.data.objects.get(endPos)
if start_piece and end_square:
# Lift the piece up
start_piece.location.z += 1.0
# Move to the new position while lifted
start_piece.location.x = end_square.location.x
start_piece.location.y = end_square.location.y
# Place the piece down
start_piece.location.z -= 1.0
#Update the position dictionary
positions[endPos] = start_piece_name
del positions[startPos]
for piece_name in positions.values():
piece = bpy.data.objects.get(piece_name)
# Define the MQTT message callback
def on_message(client, userdata, message):
try:
global robotMove, playerMove
# Decode message payload from binary to a string
decoded_message = message.payload.decode('utf-8')
print(decoded_message)
data = json.loads(decoded_message)
# accessing variables
robotMove = data.get('robotMove')
playerMove = data.get('playerMove')
#Extracting to and from positions for robotMove
if len(robotMove) == 4:
startPos = robotMove.upper()[:2]
endPos = robotMove.upper()[2:]
movement(startPos, endPos)
#Extracting to and from positions for playerMove
if len(playerMove) == 4:
startPos = playerMove.upper()[:2]
endPos = playerMove.upper()[2:]
movement(startPos, endPos)
except json.JSONDecodeError as e:
print("Error decoding JSON: " + str(e))
client.on_message = on_message
def run_mqtt_client():
client.loop_forever()
mqtt_thread = threading.Thread(target=run_mqtt_client)
mqtt_thread.daemon = True
mqtt_thread.start()
Editor is loading...
Leave a Comment