API call to Sony AG9 via HTTP

Hi There,

(My German is not that good so I hope English is ok for you guys)

Since a couple of weeks I own a Sony AG9 TV which can be controlled via rest API / JSON RPC, so now during the holidays the time has come to have IP Symcon doing at least some basic stuff like switching the TV on and off i.e. when the alarm system is activated / de-activated.

In order to do that I tried to build some basic scripts. As the examples of Sony are in javascript I tried to ‚convert‘ it to php using several examples also from this forum but I can’t seem to get it right.

The javascript example from sony switches the input to HDMI 2. See the JS code underneath:

var xhr = new XMLHttpRequest();
xhr.open(‚POST‘, ‚http://192.168.0.1/sony/avContent‘);
xhr.setRequestHeader(‚X-Auth-PSK‘, ‚1234‘);
xhr.send(JSON.stringify(
{method: „setPlayContent“,
version: „1.0“,
id: 1,
params: [{uri: „extInput:hdmi?port=2“}]}
));

My own code in symcon php looks like:

    HTTP = new XMLHttpRequest();
    HTTP.open("POST","192.168.1.118/sony/avContent",true);
        var rpc = JSON.stringify({"method":"setPlayContent", "version":"1.0", "id":"1", "params":"[{uri: "extInput:hdmi?port=2"}]";
    HTTP.setRequestHeader("X-Auth-PSK", "1234");
    //HTTP.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
    HTTP.send(rpc);

However I get an error message w.r.t. the first line of code (line 21):
parse error: syntax error, unexpected ‚=‘ in C:\ProgramData\Symcon\scripts\45885.ips.php on line 21

I’ve searched through the forum but I cannot get rid of the error …

I would ‚more than appreciate‘ your help.
Many thanks in advance!

Jan

Hello Jan,

in PHP a variable starts with a $ sign.
For example $ myvar = „“;
Please show us line 21…

lueralba

Hi Lueralba,

I am aware of that and also tried it of course. In the code examples I got from this forum for this this case no $ signs were used.

Line 21 is the first line of code of the script:

HTTP = new XMLHttpRequest();

Kind regards,
Jan

Nevertheless a variable has to start with $.

What is the error message when you use $HTTP?

By the way there is a Sony Module in the store. Perhaps you‘ll give it a try.

Burkhard

I did put $ signs for all the variables:


        $HTTP = new XMLHttpRequest();
        $HTTP.open("POST","192.168.1.118/sony/avContent",true);
            var $rpc = JSON.stringify({"method":"setPlayContent", "version":"1.0", "id":"1", "params":"[{uri: "extInput:hdmi?port=2"}]";
        $HTTP.setRequestHeader("X-Auth-PSK", "1234");
        $HTTP.send($rpc);

Unlike last time the error now shifts to line 23:
var $rpc = JSON.stringify({„method“:„setPlayContent“, „version“:„1.0“, „id“:„1“, „params“:"[{uri: „extInput:hdmi?port=2“}]";

The error code:
Parse error: syntax error, unexpected ‚var‘ (T_VAR), expecting end of file in C:\ProgramData\Symcon\scripts\45885.ips.php on line 23

I’ll take a look at the sony module. But you can imagine that I need to get this working now :wink:

thx,
Jan

Hi Burkhard,
I looked for the Sony modul but I can’t find it in the Symcon modul store?
Regards, Jan

Hi Jan,

the Sony Module has only a documentation in German.
Perhaps that is the reason you can‘t find it.

Does @anybody know, if there is a work around to install a „German module“ in an English environment?

Burkhard

Edit: the trick is to set the language of your browser to German. Then in the module store the German modules are offered, too.

Thx. I’ll give it a try.
Jan

Hi Burkhard,
The trick worked using the german language in google chrome. I’ve got it working now.
Regards, Jan

Hi Burkhard,
I can’t seem to call the functions in your module directly from a script but using this command I can easily switch on and off my tv now:

RequestAction(22271, 2);

22271 is the id of the ‚Status‘ variable, 2 is the value for switching it to „Eingeschaltet“ (1 for switching it to standby)

So for this purpose it works.

What I would like to do further is to switch on the TV but then also to startup an app. In this case Puffin with an URL to a news site http://nu.nl

The list of applications in the symcon app seems empty however. Is there something I can do to fix that?

thx

Jan

You can also use the STV_* (i.e. STV_SetPowerStatus) functions.

You can check with

STV_ReadApplicationList(int $InstanceID):string

the list of callable apps reported by the Sony API. But make sure that the Sony is switched on.:). The test function „Update application list“ also updates the list of applications.

Also you can get the complete API with

STV_WriteAPIInformationToFile(int $InstanceID, $filename)

Burkhard