HTTP post with stored data

Hi Bill

Probably there were some topics created about this, but sorry i can’t get my head around this.

Lets say I have a JSON data that is coming from a http request and it has two pair of values.
{height: 185, age:20}, these are attributes of a device.

So far so good, i can write them out into a Label or Button text by device binding…

However, if i want to have the possibililty of changing these values and sending them back with new values in a http POST, i am getting stuck.

I think its not possible in HR is it? (also in another topic i commented to another user that its not possible so i want to be sure)

I mean, first we need a storage where the changed values will be stored.
(we dont want to send HTTP POSTs on all the button presses while user is changing values)

When user has finished the setting up values, he can press a SAVE button, and upon that press all the changed (and stored) values of HEIGHT and AGE should be sent by a HTTP post.

Is there any possible way to do it in HR?

Any workaround?

Thanks

Yes, you can do that. Study some of the examples in the Plugins section. The Fibaro Global Variables plugin is fairly straight forward & will be a good one for you to reference, especially since you already have a Fibaro controller. It reads global variables in onPoll & it writes to them in onChangeRequest.

Bill, thanks for the suggestion…
This approach i am aware of, but i can’t get, how can this help to overcome the problem.
I know, we can send POST requests from plugins, and send some complex objects with values inside its body part.
The question is, how can we populate that body with values.
Like i have 80 variables that should be sent in a POST request back to Fibaro…
Now, when the user opens his dashboard in HR he can see those values on some buttonfaces, or labels or whatever, becuase I am reading those values by GET request and populate the controls on HR dashboard accordinlgy, so the user can see these values.

Then comes the interaction, when user changes these values on HR dahsboard.

How to do that? What should be done here? How can user change these values, and then when he finished, how can he send back that POST request? What values will be sent in its body?
Where will be that POST body get its values?

What controls can be used for changing values?

Slider is not really an option. It can be bound to some variable, but its not really usable for sending values in HTTP, becuase user can’t see the actual valu of the slider, so he doesn’t know what will be sent when its releases it. And anyway, we dont want to send any value separtely.

We ned to send a JSON object back with some updated values that come from HR.

I would try to go for some buttons, maybe create one for increasing and other one for decreasing.
But the problem is similar. When he presses the button, the value that is represented by this button should be changed in Fibaro, only when he presses another button, kinda “Save” button. Otherwise every single press of a button will perform a HTTP request, which is incredible slow solution if we consider that we can have like dozens of atrributes that need to be changed upon some button presses.

I am looking for some very simple solution that i used to use in other web based programs.
User makes the changes. Those updated values should be stored somewhere temporarily, and when
he presses a Save button, the body of a POST request should be populated by them, and be sent back to Fibaro.

I have checked i think every single plugin example that is available here, but i couldn’t find anything that can handle this simple situatioin.

Almost every plugin is sending data back upon a onChangedRequest what is bind to some button or other control, that is directly bound to some value on the server.

to be more simple
Here is a snippet from your FibaroGlobalVariable code

` http.put(baseURL + “/globalVariables/” + device.Id, { name: device.Id, value: value, readOnly: false, isEnum: false }, { auth: credentials });

If i want to change the values of value variable and name variable and only after i have done the changes, send back that put request…how to do it?
`

If I understand you correctly, you wish to send 1 HTTP PUT to update 80 devices. What I would do here is in “onChangeRequest” for most of these, don’t send a PUT. Just forward the new value into the device attribute. Then use a new device to actually send the PUT. When building the JSON for that PUT you can read the attribute data from all of your plugin’s 80 other device objects. The important takeaway here is you can handle every device object differently. You can also read the current states of all your plugin’s device objects & reference them in the JSON PUT data you build.

1 Like

Ohh, i think i am getting close to understand it…Just gonna show it in 1 attribute example, and please fix it where it should be fixed.I am stuck in the last step

plugin.Name = "FibaroZoneManager";
plugin.OnChangeRequest = onChangeRequest;
plugin.OnConnect = onConnect;
plugin.OnDisconnect = onDisconnect;
plugin.OnPoll = onPoll;
plugin.OnSynchronizeDevices = onSynchronizeDevices;
plugin.PollingInterval = 5000;
plugin.DefaultSettings = { "Host": "192.168.100.218", "Port": "80", "Username": "admin", "Password": "admin","ZoneID":"39" };

var http = new HTTPClient();
var baseURL = null;
var credentials = {}



function onChangeRequest(device, attribute, value) {
	console.log("onChangeRequest");
	 var deviceID =  plugin.Settings["ZoneID" ];
     var zoneDevice = plugin.Devices[deviceID];
		
	if (attribute == "action") 
	{
	
	switch (value) {
        
        //reading the values from Fibaro..for sake of the example its only the heatingSetpoint of the zone
        case "GetZone":
           
            var r = http.get(baseURL + "/panels/climate/" +deviceID, { auth: credentials });
            var responsed_zone = r.data;
            
            
            zoneDevice.id = responsed_zone.id;
            zoneDevice.heatingSetpoint = responsed_zone.heatingSetpoint;

        case "SetZone":
            //as you recommended, here I only update the device heatingSetpoint, without sending the PUT request
            
            zoneDevice.heatingSetpoint = device.heatingSetpoint;
        case "SendPUTrequest":

                // HERE I AM STUCK..WHAT NEW DEVICE CAN BE USED HERE, I DONT GET IT..CAN YOU PLEASE PROVIDE SOME EXAMPLE?
                //WHERE WOULD THIS NEW DEVICE COME FROM, AND HOW CAN I INTERACT WITH IT HERE INSIDE THIS PLUGIN'S "SendPUTrequest" FUNCTION?
		
            break;
        default:
            break;
    }}
		
	
	}
	
	 
function onConnect() {
    baseURL = "http://" + plugin.Settings["Host"] + ":" + plugin.Settings["Port"] + "/api";
    credentials = {
        username: plugin.Settings["Username"],
        password: plugin.Settings["Password"]
    }
}

function onDisconnect() {
}

function onPoll() {
    
}



function onSynchronizeDevices() {
    
    var device = new Device();
    device.Id = plugin.Settings["ZoneID"];
    device.DisplayName = "Zone";
    device.DeviceType = "Zone";
    device.TileTemplate = "ZoneTile.xaml";
    device.Capabilities = ["GetZone","SetZone","SendPUTrequest"];
    device.Attributes = 
        [
            "id",
            "heatingSetpoint",
            
        ];
    
    plugin.Devices[device.Id] = device; 
    
    

}


You would create a device object just like you did for all of the others. Your plugin can have more than 1 device object. It can have hundreds. There’s no limit. They can all serve a different purpose. You can add special if-else cases & do anything else you want to handle them differently.

if(device.Id == "PUT_DEVICE")
{
    // Send PUT request.
}