Ajout de la liste des Nuts pour Chaque Shelly.
let origine = Shelly.getDeviceInfo().name;
let genericTopic = "shellies/script/scanNut/";
let nutsShelly = []; //nuts vus par ce Shelly
let nutsBroker = []; //nuts vus par le broker
let nutsName = {
"aa:aa:aa:aa:aa:aa": "Nut01",
"bb:bb:bb:bb:bb:bb": "Nut02",
"cc:cc:cc:cc:cc:cc": "Nut03",
"dd:dd:dd:dd:dd:dd": "Nut04"
};
if(MQTT.isConnected()) {
MQTT.publish(genericTopic + origine + '/nombreNut', "0", 0, false);
MQTT.publish(genericTopic + origine + '/nameNut', "", 0, false);
}
function isMacValid(mac) {
if (mac.length !== 17) {
return false;
}
for (let i=0; i<17; i++) {
if (i%3 === 2) {
if (mac.at(i) !== 0x3a) { //0x3a = :
return false;
}
} else {
//0x30 = 0, 0x39 = 9, 0x61 = a et 0x66 = f
if (mac.at(i) < 0x30 || (mac.at(i) > 0x39 && mac.at(i) < 0x61) || mac.at(i) > 0x66) {
return false;
}
}
}
return true;
}
function nutList (nuts, nutsName) {
let nutsList = "";
for (let i=0; i<nuts.length; i++) {
nutsList += nutsName[nuts[i].mac] + " ";
}
let long = nutsList.length - 1;
nutsList = nutsList.slice(0, long);
return nutsList;
}
function nutAjout (mac, unixtime, nuts) {
let existePas = 1;
for (let i=0; i<nuts.length; i++) {
if (nuts[i].mac === mac) {
nuts[i].timePublish = unixtime;
existePas = 0;
}
}
if (existePas === 1 && isMacValid(mac)) {
nuts.push({mac: mac, timePublish: unixtime});
}
}
MQTT.subscribe(
genericTopic + "#",
function (topic, message, callback) {
let endTopic = topic.slice(callback[0].length + 18);
if (endTopic === "unixtime") {
let mac = topic.slice(callback[0].length, callback[0].length + 17);
nutAjout (mac, JSON.parse(message), callback[1]);
}
},
[genericTopic, nutsBroker]
);
Timer.set(
10000,
true,
function (callback) {
for (let i=0; i<callback[1].length; i++) {
if (callback[1][i].timePublish + 110 <= Shelly.getComponentStatus("sys").unixtime) {
if(MQTT.isConnected()) {
MQTT.publish(callback[0] + callback[1][i].mac + '/presence', "0", 0, false);
}
}
}
for (let i=0; i<callback[3].length; i++) {
if (callback[3][i].timePublish + 110 <= Shelly.getComponentStatus("sys").unixtime) {
callback[3].splice(i, 1);
let list = nutList (callback[3], callback[4]);
if(MQTT.isConnected()) {
MQTT.publish(callback[0] + callback[2] + '/nombreNut', JSON.stringify(callback[3].length), 0, false);
MQTT.publish(callback[0] + callback[2] + '/nameNut', list, 0, false);
}
}
}
},
[genericTopic, nutsBroker, origine, nutsShelly, nutsName]
);
function scanCB(ev, res, callback) {
if (ev === BLE.Scanner.SCAN_RESULT) {
if (res.local_name === 'nut' && isMacValid(res.addr)) {
let mac = res.addr;
let topic = callback[0] + mac + '/';
let unixtime = Shelly.getComponentStatus("sys").unixtime;
nutAjout (mac, unixtime, callback[1]);
let list = nutList (callback[1], callback[3]);
if(MQTT.isConnected()) {
MQTT.publish(topic + 'rssi', JSON.stringify(res.rssi), 0, false);
MQTT.publish(topic + 'unixtime', JSON.stringify(unixtime), 0, false);
MQTT.publish(topic + 'presence', "1", 0, false);
MQTT.publish(topic + 'origine', JSON.stringify(callback[2]), 0, false);
MQTT.publish(callback[0] + callback[2] + '/nombreNut', JSON.stringify(callback[1].length), 0, false);
MQTT.publish(callback[0] + callback[2] + '/nameNut', list, 0, false);
}
}
}
}
BLE.Scanner.Start({ duration_ms: -1}, scanCB, [genericTopic, nutsShelly, origine, nutsName]);