53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
#!/usr//bin/env python
|
|
# vim: ts=8 sts=4 sw=4 expandtab
|
|
# Author: Douglas Clowes (dcl@ansto.gov.au) 2012-06-29
|
|
from twisted.internet.protocol import Protocol
|
|
from twisted.internet.protocol import ReconnectingClientFactory
|
|
|
|
myCounters = {}
|
|
|
|
class Counter(Protocol):
|
|
def dataReceived(self, data):
|
|
print "Data:", data
|
|
|
|
def connectionMade(self):
|
|
print "connectionMade", self.transport.getHost(), self.transport.getPeer()
|
|
|
|
def connectionLost(self, reason):
|
|
print "connectionLost"
|
|
|
|
def sendMessage(self, m):
|
|
self.transport.write(m)
|
|
|
|
class CounterClientFactory(ReconnectingClientFactory):
|
|
def __init__(self):
|
|
self.device = None
|
|
|
|
def startedConnecting(self, connector):
|
|
print 'Started to connect.'
|
|
|
|
def buildProtocol(self, addr):
|
|
global myCounters
|
|
print 'Connected to:', addr
|
|
self.resetDelay()
|
|
p = Counter()
|
|
myCounters[addr.port] = p
|
|
self.device = p
|
|
print "My Counters", myCounters
|
|
return p
|
|
|
|
def clientConnectionLost(self, connector, reason):
|
|
global myCounters
|
|
print 'Lost connection', connector.getDestination().port,'Reason:', reason
|
|
del myCounters[connector.getDestination().port]
|
|
self.device = None
|
|
ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
|
|
|
|
def clientConnectionFailed(self, connector, reason):
|
|
print 'Connection failed. Reason:', reason
|
|
ReconnectingClientFactory.clientConnectionFailed(self, connector,reason)
|
|
|
|
def setCountRate(self, rate):
|
|
if self.device:
|
|
self.device.sendMessage("SET SOURCE=%d\r\n" % rate)
|