Aller au contenu
jjacques68

API - Erreur - mise à jour propriété de type table

Recommended Posts

Hello tout le monde !

 

Je bloque sur un problème que je ne comprends pas

 

Je gère le panel Sprinkler à travers un QA... ça marche très bien sauf pour un cas : 

Où je souhaite supprimer tous les jours programmés...

 

Voici un bout de code qui fonctionne : 

J'ai une variable de type table où sont insérées plusieurs jours

Ensuite je vide cette table

Pour enfin la transférer dans l'API

tout est ok.

local ListeDay = {}

table.insert(ListeDay, "monday")
table.insert(ListeDay, "thusday")
...
for j = 1, #ListeDay do table.remove(ListeDay) end

res = api.put("/panels/sprinklers/"..MonID, {days=ListeDay})

 

Voici un bout de code qui ne fonctionne pas

J'ai toujours ma variable de type table (mais ce coup-ci, elle reste vide)

Et je mets à jour l'API...

local ListeDay = {}

res = api.put("/panels/sprinklers/"..MonID, {days=ListeDay})

et voici l'erreur : 

{"type":"ERROR","reason":"http:\/\/{$gateway-ip}\/json-schema\/panels\/sprinklers\/PUT_sprinklers.json. Invalid schema: #\/properties\/days. Invalid keyword: type. Invalid document: #\/days","message":""}

Je comprends bien qu'il aime pas ma variable "ListeDay", mais pourquoi ??

où est le problème entre le premier exemple et le second ????

 

merci pour vos lumières !!

Partager ce message


Lien à poster
Partager sur d’autres sites
Il y a 14 heures, jjacques68 a dit :

Hello everyone !

 

I'm stuck on a problem I don't understand

 

I manage the Sprinkler panel through a QA ... it works very well except for one case: 

Where I want to delete all scheduled days ...

 

Here's a piece of code that works: 

I have a table type variable where several days are inserted

Then I empty this table

To finally transfer it to the API

everything is OK.


 
  

   
 

 

Here's a piece of code that doesn't work

I still have my table type variable (but this time it remains empty)

And I update the API ...


 
 

and here is the error: 


I understand he doesn't like my "ListDay" variable, but why ??

where is the problem between the first example and the second ????

 

Thank you for your lights !!

I suspect a "bug"

json.encode({42})  -> "[42]", an array with one item.

json.encode({}) -> "{}",  an empty key-value json table

api.put /panels/sprinklers/ID expect days:<array>,  an empty json array "[]"

Somehow, a Lua array where you remove all items is still seen as an array by the "C" code that encode the Lua table. However, an empty table is default encoded as a key-value.

The API type checks the argument against pre-defined schemas, and the 'days' value is found to be of the wrong type (not an array).

Partager ce message


Lien à poster
Partager sur d’autres sites

okay, 

so before updating the API, I must to check if the array is empty,

if yes, I add an element and remove it to be sure that the array is good type...

strange... :) but I understand.

if it's a bug, it will be maybe fixed in the future...

Partager ce message


Lien à poster
Partager sur d’autres sites

En effet, il y a un gros soucis sur le formatage des tables JSON !!!!

 

exemple simple à reproduire

 

dans un QA, faire cette fonction : 

vous aurez compris que celle-ci va nous afficher le type de la variable transmise...

function QuickApp:Display_Type(value)
	print(type(value))
end

 

On peut la tester depuis ce même QA ou un autre avec

self:Display_Type( {1, 2, 3} )

 

le résultat sera donc forcément un type "table" ! :yes:

 

mais depuis une scène maintenant : 

 

en appelant la fonction comme ceci :

fibaro.call(id_QA, "Display_Type", {1, 2, 3})

le résultat affichée est de type "string" !!!!!!! :23:

 

il faut faire :

fibaro.call(id_QA, "Display_Type", json.encode({1, 2, 3}))

pour avoir le type "table" !!!

 

J'ai encore loupé un épisode ????

Modifié par jjacques68

Partager ce message


Lien à poster
Partager sur d’autres sites
Il y a 14 heures, jjacques68 a dit :

but from a scene now: 

 

by calling the function like this :


the displayed result is of type "string" !!!!!!! : 23:

...but from another QA you have a table...

It's the marshalling of arguments that is broken - try this

function QuickApp:test(arg)
    self:debug("Argument ",tostring(arg)," is a ",type(arg))
end

function QuickApp:onInit()
    fibaro.call(self.id,"test","0042")    
    fibaro.call(self.id,"test","true")   
    fibaro.call(self.id,"test",'{"a":9}')
end

I complained in February about it...

  • Like 2

Partager ce message


Lien à poster
Partager sur d’autres sites

I tried and in deed it's very strange...

 

It transforms the values...

I understand more why I had some problems with the boolean too... they are transformed in 0/1...

I understand the "marshalling" too...

 

You reported this in February !!! - there was however some update since... :angry:

I hope they will make something...

 

thanks for explains !!

 

Partager ce message


Lien à poster
Partager sur d’autres sites

×