Lua Configuration

I have now officially converted all of the configurations in OpenDAX to Lua scripts. It is quite simple to do when all that the configuration needs is "token = value" pairs. The token simply becomes a global variable in Lua and the modules just runs the script and reads the globals that it expects. The opendax.conf and modbus.conf files are a tad more complex because they have some structured information that needed to be added. Previously I had used a configuration that looked something like this...

<module>
name test
startup 0
path /Users/phil/opendax/modules/test/daxtest
args -C
args opendax.conf
restart yes
openpipes no # Have dax connect to stdin, stdout and stderr
</module>

There were a couple of things about this that aggravated me. The most obvious was that I had to retype this for each module even if nothing other than the name and path were to change. The second is that the path on every module in the configuration file has the "/Users/phil/opendax/modules/......" at the beginning of it. I was thinking about programming in some kind of default module configuration (I had something like this on my modbus module) and / or adding support for substitutions. The more I thought about it the more I realized that Lua makes all this simple. Now the previous configuration becomes...

mod_root = "/Users/phil/opendax/modules/"

m = {}
m.name = "test"
m.startup = 0
m.path = mod_root.."test/daxtest"
m.args = "-C opendax.conf"
m.restart = true
m.openpipes = false
add_module(m)

Then if I need another module that has only slight changes...

m.name = "modbus"
m.path = mod_root.."modbus/modbus"
add_module(m)

Substitutions, loops, decisions, expressions and probably some other stuff that would have taken me months to add to some kind of fancy configuration language that is now built in and can be done with < 100 lines of code. I had a project offshore once that had 30 flow computers tied to one Modbus network. Each one had about 10 pieces of data that needed to be read from two different tables within the flow computer. I had to copy/paste, change the parameters, repeat, 30 times. I still wound up with data overlapping and causing me problems. Then they added 4 more. If I'd had a Lua configuration file I could have written that configuration as a script it would have been painless. I could have configured the system in a for loop, and been done. If the make a change I don't have to change it 34 times, only once.

Also notice that if I want to disable a single module for some reason but not delete it, all I have to do is comment the add_module() line instead of the whole <module>...</module> section.

Another benefit that I've noticed (especially in the modbus module) is that reading the Lua configuration is asynchronous. Before I read the file top to bottom and then saved the configuration tokens as I found them. This made error detection more difficult and made it really hard to handle conflicts. For instance, if the modbus port that is being configured is a Modbus/TCP port (which uses TCP/IP networking instead of a serial port) it would be silly to worry about what baudrate was configured. On the other hand the the Modbus/RTU port doesn't need an IP address. I could have read the whole file in and then gone back and did the checks but it's natural in Lua and would have been a pain to code by hand. The whole thing was already a pain and it still didn't have all the features that I wanted.

Also the code to parse the Lua scripts is much simpler. It may take up a few more lines of source file to do it but there is much more functionality contained in those lines. It's much easier to add parameter to configure too.

My experiences with Lua have been very positive thus far. I think it's perfect for OpenDAX and it's well worth the added library dependency. Now if I just had a nice way for autoconf to figure out where the libraries and header files are.