-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsocket.py
161 lines (147 loc) · 5.66 KB
/
socket.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
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
import socket, time, sys, json, time
import paho.mqtt.client as mqtt
import logging
_debug = True
_trace = True
logging.basicConfig(filename="./ASIAIR_"+str(sys.argv[2])+".log",
filemode="a",
format="%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s",
datefmt="%H:%M:%S",
level=logging.DEBUG)
# create socket object
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# get connection details from params
asisair_host = str(sys.argv[1])
asisair_port = int(sys.argv[2])
asisair_ident = str(sys.argv[3])
mqtt_host = str(sys.argv[4])
mqtt_port = int(sys.argv[5])
# PORT 4400 = Guiding & Telescope
# PORT 4700 = Imaging, FindStar, Annotate, PlateSolve, CameraControlChange, PiStatus
# Topics to include (wildcard for all)
'''
Alert
Annotate
AutoFocus
AutoGoto
AviRecord
Calibrating
CalibrationComplete
CalibrationFailed
CalibrationFailed
CalibrationFailed
CameraControlChange
CoolerPower
Exposure
FocuserMove
GuideStarLostTooMuch
GuideStep
GuidingStopped
INDIServer
LockPositionLost
LockPositionSet
LoopingExposures
LoopingExposuresStopped
LoopingFrames
PiStatus
PlateSolve
RestartGuide
ScopeHome
ScopeTrack
SettleBegin
SettleDone
Settling
StarLost
StarSelected
StartCalibration
StartGuiding
Temperature
Version
Nginx // Related to video stacking
RTMP // Related to video stacking
'''
topics = ['*']
#if _debug: print('connecting ASIAIR: ' + str(asisair_host) + ':' + str(asisair_port))
#clientSocket.connect((asisair_host, asisair_port))
## keep track of connection status
#connected = True
reconnect = 0
retry = 0
# setup
if _debug: print("connecting MQTT: " + str(mqtt_host) + ':' + str(mqtt_port))
clientMQTT = mqtt.Client()
clientMQTT.connect(mqtt_host, mqtt_port, 60)
while True:
# attempt to send and receive wave, otherwise reconnect
try:
message = b""
while True:
c = clientSocket.recv(1)
if c == b"\n":
break
elif c == b"":
if _debug: print("force reconnect")
clientSocket.close()
else:
message += c
x = message
# replace bad characters
message = message.replace(b"<\x90\xadE\xb6>", b"???")
message = message.replace(b"<\xe8>", b"???")
message = message.decode('iso-8859-1')
message = json.loads(message)
message['utime'] = int(time.time())
message['instance'] = asisair_ident
if message["Event"] in topics or "*" in topics:
if _debug: print(asisair_host + ":" + str(asisair_port) + " -> " + str(json.dumps(message)))
#if _debug: print(asisair_host + ":" + str(asisair_port) + " -> " + str(json.dumps(x)))
y = clientMQTT.publish(asisair_ident + "/" + message["Event"], str(json.dumps(message)))
else:
if _debug: print(str(asisair_host) + ":" + str(asisair_port) + "(ignored event: " + message["Event"] + ") -> " + str(json.dumps(message)))
pass
logging.debug(str(x))
reconnect += 1
except socket.error:
connected = False
while not connected:
# attempt to reconnect, otherwise sleep for 2 seconds
try:
# socket and reset connection status
clientSocket.close()
clientSocket = None
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientSocket.settimeout(2)
clientSocket.connect((asisair_host, asisair_port))
clientSocket.settimeout(None)
connected = True
if reconnect == 0:
if _debug: print("initial connection successful")
x = {'Event': "Connected", 'instance': asisair_ident, 'utime': int(time.time())}
y = clientMQTT.publish(asisair_ident + "/Connected", str(json.dumps(x)))
print()
if reconnect >= 1:
if _debug: print("reconnect successful")
retry = 0
#except socket.error as error:
except OSError as error:
if(clientSocket is not None):
clientSocket.close()
#if _debug: print("socket error, retry in 10 seconds [" + str(reconnect) + "/" + str(retry) + "]: " + str(error))
# if reconnect > 0 and retry == 0 an existing connection has been lost
if reconnect > 0 and retry == 0:
if _debug: print("connection lost [" + str(reconnect) + "/" + str(retry) + "]: " + str(error))
x = {'Event': "Disconnected", 'instance': asisair_ident, 'utime': int(time.time())}
y = clientMQTT.publish(asisair_ident + "/Disconnected", str(json.dumps(x)))
# if reconnect == 0 and retry == 0 the initial connection did fail (first attempt)
elif reconnect == 0 and retry == 0:
if _debug: print("initial connection failed [" + str(reconnect) + "/" + str(retry) + "]: " + str(error))
# if reconnect == 0 and retry > 0 the initial connection did fail (additional attempts)
elif reconnect == 0 and retry > 0:
if _debug: print("initial connection still not available [" + str(reconnect) + "/" + str(retry) + "]: " + str(error))
else:
if _debug: print("reconnect failed [" + str(reconnect) + "/" + str(retry) + "]: " + str(error))
connected = False
retry += 1
#time.sleep(1)
clientSocket.close()
if _debug: print("ENDE...")