XML-RPC Valueserver

In this example, we start an XML RPC server, which can simply retrieve the values ​​of the IOs. A member from the RevolutionPi Forum had the idea to call the counterstands of the DIO modules over the network. He processes the data on another system. (Thanks for the idea!)

import revpimodio
import signal
from concurrent import futures
from xmlrpc.server import SimpleXMLRPCServer


class RevPiValueServer():
    
    def __init__(self):
        """Init of the class."""
        # RevPiModIO - with monitoring=True so it is ONLY being read!!!
        self.rpi = revpimodio.RevPiModIO(auto_refresh=True, monitoring=True)

        # XMLRPC-Server
        self.xsrv =  SimpleXMLRPCServer(("", 55000), logRequests=False)
        self.xsrv.register_introspection_functions()
        self.xsrv.register_function(self.get_iovalue)

        # Signal events
        signal.signal(signal.SIGINT, self._sigexit)
        signal.signal(signal.SIGTERM, self._sigexit)

    def _sigexit(self, signum=None, frame=None):
        """Handles the exit signal."""
        self.xsrv.shutdown()

    def get_iovalue(self, device, io):
        """Returns a value.
        @param device: Modulename
        @param io: IO-Name
        @returns: IO-Value"""
        if type(io) != str:
            raise ValueError("Work with IO-Names only!")

        io = self.rpi.devices[device][io]
        return io.value

    def start(self):
        """Starts the XMLRPC-Server."""
        e = futures.ThreadPoolExecutor(max_workers=1)
        self._futsrv = e.submit(self.xsrv.serve_forever)
        print("Server started!")
        e.shutdown()


if __name__ == "__main__":
    root = RevPiValueServer()
    root.start()

You can retrieve the values ​​simply via:

# Tested via IDLE

>>> from xmlrpc.client import ServerProxy
>>> cli = ServerProxy("http://192.168.50.152:55000")
>>> cli.get_iovalue(32,"Counter_1_i07")
31

Important is the piCtory configuration for the MEM value so the input also works as a Counter:

The counter value is then on the input Counter_1_i07:

Leave a Reply

Your email address will not be published.