Blame view

src/common/Device.py 1.64 KB
9774228b   haribo   Date: 22/06/2016
1
2
3
4
from pyrosapp.models import *


class DeviceObj():
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
18
        set_msgs = []
        get_msgs = []
        do_msgs = []

    def set(self, cmd, *args):
9b5bad52   haribo   Commented all the...
19
20
21
        '''
            Send a SET message to the device
        '''
9774228b   haribo   Date: 22/06/2016
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
        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],))

        print("set ", cmd)


    def get(self, cmd):
9b5bad52   haribo   Commented all the...
37
38
39
        '''
            Send a GET message to the device
        '''
9774228b   haribo   Date: 22/06/2016
40
41
42
43
44
45
46
        if cmd not in self.get_msgs:
            raise ValueError("Invalid message argument %s" % (cmd,))

        print("get ", cmd)


    def do(self, cmd, *args):
9b5bad52   haribo   Commented all the...
47
48
49
        '''
            Send a DO message to the device
        '''
9774228b   haribo   Date: 22/06/2016
50
51
52
53
54
55
56
57
58
59
60
61
        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],))

        print("do ", cmd)