How to do better programming?

Moderator: leecollings

jannl
Posts: 673
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: How to do better programming?

Post by jannl »

In plain lua I also use some soet of masterscript.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: How to do better programming?

Post by dannybloe »

When I started dzVents two years ago I wanted exactly that: have the advantage of one commandArray-manipulator (the main script) but still have the possibility to work modular as I (and many others) believe that's a better way of writing code for obvious reasons. So I came up with a very simple approach. Each event script (a self-contained piece of code dealing with a particular event, basically a Lua module returning a table) has a generic set of rules that determines if the script has to be executed or not. So when Domoticz fires an event, it calls dzVents.lua and that script determines which of your rules defined in your script modules matches with the event. That's it. That's basically all the preprocessing that happens when an event is fired. So, with a non-dzVents approach and working with one giant file, you have the same preprocessing as you still have to determine which piece of code you have to execute given the current event. The only difference I guess is that dzVents has defined a generic declarative way of creating rules (the on-section) that read more like sentences. But it does allow you to create very small pieces of scripts. Modular.

But then, whenever it is decided that your script is executed based on the trigger rules, the true power of dzVents kicks in. It encapsulates almost your entire Domoticz state in a clear and logic object structure (again: still plain old Lua, no magic there) and it does that by providing your script with a domoticz object (parameter) combined with the actual object that caused the event to happen in the first place. The domoticz object has lots of handy methods and access to all your collections (devices, scenes, groups etc). All objects in these collections (artifacts in your Domoticz system) have a simple well documented interface to interact with them: attributes and methods that are relevant for the specific kind of object. dzVents has knowledge about most of the different hardware types out there and extends each device with the appropriate extensions based on so called device adapaters. Highly modular and extensible.

But all this is done in a lazy way. It only creates the objects when you need it. If you don't touch a certain temperature device in your code, that device isn't created by dzVents. No processing is done until you wake it up. All this to make sure it doesn't eat unnecessary cpu cycles. (That's why getting a specific device is a function: myTempDevice = domoticz.devices('myTemperatureDevice')).

So, as far as performance it is all optimized but surely it is slower than without dzVents and just one Lua script with a couple of lines of code. But not much. I have a couple of PIR scripts running on my Pi3 system that respond within less than 200ms from the moment the event occurs. And to me that's all that matters. I always said that if it passes the PIR-test then it's good enough. (More often than not my zwave network is slower when it tries to switch on the light but that's another story).

So to summarize, dzVents is a light-weight framework but, language-wise, just Lua without any magic. It takes an approach that is enough to be both efficient (only execute what has to be executed when you stick to a few simple rules) and powerful as it takes away the need a lot of unnecessary coding.

And to be honest (and I'll try to be modest ;) ), I don't think there are many other domotica systems out there that have such a rich and powerful event scripting system as Domoticz has in combi with dzVents and Lua. And I haven't even talked about what dzVents 2.4 will have (ok.. a hint (among others): easy to use event-based asynchronous http requests and post-processing (in your Lua scripts) with just a few lines of code.)
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
jake
Posts: 744
Joined: Saturday 30 May 2015 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: How to do better programming?

Post by jake »

I'm a happy user of dzvents. I leave most of my old scripts as standard lua, but new ones in dzvents. It makes life easier, especially with time and variables and... And...etc
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: How to do better programming?

Post by BakSeeDaa »

IMHO dzVents is the best thing ever happened to Domoticz.

I have written and shared various dzVents scripts in different sizes varying from a few lines of code up to about 1000 lines.

I find dzvents to be a very well documented framework adding simplicity into otherwise tedious tasks. It also adds missing features and it's well tested by many users.

There is no reason to use dzVents if you are a very skilled programmer and prefer to write your (probably also well documented) own code. That's perfectly fine and it doesn't have to be a case of the classic management pathology NIH. There's nothing wrong in reinventing wheels if you like low level coding. With that being said, I must emphasize that it's my personal opinion.

Consider the following example dzVents code.

When someone rings the door bell at night time on monday to friday and the front light hasn't been flashed in the last 20 seconds, flash the "Front Light" device 2 times (but don't trigger any follow up events by other scripts). Note that after 20 seconds, the front light can be flashed again (even though the door bell might have been pushed several times during the initial 20 seconds period.)

Code: Select all

return {
	on = {
		devices = {
			['Door Bell'] = {'at nighttime on mon,tue,wed,thu,fri '}
		},
	},
	data = { previousPush = { history = true, maxItems = 1 } },
	execute = function(domoticz, doorBell)
		local previous = domoticz.data.previousPush.getLatest()
		if (doorBell.active and (not previous or (previous and previous.time.secondsAgo >= 20))) then
			domoticz.devices('Front Light').toggleSwitch().forSec(1).repeatAfterSec(2, 1).silent()
			domoticz.data.previousPush.add('ring ring')
		end
	end
}
I'm too lazy to translate the above dzVents script into old style Domoticz LUA but I would love if someone would would care to do it so that we can compare. :D

dzVents rocks and is the way to go for "better programming".
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest