Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Server Functionality with Decorator Style Syntax #8

Open
engineerjoe440 opened this issue Apr 30, 2024 · 2 comments
Open
Labels
enhancement New feature or request question Further information is requested

Comments

@engineerjoe440
Copy link
Owner

engineerjoe440 commented Apr 30, 2024

It would be pretty cool to have a full decorator-styled syntax for a server to provide access to the various data points that could be defined for SEL Protocol Regions. Take this for example:

from selprotopy.server import SELProtoServer

# Create a simple server listening on all network interfaces on port 23
server = SELProtoServer(
    host="0.0.0.0",
    port=23,
    fid="SEL-751....",
)


@server.fast_meter_binary("52A")
def serve_52A() -> bool:
    """Respond with a Boolean to Represent the 52A Status."""
    return True

@server.fast_meter_analog("IA")
def serve_IA() -> complex:
    """Return the Complex Value for the Phase-A Current."""
    # Return the static value: 67 /_ 120
    return complex(-33.45 + 58.024j)

@server.command_response("MET E")
def met_e_command() -> str:
    """Respond to the `MET E` Command with an Appropriate set of Data."""
    with open("my_saved_met_e.txt", 'r', encoding='utf-8') as file:
        return file.read()


if __name__ == "__main__":
    server.run()
@engineerjoe440 engineerjoe440 added enhancement New feature or request question Further information is requested labels Apr 30, 2024
@engineerjoe440
Copy link
Owner Author

Here's a little sample code that could be used as the functional structure of the code being used, above:

class DecoratedRecorder:

    def __init__(self) -> None:
        """Set up Storage Facility for Lookups."""
        self._callbacks = {}
    
    def new_attachment(self, name):
        def decorator(func):
            self._callbacks[name] = func
            return func
        return decorator

    def associate_callback(self, name):
        """Test for Creating an Associative Callback."""
        print("does something")

        return self.new_attachment(name)

recorder = DecoratedRecorder()


@recorder.associate_callback("test")
def test_this():
    """Test to See What Happens."""
    return "foo"

print(recorder._callbacks)

@engineerjoe440
Copy link
Owner Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant