The Vera Community forums have moved!

Advanced => Programming => General => Topic started by: mikkelj on April 14, 2011, 10:07:01 am

Title: Scene manipulation with HTTP requests
Post by: mikkelj on April 14, 2011, 10:07:01 am
Hi guys,

I've been reading through the wiki and grinding the search-function, but I can't figure this one out myself. I want to modify some properties of a scene. Consider a simple example:

I have a scene that turns on the heat, whenever the temperature drops to say 15C.

The property of the scene looks like this:

http://img402.imageshack.us/img402/9703/screenshotevent.jpg
(Can I embed images in my post? Usually [img] works, but not here)


... and the JSON data for this event looks like this:

Code: [Select]
...
    "scenes": [
        {
            "name": "Heat ON",
            "posx": 0,
            "posy": 0,
            "Device_Num_56": {
                "event_2_1": {
                    "name": "Heat on",
                    "Enabled": "1",
                    "argument_1": "15",
                    "LastEval": 0
                }
            },
            "id": 1,
            "Timestamp": 1299829983,
            "room": 0
        }
    ],
...


I wanna modify those 15 degrees, but I can't get my head around it  >:(


Now this is what I know:

Assuming my scene has the number 1

If I want to run a scene I do it like this:
http://ip:3480/data_request?id=lu_action&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=RunScene&SceneNum=1

If I want to rename a scene, I can do it like this:
http://ip:3480/data_request?id=scene&action=rename&scene=1&name=NEW_SCENE_NAME


According to the wiki, I'm supposed to use an action called ModifyUserData, to modify the variable argument_1.

I got this from a colleague, but I can't modify it to work:

Quote
http://ip:3480/data_request?id=lu_action&DeviceNum=0&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=ModifyUserData&inUserData=
{
    "devices": {
        "devices_19": {
            "states": {
                "states_25": {
                    "value": "2,1d,5,3,1d,40"
                }
            }
        }
    },
    "scenes": {
        
    },
    "sections": {
        
    },
    "rooms": {
        
    },
    "users": {
        
    }
}
(I've inserted a newline and indented the JSON for readability)

Can any of you guys point me in the right direction - I think I'm close! ;D

Regards
/Mikkel
Title: Re: Scene manipulation with HTTP requests
Post by: Ap15e on April 14, 2011, 12:10:16 pm
My guess:

Code: [Select]
{
  "scenes": [ {
                "name": "Heat ON",
                "Device_Num_56": {
                                   "event_2_1": {
                                                  "argument_1": "25"
                                                }
                                 }
              }
            ]
}

Use at your own risk. Experimenting with ModifyUserData is dangerous. Backup your user_data.json.lzo ...
Title: Re: Scene manipulation with HTTP requests
Post by: mcvflorin on April 15, 2011, 04:35:57 am
Do you want to modify it dynamically, based on a certain event?

If not, why don't you edit the event and modify that value by hand?
Title: Re: Scene manipulation with HTTP requests
Post by: mikkelj on April 15, 2011, 04:39:05 am
Use at your own risk. Experimenting with ModifyUserData is dangerous. Backup your user_data.json.lzo ...

Thank you :) The JSON looks solid, and validates properliy - however I'm having trouble with my request...

Trying: http://ip:3480/data_request?id=lu_action&DeviceNum=56&scene=1&action=ModifyUserData&inUserData={"scenes":[{"name":"HeatON","Device_Num_56":{"event_2_1":{"argument_1":"25"}}}]}

Yields: "error: no service/action"

Of course, I'm not stating any service ID's like my colleague did in his example:

http://ip:3480/data_request?id=lu_action&DeviceNum=0&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=ModifyUserData&inUserData=JSON_CODE_GOES_HERE

But then again - what serviceID am I supposed to give? I can't find any useful serviceID when looking in UI4, and I'm not sure where to look, in the user_data or lu_sdata output. user_data is 5500+ lines big, so trying to get an overview of all the objects seems a bit futile. I've grepped the file for serviceID's and some other keywords I thought relevant, but to no avail...  :'(

The documentation of the LuuP actions and the parameters they take are somewhat sparse..



EDIT:

Do you want to modify it dynamically, based on a certain event?

If not, why don't you edit the event and modify that value by hand?

I wanna be able to modify the events remotely.

Maybe I should give some background info :) I work for a small company specialized in home automation, developing software for the Vera among other things. The above mentioned scenario is a real world Use Case: One of our customers wants to control the thermostat in a set of houses. I wanna make a tool/script to automate that process, so the customer doesn't have to manually log into all the Vera boxes and configure the scenes by hand.

Title: Re: Scene manipulation with HTTP requests
Post by: Ap15e on April 15, 2011, 05:06:14 am
ModifyUserData is an UPnP action provided by the UPnP service urn:micasaverde-com:serviceId:HomeAutomationGateway1.

http://ip:3480/data_request?id=lu_action&DeviceNum=0&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=ModifyUserData&inUserData=... is the correct syntax.

Do not experiment with a live system. You have been warned.
Title: Re: Scene manipulation with HTTP requests
Post by: mikkelj on April 15, 2011, 05:09:28 am
ModifyUserData is an UPnP action provided by the UPnP service urn:micasaverde-com:serviceId:HomeAutomationGateway1.

http://ip:3480/data_request?id=lu_action&DeviceNum=0&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=ModifyUserData&inUserData=... is the correct syntax.

Do not experiment with a live system. You have been warned.

I was just writing a new post, when I saw you beat me to it :)

Removing DeviceNum=56 and adding serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1 gave a result:

http://ip:3480/data_request?id=lu_action&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&scene=1&action=ModifyUserData&inUserData={"scenes":[{"name":"HeatON","Device_Num_56":{"event_2_1":{"argument_1":"25"}}}]}
Code: [Select]
<?xml version="1.0"?>
<u:ModifyUserDataResponse xmlns:u="urn:micasaverde-com:serviceId:HomeAutomationGateway1">
<UserData>{}</UserData>
</u:ModifyUserDataResponse>

.. and now the scene is gone. Well, at least the command worked :D

Am I supposed to send back the whole shebang instead of just the parts of the scene I wanna change?
Title: Re: Scene manipulation with HTTP requests
Post by: mikkelj on April 15, 2011, 05:20:20 am
Thank you guys for your help.

Am I supposed to send back the whole shebang instead of just the parts of the scene I wanna change?

Yes I am :) Sending the whole scene back, with whatever changes made, works great!

http://ip:3480/data_request?id=lu_action&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&scene=1&action=ModifyUserData&inUserData={"scenes":[{"name":"HeatON","posx":0,"posy":0,"Device_Num_56":{"event_2_1":{"name":"Heaton","Enabled":"1","argument_1":"25","LastEval":0}},"id":1,"Timestamp":1299829983,"room":0}]}

And the JSON indented:

Code: [Select]
{
    "scenes": [
        {
            "name": "HeatON",
            "posx": 0,
            "posy": 0,
            "Device_Num_56": {
                "event_2_1": {
                    "name": "Heaton",
                    "Enabled": "1",
                    "argument_1": "25",
                    "LastEval": 0
                }
            },
            "id": 1,
            "Timestamp": 1299829983,
            "room": 0
        }
    ]
}

I get the same XML response though, but now my data is changed :)
Title: Re: Scene manipulation with HTTP requests
Post by: Ap15e on April 15, 2011, 05:28:41 am
Quote
Am I supposed to send back the whole shebang instead of just the parts of the scene I wanna change?

According to http://wiki.micasaverde.com/index.php/ModifyUserData the default action is 'merge' ...

BTW, it might be necessary to restart the LuaUPnP engine to activate the changes.
Title: Re: Scene manipulation with HTTP requests
Post by: mikkelj on April 15, 2011, 05:52:08 am
Quote
Am I supposed to send back the whole shebang instead of just the parts of the scene I wanna change?

According to http://wiki.micasaverde.com/index.php/ModifyUserData the default action is 'merge' ...

BTW, it might be necessary to restart the LuaUPnP engine to activate the changes.

My boss had a correspondance with a guy from Mios:

Quote
Regarding changing the scene in your backend, what you have to do is send the ModifyUserData command from the API, passing in the exact same JSON structure as the scene current has (as retrieved by the user_data request), but changing the 1 paramater you want.  ModifyUserData takes a JSON structure as an input, and it merges the new JSON object in with the existing one.  The only thing is that, as I recall, when you pass in a scene it replaces the existing scene object, so you have to send the entire scene in again.  We don't have a programmatical way of changing the events without resending the scene.  We will be adding this for our new UI5, but it's still a few months away.

So the semantics of "merging" seems to be a bit muddy :) But thanks a lot for your help - I couldn't have done it without you. Can I add an example to the wiki, so others won't have to ask the same question?
Title: Re: Scene manipulation with HTTP requests
Post by: Ap15e on April 15, 2011, 07:39:28 am
Yes, please add your findings to the wiki.
Title: Re: Scene manipulation with HTTP requests
Post by: mikkelj on April 15, 2011, 09:39:29 am
Yes, please add your findings to the wiki.

Unfortunately I'm not allowed to do so. I can't even write in the discussion's section it seems.
Title: Re: Scene manipulation with HTTP requests
Post by: guessed on April 15, 2011, 11:57:18 am
Yes, please add your findings to the wiki.

Unfortunately I'm not allowed to do so. I can't even write in the discussion's section it seems.

Why?  We have all requested Wiki accounts and gotten them.  If you're not being permitted to have an account, please let us know who's given you the rejection notice, and we'll get it sorted.

It's very important that this type of knowledge get captured for others to consume.
Title: Re: Scene manipulation with HTTP requests
Post by: Ap15e on April 15, 2011, 01:43:59 pm
Not being able to edit the wiki seems to be a common problem. I had the same problem some time ago.
Title: Re: Scene manipulation with HTTP requests
Post by: guessed on April 15, 2011, 02:16:48 pm
@Ap15e,
They went through a period where they were being spammed by link promoters in the Wiki, and I asked them to require validation prior to letting in an Editor, since there really weren't that many of us anyhow.

It's possible they missed a few in this validation process, so let's get them listed/verified so that those that want to edit can do so.

...without the link promoters wreaking havoc, of course...   8)
Title: Re: Scene manipulation with HTTP requests
Post by: mikkelj on April 16, 2011, 11:16:58 am
Why?  We have all requested Wiki accounts and gotten them.  If you're not being permitted to have an account, please let us know who's given you the rejection notice, and we'll get it sorted.

It's very important that this type of knowledge get captured for others to consume.

I haven't been rejected. I only just created a user for the wiki pages friday, so I'm sure I'll be granted permission with time :)
Title: Re: Scene manipulation with HTTP requests
Post by: mikkelj on May 13, 2011, 12:13:48 pm
Sorry for bumping an old topic..

Is it possible to be granted some privileges on the Wiki? I'd like to add some more explicit examples for the LuuP HTTP requests.. I'm writing all these examples down for documentation of my own - figured I might as well contribute to the overall documentation of the system?

Have a nice weekend 8)

/Mikkel
Title: Re: Scene manipulation with HTTP requests
Post by: guessed on May 13, 2011, 10:33:38 pm
I PM'd @mcvovidiu to see why they haven't approved your Wiki account yet.  Hopefully we'll hear back from them shortly.