aboutsummaryrefslogtreecommitdiff
path: root/client.py
blob: 7f0bb60ce73c41d6147373b48fb23e7f1fc12845 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/python3

import sys
from queue import Queue
import json

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()

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.in_queue = Queue()
        s.queue = self.in_queue

    def run(self):

        while True:
            while not self.queue.empty():
                text = self.queue.get(False)
                if text == -1:
                    sys.exit(0)
                elif text == "":
                    continue
                j = json.loads(text)
                s.send(j)

            if not s.isConnected():
                time.sleep(2)
                s.connect()
                continue


            s.fetch()

            while not self.in_queue.empty():
                msg = self.in_queue.get(False)
                print(msg)
                # Print the promot again
                print("\n> ", end=u"", flush=True)

            time.sleep(0.2)

queue = Queue()
c = Conn(queue)
c.start()
print("Press ^D to quit")
while True:
    try:
        text = input("> ")
        queue.put(text)
        if text == "":
            continue
    except:
        queue.put(-1)
        break