File: server.md

package info (click to toggle)
pytango 10.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,216 kB
  • sloc: python: 28,206; cpp: 16,380; sql: 255; sh: 82; makefile: 43
file content (302 lines) | stat: -rw-r--r-- 11,187 bytes parent folder | download | duplicates (3)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
```{eval-rst}
.. currentmodule:: tango.server
```

(pytango-hlapi)=

# High level server API

```{eval-rst}
.. automodule:: tango.server
```

```{eval-rst}
.. hlist::

   * :class:`~tango.server.Device`
   * :class:`~tango.server.attribute`
   * :class:`~tango.server.command`
   * :class:`~tango.server.pipe`
   * :class:`~tango.server.device_property`
   * :class:`~tango.server.class_property`
   * :func:`~tango.server.run`
   * :func:`~tango.server.server_run`
```

This module provides a high level device server API. It implements
[TEP1](#pytango-TEP1). It exposes an easier API for developing a Tango
device server.

Here is a simple example on how to write a *Clock* device server using the
high level API:

```
import time
from tango.server import run
from tango.server import Device
from tango.server import attribute, command


class Clock(Device):

    time = attribute()

    def read_time(self):
        return time.time()

    @command(dtype_in=str, dtype_out=str)
    def strftime(self, format):
        return time.strftime(format)


if __name__ == "__main__":
    run((Clock,))
```

Here is a more complete  example on how to write a *PowerSupply* device server
using the high level API. The example contains:

1. device description (via docstring), which user can get later as DeviceProxy.description()
2. default state DevState.ON and default status "Device is current supply mode"
3. a read-only double scalar attribute called *voltage*
4. a read/write double scalar expert attribute *current*
5. a read-only double image attribute called *noise*
6. a *ramp* command
7. a *host* device property
8. a *port* class property

```{code-block} python
:linenos: true

from time import time
from numpy.random import random_sample

from tango import AttrQuality, AttrWriteType, DevState, DispLevel
from tango.server import Device, attribute, command
from tango.server import class_property, device_property

class PowerSupply(Device):
    """PyTango example of PowerSuppy device."""

    # alternative way to add device description (see note below)
    DEVICE_CLASS_DESCRIPTION = "PyTango example of PowerSuppy device."

    DEVICE_CLASS_INITIAL_STATUS = "Device is in current supply mode"
    DEVICE_CLASS_INITIAL_STATE  = DevState.ON

    voltage = attribute()

    current = attribute(label="Current", dtype=float,
                        display_level=DispLevel.EXPERT,
                        access=AttrWriteType.READ_WRITE,
                        unit="A", format="8.4f",
                        min_value=0.0, max_value=8.5,
                        min_alarm=0.1, max_alarm=8.4,
                        min_warning=0.5, max_warning=8.0,
                        fget="get_current", fset="set_current",
                        doc="the power supply current")

    noise = attribute(label="Noise", dtype=((float,),),
                      max_dim_x=1024, max_dim_y=1024,
                      fget="get_noise")

    host = device_property(dtype=str)
    port = class_property(dtype=int, default_value=9788)

    def read_voltage(self):
        self.info_stream(f"Get voltage({self.host}, {self.port})")
        return 10.0

    def get_current(self):
        return 2.3456, time(), AttrQuality.ATTR_CHANGING

    def set_current(self, current):
        self.info_stream(f"Current set to {current}")

    def get_noise(self):
        return random_sample((1024, 1024))

    @command(dtype_in=float)
    def ramp(self, value):
        self.info_stream(f"Ramp up requested: {value} seconds")

if __name__ == "__main__":
    PowerSupply.run_server()
```

*Pretty cool, uh?*

:::{note}
the device description can be added either by class docstring or by DEVICE_CLASS_DESCRIPTION class member, the latter has priority over the docstring.
The important difference is that DEVICE_CLASS_DESCRIPTION will be inherited by child classes, while the docstring will not be.
:::

(pytango-hlapi-datatypes)=

```{rubric} Data types
```

When declaring attributes, properties or commands, one of the most important
details is the data type. It is given by the keyword argument *dtype*.
In order to provide a more *pythonic* interface, this argument is not restricted
to the {obj}`~tango.CmdArgType` options.

For example, to define a *SCALAR* {obj}`~tango.CmdArgType.DevLong`
attribute you have several possibilities:

1. {obj}`int`
2. 'int'
3. 'int64'
4. {obj}`tango.CmdArgType.DevLong64`
5. 'DevLong64'
6. {obj}`numpy.int64`

To define a *SPECTRUM* attribute simply wrap the scalar data type in any
python sequence:

- using a *tuple*: `` (:obj:`int`,) `` or
- using a *list*: `` [:obj:`int`] `` or
- any other sequence type

To define an *IMAGE* attribute simply wrap the scalar data type in any
python sequence of sequences:

- using a *tuple*: `` ((:obj:`int`,),) `` or
- using a *list*: `` [[:obj:`int`]] `` or
- any other sequence type

Below is the complete table of equivalences.

| dtype argument              | converts to tango type    |
| --------------------------- | ------------------------- |
| `None`                      | `DevVoid`                 |
| `'None'`                    | `DevVoid`                 |
| `DevVoid`                   | `DevVoid`                 |
| `'DevVoid'`                 | `DevVoid`                 |
| `DevState`                  | `DevState`                |
| `'DevState'`                | `DevState`                |
| {py:obj}`bool`              | `DevBoolean`              |
| `'bool'`                    | `DevBoolean`              |
| `'boolean'`                 | `DevBoolean`              |
| `DevBoolean`                | `DevBoolean`              |
| `'DevBoolean'`              | `DevBoolean`              |
| {py:obj}`numpy.bool_`       | `DevBoolean`              |
| `'char'`                    | `DevUChar`                |
| `'chr'`                     | `DevUChar`                |
| `'byte'`                    | `DevUChar`                |
| `chr`                       | `DevUChar`                |
| `DevUChar`                  | `DevUChar`                |
| `'DevUChar'`                | `DevUChar`                |
| {py:obj}`numpy.uint8`       | `DevUChar`                |
| `'int16'`                   | `DevShort`                |
| `DevShort`                  | `DevShort`                |
| `'DevShort'`                | `DevShort`                |
| {py:obj}`numpy.int16`       | `DevShort`                |
| `'uint16'`                  | `DevUShort`               |
| `DevUShort`                 | `DevUShort`               |
| `'DevUShort'`               | `DevUShort`               |
| {py:obj}`numpy.uint16`      | `DevUShort`               |
| `'int32'`                   | `DevLong`                 |
| `DevLong`                   | `DevLong`                 |
| `'DevLong'`                 | `DevLong`                 |
| {py:obj}`numpy.int32`       | `DevLong`                 |
| `'uint32'`                  | `DevULong`                |
| `DevULong`                  | `DevULong`                |
| `'DevULong'`                | `DevULong`                |
| {py:obj}`numpy.uint32`      | `DevULong`                |
| {py:obj}`int`               | `DevLong64`               |
| `'int'`                     | `DevLong64`               |
| `'int64'`                   | `DevLong64`               |
| `DevLong64`                 | `DevLong64`               |
| `'DevLong64'`               | `DevLong64`               |
| {py:obj}`numpy.int64`       | `DevLong64`               |
| `'uint'`                    | `DevULong64`              |
| `'uint64'`                  | `DevULong64`              |
| `DevULong64`                | `DevULong64`              |
| `'DevULong64'`              | `DevULong64`              |
| {py:obj}`numpy.uint64`      | `DevULong64`              |
| `'float32'`                 | `DevFloat`                |
| `DevFloat`                  | `DevFloat`                |
| `'DevFloat'`                | `DevFloat`                |
| {py:obj}`numpy.float32`     | `DevFloat`                |
| {py:obj}`float`             | `DevDouble`               |
| `'double'`                  | `DevDouble`               |
| `'float'`                   | `DevDouble`               |
| `'float64'`                 | `DevDouble`               |
| `DevDouble`                 | `DevDouble`               |
| `'DevDouble'`               | `DevDouble`               |
| {py:obj}`numpy.float64`     | `DevDouble`               |
| {py:obj}`str`               | `DevString`               |
| `'str'`                     | `DevString`               |
| `'string'`                  | `DevString`               |
| `'text'`                    | `DevString`               |
| `DevString`                 | `DevString`               |
| `'DevString'`               | `DevString`               |
| {py:obj}`bytearray`         | `DevEncoded`              |
| `'bytearray'`               | `DevEncoded`              |
| `'bytes'`                   | `DevEncoded`              |
| `DevEncoded`                | `DevEncoded`              |
| `'DevEncoded'`              | `DevEncoded`              |
| `DevVarBooleanArray`        | `DevVarBooleanArray`      |
| `'DevVarBooleanArray'`      | `DevVarBooleanArray`      |
| `DevVarCharArray`           | `DevVarCharArray`         |
| `'DevVarCharArray'`         | `DevVarCharArray`         |
| `DevVarShortArray`          | `DevVarShortArray`        |
| `'DevVarShortArray'`        | `DevVarShortArray`        |
| `DevVarLongArray`           | `DevVarLongArray`         |
| `'DevVarLongArray'`         | `DevVarLongArray`         |
| `DevVarLong64Array`         | `DevVarLong64Array`       |
| `'DevVarLong64Array'`       | `DevVarLong64Array`       |
| `DevVarULong64Array`        | `DevVarULong64Array`      |
| `'DevVarULong64Array'`      | `DevVarULong64Array`      |
| `DevVarFloatArray`          | `DevVarFloatArray`        |
| `'DevVarFloatArray'`        | `DevVarFloatArray`        |
| `DevVarDoubleArray`         | `DevVarDoubleArray`       |
| `'DevVarDoubleArray'`       | `DevVarDoubleArray`       |
| `DevVarUShortArray`         | `DevVarUShortArray`       |
| `'DevVarUShortArray'`       | `DevVarUShortArray`       |
| `DevVarULongArray`          | `DevVarULongArray`        |
| `'DevVarULongArray'`        | `DevVarULongArray`        |
| `DevVarStringArray`         | `DevVarStringArray`       |
| `'DevVarStringArray'`       | `DevVarStringArray`       |
| `DevVarLongStringArray`     | `DevVarLongStringArray`   |
| `'DevVarLongStringArray'`   | `DevVarLongStringArray`   |
| `DevVarDoubleStringArray`   | `DevVarDoubleStringArray` |
| `'DevVarDoubleStringArray'` | `DevVarDoubleStringArray` |
| `DevPipeBlob`               | `DevPipeBlob`             |
| `'DevPipeBlob'`             | `DevPipeBlob`             |

```{eval-rst}
.. autoclass:: Device
   :show-inheritance:
   :inherited-members:
   :members:
```

```{eval-rst}
.. autoclass:: attribute
```

```{eval-rst}
.. autofunction:: command
```

```{eval-rst}
.. autoclass:: pipe
```

```{eval-rst}
.. autoclass:: device_property
```

```{eval-rst}
.. autoclass:: class_property
```

```{eval-rst}
.. autofunction:: run
```

```{eval-rst}
.. autofunction:: server_run
```