Blame view

src/devices/Device.py 2.53 KB
ddf59dd4   haribo   Remaniement :
1
from common.models import *
ac26ad2b   haribo   Date: 22/07/2016
2
import socket
9774228b   haribo   Date: 22/06/2016
3

ddf59dd4   haribo   Remaniement :
4
class DeviceController():
9b5bad52   haribo   Commented all the...
5
6
7
8
    '''
        Generic object for the communication with all the devices (need inheritance)
    '''

9774228b   haribo   Date: 22/06/2016
9
10

    def __init__(self):
9b5bad52   haribo   Commented all the...
11
12
13
        '''
            The messages and their parameters will be defined in the objects that inherits from this class
        '''
9774228b   haribo   Date: 22/06/2016
14
15
16
17
        set_msgs = []
        get_msgs = []
        do_msgs = []

ac26ad2b   haribo   Date: 22/07/2016
18
19
20
21
22
23
        self.ip = None
        self.port = None

    def init_socket(self):
        if self.ip is None or self.port is None:
            raise ValueError("IP and/or PORT not initialized")
ddf59dd4   haribo   Remaniement :
24
        # TODO: gérer un fail de connexion
ac26ad2b   haribo   Date: 22/07/2016
25
26
27
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect((self.ip, self.port))

9774228b   haribo   Date: 22/06/2016
28
    def set(self, cmd, *args):
9b5bad52   haribo   Commented all the...
29
30
31
        '''
            Send a SET message to the device
        '''
9774228b   haribo   Date: 22/06/2016
32
33
34
35
36
37
38
39
40
41
42
        msg = [msg for msg in self.set_msgs if msg[0] == cmd]
        if len(msg) == 0:
            raise ValueError("Invalid message argument %s" % (cmd,))
        msg = msg[0]
        if len(args) not in msg[1]:
            raise ValueError("Bad number of arguments")

        for arg in args:
            if type(arg) != msg[2]:
                raise TypeError("Bad type of argument: expected %s" % (msg[2],))

ac26ad2b   haribo   Date: 22/07/2016
43
        self.init_socket()
ddf59dd4   haribo   Remaniement :
44
        message = "SET " + cmd + " " + " ".join([str(arg) for arg in args])
ac26ad2b   haribo   Date: 22/07/2016
45
        self.sock.send(bytes(message, "UTF-8"))
9774228b   haribo   Date: 22/06/2016
46
47
48
49
        print("set ", cmd)


    def get(self, cmd):
9b5bad52   haribo   Commented all the...
50
51
52
        '''
            Send a GET message to the device
        '''
9774228b   haribo   Date: 22/06/2016
53
54
55
        if cmd not in self.get_msgs:
            raise ValueError("Invalid message argument %s" % (cmd,))

ac26ad2b   haribo   Date: 22/07/2016
56
57
58
        self.init_socket()
        message = "GET " + cmd
        self.sock.send(bytes(message, "UTF-8"))
9774228b   haribo   Date: 22/06/2016
59
        print("get ", cmd)
ddf59dd4   haribo   Remaniement :
60
        # TODO: gérer le timeout
ac26ad2b   haribo   Date: 22/07/2016
61
62
63
        ret = self.sock.recv(1024).decode()
        print("return : ", ret)
        return ret
9774228b   haribo   Date: 22/06/2016
64
65
66


    def do(self, cmd, *args):
9b5bad52   haribo   Commented all the...
67
68
69
        '''
            Send a DO message to the device
        '''
9774228b   haribo   Date: 22/06/2016
70
71
72
73
74
75
76
77
78
79
80
        msg = [msg for msg in self.do_msgs if msg[0] == cmd]
        if len(msg) == 0:
            raise ValueError("Invalid message argument %s" % (cmd,))
        msg = msg[0]
        if len(args) not in msg[1]:
            raise ValueError("Bad number of arguments")

        for arg in args:
            if type(arg) != msg[2]:
                raise TypeError("Bad type of argument: expected %s" % (msg[2],))

ac26ad2b   haribo   Date: 22/07/2016
81
        self.init_socket()
ddf59dd4   haribo   Remaniement :
82
        message = "DO " + cmd + " " + " ".join([str(arg) for arg in args])
ac26ad2b   haribo   Date: 22/07/2016
83
        self.sock.send(bytes(message, "UTF-8"))
9774228b   haribo   Date: 22/06/2016
84
        print("do ", cmd)