我正在为现有软件编写一个插件,该插件将使用 twisted 与 Denon AV 接收器进行通信。我在我的协议中创建了一个方法 powerDenonOff,用于关闭设备,并在应用程序通过 runConcurrentThread 启动时在线程中启动反应器。我还有另一种方法,powerOff,当用户在软件中选择关闭设备时将调用该方法。我无法弄清楚如何从 Plugin 类调用协议中的 powerDenonOff 方法。
from twisted.internet.protocol import Factory, Protocol
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
class DenonProtocol(LineReceiver):
delimiter='\r'
def lineReceived(self, line):
pass
def connectionMade(self):
pass
def powerDenonOff(self):
self.transport.write("PWSTANDBY")
def __call__(self):
pass
class DenonFactory(Factory):
def __init__(self):
pass
def startedConnecting(self, connector):
pass
def clientConnectionFailed(self, one, two):
pass
def connectionMade(self):
pass
def buildProtocol(self, addr):
return DenonProtocol()
class Plugin(software.PluginClass):
def powerOff(self):
reactor.callInThread(powerDenonOff) #I think this may need to be callFromThread but
#but I left it since that was where the poorly
#worded version of my question left off.
def runConcurrentThread(self):
try:
while True:
#hardcoded for testing
port = 23
host = "111.11.11.11"
denon=DenonFactory()
reactor.connectTCP(host, port, denon)
reactor.run()
except self.StopThread:
pass
def stopConcurrentThread(self):
reactor.callFromThread(reactor.stop)
如何在我的协议类中使用该方法?有没有更好的方法来实现这一点?
谢谢