// Décodeur LoRaWAN officiel : R718N3 - Wireless 3-Phase Current Meter With 3 x 50A Solid Core CT (Netvox) // Source : https://github.com/TheThingsNetwork/lorawan-devices/blob/master/vendor/netvox/payload/r718n3.js // The Things Network Device Repository, fichier publié par le constructeur, distribué sans modification. // Format : decodeUplink(input) (TTN v3 / ChirpStack v4). Hébergé par smartbuildings.fr. function getCfgCmd(cfgcmd){ var cfgcmdlist = { 1: "ConfigReportReq", 129: "ConfigReportRsp", 2: "ReadConfigReportReq", 130: "ReadConfigReportRsp" }; return cfgcmdlist[cfgcmd]; } function getCmdToID(cmdtype){ if (cmdtype == "ConfigReportReq") return 1; else if (cmdtype == "ConfigReportRsp") return 129; else if (cmdtype == "ReadConfigReportReq") return 2; else if (cmdtype == "ReadConfigReportRsp") return 130; } function getDeviceName(dev){ var deviceName = { 74: "R718N3" }; return deviceName[dev]; } function getDeviceID(devName){ if (devName == "R718N3") return 74; } function padLeft(str, len) { str = '' + str; if (str.length >= len) { return str; } else { return padLeft("0" + str, len); } } function decodeUplink(input) { var data = {}; switch (input.fPort) { case 6: if (input.bytes[2] === 0x00) { data.Device = getDeviceName(input.bytes[1]); data.SWver = input.bytes[3]/10; data.HWver = input.bytes[4]; data.Datecode = padLeft(input.bytes[5].toString(16), 2) + padLeft(input.bytes[6].toString(16), 2) + padLeft(input.bytes[7].toString(16), 2) + padLeft(input.bytes[8].toString(16), 2); return { data: data, }; } var map = new Map([ [0,1],[1,5],[2,10],[3,100] ]); data.Device = getDeviceName(input.bytes[1]); if (input.bytes[3] & 0x80) { var tmp_v = input.bytes[3] & 0x7F; data.Volt = (tmp_v / 10).toString() + '(low battery)'; } else data.Volt = input.bytes[3]/10; if (input.bytes[2] === 0x01) { data.Current1 = (input.bytes[4]<<8 | input.bytes[5]); data.Current2 = (input.bytes[6]<<8 | input.bytes[7]); data.Current3 = (input.bytes[8]<<8 | input.bytes[9]); data.Multiplier1 = input.bytes[10]; } else if (input.bytes[2] === 0x02) { data.Multiplier2 = input.bytes[4]; data.Multiplier3 = input.bytes[5]; } else if (input.bytes[2] === 0x03) { data.Current1 = (input.bytes[4]<<8 | input.bytes[5]); data.Current2 = (input.bytes[6]<<8 | input.bytes[7]); data.Current3 = (input.bytes[8]<<8 | input.bytes[9]); data.Multiplier1 = map.get(input.bytes[10] & 3); data.Multiplier2 = map.get(input.bytes[10]>>2 & 3); data.Multiplier3 = map.get(input.bytes[10]>>4 & 3); } else if (input.bytes[2] === 0x04) { data.LowCurrent1Alarm = input.bytes[4] & 1; data.HighCurrent1Alarm = input.bytes[4]>>1 & 1; data.LowCurrent2Alarm = input.bytes[4] >>2 & 1; data.HighCurren2Alarm = input.bytes[4]>>3 & 1; data.LowCurrent3Alarm = input.bytes[4]>>4 & 1; data.HighCurrent3Alarm = input.bytes[4]>>5 & 1; } break; case 7: data.Cmd = getCfgCmd(input.bytes[0]); data.Device = getDeviceName(input.bytes[1]); if (input.bytes[0] === getCmdToID("ConfigReportRsp")) { data.Status = (input.bytes[2] === 0x00) ? 'Success' : 'Failure'; } else if (input.bytes[0] === getCmdToID("ReadConfigReportRsp")) { data.MinTime = (input.bytes[2]<<8 | input.bytes[3]); data.MaxTime = (input.bytes[4]<<8 | input.bytes[5]); data.CurrentChange = (input.bytes[6]<<8 | input.bytes[7]); } break; default: return { errors: ['unknown FPort'], }; } return { data: data, }; } function encodeDownlink(input) { var ret = []; var devid; var getCmdID; getCmdID = getCmdToID(input.data.Cmd); devid = getDeviceID(input.data.Device); if (input.data.Cmd == "ConfigReportReq") { var mint = input.data.MinTime; var maxt = input.data.MaxTime; var currentChg = input.data.CurrentChange; ret = ret.concat(getCmdID, devid, (mint >> 8), (mint & 0xFF), (maxt >> 8), (maxt & 0xFF), (currentChg >> 8), (currentChg & 0xFF), 0x00, 0x00, 0x00); } else if (input.data.Cmd == "ReadConfigReportReq") { ret = ret.concat(getCmdID, devid, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); } return { fPort: 7, bytes: ret }; } function decodeDownlink(input) { var data = {}; switch (input.fPort) { case 7: data.Cmd = getCfgCmd(input.bytes[0]); data.Device = getDeviceName(input.bytes[1]); if (input.bytes[0] === getCmdToID("ConfigReportReq")) { data.MinTime = (input.bytes[2]<<8 | input.bytes[3]); data.MaxTime = (input.bytes[4]<<8 | input.bytes[5]); data.CurrentChange = (input.bytes[6]<<8 | input.bytes[7]); } break; default: return { errors: ['invalid FPort'], }; } return { data: data, }; }