<commit_before>/**
 *
 *      ioBroker OWFS Adapter
 *
 *      Copyright (c) 2015-2016 bluefox<<EMAIL>>
 *
 *      MIT License
 *
 */
/* jshint -W097 */// jshint strict:false
/*jslint node: true */
'use strict';
var utils   = require(__dirname + '/lib/utils'); // Get common adapter utils
var adapter = utils.adapter('owfs');
var OWJS    = null;
var fs      = null;

var timers  = {};
var client  = null;
var objects = {};
var path1wire;
var OW_DIRALL = 7; // list 1-wire bus, in one packet string // workaround for owserver

adapter.on('message', function (obj) {
    if (obj) processMessage(obj);
    processMessages();
});

adapter.on('ready', function () {
    main();
});

adapter.on('unload', function () {
    for (var t in timers) {
        clearInterval(timers[t].timer);
        timers[t].timer = null;
    }
});

adapter.on('stateChange', function (id, state) {
    if (!id || !state || state.ack) return;
    if (!adapter.config.wires) return;

    var wire;
    for (var i = 0; i < adapter.config.wires.length; i++) {
        if (adapter.config.wires[i] && id === adapter.namespace + '.wires.' + adapter.config.wires[i]._name) {
            if (state.val === true || state.val === 'true') {
                state.val = 1;
            } else
            if (state.val === false || state.val === 'false') {
                state.val = 0;
            }
            wire = adapter.config.wires[i];
            break;
        }
    }
    if (wire) {
        if (state.val === null) return;
        if (!objects[id]) {
            adapter.getForeignObject(id, function (err, obj) {
                objects[id] = obj;
                if (obj && obj.common && !obj.common.write) {
                    adapter.log.debug('Cannot write read only "' + id + '"');
                    return;
                }
                writeWire(wire, state.val);
            });
        } else {
            if (objects[id] && objects[id].common && !objects[id].common.write) {
                adapter.log.debug('Cannot write read only "' + id + '"');
                return;
            }
            writeWire(wire, state.val);
        }
    } else {
        adapter.log.warn('Wire "' + id + '" not found');
    }
});

function readSensors(oClientOrPath, sensors, result, cb) {
    result = result || {};
    if (!sensors || !sensors.length) {
        return cb && cb(result);
    }
    var sensor = sensors.shift();

    if (typeof oClientOrPath === 'object') {
        oClientOrPath.list('/' + sensor, function(err, dirs) {
            result[sensor] = dirs;
            if (dirs) {
                for (var d = 0; d < dirs.length; d++) {
                    if (dirs[d].substring(0, sensor.length + 2) === '/' + sensor + '/') {
                        dirs[d] = dirs[d].substring(sensor.length + 2);
                    }
                    if (possibleSubTrees.indexOf(dirs[d]) !== -1) {
                        sensors.push(sensor + '/' + dirs[d]);
                    }
                }
            }
            setTimeout(function () {
                readSensors(oClientOrPath, sensors, result, cb);
            }, 0);
        });
    } else {
        fs.readdir(oClientOrPath + '/' + sensor, function (err, dirs) {
            result[sensor] = dirs;
            if (dirs) {
                for (var d = 0; d < dirs.length; d++) {
                    if (possibleSubTrees.indexOf(dirs[d]) !== -1) {
                        sensors.push(sensor + '/' + dirs[d]);
                    }
                }
            }
            setTimeout(function () {
                readSensors(oClientOrPath, sensors, result, cb);
            }, 0);
        });
    }
}

var possibleSubTrees = [
    'LED',
    'relay',
    'set_alarm'
];

function processMessage(msg) {
    if (!msg || !msg.command) return;

    switch (msg.command) {
        case 'readdir':
            if (msg.callback) {
                var _client = null;
                if (msg.message.config && msg.message.config.ip) {
                    adapter.log.debug('Connect to ' + msg.message.config.ip + ':' + msg.message.config.port);
                    _client = getOWFSClient({host: msg.message.config.ip, port: msg.message.config.port});
                } else if (msg.message.config && !msg.message.config.path) {
                    _client = client;
                }

                if (_client) {
                    _client.list('/', function(err, dirs) {
                        if (err) {
                            adapter.log.error('Cannot read dir: ' + err);
                            adapter.sendTo(msg.from, msg.command, {error: err.toString()}, msg.callback);
                            _client = null;
                        } else {
                            adapter.log.debug('Result for list: ' + JSON.stringify(dirs));
                            for (var d = dirs.length - 1; d >= 0; d--) {
                                if (!dirs[d] || dirs[d][0] !== '/') {
                                    dirs.splice(d, 1);
                                } else {
                                    dirs[d] = dirs[d].substring(1);
                                }
                            }
                            adapter.log.debug('Result for list_: ' + JSON.stringify(dirs));

                            // read all sensors
                            readSensors(_client, dirs, null, function (result) {
                                adapter.log.debug('Result for dir: ' + JSON.stringify(result));
                                adapter.sendTo(msg.from, msg.command, {sensors: result}, msg.callback);
                                _client = null;
                            });
                        }
                    });
                } else {
					fs = fs || require('fs');
                    var _path1wire =  msg.message.config ? msg.message.config.path || '/mnt/1wire' : '/mnt/1wire';
                    if (_path1wire[_path1wire.length - 1] === '/') _path1wire = _path1wire.substring(0, _path1wire.length - 1);
                    fs.readdir(_path1wire, function (err, dirs) {
                        if (err) {
                            adapter.log.error('Cannot read dir: ' + err);
                            adapter.sendTo(msg.from, msg.command, {error: err.toString()}, msg.callback);
                        } else {
							for (var d = dirs.length - 1; d >= 0; d--) {
                                if (!dirs[d] || (dirs[d].indexOf('.') === -1 && dirs[d].indexOf('-') === -1)) {
                                    dirs.splice(d, 1);
                                }
                            }
							
                            // read all sensors
                            readSensors(_path1wire, dirs, null, function (result) {
                                adapter.sendTo(msg.from, msg.command, {sensors: result}, msg.callback);
                            });
                        }
                    });
                }
            }

            break;
        default:
            adapter.log.error('Messages are not supported');
            break;
    }
}

function processMessages() {
    adapter.getMessage(function (err, obj) {
        if (obj) {
            processMessage(obj.command, obj.message);<commit_msg>(bluefox) do not filter sensors, just return them all<commit_after>/**
 *
 *      ioBroker OWFS Adapter
 *
 *      Copyright (c) 2015-2016 bluefox<<EMAIL>>
 *
 *      MIT License
 *
 */
/* jshint -W097 */// jshint strict:false
/*jslint node: true */
'use strict';
var utils   = require(__dirname + '/lib/utils'); // Get common adapter utils
var adapter = utils.adapter('owfs');
var OWJS    = null;
var fs      = null;

var timers  = {};
var client  = null;
var objects = {};
var path1wire;
var OW_DIRALL = 7; // list 1-wire bus, in one packet string // workaround for owserver

adapter.on('message', function (obj) {
    if (obj) processMessage(obj);
    processMessages();
});

adapter.on('ready', function () {
    main();
});

adapter.on('unload', function () {
    for (var t in timers) {
        clearInterval(timers[t].timer);
        timers[t].timer = null;
    }
});

adapter.on('stateChange', function (id, state) {
    if (!id || !state || state.ack) return;
    if (!adapter.config.wires) return;

    var wire;
    for (var i = 0; i < adapter.config.wires.length; i++) {
        if (adapter.config.wires[i] && id === adapter.namespace + '.wires.' + adapter.config.wires[i]._name) {
            if (state.val === true || state.val === 'true') {
                state.val = 1;
            } else
            if (state.val === false || state.val === 'false') {
                state.val = 0;
            }
            wire = adapter.config.wires[i];
            break;
        }
    }
    if (wire) {
        if (state.val === null) return;
        if (!objects[id]) {
            adapter.getForeignObject(id, function (err, obj) {
                objects[id] = obj;
                if (obj && obj.common && !obj.common.write) {
                    adapter.log.debug('Cannot write read only "' + id + '"');
                    return;
                }
                writeWire(wire, state.val);
            });
        } else {
            if (objects[id] && objects[id].common && !objects[id].common.write) {
                adapter.log.debug('Cannot write read only "' + id + '"');
                return;
            }
            writeWire(wire, state.val);
        }
    } else {
        adapter.log.warn('Wire "' + id + '" not found');
    }
});

function readSensors(oClientOrPath, sensors, result, cb) {
    result = result || {};
    if (!sensors || !sensors.length) {
        return cb && cb(result);
    }
    var sensor = sensors.shift();

    if (typeof oClientOrPath === 'object') {
        oClientOrPath.list('/' + sensor, function(err, dirs) {
            result[sensor] = dirs;
            if (dirs) {
                for (var d = 0; d < dirs.length; d++) {
                    if (dirs[d].substring(0, sensor.length + 2) === '/' + sensor + '/') {
                        dirs[d] = dirs[d].substring(sensor.length + 2);
                    }
                    if (possibleSubTrees.indexOf(dirs[d]) !== -1) {
                        sensors.push(sensor + '/' + dirs[d]);
                    }
                }
            }
            setTimeout(function () {
                readSensors(oClientOrPath, sensors, result, cb);
            }, 0);
        });
    } else {
        fs.readdir(oClientOrPath + '/' + sensor, function (err, dirs) {
            result[sensor] = dirs;
            if (dirs) {
                for (var d = 0; d < dirs.length; d++) {
                    if (possibleSubTrees.indexOf(dirs[d]) !== -1) {
                        sensors.push(sensor + '/' + dirs[d]);
                    }
                }
            }
            setTimeout(function () {
                readSensors(oClientOrPath, sensors, result, cb);
            }, 0);
        });
    }
}

var possibleSubTrees = [
    'LED',
    'relay',
    'set_alarm'
];

function processMessage(msg) {
    if (!msg || !msg.command) return;

    switch (msg.command) {
        case 'readdir':
            if (msg.callback) {
                var _client = null;
                if (msg.message.config && msg.message.config.ip) {
                    adapter.log.debug('Connect to ' + msg.message.config.ip + ':' + msg.message.config.port);
                    _client = getOWFSClient({host: msg.message.config.ip, port: msg.message.config.port});
                } else if (msg.message.config && !msg.message.config.path) {
                    _client = client;
                }

                if (_client) {
                    _client.list('/', function(err, dirs) {
                        if (err) {
                            adapter.log.error('Cannot read dir: ' + err);
                            adapter.sendTo(msg.from, msg.command, {error: err.toString()}, msg.callback);
                            _client = null;
                        } else {
                            adapter.log.debug('Result for list: ' + JSON.stringify(dirs));
                            for (var d = dirs.length - 1; d >= 0; d--) {
                                if (!dirs[d] || dirs[d][0] !== '/') {
                                    dirs.splice(d, 1);
                                } else {
                                    dirs[d] = dirs[d].substring(1);
                                }
                            }
                            adapter.log.debug('Result for list_: ' + JSON.stringify(dirs));

                            // read all sensors
                            readSensors(_client, dirs, null, function (result) {
                                adapter.log.debug('Result for dir: ' + JSON.stringify(result));
                                adapter.sendTo(msg.from, msg.command, {sensors: result}, msg.callback);
                                _client = null;
                            });
                        }
                    });
                } else {
					fs = fs || require('fs');
                    var _path1wire =  msg.message.config ? msg.message.config.path || '/mnt/1wire' : '/mnt/1wire';
                    if (_path1wire[_path1wire.length - 1] === '/') _path1wire = _path1wire.substring(0, _path1wire.length - 1);
                    fs.readdir(_path1wire, function (err, dirs) {
                        if (err) {
                            adapter.log.error('Cannot read dir: ' + err);
                            adapter.sendTo(msg.from, msg.command, {error: err.toString()}, msg.callback);
                        } else {
							/*for (var d = dirs.length - 1; d >= 0; d--) {
                                if (!dirs[d] || (dirs[d].indexOf('.') === -1 && dirs[d].indexOf('-') === -1)) {
                                    dirs.splice(d, 1);
                                }
                            }*/
							
                            // read all sensors
                            readSensors(_path1wire, dirs, null, function (result) {
                                adapter.sendTo(msg.from, msg.command, {sensors: result}, msg.callback);
                            });
                        }
                    });
                }
            }

            break;
        default:
            adapter.log.error('Messages are not supported');
            break;
    }
}

function processMessages() {
    adapter.getMessage(function (err, obj) {
        if (obj) {
            processMessage(obj.command, obj.message);
{'fraction_non_alphanumeric': 0.14361118727721414, 'fraction_numerical': 0.0067178502879078695, 'mean_word_length': 1.2434261110256803, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7494649', 'n_tokens_mistral': 4230, 'n_tokens_neox': 3997, 'n_words': 861}