|
| 1 | +import bluetooth |
| 2 | +import pygatt |
| 3 | +import pathlib |
| 4 | +import time |
| 5 | +import json |
| 6 | +import hashlib |
| 7 | +from func_timeout import func_timeout, FunctionTimedOut |
| 8 | +import os |
| 9 | +import sys |
| 10 | +sys.path.append('.') |
| 11 | + |
| 12 | +from typing import Tuple |
| 13 | + |
| 14 | +import ai.action.move.movement |
| 15 | + |
| 16 | +mars = ai.action.move.movement.Movements() |
| 17 | + |
| 18 | + |
| 19 | +class BluetoothService: |
| 20 | + pass |
| 21 | + |
| 22 | + |
| 23 | +def list_devices() -> Tuple[str, str]: |
| 24 | + nearby_devices = bluetooth.discover_devices(lookup_names=True) |
| 25 | + return nearby_devices |
| 26 | + |
| 27 | + |
| 28 | +def filter_marscat(bluetooth_devices: Tuple[str, str]) -> Tuple[str, str]: |
| 29 | + marscats = [] |
| 30 | + for addr, name in bluetooth_devices: |
| 31 | + name.encode('utf-8', 'replace') |
| 32 | + if name == 'marscat': |
| 33 | + marscats.append((addr, name)) |
| 34 | + return marscats |
| 35 | + |
| 36 | + |
| 37 | +def start_marscat_bt() -> None: |
| 38 | + server_sock = bluetooth.BluetoothSocket(bluetooth.L2CAP) |
| 39 | + server_sock.bind(("", bluetooth.PORT_ANY)) |
| 40 | + server_sock.listen(1) |
| 41 | + port = server_sock.getsockname()[1] |
| 42 | + uuid = '26754beb-1bd0-4017-b341-154bed30b71a' |
| 43 | + name = 'marscat_bt' |
| 44 | + bluetooth.advertise_service( |
| 45 | + server_sock, |
| 46 | + name, |
| 47 | + service_id=uuid, |
| 48 | + service_classes=[uuid, bluetooth.SERIAL_PORT_CLASS], |
| 49 | + profiles=[bluetooth.SERIAL_PORT_PROFILE], |
| 50 | + provider='', |
| 51 | + description='', |
| 52 | + protocols=[bluetooth.OBEX_UUID]) |
| 53 | + print("Waiting for connection on RFCOMM channel {}".format(port)) |
| 54 | + |
| 55 | + client_sock, client_info = server_sock.accept() |
| 56 | + |
| 57 | + |
| 58 | + print("Accepted connection from", client_info) |
| 59 | + |
| 60 | + while True: |
| 61 | + time.sleep(0.1) |
| 62 | + try: |
| 63 | + data = client_sock.recv(1024) |
| 64 | + except OSError: |
| 65 | + print("Remote client disconnected") |
| 66 | + data = None |
| 67 | + |
| 68 | + if not data: |
| 69 | + client_sock.close() |
| 70 | + print("Close Client Socket") |
| 71 | + print("Waiting for connection on RFCOMM channel {}".format(port)) |
| 72 | + client_sock, client_info = server_sock.accept() |
| 73 | + print("Accepted connection from", client_info) |
| 74 | + continue |
| 75 | + |
| 76 | + if data == b'run': |
| 77 | + pass |
| 78 | + #mars.set_run() |
| 79 | + elif data == b'walk': |
| 80 | + pass |
| 81 | + #mars.set_walk() |
| 82 | + elif data == b'stand': |
| 83 | + pass |
| 84 | + #mars.set_stand() |
| 85 | + elif data == b'sit': |
| 86 | + pass |
| 87 | + #mars.set_sit() |
| 88 | + elif data == b'left': |
| 89 | + pass |
| 90 | + #mars.set_turn() |
| 91 | + elif data == b'right': |
| 92 | + pass |
| 93 | + #mars.set_turn(-1) |
| 94 | + elif data == b'send_update_start': |
| 95 | + print('start receive') |
| 96 | + |
| 97 | + # recv file information |
| 98 | + data = client_sock.recv(1024) |
| 99 | + file_len = int(str(data, encoding='utf8')) |
| 100 | + print('file size : ', file_len) |
| 101 | + |
| 102 | + data = client_sock.recv(1024) |
| 103 | + file_md5 = str(data, encoding='utf8') |
| 104 | + print(file_md5) |
| 105 | + |
| 106 | + # receive marsai.zip |
| 107 | + if os.path.exists(str(pathlib.Path.home()) + '/marsai.zip'): |
| 108 | + os.system( |
| 109 | + 'rm {}'.format(str(pathlib.Path.home()) + '/marsai.zip')) |
| 110 | + os.system( |
| 111 | + 'touch {}'.format(str(pathlib.Path.home()) + '/marsai.zip')) |
| 112 | + |
| 113 | + i = 0 |
| 114 | + buffer = 4096 |
| 115 | + |
| 116 | + def recv_data(buffer): |
| 117 | + print('start recv_data') |
| 118 | + data = client_sock.recv(buffer) |
| 119 | + return data |
| 120 | + |
| 121 | + with open(str(pathlib.Path.home()) + '/marsai.zip', 'ab+') as f: |
| 122 | + while file_len != 0: |
| 123 | + time.sleep(0.1) |
| 124 | + data = recv_data(buffer) |
| 125 | + #while True: |
| 126 | + # try: |
| 127 | + # time.sleep(0.1) |
| 128 | + # data = func_timeout(1, recv_data, (buffer, )) |
| 129 | + # except FunctionTimedOut: |
| 130 | + # print('time out') |
| 131 | + # client_sock.send(str(file_len).encode('utf-8')) |
| 132 | + # client_sock.close() |
| 133 | + # print('client closed') |
| 134 | + # client_sock, client_info = server_sock.accept() |
| 135 | + # print('accepted') |
| 136 | + # except bluetooth.BluetoothError as e: |
| 137 | + # print("error") |
| 138 | + # else: |
| 139 | + # break |
| 140 | + f.write(data) |
| 141 | + print('write', i, len(data)) |
| 142 | + i += 1 |
| 143 | + file_len -= len(data) |
| 144 | + print(file_len) |
| 145 | + if file_len < buffer: |
| 146 | + buffer = file_len |
| 147 | + print('write finished') |
| 148 | + print('receive over') |
| 149 | + |
| 150 | + # recv over flag and update marsai |
| 151 | + while True: |
| 152 | + data = client_sock.recv(4096) |
| 153 | + if data == b'over_and_update': |
| 154 | + md5 = get_md5(str(pathlib.Path.home()) + '/marsai.zip') |
| 155 | + if file_md5 == md5: |
| 156 | + os.system('sh ~/marsai/tools/stop-systemd-services.sh') |
| 157 | + time.sleep(3) |
| 158 | + os.system('unzip -o ~/marsai.zip -d ~/marsai') |
| 159 | + time.sleep(1) |
| 160 | + os.system('sh ~/marsai/tools/start-systemd-services.sh') |
| 161 | + client_sock.send(b'update_over') |
| 162 | + print('over') |
| 163 | + break |
| 164 | + else: |
| 165 | + print('md5 error') |
| 166 | + break |
| 167 | + else: |
| 168 | + pass |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | + else: |
| 174 | + pass |
| 175 | + |
| 176 | + print("Received " + str(data)) |
| 177 | + |
| 178 | + client_sock.close() |
| 179 | + print("Close Client Socket") |
| 180 | + |
| 181 | + server_sock.close() |
| 182 | + print("Stop Server") |
| 183 | + |
| 184 | +def get_md5(_file): |
| 185 | + m = hashlib.md5() |
| 186 | + with open(_file, 'rb') as f: |
| 187 | + for line in f: |
| 188 | + m.update(line) |
| 189 | + md5_code = m.hexdigest() |
| 190 | + print(md5_code) |
| 191 | + return md5_code |
| 192 | + |
| 193 | + |
| 194 | +if __name__ == "__main__": |
| 195 | + start_marscat_bt() |
0 commit comments