1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

SOLVED Some advice to use properly the JSON API

Discussion in 'Software Support' started by NeeeeB, 3 February 2021.

  1. NeeeeB

    NeeeeB Active Member

    Messages:
    251
    Hardware:
    RPi3, +Arduino, +nodeMCU/ESP8266
    Hi,
    I'm trying to build a little android app for myself, so I can manage my instances.
    I need some info please.
    @Lord-Grey can you tell me if it is possible to aggregate more than 1 json command in a single request ?
    Let's say, I want to switch to instance 1 and then disable LEDDEVICE.
    Currently I need to run 2 requests, with the 2 JSON parts.
    Is it possible to do this in a single request ?
    If so can you show me how ?

    Many thanks.
     
  2. Lord-Grey

    Lord-Grey Moderator Staff Member Developer

    Messages:
    459
    Hardware:
    RPi3, 32/64bit
    @NeeeeB Let me have a look and come back to you....
     
    • Like Like x 1
  3. NeeeeB

    NeeeeB Active Member

    Messages:
    251
    Hardware:
    RPi3, +Arduino, +nodeMCU/ESP8266
    Hi,
    Any update on this ?
    thanks.
     
  4. Lord-Grey

    Lord-Grey Moderator Staff Member Developer

    Messages:
    459
    Hardware:
    RPi3, 32/64bit
    Hi
    Sorry, I have not had the chance to look into it yet... it is still on my radar...
     
  5. NeeeeB

    NeeeeB Active Member

    Messages:
    251
    Hardware:
    RPi3, +Arduino, +nodeMCU/ESP8266
    No problem ;-)
     
  6. Lord-Grey

    Lord-Grey Moderator Staff Member Developer

    Messages:
    459
    Hardware:
    RPi3, 32/64bit
    @NeeeeB Via curl you can execute two commands via one session using the --next option (sample below).

    In general, if one woudl like executing commands on instances which are not the default one (instance number 0), then it is required to do the switch instance command and then control action in one session (json-rpc or websocket). While for websocket this is not a problem json-rpc tools often execute every request in one session and the result is that the request is done on instance 0.
    Unfortunately, the current API does not allow to have the instance and action in one command.

    Sample: Following command would disable the LED-Device for instance 1 (e.g. the 2nd instance configured).

    Code:
    curl -i -X POST 'http://YOURHOSTIP:8090/json-rpc' --data '{"command" : "instance","subcommand" : "switchTo","instance" : 1}' --next 'http://YOURHOSTIP:8090/json-rpc' --data '{"command":"componentstate","componentstate":{"component":"LEDDEVICE","state":false}}' 
    Other API commands, please see here:

    https://docs.hyperion-project.org/en/json/Control.html
     
    • Informative Informative x 1
  7. NeeeeB

    NeeeeB Active Member

    Messages:
    251
    Hardware:
    RPi3, +Arduino, +nodeMCU/ESP8266
    Ok, many thanks for your answer.
    I can't use curl in my app, so no big deal, I'll send commands one after the other ;-)
     
  8. NeeeeB

    NeeeeB Active Member

    Messages:
    251
    Hardware:
    RPi3, +Arduino, +nodeMCU/ESP8266
    @Lord-Grey I'm sorry to bother you again, but I have another question about the JSON API.
    In particular, the serverinfo command.
    Let's say I have 3 instances running on my Hyperion.
    Now I do a serverinfo to get infos.
    To what instance are related the infos returned by "Components" ?
    https://docs.hyperion-project.org/en/json/ServerInfo.html#components

    I need to know this, because I use LEDDEVICE to disable leds from an instance (which I find more reliable than disabling the instance).

    Thanks !!
     
  9. davieboy

    davieboy Active Member

    Messages:
    242
    Hardware:
    RPi1/Zero, RPi2, RPi3, RPi4
    going by above it looks like unless you switch to a specific instance then run the -next option, by default you will get the first instance

    so if i understood it correct you would

    Code:
    curl -i -X POST 'http://YOURHOSTIP:8090/json-rpc' --data '{"command" : "instance","subcommand" : "switchTo","instance" : 1}' --next 'http://YOURHOSTIP:8090/json-rpc' --data '{"command": "serverinfo"}'
    
    that should give you the array of components in instance 1 - well in my head it does :roflmao:
     
    • Like Like x 1
  10. NeeeeB

    NeeeeB Active Member

    Messages:
    251
    Hardware:
    RPi3, +Arduino, +nodeMCU/ESP8266
    Yeah that is what I thought too but I wanted a confirmation before writing a lot of code to switch instance before running serverinfo, for nothing ^^
     
  11. davieboy

    davieboy Active Member

    Messages:
    242
    Hardware:
    RPi1/Zero, RPi2, RPi3, RPi4
    do a serverinfo query to get the entire JSON and save it to a file, least then you can look through it all and work out what sections you need to target, as it should show you all the specific settings relating to the instances you have on the server
     
  12. NeeeeB

    NeeeeB Active Member

    Messages:
    251
    Hardware:
    RPi3, +Arduino, +nodeMCU/ESP8266
    I already parse the JSON response ;-)
    I'll compare 2 queries, each one with a different instance disabled, to see if the components section is related to the "current" instance.
    Thanks btw.
     
  13. davieboy

    davieboy Active Member

    Messages:
    242
    Hardware:
    RPi1/Zero, RPi2, RPi3, RPi4
    no probs man, I had similar probe earlier on with how to store the JSON so i could reuse it without repeating the code or defining them manually :LOL:

    Good Luck
     
  14. Lord-Grey

    Lord-Grey Moderator Staff Member Developer

    Messages:
    459
    Hardware:
    RPi3, 32/64bit
    If you do not switch the instance, the data for instance 0 will be returned by default.

    Just to reiterate, you need to execute the instance switch and the command in one session!!!
    Otherwise the switch instance will be executed and then the command will be executed independently (which means it will run against instance 0).

    In case you are scripting something, you find the following library useful:
    https://github.com/dermotduffy/hyperion-py
     
    • Like Like x 1
  15. NeeeeB

    NeeeeB Active Member

    Messages:
    251
    Hardware:
    RPi3, +Arduino, +nodeMCU/ESP8266
    Wait...are you telling me that switching to an instance, and after that running another request to execute an action on it would fail ?
    Because that is not at all the results I get currently.
    I do a first request to switch to a particular instance, and after that another request to disable LEDDEVICE (or run an effect, or set a color), and everything works as intended...
    Also, thanks for the python library, but I'm writing an Android app in my native language which is Delphi, so it will be no use for me ^^
     
  16. Lord-Grey

    Lord-Grey Moderator Staff Member Developer

    Messages:
    459
    Hardware:
    RPi3, 32/64bit
    No. The point I tried to make is that the commands executed need to be in the same session.
    If one executes two curl commands in sequence, that is two sessions.
    As it was not clear how you are going to solution your scenario, I just wanted to be on the save side that there is no misunderstanding and wrong expectations....

    e.g. if you use Web-sockets, then you open the web-socket (1 session) and you can run as many commands as you like...
     
    • Like Like x 1
  17. NeeeeB

    NeeeeB Active Member

    Messages:
    251
    Hardware:
    RPi3, +Arduino, +nodeMCU/ESP8266
    Ah yes I see, sorry for the misunderstanding ;-)
    And thanks for the info.
     
  18. Lord-Grey

    Lord-Grey Moderator Staff Member Developer

    Messages:
    459
    Hardware:
    RPi3, 32/64bit
    • Like Like x 1
    • Thank you Thank you x 1
  19. NeeeeB

    NeeeeB Active Member

    Messages:
    251
    Hardware:
    RPi3, +Arduino, +nodeMCU/ESP8266
    ahah, at least my stupid questions turned into something useful ^^
     
    • Like Like x 1