switching & controlling instances via JSON API

  • I have Hyperion up & running on a windows machine.
    I am attempting to control multiple instances using python requests as follows

    [INDENT]command1 = post("", data = '{"command" : "instance","subcommand" : "switchTo","instance" : 1}')
    command2 = post("", data = '{"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":true}}')

    This generates the following logs:

    [INDENT] 2020-10-04T23:35:15.114Z [hyperiond HTTPJSONRPC] (DEBUG) (JsonAPI.cpp:81:JsonAPI::handleInstanceSwitch()) Client '::ffff:' switch to Hyperion instance 0
    2020-10-04T23:35:15.115Z [hyperiond HTTPJSONRPC] (DEBUG) (JsonAPI.cpp:81:JsonAPI::handleInstanceSwitch()) Client '::ffff:' switch to Hyperion instance 1
    2020-10-04T23:35:15.129Z [hyperiond HTTPJSONRPC] (DEBUG) (JsonAPI.cpp:81:JsonAPI::handleInstanceSwitch()) Client '::ffff:' switch to Hyperion instance 0

    As I understood it, switching instance would make the subsequent commands apply to that instance. But obviously each request I am sending counts as a new connection and switches to instance 0 again.
    Can anyone enlighten me as to how I might send both commands in one connection? Is this possible using requests, or any other methods?

    For the moment my workaround is to use the startInstance/stopInstance subcommands from the docs, but I would rather leave the instances running and just disable the LED Devices to avoid the startup delay.

    Would appreciate any help or advice! Thanks.

  • I found some related info on github, Lord-Grey commented:
    "Note: This does not properly work, if you are using individual sessions for each command (as with echo and nc).
    Plus it is counter intuitive that you currently cannot run one command against one or all instances.
    This is a know issue #809 and planned to be improved."

    So I understand it is not currently possible the way I am trying it, although it may be in a future update.

    edit: Durrr! Figured it out. Not a Hyperion issue but down to my poor knowledge of python requests library(in my defence it's under "Advanced Usage" there... :whistle:).

    The solution here was to use a requests session:

    [INDENT]s = requests.Session()
    command1 = s.post("", data = '{"command" : "instance","subcommand" : "switchTo","instance" : 2}')
    command2 = s.post("", data = '{"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":false}}')

    Maybe that can be of help to anyone who encounters the same issue. I would not have figured out what to google were it not for reading Lord-Grey's comments on github.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!