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

Is there any way to get input from an external source for an agent? #313

Closed
ghost opened this issue Sep 30, 2018 · 2 comments
Closed

Is there any way to get input from an external source for an agent? #313

ghost opened this issue Sep 30, 2018 · 2 comments
Labels
Milestone

Comments

@ghost
Copy link

ghost commented Sep 30, 2018

I'm somewhat new to python and coding in general so this question might be somewhat stupid but I wanted to know if there is anyway to receive an input for an agent. I've tried doing it myself but receive Eof error everytime. I've pasted my code below.

from osbrain import run_agent
 from osbrain import run_nameserver
 import Fan
import Controller
import AirConditioner
import time
import Light
import User

class User(Agent):

    def on_init(self):
        self.bind('PUSH', alias='main')
        self.x = None

    def takeInput(self):
        try:
            self.x = input('Please enter some fucking thing: ')
        except EOFError:
            return

        self.send('main', self.x)

if __name__ == '__main__':

    # System Deployment
    ns = run_nameserver()
    fan1 = run_agent('fan1', base=Fan.Fan)
    fan2 = run_agent('fan2', base=Fan.Fan)
    light1 = run_agent('light1', base=Light.Lights)
    light2 = run_agent('light2', base=Light.Lights)
    AC = run_agent('AC', base=AirConditioner.AC)
    controller = run_agent('controller', base=Controller.Controller)
    user = run_agent('user', base=User.User)

    # System Connections
    controller.connect(server=fan1.addr('main'), handler='fan1status')
    controller.connect(fan2.addr('main'), handler='fan2status')
    controller.connect(AC.addr('main'), handler='ACStatus')
    controller.connect(light1.addr('main'), handler='light1status')
    controller.connect(light2.addr('main'), handler='light2status')
    controller.connect(user.addr('main'), handler='userInput')
    fan1.connect(controller.addr('main'), handler='receive')
    fan2.connect(controller.addr('main'), handler='receive')
    light1.connect(controller.addr('main'), handler='receive')
    light2.connect(controller.addr('main'), handler='receive')
    AC.connect(controller.addr('main'), handler='receive')

    # sensor.send_values()
    controller.send_values()
    i = 5

    while(i > 0):
        user.takeInput()
        controller.send_values()
        controller.machineStatus()

Everything seems to be working rather well (the code might be a bit ugly though considering my newbish background) except for receiving the input. I'd love some help from people over here.

@Peque
Copy link
Member

Peque commented Oct 1, 2018

There is no way to do that in osBrain.

In theory, you could pass (a copy of) your current standard input file descriptor to a process (agent) on creation.

However, I do not think that is what you want to do. Agents are running in parallel, so how would you want your system to behave if there are multiple agents waiting for "input" at the same time? Would you want to have them all receive the same input? I am guessing you only have a single terminal to write that input.

If you are simply trying to input random data for testing your system, then I would recommend you to use timers instead, and generate random data for your agents periodically. This would probably mimic your real application better than if you have to manually input some values every time. 😊

If your "User" agent is a single and unique agent whose only purpose is to read user input and send it to the other agents in the network, then I would rather not make that an agent. Use your "main" script and call input() in the infinite loop you created.

@Peque Peque added the question label Oct 1, 2018
@Peque Peque added this to the 0.7.0 milestone Oct 1, 2018
@ghost
Copy link
Author

ghost commented Oct 1, 2018 via email

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant