Monthly Archives: April 2017

PHP Redis cache wrapper

Today I want to share a simple Redis Wrapper for PHP I’m using in one project. Nowadays I don’t like reinvent the wheel as years ago. I’m not going to create a new Redis Library for PHP there’re too many. I normally use Predis (it’s the “de facto” standard, indeed).

As I said before I don’t like to re-invent the wheel, but I like to create wrappers to adapt libraries to my common operations. For example with Redis I normally use a lot read key from cache (and create the key in the cache if it doesn’t exits). Because of that I’ve created this simple wrapper to enclose this common operation for me.

The idea is to use something like this:

use G\Redis\Cache;
use Predis\Client;

$cache = new Cache(new Client(json_decode(file_get_contents(__DIR__ . '/conf.json'), true)));

$value = $cache->get("aaa.aaa.aaa", function () {
return [
'a' => 1,
];
});

$cache->delete("aaa.aaa.aaa");

It’s very simple. I create a new instance (injecting a Predis instance), and when I want to get the key from Redis I also pass a callback with all that I need if to create the key in the cache if it doesn’t exits.

The wrapper’s code is very simple also

namespace G\Redis;
use Predis\ClientInterface;
class Cache
{
private $redis;

public function __construct(ClientInterface $redis)
{
$this->redis = $redis;
}

public function get($key, callable $callback = null, $ttl = null)
{
if ($this->redis->exists($key)) {
return json_decode($this->redis->get($key), true);
}
if (!is_callable($callback)) {
throw new Exception("Key value '{$key}' not in cache and not available callback to populate cache");
}
$out = call_user_func($callback);
if (!is_null($ttl)) {
$this->redis->set($key, json_encode($out), 'EX', $ttl);
} else {
$this->redis->set($key, json_encode($out));
}
return $out;
}

public function delete($key)
{
$keys = $this->redis->keys($key);
if (count($keys) > 0) {
$this->redis->del($keys);
}
return $keys;
}
}

The code is available in my github and also in Packagist

Advertisements

Control humidity with a Raspberry Pi and IoT devices

I’ve got a Wemo switch and a BeeWi temperature/humidity sensor. I’ve use them in previous projects. Today I want a control humidity level in a room. The idea is switch on/off a dehumidifier (plugged to Wemo switch) depending on the humidity (from BeeWi sensor). Let’s start.

I’ve got one script (node) that reads humidity from the sensor (via BTLE)

#!/usr/bin/env node
noble = require('noble');

var status = false;
var address = process.argv[2];

if (!address) {
    console.log('Usage "./reader.py <sensor mac address>"');
    process.exit();
}

function hexToInt(hex) {
    var num, maxVal;
    if (hex.length % 2 !== 0) {
        hex = "0" + hex;
    }
    num = parseInt(hex, 16);
    maxVal = Math.pow(2, hex.length / 2 * 8);
    if (num > maxVal / 2 - 1) {
        num = num - maxVal;
    }

    return num;
}

noble.on('stateChange', function(state) {
    status = (state === 'poweredOn');
});

noble.on('discover', function(peripheral) {
    if (peripheral.address == address) {
        var data = peripheral.advertisement.manufacturerData.toString('hex');
        console.log(Math.min(100,parseInt(data.substr(14, 2),16)));
        noble.stopScanning();
        process.exit();
    }
});

noble.on('scanStop', function() {
    noble.stopScanning();
});

setTimeout(function() {
    noble.stopScanning();
    noble.startScanning();
}, 3000);

Now I’ve got another script to control the switch. A Python script using ouimeaux library

#!/usr/bin/env python
from ouimeaux.environment import Environment
from subprocess import check_output
import sys
import os

threshold = 3

def action(switch):
    humidity = int(check_output(["%s/reader.js" % os.path.dirname(sys.argv[0]), sensorMac]))
    if "Switch1" == switch.name:
        botton = expected - threshold
        isOn = False if switch.get_state() == 0 else True
        log = ""

        if isOn and humidity < botton:
            switch.basicevent.SetBinaryState(BinaryState=0)
            log = "humidity < %s Switch to OFF" % botton
        elif not isOn and humidity > expected:
            switch.basicevent.SetBinaryState(BinaryState=1)
            log = "humidity > %s Switch to ON" % expected

        print "Humidity: %s Switch is OK (%s) %s" % (humidity, 'On' if isOn else 'Off', log)

if __name__ == '__main__':
    try:
        sensorMac = sys.argv[1]
        mySwitch = sys.argv[2]
        expected = int(sys.argv[3])
    except:
        print 'Usage "./dehumidifier.py <sensorMac> <switch name> <expected humidity>"'
        sys.exit()

    env = Environment(action)
    env.start()
    env.discover(seconds=3)

And that’s all. Now I only need to configure my Raspberry Pi’s crontab and run the script each minute

*/1 * * * *     /mnt/media/projects/hum/dehumidifier.py ff:ff:ff:ff:ff:ff Switch1 50

Project is available in my github account.

Nowadays I’m involved with Arduino and iot, so I wand to do something similar with cheaper Arduino stuff.