summaryrefslogtreecommitdiff
path: root/lib/taurus/core/taurusdevice.py
blob: ae605e1f65a32ae25bc1c0883bc99b6d5efe1181 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python

#############################################################################
##
# This file is part of Taurus
##
# http://taurus-scada.org
##
# Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain
##
# Taurus is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
##
# Taurus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
##
# You should have received a copy of the GNU Lesser General Public License
# along with Taurus.  If not, see <http://www.gnu.org/licenses/>.
##
#############################################################################

"""This module contains the base class for a taurus device"""

__all__ = ["TaurusDevice"]

__docformat__ = "restructuredtext"

from .taurusbasetypes import TaurusDevState, TaurusElementType
from .taurusmodel import TaurusModel


class TaurusDevice(TaurusModel):

    """A Device object. Different schemes may assign different roles, but
       in general it is a parent of Taurus Attribute objects and a child
       of a Taurus Authority"""

    _description = "A Taurus Device"

    def __init__(self, name='', **kw):
        """Object initialization."""
        parent = kw.pop('parent', None)
        storeCallback = kw.pop('storeCallback', None)
        self.__dict__.update(kw)
        self.call__init__(TaurusModel, name, parent)

        if storeCallback:
            storeCallback(self)

    def __contains__(self, key):
        """Reimplement in schemes if you want to support membership testing for
        attributes of the device
        """
        raise TypeError("'%s' does not support membership testing" %
                        self.__class__.__name__)

    @property
    def state(self):
        """Returns a scheme-agnostic representation of the state of a Taurus
        device. This default implementation always returns
        `TaurusDevState.Ready`

        Subclasses of TaurusDevice may reimplement it to return other
        :class:`taurus.core.TaurusDevState` enumeration values.

        :return: (TaurusDevState) `TaurusDevState.Ready`
        """
        return TaurusDevState.Ready

    #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    # TaurusModel implementation
    #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

    @classmethod
    def getTaurusElementType(cls):
        return TaurusElementType.Device

    @classmethod
    def buildModelName(cls, parent_model, relative_name):
        """build an 'absolute' model name from the parent model and the 'relative'
        name.
        - If parent_model is a TaurusAuthority, the return is a composition of
        the authority model name and the device name
        - If parent_model is a TaurusDevice, the relative name is ignored and
        the parent name is returned

        Note: This is a basic implementation. You may need to reimplement this
              for a specific scheme if it supports "useParentModel".
        """
        if parent_model is None:
            return relative_name
        parent_name = parent_model.getFullName()
        if not parent_name:
            return relative_name
        if isinstance(parent_model, cls):
            return parent_name
        return '%s/%s' % (parent_name, relative_name)

    @classmethod
    def getNameValidator(cls):
        return cls.factory().getDeviceNameValidator()

    def getDisplayDescrObj(self, cache=True):
        obj = []
        obj.append(('name', self.getDisplayName(cache=cache)))
        obj.append(('description', self.description))
        obj.append(('device state', self.state.name))
        return obj

    def getChildObj(self, child_name):
        if child_name is None or len(child_name) == 0:
            return None
        obj_name = "%s%s" % (self.getFullName(), child_name)
        return self.factory().findObject(obj_name)

    def poll(self, attrs, asynch=False, req_id=None):
        '''Polling certain attributes of the device. This default
        implementation simply polls each attribute one by one'''

        # asynchronous requests are not supported. If asked to do it,
        # just return an ID of 1 and in the reply (req_id != None) we do a
        # synchronous polling.
        if asynch is True:
            return 1
        for attr in attrs.values():
            attr.poll()

    @property
    def description(self):
        return self._description