User Tools

Site Tools


oddstr13:node-red

UKHASnet logtail -> MQTT

TODO: Split the flow out to a separate git repo, and host code on GitHub instead of here

NOTE: This is not a strict parser. Garbage in, garbage out.

Mapping config

Topic: config

Inject once after: 0.1s

{
    "OSs2": {
        "_comment": "Zone/Building/Area/(air,soil,water)/type(temp,hum)/id",
        "_example": "Solvang/+/+/air/temp/#",
        "temperature_1_1": "Solvang/_/Drivhus/air/temp/1"
    }
}

UKHASnet parse

const type_map = {
    V: 'voltage',
    I: 'current',
    T: 'temperature',
    H: 'humidity',
    P: 'pressure',
    X: 'custom',
    S: 'sun',
    R: 'rssi',
    C: 'count',
    W: 'windspeed',
    L: 'location',
    Z: 'zombie',
};
 
const packet_re = /^([0-9])([a-z])([^:\[]+)?(?::([^\[]*))?\[([^\]]*)\]$/;
const values_re = /([A-Z])([^A-Z]*)/g;
const value_re = /([0-9.+-]+)/g;
 
function parse(packet) {
    let pp = packet.match(packet_re);
    let result = {};
    let _track = {};
 
    result.hops = parseInt(pp[1]);
    result.sequence = pp[2];
    result.comment = pp[4];
    result.path = pp[5].split(',');
    result.values = {};
 
    if (pp[3]) {
        let values;
				while ((values = values_re.exec(pp[3])) !== null) {
            let type = values[1];
            let value_list = values[2].match(value_re);
 
            let name = type_map[type] || type;
 
            let num = (_track[type] || 0) + 1;
            _track[type] = num;
 
            for (let j in value_list) {
                let num2 = parseInt(j) + 1;
                let id = name + '_' + num + '_' + num2;
                result.values[id] = parseFloat(value_list[j]);
            }
 
        }
    }
 
    return result;
}
 
 
let gw = msg.payload.gateway;
let rssi = msg.payload.rssi;
let raw_packet = msg.payload.packet;
 
let packet = parse(raw_packet);
packet.gw = gw;
packet.rssi = rssi;
 
return [{payload:packet}];

Filter and map MQTT

if (msg.topic == 'config') {
    flow.set('ukhasnet2mqtt', msg.payload);
} else {
    let map = flow.get('ukhasnet2mqtt');
    let packet = msg.payload;
    let sender = packet.path[0];
 
    let output = [];
 
    if (map[sender]) {
        for (let k in map[sender]) {
            if (typeof(packet.values[k]) !== 'undefined') {
                let mqtt = {
                    topic: map[sender][k],
                    payload: packet.values[k],
                }
                output.push(mqtt);
            }
        }
    }
 
    return [output];
}

Change node

Maps some of the logtail key names to human friendly versions.

oddstr13/node-red.txt · Last modified: 2020/05/14 23:20 by oddstr13