/
testindiclient.py
126 lines (101 loc) · 4.71 KB
/
testindiclient.py
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
# for logging
import sys
import time
import logging
# import the PyIndi module
import PyIndi
# The IndiClient class which inherits from the module PyIndi.BaseClient class
# Note that all INDI constants are accessible from the module as PyIndi.CONSTANTNAME
class IndiClient(PyIndi.BaseClient):
def __init__(self):
super(IndiClient, self).__init__()
self.logger = logging.getLogger("IndiClient")
self.logger.info("creating an instance of IndiClient")
def newDevice(self, d):
"""Emmited when a new device is created from INDI server."""
self.logger.info(f"new device {d.getDeviceName()}")
def removeDevice(self, d):
"""Emmited when a device is deleted from INDI server."""
self.logger.info(f"remove device {d.getDeviceName()}")
def newProperty(self, p):
"""Emmited when a new property is created for an INDI driver."""
self.logger.info(
f"new property {p.getName()} as {p.getTypeAsString()} for device {p.getDeviceName()}"
)
def updateProperty(self, p):
"""Emmited when a new property value arrives from INDI server."""
self.logger.info(
f"update property {p.getName()} as {p.getTypeAsString()} for device {p.getDeviceName()}"
)
def removeProperty(self, p):
"""Emmited when a property is deleted for an INDI driver."""
self.logger.info(
f"remove property {p.getName()} as {p.getTypeAsString()} for device {p.getDeviceName()}"
)
def newMessage(self, d, m):
"""Emmited when a new message arrives from INDI server."""
self.logger.info(f"new Message {d.messageQueue(m)}")
def serverConnected(self):
"""Emmited when the server is connected."""
self.logger.info(f"Server connected ({self.getHost()}:{self.getPort()})")
def serverDisconnected(self, code):
"""Emmited when the server gets disconnected."""
self.logger.info(
f"Server disconnected (exit code = {code},{self.getHost()}:{self.getPort()})"
)
logging.basicConfig(format="%(asctime)s %(message)s", level=logging.INFO)
# Create an instance of the IndiClient class and initialize its host/port members
indiClient = IndiClient()
indiClient.setServer("localhost", 7624)
# Connect to server
print("Connecting and waiting 1 sec")
if not indiClient.connectServer():
print(
f"No indiserver running on {indiClient.getHost()}:{indiClient.getPort()} - Try to run"
)
print(" indiserver indi_simulator_telescope indi_simulator_ccd")
sys.exit(1)
# Waiting for discover devices
time.sleep(1)
# Print list of devices. The list is obtained from the wrapper function getDevices as indiClient is an instance
# of PyIndi.BaseClient and the original C++ array is mapped to a Python List. Each device in this list is an
# instance of PyIndi.BaseDevice, so we use getDeviceName to print its actual name.
print("List of devices")
deviceList = indiClient.getDevices()
for device in deviceList:
print(f" > {device.getDeviceName()}")
# Print all properties and their associated values.
print("List of Device Properties")
for device in deviceList:
print(f"-- {device.getDeviceName()}")
genericPropertyList = device.getProperties()
for genericProperty in genericPropertyList:
print(f" > {genericProperty.getName()} {genericProperty.getTypeAsString()}")
if genericProperty.getType() == PyIndi.INDI_TEXT:
for widget in PyIndi.PropertyText(genericProperty):
print(
f" {widget.getName()}({widget.getLabel()}) = {widget.getText()}"
)
if genericProperty.getType() == PyIndi.INDI_NUMBER:
for widget in PyIndi.PropertyNumber(genericProperty):
print(
f" {widget.getName()}({widget.getLabel()}) = {widget.getValue()}"
)
if genericProperty.getType() == PyIndi.INDI_SWITCH:
for widget in PyIndi.PropertySwitch(genericProperty):
print(
f" {widget.getName()}({widget.getLabel()}) = {widget.getStateAsString()}"
)
if genericProperty.getType() == PyIndi.INDI_LIGHT:
for widget in PyIndi.PropertyLight(genericProperty):
print(
f" {widget.getLabel()}({widget.getLabel()}) = {widget.getStateAsString()}"
)
if genericProperty.getType() == PyIndi.INDI_BLOB:
for widget in PyIndi.PropertyBlob(genericProperty):
print(
f" {widget.getName()}({widget.getLabel()}) = <blob {widget.getSize()} bytes>"
)
# Disconnect from the indiserver
print("Disconnecting")
indiClient.disconnectServer()