commit 133eb26a0b07dadeff38683b24b6e1eeab7ee444 Author: Samuel Sloniker Date: Tue Jun 15 12:50:51 2021 -0700 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/app.py b/app.py new file mode 100644 index 0000000..09edec9 --- /dev/null +++ b/app.py @@ -0,0 +1,11 @@ +from flask import Flask, request, Response +import os +import requests +import base64 +#import hcapi +app = Flask(__name__) + +@app.route('/client') +def client(): + with open('client.html') as f: + return f.read() diff --git a/client.html b/client.html new file mode 100644 index 0000000..84dedb0 --- /dev/null +++ b/client.html @@ -0,0 +1,123 @@ + + + + HamClock + + + + +
+

HamClock Remote Access

+
+
+ + +
+
+ + + + diff --git a/crops.json b/crops.json new file mode 100644 index 0000000..815c5d9 --- /dev/null +++ b/crops.json @@ -0,0 +1 @@ +["232x53+0+0", "232x11+0+53", "232x84+0+64", "163x148+232+0", "181x148+395+0", "170x148+576+0", "54x148+746+0", "139x148+0+148", "139x184+0+296", "110x166+139+148", "110x166+249+148", "110x166+359+148", "110x166+469+148", "110x166+579+148", "111x166+689+148", "110x166+139+314", "110x166+249+314", "110x166+359+314", "110x166+469+314", "110x166+579+314", "111x166+689+314"] diff --git a/hcapi.py b/hcapi.py new file mode 100644 index 0000000..f1c89e8 --- /dev/null +++ b/hcapi.py @@ -0,0 +1,53 @@ +import os +import requests +import threading +import json + +def get_img(): + raw_img = requests.get('http://localhost:8080/get_capture.bmp').content + + with open('full_img.bmp', 'wb+') as f: + f.write(raw_img) + + os.system('convert full_img.bmp -resize 800x480 img.bmp') + + #crops = [] + #for n in range(10): + # for m in range(6): + # crops.append(f'80x80+{n*80}+{m*80}') + #print(crops) + with open('crops.json') as f: + crops = json.load(f) + + threads = [] + for crop in crops: + threads.append(threading.Thread(target=os.system, args=(f'convert img.bmp -crop {crop} pieces/{crop.split("+", 1)[1].replace("+", "x")}.jpg',))) + threads[-1].start() + for thread in threads: + thread.join() + + os.system("md5sum -c oldlist 2>/dev/null | grep FAILED > newlist") + changed = [] + with open('newlist') as f: + lines = f.readlines() + for line in lines: + changed.append(line.split(": ")[0].split('.')[0].split('/')[1]) + os.system("md5sum pieces/* > oldlist") + return changed + +def get_full_img(): + raw_img = requests.get('http://localhost:8080/get_capture.bmp').content + + with open('full_full.bmp', 'wb+') as f: + f.write(raw_img) + + os.system('convert full_full.bmp -resize 800x480 full.jpg') + + os.unlink('full_full.bmp') + + return 'full.jpg' + +def touch(x, y, w): + x = round(800 * x / w) + y = round(800 * y / w) + requests.get(f'http://localhost:8080/set_touch?x={x}&y={y}&hold={0}') diff --git a/wss.py b/wss.py new file mode 100644 index 0000000..ddf5c9f --- /dev/null +++ b/wss.py @@ -0,0 +1,73 @@ +import os +import base64 +import hcapi +import threading +import logging +import random +import time +import shutil +import tempfile +from websocket_server import WebsocketServer + +def cycle(): + try: + changed = hcapi.get_img() + except ImportError as e: + server.send_message_to_all('err%noconn%Server failed to capture screenshot') + time.sleep(3) + return + + for i in changed: + threading.Thread(target=do_img, args=(i,)).start() + + +def do_img(imgname): + server.send_message_to_all(img(imgname)) + + +def img(imgname): + with open(f'pieces/{imgname}.jpg', 'rb') as f: + img = f.read() + + response = f'pic%{imgname}%data:imgage/jpeg;base64,{base64.b64encode(img).decode("utf-8")}' + + return response + +def new_client(client, server): + print('got client') + try: + imgname = hcapi.get_full_img() + except Exception as e: + server.send_message(client, 'err%noconn%Server failed to capture screenshot') + return + + with open(imgname, 'rb') as f: + img = f.read() + os.unlink(imgname) + server.send_message(client, f'pic%0x0%data:imgage/jpeg;base64,{base64.b64encode(img).decode("utf-8")}') + +def do_touch(client, server, message): + password, x, y, w = message.split(' ') + if password == 'password': + x, y, w = int(x), int(y), int(w) + hcapi.touch(x, y, w) + else: + server.send_message(client, f'badpass') + +def do_cycles(): + while True: + cycle() + +tmp = tempfile.mkdtemp(prefix="HCRA-") +try: + shutil.copy('crops.json', tmp) + os.chdir(tmp) + os.mkdir('pieces') + server = WebsocketServer(1234, host='0.0.0.0', loglevel=logging.INFO) + threading.Thread(target=do_cycles).start() + server.set_fn_new_client(new_client) + server.set_fn_message_received(do_touch) + server.run_forever() +finally: + os.chdir('/') + shutil.rmtree(tmp)