From 67548ad42b560e0237613206e243677c12ff7f3d Mon Sep 17 00:00:00 2001
From: hannes <hannes.tiltmann@smail.th-koeln.de>
Date: Thu, 25 Nov 2021 12:22:13 +0100
Subject: [PATCH] new frontend for py3

---
 .gitignore            |  1 +
 custom_py3/example.py |  6 ++++++
 custom_py3/leap.py    | 41 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 48 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 custom_py3/example.py
 create mode 100644 custom_py3/leap.py

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ed8ebf5
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+__pycache__
\ No newline at end of file
diff --git a/custom_py3/example.py b/custom_py3/example.py
new file mode 100644
index 0000000..493c188
--- /dev/null
+++ b/custom_py3/example.py
@@ -0,0 +1,6 @@
+from leap import LeapController
+
+l = LeapController("127.0.0.1", logging=True)
+l.run()
+
+input()
\ No newline at end of file
diff --git a/custom_py3/leap.py b/custom_py3/leap.py
new file mode 100644
index 0000000..286a94e
--- /dev/null
+++ b/custom_py3/leap.py
@@ -0,0 +1,41 @@
+import websocket
+import _thread
+import json
+
+class LeapController:
+    def __init__(self, ip: str, logging :bool=False):
+        self.ws = websocket.WebSocketApp("ws://"+ip+":6437")
+        self.ws.on_open = self.__on_open
+        self.ws.on_close = self.__on_close
+        self.ws.on_message = self.__on_message
+        self.logging = logging
+
+    def __on_open(self, ws):
+        if self.logging: print("jey, connected to leap") 
+
+    def __on_close(self, ws, status, message):  
+        if self.logging: print("autsch, disconnected from leap")
+
+    def __on_message(self, ws, m):  
+        self.frame = json.loads(m)
+
+        if self.logging: 
+            print("wow, new frame")
+            for h in self.frame["hands"]:
+                print(json.dumps(h["t"], indent=3))
+
+    def close(self):
+        self.ws.close()
+        
+    def run(self, blocking=False):
+        def eventloop():
+            try:
+                self.ws.run_forever()
+            finally:
+                self.ws.close()
+
+        if not blocking:
+            _thread.start_new_thread(eventloop,())
+            return
+            
+        eventloop()
-- 
GitLab