/usr/lib/python2.7/dist-packages/ola/OlaClient.py is in ola-python 0.10.5.nojsmin-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 | # This library 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 2.1 of the License, or (at your option) any later version.
#
# This library 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 this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# OlaClient.py
# Copyright (C) 2005 Simon Newton
import array
import socket
import struct
from ola.rpc.StreamRpcChannel import StreamRpcChannel
from ola.rpc.SimpleRpcController import SimpleRpcController
from ola import Ola_pb2
from ola.UID import UID
"""The client used to communicate with the Ola Server."""
__author__ = 'nomis52@gmail.com (Simon Newton)'
"""The port that the OLA server listens on."""
OLA_PORT = 9010
class Error(Exception):
"""The base error class."""
class OLADNotRunningException(Error):
"""Thrown if we try to connect and olad isn't running."""
class Plugin(object):
"""Represents a plugin.
Attributes:
id: the id of this plugin
name: the name of this plugin
active: whether this plugin is active
enabled: whether this plugin is enabled
"""
def __init__(self, plugin_id, name, active, enabled):
self._id = plugin_id
self._name = name
self._active = active
self._enabled = enabled
@property
def id(self):
return self._id
@property
def name(self):
return self._name
@property
def active(self):
return self._active
@property
def enabled(self):
return self._enabled
@staticmethod
def FromProtobuf(plugin_pb):
return Plugin(plugin_pb.plugin_id,
plugin_pb.name,
plugin_pb.active,
plugin_pb.enabled)
def __repr__(self):
s = 'Plugin(id={id}, name="{name}", active={active}, enabled={enabled})'
return s.format(id=self.id,
name=self.name,
active=self.active,
enabled=self.enabled)
def __lt__(self, other):
return self.id < other.id
def __eq__(self, other):
return self.id == other.id
# These 4 could be replaced by functools.total_ordering when support
# for 2.6 is dropped.
def __le__(self, other):
return self.id <= other.id
def __gt__(self, other):
return self.id > other.id
def __ge__(self, other):
return self.id >= other.id
def __ne__(self, other):
return self.id != other.id
# Populate the Plugin class attributes from the protobuf
for value in Ola_pb2._PLUGINIDS.values:
setattr(Plugin, value.name, value.number)
class Device(object):
"""Represents a device.
Attributes:
id: the unique id of this device
alias: the integer alias for this device
name: the name of this device
plugin_id: the plugin that this device belongs to
input_ports: a list of Input Port objects
output_ports: a list of Output Port objects
"""
def __init__(self, device_id, alias, name, plugin_id, input_ports,
output_ports):
self._id = device_id
self._alias = alias
self._name = name
self._plugin_id = plugin_id
self._input_ports = sorted(input_ports)
self._output_ports = sorted(output_ports)
@property
def id(self):
return self._id
@property
def alias(self):
return self._alias
@property
def name(self):
return self._name
@property
def plugin_id(self):
return self._plugin_id
@property
def input_ports(self):
return self._input_ports
@property
def output_ports(self):
return self._output_ports
@staticmethod
def FromProtobuf(device_pb):
input_ports = [Port.FromProtobuf(x) for x in device_pb.input_port]
output_ports = [Port.FromProtobuf(x) for x in device_pb.output_port]
return Device(device_pb.device_id,
device_pb.device_alias,
device_pb.device_name,
device_pb.plugin_id,
input_ports,
output_ports)
def __repr__(self):
s = 'Device(id="{id}", alias={alias}, name="{name}", ' \
'plugin_id={plugin_id}, {nr_inputs} inputs, {nr_outputs} outputs)'
return s.format(id=self.id,
alias=self.alias,
name=self.name,
plugin_id=self.plugin_id,
nr_inputs=len(self.input_ports),
nr_outputs=len(self.output_ports))
def __lt__(self, other):
return self.alias < other.alias
def __eq__(self, other):
return self.alias == other.alias
# These 3 could be replaced by functools.total_ordering when support
# for 2.6 is dropped.
def __le__(self, other):
return self.alias <= other.alias
def __gt__(self, other):
return self.alias > other.alias
def __ge__(self, other):
return self.alias >= other.alias
def __ne__(self, other):
return self.alias != other.alias
class Port(object):
"""Represents a port.
Attributes:
id: the unique id of this port
universe: the universe that this port belongs to
active: True if this port is active
description: the description of the port
supports_rdm: if the port supports RDM
"""
def __init__(self, port_id, universe, active, description, supports_rdm):
self._id = port_id
self._universe = universe
self._active = active
self._description = description
self._supports_rdm = supports_rdm
@property
def id(self):
return self._id
@property
def universe(self):
return self._universe
@property
def active(self):
return self._active
@property
def description(self):
return self._description
@property
def supports_rdm(self):
return self._supports_rdm
@staticmethod
def FromProtobuf(port_pb):
universe = port_pb.universe if port_pb.HasField('universe') else None
return Port(port_pb.port_id,
universe,
port_pb.active,
port_pb.description,
port_pb.supports_rdm)
def __repr__(self):
s = 'Port(id={id}, universe={universe}, active={active}, ' \
'description="{desc}", supports_rdm={supports_rdm})'
return s.format(id=self.id,
universe=self.universe,
active=self.active,
desc=self.description,
supports_rdm=self.supports_rdm)
def __lt__(self, other):
return self.id < other.id
def __eq__(self, other):
return self.id == other.id
# These 4 could be replaced by functools.total_ordering when support
# for 2.6 is dropped.
def __le__(self, other):
return self.id <= other.id
def __gt__(self, other):
return self.id > other.id
def __ge__(self, other):
return self.id >= other.id
def __ne__(self, other):
return self.id != other.id
class Universe(object):
"""Represents a universe.
Attributes:
id: the integer universe id
name: the name of this universe
merge_mode: the merge mode this universe is using
"""
LTP = Ola_pb2.LTP
HTP = Ola_pb2.HTP
def __init__(self, universe_id, name, merge_mode, input_ports, output_ports):
self._id = universe_id
self._name = name
self._merge_mode = merge_mode
self._input_ports = sorted(input_ports)
self._output_ports = sorted(output_ports)
@property
def id(self):
return self._id
@property
def name(self):
return self._name
@property
def merge_mode(self):
return self._merge_mode
@property
def input_ports(self):
return self._input_ports
@property
def output_ports(self):
return self._output_ports
@staticmethod
def FromProtobuf(universe_pb):
input_ports = [Port.FromProtobuf(x) for x in universe_pb.input_ports]
output_ports = [Port.FromProtobuf(x) for x in universe_pb.output_ports]
return Universe(universe_pb.universe,
universe_pb.name,
universe_pb.merge_mode,
input_ports,
output_ports)
def __repr__(self):
merge_mode = 'LTP' if self.merge_mode == Universe.LTP else 'HTP'
s = 'Universe(id={id}, name="{name}", merge_mode={merge_mode})'
return s.format(id=self.id,
name=self.name,
merge_mode=merge_mode)
def __lt__(self, other):
return self.id < other.id
def __eq__(self, other):
return self.id == other.id
# These 4 could be replaced by functools.total_ordering when support
# for 2.6 is dropped.
def __le__(self, other):
return self.id <= other.id
def __gt__(self, other):
return self.id > other.id
def __ge__(self, other):
return self.id >= other.id
def __ne__(self, other):
return self.id != other.id
class RequestStatus(object):
"""Represents the status of an reqeust.
Attributes:
state: the state of the operation
message: an error message if it failed
"""
SUCCESS, FAILED, CANCELLED = range(3)
def __init__(self, controller):
if controller.Failed():
self._state = self.FAILED
self._message = controller.ErrorText()
elif controller.IsCanceled():
self._state = self.CANCELLED
self._message = controller.ErrorText()
else:
self._state = self.SUCCESS
self._message = None
def Succeeded(self):
"""Returns true if this request succeeded."""
return self._state == self.SUCCESS
@property
def state(self):
return self._state
@property
def message(self):
return self._message
class RDMNack(object):
NACK_SYMBOLS_TO_VALUES = {
'NR_UNKNOWN_PID': (0, 'Unknown PID'),
'NR_FORMAT_ERROR': (1, 'Format Error'),
'NR_HARDWARE_FAULT': (2, 'Hardware fault'),
'NR_PROXY_REJECT': (3, 'Proxy reject'),
'NR_WRITE_PROTECT': (4, 'Write protect'),
'NR_UNSUPPORTED_COMMAND_CLASS': (5, 'Unsupported command class'),
'NR_DATA_OUT_OF_RANGE': (6, 'Data out of range'),
'NR_BUFFER_FULL': (7, 'Buffer full'),
'NR_PACKET_SIZE_UNSUPPORTED': (8, 'Packet size unsupported'),
'NR_SUB_DEVICE_OUT_OF_RANGE': (9, 'Sub device out of range'),
'NR_PROXY_BUFFER_FULL': (10, 'Proxy buffer full'),
}
# this is populated below
_CODE_TO_OBJECT = {}
def __init__(self, nack_value, description):
self._value = nack_value
self._description = description
@property
def value(self):
return self._value
@property
def description(self):
return self._description
def __repr__(self):
s = 'RDMNack(value={value}, desc="{desc}")'
return s.format(value=self.value,
desc=self.description)
def __lt__(self, other):
return self.value < other.value
def __eq__(self, other):
return self.value == other.value
# These 4 could be replaced by functools.total_ordering when support
# for 2.6 is dropped.
def __le__(self, other):
return self.value <= other.value
def __gt__(self, other):
return self.value > other.value
def __ge__(self, other):
return self.value >= other.value
def __ne__(self, other):
return self.value != other.value
@classmethod
def LookupCode(cls, code):
obj = cls._CODE_TO_OBJECT.get(code, None)
if not obj:
obj = RDMNack(code, 'Unknown')
return obj
for symbol, (value, description) in RDMNack.NACK_SYMBOLS_TO_VALUES.items():
nack = RDMNack(value, description)
setattr(RDMNack, symbol, nack)
RDMNack._CODE_TO_OBJECT[value] = nack
class RDMFrame(object):
"""The raw data in an RDM frame.
The timing attributes may be 0 if the plugin does not provide timing
information. All timing data is in nano-seconds.
Attributes:
data: The raw byte data.
response_delay: The time between the request and the response.
break_time: The break duration.
mark_time: The mark duration.
data_time: The data time.
"""
def __init__(self, frame):
self._data = frame.raw_response
self._response_delay = frame.timing.response_delay
self._break_time = frame.timing.break_time
self._mark_time = frame.timing.mark_time
self._data_time = frame.timing.data_time
@property
def data(self):
return self._data
@property
def response_delay(self):
return self._response_delay
@property
def break_time(self):
return self._break_time
@property
def mark_time(self):
return self._mark_time
@property
def data_time(self):
return self._data_time
class RDMResponse(object):
"""Represents a RDM Response.
Failures can occur at many layers, the recommended way for dealing with
responses is:
Check .status.Succeeded(), if not true this indicates a rpc or server
error.
Check .response_code, if not RDM_COMPLETED_OK, it indicates a problem with
the RDM transport layer or malformed response.
If .response_code is RDM_COMPLETED_OK, .sub_device, .command_class, .pid,
.queued_messages hold the properties of the response.
Then check .response_type:
if .response_type is ACK:
.data holds the param data of the response.
If .response_type is ACK_TIMER:
.ack_timer: holds the number of ms before the response should be
available.
If .response_type is NACK_REASON:
.nack_reason holds the reason for nack'ing
Attributes:
status: The RequestStatus object for this request / response
response_code: The response code for the RDM request
response_type: The response type (ACK, ACK_TIMER, NACK_REASON) for
the request.
sub_device: The sub device that sent the response
command_class:
pid:
data:
queued_messages: The number of queued messages the remain.
nack_reason: If the response type was NACK_REASON, this is the reason for
the NACK.
ack_timer: If the response type was ACK_TIMER, this is the number of ms to
wait before checking for queued messages.
transaction_number:
frames: A list of RDM frames that made up this response.
"""
RESPONSE_CODES_TO_STRING = {
Ola_pb2.RDM_COMPLETED_OK: 'Ok',
Ola_pb2.RDM_WAS_BROADCAST: 'Request was broadcast',
Ola_pb2.RDM_FAILED_TO_SEND: 'Failed to send request',
Ola_pb2.RDM_TIMEOUT: 'Response Timeout',
Ola_pb2.RDM_INVALID_RESPONSE: 'Invalid Response',
Ola_pb2.RDM_UNKNOWN_UID: 'Unknown UID',
Ola_pb2.RDM_CHECKSUM_INCORRECT: 'Incorrect Checksum',
Ola_pb2.RDM_TRANSACTION_MISMATCH: 'Transaction number mismatch',
Ola_pb2.RDM_SUB_DEVICE_MISMATCH: 'Sub device mismatch',
Ola_pb2.RDM_SRC_UID_MISMATCH: 'Source UID in response doesn\'t match',
Ola_pb2.RDM_DEST_UID_MISMATCH: (
'Destination UID in response doesn\'t match'),
Ola_pb2.RDM_WRONG_SUB_START_CODE: 'Incorrect sub start code',
Ola_pb2.RDM_PACKET_TOO_SHORT: (
'RDM response was smaller than the minimum size'),
Ola_pb2.RDM_PACKET_LENGTH_MISMATCH: (
'The length field of packet didn\'t match length received'),
Ola_pb2.RDM_PARAM_LENGTH_MISMATCH: (
'The parameter length exceeds the remaining packet size'),
Ola_pb2.RDM_INVALID_COMMAND_CLASS: (
'The command class was not one of GET_RESPONSE or SET_RESPONSE'),
Ola_pb2.RDM_COMMAND_CLASS_MISMATCH: (
'The command class didn\'t match the request'),
Ola_pb2.RDM_INVALID_RESPONSE_TYPE: (
'The response type was not ACK, ACK_OVERFLOW, ACK_TIMER or NACK'),
Ola_pb2.RDM_PLUGIN_DISCOVERY_NOT_SUPPORTED: (
'The DISCOVERY Command Class is not supported by this controller'),
Ola_pb2.RDM_DUB_RESPONSE: (
'Discovery Unique Branch response')
}
def __init__(self, controller, response):
"""
Create a new RDMResponse object.
Args:
controller: The RpcController
response: A RDMResponse proto message.
"""
self._frames = []
self.status = RequestStatus(controller)
if self.status.Succeeded() and response is not None:
self._response_code = response.response_code
self._response_type = response.response_type
self._queued_messages = response.message_count
self._transaction_number = response.transaction_number
self.sub_device = response.sub_device
self.command_class = response.command_class
self.pid = response.param_id
self.data = response.data
for frame in response.raw_frame:
self._frames.append(RDMFrame(frame))
# we populate these below if required
self._nack_reason = None
self._ack_timer = None
if (self.status.Succeeded() and
self._response_code == Ola_pb2.RDM_COMPLETED_OK):
# check for ack timer or nack
if self._response_type == Ola_pb2.RDM_NACK_REASON:
nack_value = self._get_short_from_data(response.data)
if nack_value is None:
self._response_code = Ola_pb2.RDM_INVALID_RESPONSE
else:
self._nack_reason = RDMNack.LookupCode(nack_value)
elif self._response_type == Ola_pb2.RDM_ACK_TIMER:
self._ack_timer = self._get_short_from_data(response.data)
if self._ack_timer is None:
self._response_code = Ola_pb2.RDM_INVALID_RESPONSE
@property
def response_code(self):
return self._response_code
def ResponseCodeAsString(self):
return self.RESPONSE_CODES_TO_STRING.get(self._response_code, 'Unknown')
@property
def response_type(self):
return self._response_type
@property
def queued_messages(self):
return self._queued_messages
@property
def nack_reason(self):
return self._nack_reason
@property
def transaction_number(self):
return self._transaction_number
@property
def frames(self):
return self._frames
@property
def raw_response(self):
"""The list of byte strings in the response packets."""
data = []
for frame in self._frames:
data.append(frame.data)
return data
def WasAcked(self):
"""Returns true if this RDM request returned a ACK response."""
return (self.status.Succeeded() and
self.response_code == OlaClient.RDM_COMPLETED_OK and
self.response_type == OlaClient.RDM_ACK)
@property
def ack_timer(self):
return 100 * self._ack_timer
def __repr__(self):
if self.response_code != Ola_pb2.RDM_COMPLETED_OK:
s = 'RDMResponse(error="{error}")'
return s.format(error=self.ResponseCodeAsString())
if self.response_type == OlaClient.RDM_ACK:
s = 'RDMResponse(type=ACK, command_class={cmd})'
return s.format(cmd=self._command_class())
elif self.response_type == OlaClient.RDM_ACK_TIMER:
s = 'RDMResponse(type=ACK_TIMER, ack_timer={timer} ms, ' \
'command_class={cmd})'
return s.format(timer=self.ack_timer,
cmd=self._command_class())
elif self.response_type == OlaClient.RDM_NACK_REASON:
s = 'RDMResponse(type=NACK, reason="{reason}")'
return s.format(reason=self.nack_reason.description)
else:
s = 'RDMResponse(type="Unknown")'
return s
def _get_short_from_data(self, data):
"""Try to unpack the binary data into a short.
Args:
data: the binary data
Returns:
value: None if the unpacking failed
"""
try:
return struct.unpack('!h', data)[0]
except struct.error:
return None
def _command_class(self):
if self.command_class == OlaClient.RDM_GET_RESPONSE:
return 'GET'
elif self.command_class == OlaClient.RDM_SET_RESPONSE:
return 'SET'
elif self.command_class == OlaClient.RDM_DISCOVERY_RESPONSE:
return 'DISCOVERY'
else:
return "UNKNOWN_CC"
class OlaClient(Ola_pb2.OlaClientService):
"""The client used to communicate with olad."""
def __init__(self, our_socket=None, close_callback=None):
"""Create a new client.
Args:
socket: the socket to use for communications, if not provided one is
created.
close_callback: A callable to run if the socket is closed
"""
self._socket = our_socket
if self._socket is None:
self._socket = socket.socket()
try:
self._socket.connect(('localhost', 9010))
except socket.error:
raise OLADNotRunningException('Failed to connect to olad')
self._close_callback = close_callback
self._channel = StreamRpcChannel(self._socket, self, self._SocketClosed)
self._stub = Ola_pb2.OlaServerService_Stub(self._channel)
self._universe_callbacks = {}
def GetSocket(self):
"""Returns the socket used to communicate with the server."""
return self._socket
def SocketReady(self):
"""Called when the socket has new data."""
self._channel.SocketReady()
def _SocketClosed(self):
"""Called by the RPCChannel if the socket is closed."""
try:
self._socket.shutdown(socket.SHUT_RDWR)
except socket.error:
pass
self._socket.close()
self._socket = None
if self._close_callback:
self._close_callback()
def FetchPlugins(self, callback):
"""Fetch the list of plugins.
Args:
callback: the function to call once complete, takes two arguments, a
RequestStatus object and a list of Plugin objects
Returns:
True if the request was sent, False otherwise.
"""
if self._socket is None:
return False
controller = SimpleRpcController()
request = Ola_pb2.PluginListRequest()
try:
self._stub.GetPlugins(
controller, request,
lambda x, y: self._GetPluginsComplete(callback, x, y))
except socket.error:
raise OLADNotRunningException()
return True
def PluginDescription(self, callback, plugin_id):
"""Fetch the description of a plugin.
Args:
callback: the function to call once complete, takes two arguments, a
RequestStatus object and the plugin description text.
plugin_id: the id of the plugin
Returns:
True if the request was sent, False otherwise.
"""
if self._socket is None:
return False
controller = SimpleRpcController()
request = Ola_pb2.PluginDescriptionRequest()
request.plugin_id = plugin_id
try:
self._stub.GetPluginDescription(
controller, request,
lambda x, y: self._PluginDescriptionComplete(callback, x, y))
except socket.error:
raise OLADNotRunningException()
return True
def FetchDevices(self, callback, plugin_filter=Plugin.OLA_PLUGIN_ALL):
"""Fetch a list of devices from the server.
Args:
callback: The function to call once complete, takes two arguments, a
RequestStatus object and a list of Device objects.
filter: a plugin id to filter by
Returns:
True if the request was sent, False otherwise.
"""
if self._socket is None:
return False
controller = SimpleRpcController()
request = Ola_pb2.DeviceInfoRequest()
request.plugin_id = plugin_filter
try:
self._stub.GetDeviceInfo(
controller, request,
lambda x, y: self._DeviceInfoComplete(callback, x, y))
except socket.error:
raise OLADNotRunningException()
return True
def FetchUniverses(self, callback):
"""Fetch a list of universes from the server
Args:
callback: The function to call once complete, takes two arguments, a
RequestStatus object and a list of Universe objects.
Returns:
True if the request was sent, False otherwise.
"""
if self._socket is None:
return False
controller = SimpleRpcController()
request = Ola_pb2.OptionalUniverseRequest()
try:
self._stub.GetUniverseInfo(
controller, request,
lambda x, y: self._UniverseInfoComplete(callback, x, y))
except socket.error:
raise OLADNotRunningException()
return True
def FetchDmx(self, universe, callback):
"""Fetch DMX data from the server
Args:
universe: the universe to fetch the data for
callback: The function to call once complete, takes three arguments, a
RequestStatus object, a universe number and a list of dmx data.
Returns:
True if the request was sent, False otherwise.
"""
if self._socket is None:
return False
controller = SimpleRpcController()
request = Ola_pb2.UniverseRequest()
request.universe = universe
try:
self._stub.GetDmx(controller, request,
lambda x, y: self._GetDmxComplete(callback, x, y))
except socket.error:
raise OLADNotRunningException()
return True
def SendDmx(self, universe, data, callback=None):
"""Send DMX data to the server
Args:
universe: the universe to send the data for
data: An array object with the DMX data
callback: The function to call once complete, takes one argument, a
RequestStatus object.
Returns:
True if the request was sent, False otherwise.
"""
if self._socket is None:
return False
controller = SimpleRpcController()
request = Ola_pb2.DmxData()
request.universe = universe
request.data = data.tostring()
try:
self._stub.UpdateDmxData(
controller, request,
lambda x, y: self._AckMessageComplete(callback, x, y))
except socket.error:
raise OLADNotRunningException()
return True
def SetUniverseName(self, universe, name, callback=None):
"""Set the name of a universe.
Args:
universe: the universe to set the name of
name: the new name for the universe
callback: The function to call once complete, takes one argument, a
RequestStatus object.
Returns:
True if the request was sent, False otherwise.
"""
if self._socket is None:
return False
controller = SimpleRpcController()
request = Ola_pb2.UniverseNameRequest()
request.universe = universe
request.name = name
try:
self._stub.SetUniverseName(
controller, request,
lambda x, y: self._AckMessageComplete(callback, x, y))
except socket.error:
raise OLADNotRunningException()
return True
def SetUniverseMergeMode(self, universe, merge_mode, callback=None):
"""Set the merge mode of a universe.
Args:
universe: the universe to set the merge mode of
merge_mode: either Universe.HTP or Universe.LTP
callback: The function to call once complete, takes one argument, a
RequestStatus object.
Returns:
True if the request was sent, False otherwise.
"""
if self._socket is None:
return False
controller = SimpleRpcController()
request = Ola_pb2.MergeModeRequest()
request.universe = universe
request.merge_mode = merge_mode
try:
self._stub.SetMergeMode(
controller, request,
lambda x, y: self._AckMessageComplete(callback, x, y))
except socket.error:
raise OLADNotRunningException()
return True
def RegisterUniverse(self, universe, action, data_callback, callback=None):
"""Register to receive dmx updates for a universe.
Args:
universe: the universe to register to
action: OlaClient.REGISTER or OlaClient.UNREGISTER
data_callback: the function to be called when there is new data, passed
a single argument of type array.
callback: The function to call once complete, takes one argument, a
RequestStatus object.
Returns:
True if the request was sent, False otherwise.
"""
if self._socket is None:
return False
controller = SimpleRpcController()
request = Ola_pb2.RegisterDmxRequest()
request.universe = universe
request.action = action
try:
self._stub.RegisterForDmx(
controller, request,
lambda x, y: self._AckMessageComplete(callback, x, y))
except socket.error:
raise OLADNotRunningException()
if action == self.PATCH:
self._universe_callbacks[universe] = data_callback
elif universe in self._universe_callbacks:
del self._universe_callbacks[universe]
return True
def PatchPort(self, device_alias, port, is_output, action, universe,
callback=None):
"""Patch a port to a universe.
Args:
device_alias: the alias of the device of which to patch a port
port: the id of the port
is_output: select the input or output port
action: OlaClient.PATCH or OlaClient.UNPATCH
universe: the universe to set the name of
callback: The function to call once complete, takes one argument, a
RequestStatus object.
Returns:
True if the request was sent, False otherwise.
"""
if self._socket is None:
return False
controller = SimpleRpcController()
request = Ola_pb2.PatchPortRequest()
request.device_alias = device_alias
request.port_id = port
request.action = action
request.is_output = is_output
request.universe = universe
try:
self._stub.PatchPort(
controller, request,
lambda x, y: self._AckMessageComplete(callback, x, y))
except socket.error:
raise OLADNotRunningException()
return True
def ConfigureDevice(self, device_alias, request_data, callback):
"""Send a device config request.
Args:
device_alias: the alias of the device to configure
request_data: the request to send to the device
callback: The function to call once complete, takes two arguments, a
RequestStatus object and a response.
Returns:
True if the request was sent, False otherwise.
"""
if self._socket is None:
return False
controller = SimpleRpcController()
request = Ola_pb2.DeviceConfigRequest()
request.device_alias = device_alias
request.data = request_data
try:
self._stub.ConfigureDevice(
controller, request,
lambda x, y: self._ConfigureDeviceComplete(callback, x, y))
except socket.error:
raise OLADNotRunningException()
return True
def SendTimeCode(self,
time_code_type,
hours,
minutes,
seconds,
frames,
callback=None):
"""Send Time Code Data.
Args:
time_code_type: One of OlaClient.TIMECODE_FILM, OlaClient.TIMECODE_EBU,
OlaClient.TIMECODE_DF or OlaClient.TIMECODE_SMPTE
hours: the hours
minutes: the minutes
seconds: the seconds
frames: the frame count
callback: The function to call once complete, takes one argument, a
RequestStatus object.
Returns:
True if the request was sent, False otherwise.
"""
if self._socket is None:
return False
controller = SimpleRpcController()
request = Ola_pb2.TimeCode()
request.type = time_code_type
request.hours = hours
request.minutes = minutes
request.seconds = seconds
request.frames = frames
try:
self._stub.SendTimeCode(
controller, request,
lambda x, y: self._AckMessageComplete(callback, x, y))
except socket.error:
raise OLADNotRunningException()
return True
def UpdateDmxData(self, controller, request, callback):
"""Called when we receive new DMX data.
Args:
controller: An RpcController object
reqeust: A DmxData message
callback: The callback to run once complete
Returns:
True if the request was sent, False otherwise.
"""
if self._socket is None:
return False
if request.universe in self._universe_callbacks:
data = array.array('B')
data.fromstring(request.data)
self._universe_callbacks[request.universe](data)
response = Ola_pb2.Ack()
callback(response)
return True
def FetchUIDList(self, universe, callback):
"""Used to get a list of UIDs for a particular universe.
Args:
universe: The universe to get the UID list for.
callback: The function to call once complete, takes two arguments, a
RequestStatus object and a iterable of UIDs.
Returns:
True if the request was sent, False otherwise.
"""
if self._socket is None:
return False
controller = SimpleRpcController()
request = Ola_pb2.UniverseRequest()
request.universe = universe
try:
self._stub.GetUIDs(controller, request,
lambda x, y: self._FetchUIDsComplete(callback, x, y))
except socket.error:
raise OLADNotRunningException()
return True
def RunRDMDiscovery(self, universe, full, callback):
"""Triggers RDM discovery for a universe.
Args:
universe: The universe to run discovery for.
full: true to use full discovery, false for incremental (if supported)
callback: The function to call once complete, takes one argument, a
RequestStatus object.
Returns:
True if the request was sent, False otherwise.
"""
if self._socket is None:
return False
controller = SimpleRpcController()
request = Ola_pb2.DiscoveryRequest()
request.universe = universe
request.full = full
try:
self._stub.ForceDiscovery(
controller, request,
lambda x, y: self._FetchUIDsComplete(callback, x, y))
except socket.error:
raise OLADNotRunningException()
return True
def RDMGet(self, universe, uid, sub_device, param_id, callback, data='',
include_frames=False):
"""Send an RDM get command.
Args:
universe: The universe to get the UID list for.
uid: A UID object
sub_device: The sub device index
param_id: the param ID
callback: The function to call once complete, takes a RDMResponse object
data: the data to send
include_frames: True if the response should include the raw frame data.
Returns:
True if the request was sent, False otherwise.
"""
if self._socket is None:
return False
return self._RDMMessage(universe, uid, sub_device, param_id, callback,
data, include_frames)
def RDMSet(self, universe, uid, sub_device, param_id, callback, data='',
include_frames=False):
"""Send an RDM set command.
Args:
universe: The universe to get the UID list for.
uid: A UID object
sub_device: The sub device index
param_id: the param ID
callback: The function to call once complete, takes a RDMResponse object
data: the data to send
include_frames: True if the response should include the raw frame data.
Returns:
True if the request was sent, False otherwise.
"""
if self._socket is None:
return False
return self._RDMMessage(universe, uid, sub_device, param_id, callback,
data, include_frames, set=True)
def SendRawRDMDiscovery(self,
universe,
uid,
sub_device,
param_id,
callback,
data='',
include_frames=False):
"""Send an RDM Discovery command. Unless you're writing RDM tests you
shouldn't need to use this.
Args:
universe: The universe to get the UID list for.
uid: A UID object
sub_device: The sub device index
param_id: the param ID
callback: The function to call once complete, takes a RDMResponse object
data: the data to send
include_frames: True if the response should include the raw frame data.
Returns:
True if the request was sent, False otherwise.
"""
if self._socket is None:
return False
controller = SimpleRpcController()
request = Ola_pb2.RDMDiscoveryRequest()
request.universe = universe
request.uid.esta_id = uid.manufacturer_id
request.uid.device_id = uid.device_id
request.sub_device = sub_device
request.param_id = param_id
request.data = data
request.include_raw_response = True
request.include_raw_response = include_frames
try:
self._stub.RDMDiscoveryCommand(
controller, request,
lambda x, y: self._RDMCommandComplete(callback, x, y))
except socket.error:
raise OLADNotRunningException()
return True
def GetCandidatePorts(self, callback, universe=None):
"""Send a GetCandidatePorts request. The result is similar to FetchDevices
(GetDeviceInfo), except that returned devices will only contain ports
available for patching to the given universe. If universe is None, then the
devices will list their ports available for patching to a potential new
universe.
Args:
callback: The function to call once complete, takes a RequestStatus
object and a list of Device objects.
universe: The universe to get the candidate ports for. If unspecified,
return the candidate ports for a new universe.
Returns:
True if the request was sent, False otherwise.
"""
if self._socket is None:
return False
controller = SimpleRpcController()
request = Ola_pb2.OptionalUniverseRequest()
if universe is not None:
request.universe = universe
try:
# GetCandidatePorts works very much like GetDeviceInfo, so we can re-use
# its complete method.
self._stub.GetCandidatePorts(
controller, request,
lambda x, y: self._DeviceInfoComplete(callback, x, y))
except socket.error:
raise OLADNotRunningException()
return True
def _RDMMessage(self, universe, uid, sub_device, param_id, callback, data,
include_frames, set=False):
controller = SimpleRpcController()
request = Ola_pb2.RDMRequest()
request.universe = universe
request.uid.esta_id = uid.manufacturer_id
request.uid.device_id = uid.device_id
request.sub_device = sub_device
request.param_id = param_id
request.data = data
request.is_set = set
request.include_raw_response = include_frames
try:
self._stub.RDMCommand(
controller, request,
lambda x, y: self._RDMCommandComplete(callback, x, y))
except socket.error:
raise OLADNotRunningException()
return True
def _GetPluginsComplete(self, callback, controller, response):
"""Called when the list of plugins is returned.
Args:
callback: the callback to run
controller: an RpcController
response: a PluginInfoReply message.
"""
if not callback:
return
status = RequestStatus(controller)
plugins = None
if status.Succeeded():
plugins = sorted([Plugin.FromProtobuf(p) for p in response.plugin])
callback(status, plugins)
def _PluginDescriptionComplete(self, callback, controller, response):
"""Called when the plugin description is returned.
Args:
callback: the callback to run
controller: an RpcController
response: a PluginInfoReply message.
"""
if not callback:
return
status = RequestStatus(controller)
description = None
if status.Succeeded():
description = response.description
callback(status, description)
def _DeviceInfoComplete(self, callback, controller, response):
"""Called when the Device info request returns.
Args:
callback: the callback to run
controller: an RpcController
response: a DeviceInfoReply message.
"""
if not callback:
return
status = RequestStatus(controller)
devices = None
if status.Succeeded():
devices = []
for device in response.device:
input_ports = []
output_ports = []
for port in device.input_port:
input_ports.append(Port.FromProtobuf(port))
for port in device.output_port:
output_ports.append(Port.FromProtobuf(port))
devices.append(Device(device.device_id,
device.device_alias,
device.device_name,
device.plugin_id,
input_ports,
output_ports))
callback(status, devices)
def _UniverseInfoComplete(self, callback, controller, response):
"""Called when the Universe info request returns.
Args:
callback: the callback to run
controller: an RpcController
response: a UniverseInfoReply message.
"""
if not callback:
return
status = RequestStatus(controller)
universes = None
if status.Succeeded():
universes = [Universe.FromProtobuf(u) for u in response.universe]
callback(status, universes)
def _GetDmxComplete(self, callback, controller, response):
"""Called when the Universe info request returns.
Args:
callback: the callback to run
controller: an RpcController
response: a UniverseInfoReply message.
"""
if not callback:
return
status = RequestStatus(controller)
data = None
universe = None
if status.Succeeded():
data = array.array('B')
data.fromstring(response.data)
universe = response.universe
callback(status, universe, data)
def _AckMessageComplete(self, callback, controller, response):
"""Called when an rpc that returns an Ack completes.
Args:
callback: the callback to run
controller: an RpcController
response: an Ack message.
"""
if not callback:
return
status = RequestStatus(controller)
callback(status)
def _ConfigureDeviceComplete(self, callback, controller, response):
"""Called when a ConfigureDevice request completes.
Args:
callback: the callback to run
controller: an RpcController
response: an DeviceConfigReply message.
"""
if not callback:
return
status = RequestStatus(controller)
data = None
if status.Succeeded():
data = response.data
callback(status, data)
def _FetchUIDsComplete(self, callback, controller, response):
"""Called when a FetchUIDList request completes.
Args:
callback: the callback to run
controller: an RpcController
response: an UIDListReply message.
"""
if not callback:
return
status = RequestStatus(controller)
uids = None
if status.Succeeded():
uids = []
for uid in response.uid:
uids.append(UID(uid.esta_id, uid.device_id))
uids.sort()
callback(status, uids)
def _RDMCommandComplete(self, callback, controller, response):
"""Called when a RDM request completes.
Args:
callback: the callback to run
controller: an RpcController
response: an RDMResponse message.
"""
if not callback:
return
callback(RDMResponse(controller, response))
# Populate the patch & register actions
for value in Ola_pb2._PATCHACTION.values:
setattr(OlaClient, value.name, value.number)
for value in Ola_pb2._REGISTERACTION.values:
setattr(OlaClient, value.name, value.number)
# populate time code enums
for value in Ola_pb2._TIMECODETYPE.values:
setattr(OlaClient, value.name, value.number)
# populate the RDM response codes & types
for value in Ola_pb2._RDMRESPONSECODE.values:
setattr(OlaClient, value.name, value.number)
for value in Ola_pb2._RDMRESPONSETYPE.values:
setattr(OlaClient, value.name, value.number)
for value in Ola_pb2._RDMCOMMANDCLASS.values:
setattr(OlaClient, value.name, value.number)
|