Configure your Driver
If you want to give your driver some information then the best way to do it is by adding a property. A property is a way of letting you give some information to the driver. Most of the drivers that you use already have properties, you can see them when you click on the device in composer and look at the properties tab. Take a look at the properties of a light for an example:
Thinking about it you already used properties before when you change the debug level and turned it on.
Add a Property
To add your own property we will update the Hello World driver to version 3 and let you set its name.
In Driver Editor on the Codes/Commands tab you can see the Properties panel on the right hand side, right click in the top to Add a new property. Lets give it the name Name and set the default value to be world. You can see there are other settings such as the type of the value, min and max values etc that will be useful for the future, but for now lets leave the defaults as we will be accepting a string.
Using the property
Now that we can set a property, lets use it in our code.
All the properties are exposed to the code in the array Properties[], so to access it we use Properties[‘Name’]
Lets see that in our code
function LUA_ACTION.helloworld() Dbg:Debug("Hello " .. Properties["Name"]) end
Run the code
Thats it all done, so lets upload and see what we get.
- First you can see that there is a new property, so go ahead and change it. Make sure you have debug turned on, so we can see it.
- The click the action Say Hello
- Check the Lua Output
ExecuteCommand(LUA_ACTION) ACTION: helloworld Hello World
Now you know how to add properties to your drivers so you can make them more generic rather than having to hard code values.
Example Code and Drivers
All the driver files and code examples are located on github at https://github.com/c4drivers/tutorial
Can u tell me how to write a code to use a web api? per example, send a get method in a URL… like silvioney.com/getClients.php?dealer=123&code=123456 etc
LikeLike
See the example for the Twilio driver it shows how to do it, its here Twilio Example
LikeLike