1

Using twistd plugin system I launch Twisted applications. With the help of this SO answer, I've implemented a PID file (where the process ID is written into it) to determine if a daemon has been successfully started or not.

The monitoring of the twisted daemon (started or not yet started) is done by native (OS) application that just checks if the PID file exists and reads it content to see if there is an active process with that PID.

So far so good. The twisted plugins that I wrote create a Service object which accepts a Driver object as argument. All hardware specific operations are contained in this Driver class.

The internal details of what a Service does I think, is out of the scope of the question. But just to clarify there would be many Service's like these that interact with each other over network.

Now, consider that the Driver interacts with hardware using UART/Serial communication. The target hardware always enumerates itself with a fixed name which is used in the Driver to recognize the (physical) availability of the hardware.

In certain situations the hardware might not be (physically) connected to the system. Therefore, I wish to implement a way to identify if the hardware is actually connected or not.

Here's a minimal twisted plugin code

class Options(usage.Options):
    optFlags = [
        ['emulate', 'e', 'Emulate the laser'],
        ]

class HardwareServiceMaker(object):
      implements(service.IServiceMaker, IPlugin)
      tapname = "test.hw"
      options = Options

      def makeService(self, options):

        from hardwaredrivers import *
        driver = HardwareDriver()

        service = HarwareService(driver)
        service.setPIDFile('test.hw.txt')

        return service

Here's the Service and Driver

class HardwareService(twisted.application.service.Service):
    def __init__(self,driver)
        self._driver = driver
    def do_something(self):
        self._driver.control_something()

class HardwareDriver():
    def __init__(self):
        # Lets say getAllSerials() returns a list of all available serial connections
        allSerials = getAllSerials()
        for serial in allSerials:
            if 'TARGET_HARDWARE' in serial:
                self.ser = serial.Serial()
                self.ser.open()

    def control_something():
        self.ser.write('ON\n')

From the above code, it can be seen that if there is no hardware device that enumerates as TARGET_HARDWARE, then self.ser is None. Which results in an erroneous operations when control_something() is called.

I want to prevent this happening, or more accurately prompt user that there is an hardware error while the plugin(therefore the service, therefore the driver) is being launched using twistd.

RHLK
  • 347
  • 3
  • 14
  • This code isn't minimal. It's smaller than minimal. It does not completely encapsulate the details of the question you're asking. – Jean-Paul Calderone Aug 07 '17 at 19:26
  • 1
    Hey Jean-Paul, I've tried to improve my question using clear explanations and a little more code. Hope that this makes it clearer to understand. Please look into it. Thanks – RHLK Aug 07 '17 at 20:21

0 Answers0