PlugWeb: Implementing the daemon using PhpTaskDaemon

PhpTask Daemon The PlugWeb daemon will be implemented using the PhpTaskDaemon library. This library I wrote provides an easy solution for defining, running and monitoring tasks. Tasks are executed by a manager, which defines the way when tasks are loaded en executed. PhpTaskDaemon provides several managers for running tasks like cron jobs, gearman workers and forked processes. Find more information about PhpTaskDaemon on my blog.

PlugWeb Tasks The plugweb daemon consists of three tasks. The first task switches a device on or off. The second tasks needs to retreive historic usage information from the devices periodically. The last tasks polls a device and saves it state to the database. This information is useful for training a neural network (future project) for adjusting or advising the user about the current planning. Next the tasks will be examined in more detail.

Task: Switch on/off devices The first task is used for switching on/off devices. The task can be triggered by the user through the website, but also from another task. When a user switches a device, the action should be executed nearly real-time. A cron task is unsuitable, because it only runs every minute. Therefore this task is implemented as a gearman worker and can be triggered using the gearman client interface.

{code type=php} public function executeTask() { $input = $this->getProcessInput(); $plugwiseDevice = new Plugwise_Driver_Device($input['mac']); switch($action) { case "on": $result = $plugwiseDevice->powerSwitchOn(); break; case "off": $result = $plugwiseDevice->powerSwitchOff(); break; default: break; } } {/code}

Task: Retrieving historical usage information The second tasks retrieves historical usage information from the plugwise devices. This needs to be periodically for the chart on the website to display recent information. The plugwise circle devices buffer the power usage information hourly and can be requested based on a log address. By storing the latest address in the database only the missing data can be retrieved.

Task: Poll current device usage information The plugwise devices also support retrieving the current usage and state. The user can trigger this task through the website and therefore needs to be implemented as a gearman worker.

Task: Trigger poll current device usage information The task above only executes when the gearman job server contains a job of this kind. Polling the devices should be executed regularly to get an actual image of the power usage. This information can also be used to adjust and/or advice the user about the planning. This task is executed by a cron manager and only invokes a method on a gearman client object to add a job to the gearman job server queue.

A more useful example: PlugWeb PlugWeb is another open source project I am working on. PlugWeb is a web application for controlling and polling Plugwise devices. The web application provides easy access for controlling and monitoring the devices. It will consist of a front end (zend framework) and a backend (phptaskdaemon). In the front end users can see a list of devices (state, current usage), view historic data (with graphs) of one or more devices, switch the current state of a device (to on/off) and request an accurate state of a device (data of last 8 secs).

Task 1: Gather historical power usage data The first background task of the PlugWeb project periodically gathers historical data about the power usage of a device. A plugwise device saves power usage information to its internal storage each hour. This information can be requested using log addresses. This task should run each hour (or less) and retrieves power usage information for missing log addresses. Normally the linux cron is good way to execute this task.

Task 2: Switch devices on/off The second task will be used for switching on/off devices. The task can be triggered by the background script to switch on/off devices according to planning or by the user through the website. The task should be executed as real time as possible. The linux cron is unsuitable for this kind of task, because it only runs once a minute at maximum. This can be solved by creating a custom daemon or a Gearman worker to execute the task on demand.

Task 3: Poll current device status The third task is used for gathering current device information. This task is triggered by the user through the website and/or periodically by a background script to check when devices are on/off. This information can be used to adjust a planning for switching on/off devices automatically. Triggering the task through the website can be solved with a gearman worker. Periodically gathering device status can be done with the linux cron.

The tasks listed above are the basis tasks of the plugweb daemon. In a later stadium tasks can be added for adjusting the planning for switching devices on/off automatically, email reports periodically and/or post power usage of last month on twitter. To start the program I need to start two gearman workers and install two cronjobs for running the tasks listed above.

Rendered Content in 0.003 seconds

Rendered UI Widgets in 0.001 seconds

Want to see the cheezy internals?
twig layout template | twig page template | json data