Ready for the future?

My Domotica systemAlthough my Domotica system is very stable, fast and everything, I’ve become somewhat unhappy with how it has evolved through the years.

It all started in 2006 with monitoring gas usage with a CNY70 and 1-Wire counter (DS2423).

Next came RFXCOM, X10, PLCBUS, IR, and so on. Right now, my system has about 30 interfaces to the outside world, all coded in Delphi; Borland Delphi 2005 to be precise. And all that code results in a monolithic executable of 2.9 MB, uses 1500 KB of memory at run-time with an average of <1% CPU usage on a Intel i3 in a Hyper-V VM.

Nothing wrong with these numbers, right? I could go on for years like this with no problem at all. However, I started having second thoughts about that statement a year ago.

  • With the ever growing number of stuff built into a single executable, the system becomes more and more vulnerable to fatal runtime errors that could make the whole thing crash.
  • I am bound to using Delphi as single programming language for the system. Yes, I’m also using other programming languages like VB.Net, ASP.Net, Arduino, (Pronto-/Java-)script, C# but those are only used for User Interfacing and hardware.
  • Delphi restricts me to Windows as platform for my Domotica system. For example, my NAS could be a good candidate to host some pieces of my Domotica system; I can’t do that right now.
  • It’s hard to make use of other non-Delphi code that can help me to ‘enrich’ my system.

6 years ago I could never have imagined that my Domotica system would grow as fast as it did, can do what it does right now and how important it would become in our current daily lives. And the time will come where the current state of my system becomes a burden with respect to adding new functionality.

Conclusion: I have to become more flexible – break the system up in small pieces and find a way to get rid of the programming language and platform dependency. Cause if I don’t, I foresee problems – either by not being able to keep up with the pace in software development, getting stuck on old operating systems or facing huge investments in writing my own Delphi code for something that’s freely available in some other language.

The big question is: how to do that? I don’t like the idea of porting more than 150000 lines of Delphi code to another programming language, cause that would get me in the same situation as it is now. Nor do I want to stop using the code I wrote during the last 6 years; it works fine, so that would be a complete waste! No, I want to keep using the code I have as much as possible, I should be able to gradually transform my system piece by piece and not be forced to some ‘Big Bang’.

So the first thing I have to do is break the current system up in the building blocks it’s being made of; right from the start I have been working in terms of ‘hardware interfaces’, ‘devices’, ‘sensors’, ‘events’, ‘actors’ in an object-oriented way, so breaking things up into smaller pieces should not be that hard; transforming a interface unit with some classes into an executable should be doable but the biggest problem is that a hardware interface object has to be able to get data from itself to the device object and vice versa: a device object (let’s say an IR controlled TV) also has to be able to send to the IR interface; some internal queues take care of that now, but I can’t use those anymore; those building blocks I mentioned need some way to get data across from one process/executable to another. And I think I’ve got the right solution for that – ZeroMQ (0MQ).

ZeroMQ was first brought to my attention during a meeting last year with some friends who are just as addicted to Domotica as I am and it has been on my mind ever since; but I never took the time to have a really good look at it; since 3 weeks I’ve started reading about it, looking at code samples and got more and more excited and realized that this is just what I need. And I’m not the only one that feels that way, as can be seen here and here.

Last Friday I had a day off and spent most part of the day to get 0MQ up and running in Delphi. I found some Delphi bindings for 0MQ (some more finished than others), tried to let 2 Delphi programs talk to each other and it worked. Another test with .Net <–> Delphi was succesful too; promising.

Now, but not after I’ve tested some basic needs very thoroughly, I can start dissecting my monolithic system into smaller pieces and gradually create an even more stable, flexible and adaptive Domotica system. One of the upcoming projects will be integration of a Smart Meter for gas and electricity; I think I’ll do that one the 0MQ way – exciting!