Gilles0181 has asked for the wisdom of the Perl Monks concerning the following question:

Hi! I am trying to program an aquarium computer using Perl. It reads sensors from the one-wire network. The network is up and running and i am allready reading the sensors, but i need some help.

Let's assume that i have 2 sensors:
temp1 and temp2

The main application waits for a sighup/sigterm/siginit to abort the application. Waits for the threads to finish and then dies. This allready works.

"temp1" and "temp2" are fired as different threads; Each subroutine is an infinite loop (as long as there is no signal) and does nothing more then read a file on the local file system, prints it and sleeps 5 seconds and then does that same trick again. This also works.

Normally the temperature is set to 25. If it falls below (e.g. 24.8), the heater is activated, it if rises above 25.2 the cooler is activated.

Now; i want to be able to control some values (Nominal, Hysteris etc) from a website. And i would like those changes to be instant.

For example; lets say i set the nominal value to 20C, and the temp is 24.7 i want the cooler to be instantly activated. Since the thread is allready running (and it may be in a "sleep 5" state) i don't know how i would do that.

First i though of sockets (e.g. the main application waits for an incomming connection/command and if it receives that, the threads get recycled) but i could not get that to work.

I hope you still follow me. How would i do such a thing?

-----

Next step is lightning (basically thesame scenario): The application is running the lightning program. It is currently 08:00, and the lights are at 0%. The next settings is 08:15 and the lights should be at 50% intensity. This can be done fairly easily.

But what if the user changes the lightning scenario at 08:10 (from the website), meaning that the lightning thread has to reinitialize since the curve shifted?

How would you accomplish such a task?
  • Comment on Change in config from outside ==> immediate reflection?

Replies are listed 'Best First'.
Re: Change in config from outside ==> immediate reflection?
by moritz (Cardinal) on May 07, 2008 at 13:54 UTC
    There are many different approaches, with varying complexity.

    One is to just change the sleep interval to 0.1 or 0.05 seconds (use Time::HiRes for that), and only perform light weight checks in your "event loop" (for example check timestamps, and if a timestamp changes you can read a file again).

    A slight variation is to install handlers for SIGUSER1 or other user specific signals, and sens a signal from to the long running application whenever you change something. See perlipc for more inspiration.

    Another approach is to use an event framework like POE that does all the hard stuff for you: it can monitor files, set timeouts and all that stuff.