Created
August 31, 2022 10:42
-
-
Save EiichiroIto/3716d92771bcd9f50dbe367425f341c6 to your computer and use it in GitHub Desktop.
ScratchLink sample program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from websocket_server import WebsocketServer | |
import json | |
import base64 | |
import time | |
from threading import Timer | |
connecting = False | |
def update(client,server): | |
while connecting: | |
print("sending {}->{}".format(client,server)) | |
b64 = base64.b64encode(bytes([0,123,1,0,1,1,1,1,1,1])).decode('utf-8') | |
response = '{"jsonrpc":"2.0","method":"characteristicDidChange","params":{"serviceId":61445,"characteristicId":"5261da01-fa7e-42ab-850b-7c80220097cc","encoding":"base64","message":"' + b64 + '"}}' | |
server.send_message(client, response) | |
time.sleep(1) | |
print("Update exited") | |
# Called for every client connecting (after handshake) | |
def new_client(client, server): | |
print("New client connected and was given id %d" % client['id']) | |
server.send_message_to_all("Hey all, a new client has joined us") | |
# Called for every client disconnecting | |
def client_left(client, server): | |
global connecting | |
print("Client(%d) disconnected" % client['id']) | |
connecting = False | |
# Called when a client sends a message | |
def message_received(client, server, message): | |
global connecting | |
if len(message) > 200: | |
message = message[:200]+'..' | |
print("Client(%d) said: %s" % (client['id'], message)) | |
dict = json.loads(message) | |
if dict['method'] == 'discover': | |
id = dict['id'] | |
response = '{"jsonrpc":"2.0","id":'+str(id)+',"result":null}' | |
server.send_message(client, response) | |
response = '{"jsonrpc":"2.0","method":"didDiscoverPeripheral","params":{"name":"python","rssi":-70,"peripheralId":65536}}' | |
server.send_message(client, response) | |
if dict['method'] == 'connect': | |
id = dict['id'] | |
response = '{"jsonrpc":"2.0","id":'+str(id)+',"result":null}' | |
server.send_message(client, response) | |
if dict['method'] == 'read': | |
connecting = True | |
timer = Timer(1, update, (client, server)) | |
timer.start() | |
if dict['method'] == 'write': | |
message = base64.b64decode(dict['params']['message']) | |
if message[0] == 0x81: | |
print('Say "'+message[1:].decode('utf-8')+'"') | |
elif message[0] == 0x82: | |
for x in list(message[1:]): | |
print(format(x,"05b")[::-1]) | |
id = dict['id'] | |
response = '{"jsonrpc":"2.0","id":'+str(id)+',"result":null}' | |
server.send_message(client, response) | |
PORT=20111 | |
server = WebsocketServer(port = PORT) | |
server.set_fn_new_client(new_client) | |
server.set_fn_client_left(client_left) | |
server.set_fn_message_received(message_received) | |
server.run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment