diff options
author | Sébastien Dailly <sebastien@dailly.me> | 2023-09-17 16:38:37 +0200 |
---|---|---|
committer | Sébastien Dailly <sebastien@dailly.me> | 2023-09-17 16:38:37 +0200 |
commit | 29164963897cff5ec24c7e56ac1c41135940da96 (patch) | |
tree | ef0767957c588523605cb70d075cb67c352c6b86 /client.py | |
parent | fcce9177e356bb27283926451433130a8809fcb0 (diff) |
Updated the client and socket server
Diffstat (limited to 'client.py')
-rwxr-xr-x | client.py | 65 |
1 files changed, 50 insertions, 15 deletions
@@ -1,30 +1,64 @@ +#!/bin/python3
+
import sys
-from threading import Thread
from queue import Queue
-import time
+import json
-#
-# This code provide an example for connecting to the socket server.
-#
+import argparse
+from os import path
from zope import component
-
from interfaces import endpoint
+import configparser
+
+script_path = path.dirname(path.realpath(__file__))
+config_file = path.join(script_path, "config.ini")
+parser = argparse.ArgumentParser(
+ prog='client',
+ description='Command line client to the keyboard',
+ epilog='')
+parser.add_argument('configuration', nargs='?', default=config_file)
+parser.add_argument('--layer')
+args = parser.parse_args()
+
+
+config = configparser.ConfigParser(delimiters="=")
+config.read(args.configuration)
+
+if config.has_section("connection.socket"):
+ print("Socket connexion")
+ from socket_conn import SocketConnection
+ conn = SocketConnection(config["connection.socket"])
+elif config.has_section("connection.serial"):
+ print("Serial connecion")
+ from serial_conn import SerialConnection
+ conn = SerialConnection(config["connection.serial"])
+
+# Connect to the endpoint right now
component.provideAdapter(endpoint.EndPoint)
+s = component.queryAdapter(conn, endpoint.IEndpoint)
+s.connect()
-from socket_conn import SocketConnection
-conn = SocketConnection()
+if args.layer is not None:
+ print(args.layer)
+ s.queue = Queue()
+ with open(args.layer, "r") as json_file:
+ json_data = json_file.read()
+ j = json.loads(json_data)
+ content = json.dumps(j)
+ s.send(j)
+ sys.exit(0)
+from threading import Thread
+import time
class Conn(Thread):
def __init__(self, queue):
Thread.__init__(self)
self.queue = queue
- self.s = component.queryAdapter(conn, endpoint.IEndpoint)
- self.s.connect()
self.in_queue = Queue()
- self.s.queue = self.in_queue
+ s.queue = self.in_queue
def run(self):
@@ -35,15 +69,16 @@ class Conn(Thread): sys.exit(0)
elif text == "":
continue
- self.s.send([{"layout": text}])
+ j = json.loads(text)
+ s.send(j)
- if not self.s.isConnected():
+ if not s.isConnected():
time.sleep(2)
- self.s.connect()
+ s.connect()
continue
- self.s.fetch()
+ s.fetch()
while not self.in_queue.empty():
msg = self.in_queue.get(False)
|