6 from twisted.internet.protocol
import DatagramProtocol
7 from twisted.internet
import reactor
9 __all__ = [
"UDPSender",
"getBinaryForUDP"]
12 """!A variant of DatagramProtocol that enables broadcasting
15 self.transport.setBroadcastAllowed(
True)
19 """!Object which will broadcast UDP Packets containing current position and other data
21 The packet format is documented here: http://www.apo.nmsu.edu/Telescopes/TCC/UDPPackets.html
23 @param[in] port port from which to send UDP packets
30 """!Shut down the port, to close cleanly
33 self.udpListen.stopListening()
37 """!Open the port to allow writing data
45 self.protocol.transport.write(binaryData, addr=(
"<broadcast>", self.
port))
49 udpFmt =
"!iiiidd8sdddddddddi4xddddddddddddd" \
50 +
"i4xi4xi4xi4xi4xi4xdddddddddi4xi4xi4x"
105 """Format a binary representation of the state of the actor, for sending via UDP
107 The format is described here: http://www.apo.nmsu.edu/Telescopes/TCC/UDPPackets.html
109 @param[in] inst: instrument block
110 @param[in] obj: object block
112 updateTime = obj.updateTime
113 netUserEquat = coordConv.PVT()
114 netUserPolar = coordConv.PVT()
115 obj.netUserPos.getSphPVT(netUserEquat, netUserPolar)
116 tccPos = obj.targetMount
117 toPack = collections.OrderedDict((
119 (
"type", -559038737),
122 (
"taiDate", updateTime),
123 (
"slewEndtime", obj.slewEndTime),
124 (
"coordSys", obj.userSys.getName()),
125 (
"epoch", obj.userSys.getDate()),
126 (
"objNetPos.pos1", netUserEquat.getPos(updateTime)),
127 (
"objNetPos.vel1", netUserEquat.vel),
128 (
"objNetPos.pos2", netUserPolar.getPos(updateTime)),
129 (
"objNetPos.vel2", netUserPolar.vel),
130 (
"boresight.pos1", obj.objInstXY[0].getPos(updateTime)),
131 (
"boresight.vel1", obj.objInstXY[0].vel),
132 (
"boresight.pos2", obj.objInstXY[1].getPos(updateTime)),
133 (
"boresight.vel2", obj.objInstXY[1].vel),
134 (
"rotType", obj.rotType),
135 (
"rotUser.pos", obj.rotUser.pos),
136 (
"rotUser.vel", obj.rotUser.vel),
137 (
"objUserInstAng.pos", obj.objUserInstAng.getPos(updateTime)),
138 (
"objUserInstAng.vel", obj.objUserInstAng.vel),
139 (
"spiderInstAng.pos", obj.spiderInstAng.getPos(updateTime)),
140 (
"spiderInstAng.vel", obj.spiderInstAng.vel),
141 (
"tccPos.pos1", tccPos[0].getPos(updateTime)),
142 (
"tccPos.vel1", tccPos[0].vel),
143 (
"tccPos.pos2", tccPos[1].getPos(updateTime)),
144 (
"tccPos.vel2", tccPos[1].vel),
145 (
"tccPos.pos3", tccPos[2].getPos(updateTime)),
146 (
"tccPos.vel3", tccPos[2].vel),
147 (
"secFocus", inst.secUserFocus),
148 (
"axisCmdState1", obj.axisCmdState[0]),
149 (
"axisCmdState2", obj.axisCmdState[1]),
150 (
"axisCmdState3", obj.axisCmdState[2]),
151 (
"axisErrCode1", obj.axisErrCode[0]),
152 (
"axisErrCode2", obj.axisErrCode[1]),
153 (
"axisErrCode3", obj.axisErrCode[2]),
156 (
"actMount.pos1", obj.actMount[0].pos),
157 (
"actMount.vel1", obj.actMount[0].vel),
158 (
"actMount.t1", obj.actMount[0].t),
159 (
"actMount.pos2", obj.actMount[1].pos),
160 (
"actMount.vel2", obj.actMount[1].vel),
161 (
"actMount.t2", obj.actMount[1].t),
162 (
"actMount.pos3", obj.actMount[2].pos),
163 (
"actMount.vel3", obj.actMount[2].vel),
164 (
"actMount.t3", obj.actMount[2].t),
165 (
"axisStatusWord1", obj.axisStatusWord[0]),
166 (
"axisStatusWord2", obj.axisStatusWord[1]),
167 (
"axisStatusWord3", obj.axisStatusWord[2]),
169 return struct.pack(udpFmt, *toPack.values())
172 """!Produce readable text from an encoded UDP packet broadcasted from the tcc
173 return the datagram in a dictionary format
176 out = struct.unpack(udpFmt,pkt)
177 print 'unwrappingPacket:'
178 for field, data
in itertools.izip(fields, out):
179 outDict[field] = data
180 print "%s: %s" % (field, data)
187 self.transport.setBroadcastAllowed(
True)
191 print "Number %i Datagram received from %s" % (len(self.
packetsReceived), repr(address),)
196 self.transport.joinGroup(
"X.X.X.X")
202 print "Listening to UDP broadcasts on port %s" % (self.
port,)
208 """!Create a twisted protocol that listens for UDP output at port
214 """!cleanly stop listening to avoid dirty reactors
217 self.listener.stopListening()
222 return self.broadcastClient.packetsReceived
def stopListening
Shut down the port, to close cleanly.
def __init__
Object which will broadcast UDP Packets containing current position and other data.
A variant of DatagramProtocol that enables broadcasting.
def startListening
Create a twisted protocol that listens for UDP output at port.
def unwrapPacket
Produce readable text from an encoded UDP packet broadcasted from the tcc return the datagram in a di...
def startListening
Open the port to allow writing data.
def sendPacket
Write binaryData.
def stopListening
cleanly stop listening to avoid dirty reactors