|
| 1 | +# Description: This is the client script that runs on the machine connected to Blossom via USB. |
| 2 | +# This is only needed if you want to use network controller. |
| 3 | +# This is the standalone script that runs on the machine connected to Blossom via USB. |
| 4 | +blossom_id_ = 0 |
| 5 | +# Define blossom ID above |
| 6 | + |
| 7 | +import socketio |
| 8 | +import os |
| 9 | +import json |
| 10 | +import dotenv |
| 11 | +import logging |
| 12 | +from blossom_local_interface import BlossomLocalInterface |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | +logger.info("Client Started.") |
| 16 | + |
| 17 | +example_data = { |
| 18 | + "function": "do_start_sequence", |
| 19 | + "kwargs": { |
| 20 | + "blossom_id": 0, |
| 21 | + "delay_time": 0.5, |
| 22 | + "audio_length": 20, |
| 23 | + "seq": "reset" |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +class BlossomClient: |
| 29 | + def __init__(self, server_ip, server_port, blossom_id=0): |
| 30 | + self.bl = BlossomLocalInterface() |
| 31 | + self.blossom_id = blossom_id |
| 32 | + self.sio = socketio.Client() |
| 33 | + self.sio.connect(f"http://{server_ip}:{server_port}") |
| 34 | + self.sio.on('data_update', self.on_data_update) |
| 35 | + self.sio.on('connect', self.on_connect) |
| 36 | + self.sio.on('disconnect', self.on_disconnect) |
| 37 | + # self.sio.wait() |
| 38 | + |
| 39 | + def on_data_update(self, data): |
| 40 | + logger.info(f"Received data: {json.dumps(data, indent=2)}") |
| 41 | + if data["kwargs"]["blossom_id"] == self.blossom_id: |
| 42 | + if data["function"] == "do_sequence": |
| 43 | + self.bl.do_sequence(data["kwargs"]["seq"], data["kwargs"]["delay_time"]) |
| 44 | + elif data["function"] == "reset": |
| 45 | + self.bl.reset() |
| 46 | + else: |
| 47 | + logger.error(f"Function {data['function']} not found.") |
| 48 | + |
| 49 | + def wait(self): |
| 50 | + self.sio.wait() |
| 51 | + |
| 52 | + def on_connect(self): |
| 53 | + logger.info("Connected to server.") |
| 54 | + |
| 55 | + def on_disconnect(self): |
| 56 | + logger.info("Disconnected from server.") |
| 57 | + |
| 58 | + |
| 59 | +if __name__ == '__main__': |
| 60 | + dotenv.load_dotenv() |
| 61 | + client = BlossomClient(os.getenv("SERVER_IP"), os.getenv("SERVER_PORT"), blossom_id_) |
| 62 | + client.wait() |
0 commit comments