wireless-mbus-parser
Version:
Parse telegram data received via wireless mbus
3 lines • 72.1 kB
JavaScript
var Pt=[0,15717,31434,18351,62868,51441,36702,45627,54861,60200,44167,37346,9177,7868,22803,25718,37375,44186,60213,54864,25707,22798,7841,9156,18354,31447,15736,29,45606,36675,51436,62857,7835,9214,25681,22836,60175,54890,37317,44192,51414,62899,45596,36729,15682,39,18312,31469,36708,45569,62894,51403,31472,18325,58,15711,22825,25676,9187,7814,44221,37336,54903,60178,15670,83,18428,31385,51362,62919,45672,36621,60283,54814,37297,44244,7919,9098,25637,22848,44233,37292,54787,60262,22877,25656,9111,7922,31364,18401,78,15659,36624,45685,62938,51391,9133,7880,22887,25602,54841,60252,44275,37270,62944,51333,36650,45647,116,15633,31422,18395,45650,36663,51352,62973,18374,31395,15628,105,25631,22906,7893,9136,37259,44270,60225,54820,31340,18185,166,15811,36856,45725,62770,51287,44065,37188,55019,60302,22965,25808,9087,7706,60307,55030,37209,44092,7687,9058,25805,22952,15838,187,18196,31345,51274,62767,45696,36837,25847,22930,7741,9048,37219,44038,60329,54988,45754,36831,51312,62741,18222,31307,15844,129,62728,51309,36802,45735,156,15865,31318,18227,9029,7712,22927,25834,54993,60340,44059,37246,18266,31295,15760,245,45774,36779,51204,62817,37143,44146,60381,54968,25731,23014,7753,9004,54949,60352,44143,37130,9009,7764,23035,25758,232,15757,31266,18247,62844,51225,36790,45779,22977,25764,8971,7790,44117,37168,54943,60410,36748,45801,62790,51235,31256,18301,210,15799,51262,62811,45812,36753,15786,207,18272,31237,7795,8982,25785,23004,60391,54914,37165,44104];function te(e,t,r){let a=0;for(let n=t;n<r;n++)a=Pt[(a>>8^e[n])&255]^a<<8;return~a&65535}function X(e,t,r){let a=te(e,t,r),n=e.readUInt16BE(r);return a===n}function ce(e,t,r){let a=e.readUInt16LE(t);return te(e,t+2,r)===a}var Ie=[140,141,142,143],oe=144,ge=120,K=122,k=114,Te=126,Fe=127,$=121,Ne=[160,161,162,163,164,165,166,167],Le=[160,161,162],Re=0,De=1,Ve=2,Ae=3,J=128,re=127,ve=47,be=0,Ce=1,_=2,b=3,M=4,Pe=5,W=6,Oe=7,Se=8,Me=9,Ue=10,he=11,we=12,U=13,Be=14,ke=15,We={0:"Other",1:"Oil",2:"Electricity",3:"Gas",4:"Heat",5:"Steam",6:"Warm Water (30 - 90 \xB0C)",7:"Water",8:"Heat Cost Allocator",9:"Compressed Air",10:"Cooling load meter (Volume measured at return temperature: outlet)",11:"Cooling load meter (Volume measured at flow temperature: inlet)",12:"Heat (Volume measured at flow temperature: inlet)",13:"Heat / Cooling load meter",14:"Bus / System component",15:"Unknown Medium",16:"Reserved for utility meter",17:"Reserved for utility meter",18:"Reserved for utility meter",19:"Reserved for utility meter",20:"Calorific value",21:"Hot water (> 90 \xB0C)",22:"Cold water",23:"Dual register (hot/cold) Water meter",24:"Pressure",25:"A/D Converter",26:"Smokedetector",27:"Room sensor (e.g. temperature or humidity)",28:"Gasdetector",29:"Reserved for sensors",30:"Reserved for sensors",31:"Reserved for sensors",32:"Breaker (electricity)",33:"Valve (gas)",34:"Reserved for switching devices",35:"Reserved for switching devices",36:"Reserved for switching devices",37:"Customer unit (Display device)",38:"Reserved for customer units",39:"Reserved for customer units",40:"Waste water",41:"Garbage",42:"Carbon dioxide",43:"Environmental meter",44:"Environmental meter",45:"Environmental meter",46:"Environmental meter",47:"Environmental meter",49:"OMS MUC",50:"OMS unidirectional repeater",51:"OMS bidirectional repeater",55:"Radio converter (Meter side)",67:"Heat meter (TCH)",98:"Hot water meter (TCH)",114:"Cold water meter (TCH)",128:"Heat cost allocator (TCH)",240:"Smoke detector (TCH)"};var s=class extends Error{name;message;constructor(t,r){super(),this.name=t,this.message=r}};var d={debug:process.env.DEBUG==="1"?(e,...t)=>{console.log(e,...t)}:(e,...t)=>{},error:(e,...t)=>{console.error(e,...t)}};function ae(e){return e[0]==104&&e[3]==104&&e[e.length-1]==22}function v(e){return e.mField!==void 0}function Ye(e){return e.ci===$}function Ge(e){return e.plainText!==void 0}function He(e,t){let r={manufacturer:e.manufacturer,id:e.meterId,type:e.type,deviceType:C(e.type),version:e.version};return t?.ci==122?{...r,status:t.status,accessNo:t.accessNo}:t?.ci===114?{manufacturer:t.meterManufacturerString,id:t.meterIdString,type:t.meterDevice,deviceType:C(t.meterDevice),version:t.meterVersion,status:t.status,accessNo:t.accessNo,radio:r}:r}function P(e,t,r=0){let a=Math.ceil(e/2),n=Buffer.from(t.subarray(r,r+a)),c=n[a-1]>>4>9?-1:1;c===-1&&(n[a-1]&=15);let l=0;for(let u=0;u<a;u++)l+=((n[u]&15)+((n[u]&240)>>4)*10)*Math.pow(100,u);return Math.trunc(c*l)}function h(e,t=0){return(Buffer.isBuffer(e)?e.readUint32LE(t):e).toString(16).padStart(8,"0")}function Z(e){return String.fromCharCode((e>>10)+64)+String.fromCharCode((e>>5&31)+64)+String.fromCharCode((e&31)+64)}function C(e){return We[e]||"unknown"}function Xe(e){let t=e&4,r=e&8,a=e&16,n=e&224,c=(t?"; low power":"")+(r?" (permanent)":"")+(a?" (temporary)":"")+(n?"; Manufacturer specific 0b"+n.toString(2).padStart(8,"0"):"");switch(e&3){case 0:return"No error"+c;case 1:return"Busy"+c;case 2:return"Error"+c;case 3:return"Alarm"+c}}function Ke(e){(e&128)>>8===0||d.debug("Warning: Invalid date encountered!");let r=e&63,a=(e&7936)>>8,n=(e&2031616)>>16,c=(e&251658240)>>24,l=(e&4026531840)>>25|(e&14680064)>>21;return new Date(l+2e3,c-1,n,a,r,0)}function $e(e){let t=e&31,r=(e&3840)>>8,a=(e&61440)>>9|(e&224)>>5;return new Date(a+2e3,r-1,t)}function Je(e){let t=Buffer.alloc(6);t.writeUIntLE(e,0,6);let r=t[0]&63,a=t[1]&63,n=t[2]&31,c=t[3]&31,l=t[4]&15,u=(t[3]&224)>>5|(t[4]&240)>>1;return new Date(u+2e3,l-1,c,n,a,r)}function ie(e){let t=e&31,r=(e&480)>>5,a=(e&32256)>>9;return new Date(a+2e3,r-1,t)}function le(e){let t=(e&496)>>4,r=(e&7680)>>9,a=new Date().getFullYear();return new Date(a,r-1,t)}function Ze(e,t){let r=(e&3968)>>7,a=(t&120)>>3,n=new Date().getFullYear();return new Date(n,a-1,r)}function w(e){let t=e.getDate(),r=e.getMonth()+1,a=e.getFullYear()-2e3,n=a&7;return(a&120)>>3<<12|r<<8|n<<5|t}function Ot(e,t=0){if(e.length<8+t)return"ERR-XXXXXXXX";let r=Z(e.readUint16LE(t+2)),a=h(e,t+4);return`${r}-${a}`}function je(e){if(!X(e,0,10))return!1;let t=12,r=Math.min(e.length,G(e))-2;for(;t<e.length;){let a=Math.min(t+16,r);if(!X(e,t,a))return!1;t+=18}return!0}function qe(e){if(e[0]>=128&&!X(e,128,e.length-2))return!1;let r=Math.min(128,e.length)-2;return X(e,0,r)}function Qe(e){let t=[];t.push(e.subarray(0,10));let r=12,a=Math.min(e.length,G(e))-2;for(;r<e.length;){let n=Math.min(r+16,a);t.push(e.subarray(r,n)),r+=18}return Buffer.concat(t)}function et(e){let t=e.subarray(0,Math.min(128,e.length)-2);return e[0]>=128?Buffer.concat([t,e.subarray(128,e.length-2)]):Buffer.from(t)}function G(e){let t=e[0]+1,r=t-10,a=Math.ceil(r/16);return t+(a+1)*2}function St(e){let t=e[0]+1;if(t>e.length)throw new s("CRC_ERROR",`Telegram data is too short! Expected at least ${t} bytes, but got only ${e.length}`);if(t==e.length)return qe(e)?et(e):Buffer.from(e);if(G(e)>e.length)throw new s("CRC_ERROR",`Telegram data is too short! Expected at least ${G(e)} bytes, but got only ${e.length}`);if(je(e))return Qe(e);throw new s("CRC_ERROR","Frame type A CRC check failed!")}function Mt(e){let t=e[0]+1;if(t>e.length)throw new s("CRC_ERROR",`Telegram data is too short! Expected at least ${t} bytes, but got only ${e.length}`);if(t==e.length){if(qe(e))return et(e);throw new s("CRC_ERROR","Frame type B CRC check failed!")}else{let r=G(e),a=e;if(r>e.length)throw new s("CRC_ERROR",`Telegram data is too short! Expected at least ${G(e)} bytes, but got only ${e.length}`);if(r<e.length&&(a=e.subarray(0,r)),je(a))return Qe(a);throw new s("CRC_ERROR","Frame type A CRC check failed!")}}function ze(e){let t=e[0]+1;return e.subarray(0,t)}function Ut(e){let t=e[1];if(e[2]!=t)throw new s("CRC_ERROR","Telegram is not a valid wired M-Bus frame!");let r=0;for(let a=4;a<e.length-2;a++)r=r+e[a]&255;if(r!=e[e.length-2])throw new s("CRC_ERROR","Wired M-Bus frame CRC check failed!");return Buffer.from(e.subarray(0,e.length-2))}function pe(e,t){return ae(e)?Ut(e):t===void 0?St(e):t?ze(Mt(e)):Buffer.from(ze(e))}import{AesCmac as tt}from"aes-cmac";import{createDecipheriv as ht}from"crypto";function wt(e,t,r,a){let n=e.length%16,c=e.length,l=ht(a,r,t);return l.setAutoPadding(!1),n&&(e=Buffer.concat([e,Buffer.alloc(16-n)])),Buffer.concat([l.update(e),l.final()]).subarray(0,c)}function q(e,t,r,a,n,c){let l=e.subarray(n,n+c);d.debug(`Encrypted data: ${l.toString("hex")}`),d.debug(`IV: ${r.toString("hex")}`);let u=wt(l,r,t,a);if(d.debug(`Decrypted data: ${u.toString("hex")}`),!u.length)throw new s("DECRYPTION_ERROR",`Decryption (${a}) failed! IV: ${r.toString("hex")}`);return Buffer.concat([e.subarray(0,n),u,e.subarray(n+c)])}async function rt(e,t){if(t.afl.mcr===void 0)throw new s("DECRYPTION_ERROR","Cannot dervice Kenc without MCR from AFL!");let r=Buffer.alloc(16,7);r[0]=0,r.writeUInt32LE(t.afl.mcr,1),t.apl.ci===k?r.writeUInt32LE(t.apl.meterId,5):r.writeUInt32LE(t.ll.aField,5);let a=new tt(e),n=Buffer.from(await a.calculate(r));d.debug(`Kenc: ${n.toString("hex")}`),r[0]=1;let c=Buffer.from(await a.calculate(r));return d.debug(`Kmac: ${c.toString("hex")}`),{kenc:n,kmac:c}}async function at(e,t,r){let a=Buffer.alloc(5+(r.fcl.mlp?2:0));a[0]=r.mclRaw,a.writeUInt32LE(r.mcr,1),r.fcl.mlp&&a.writeUInt16LE(r.ml,5);let n=Buffer.concat([a,t]),c=await new tt(e),l=Buffer.from(await c.calculate(n));if(d.debug(`MAC: ${l.toString("hex")}`),r.mac.compare(l.subarray(0,8))!=0)throw new s("WRONG_AES_KEY",`Received MAC does not match. Wrong key?
MAC received: ${r.mac.toString("hex")}`)}var Bt=Buffer.from("39BC8A10E66D83F8","hex"),kt=Buffer.from("51728910E66D83F8","hex");function Wt(e){let t=e.addressRaw[2],r=e.addressRaw[3],a=Buffer.alloc(2);a.writeUInt8(t,0),a.writeUInt8(r,1);let n=Buffer.concat([e.addressRaw.subarray(0,2),e.addressRaw.subarray(4),Buffer.from(a)]);return{lField:e.lField,cField:e.cField,mField:e.mField,aField:n.readUInt32LE(2),version:t,type:r,addressRaw:n,aFieldRaw:n.subarray(2),manufacturer:e.manufacturer,typeString:C(r),meterId:h(n,2)}}function it(e,t,r,a){return(e.readUint32BE(0)^e.readUint32BE(4)^t.readUint32BE(0)^t.readUint32BE(4)^r.readUint32BE(a))>>>0}function ot(e){let t=+((e&2)!=0)^+((e&4)!=0)^+((e&2147483648)!=0)^+((e&2048)!=0);return e<<1|t}function nt(e,t,r){for(let n=0;n<8;n++)e=ot(e);return(t[r+5]^e&255)===75}function Yt(e,t,r){let a=it(Bt,e,t,r);if(nt(a,t,r))return a;{let n=it(kt,e,t,r);if(nt(n,t,r))return n;throw new Error("No matching key to decrypt PRIOS telegram found!")}}function Gt(e,t,r){let a=Buffer.alloc(t.length-r);t.copy(a,0,r,r+10);for(let n=r+5;n<t.length;n++){for(let c=0;c<8;c++)e=ot(e);a[n-r]=t[n]^e&255}return a}function ct(e){let t=Object.keys(e).reduce((r,a)=>{if(e[a]){let n=a.replace(/[A-Z]/,c=>` ${c.toLowerCase()}`);return r?`${r}, ${n}`:n}return r},"");return t.length?t:"no alarms"}function Ht(e){let t={general:(e[1]&128)!==0,leakage:(e[2]&128)!==0,meterBlocked:(e[2]&32)!==0,backflow:(e[3]&128)!==0,underflow:(e[3]&64)!==0,overflow:(e[3]&32)!==0,submarine:(e[3]&16)!==0,sensorFraud:(e[3]&8)!==0,mechanicalFraud:(e[3]&2)!==0},r=ct(t),a={leakage:(e[2]&64)!==0,sensorFraud:(e[3]&4)!==0,mechanicalFraud:(e[3]&1)!==0},n=ct(a);return{current:r,previous:n}}function Xt(e){let t=Ht(e),r=Buffer.alloc(36+t.current.length+t.previous.length),a=0;r[a++]=M,r[a++]=e[4],e.copy(r,a,6,10),a+=4,r[a++]=M|64,r[a++]=e[4],e.copy(r,a,10,14),a+=4,r[a++]=_|64,r[a++]=108,e.copy(r,a,14,16),a+=2,r[a++]=_,r[a++]=255,r[a++]=110;let n=(e[2]&31)*6;r.writeUInt16LE(n,a),a+=2,r[a++]=b,r[a++]=255,r[a++]=44;let c=1<<(e[1]&15)+2;return r.writeUIntLE(c,a,3),a+=3,r[a++]=U,r[a++]=255,r[a++]=23,r[a++]=t.current.length,r.write(t.current.split("").reverse().join(""),a,"ascii"),a+=t.current.length,r[a++]=U,r[a++]=255,r[a++]=151,r[a++]=62,r[a++]=t.previous.length,r.write(t.previous.split("").reverse().join(""),a,"ascii"),r}function lt(e,t){if(!v(t))throw new Error("PRIOS telegram without full link layer!");let{pos:r,data:a}=e,n=Yt(t.addressRaw,a,r),c=Gt(n,a,r),l=Xt(c),u=Buffer.concat([a.subarray(0,r),l]),m={ci:a[r],offset:r};return{state:{...e,data:u,pos:r+1},applicationLayer:m,linkLayer:Wt(t)}}var B={HCA:"HCA",Heat:"Heat",Water:"Water",Smoke:"Smoke"},Kt=[97,100,105,148];function $t(e){switch(e){case 98:case 114:return B.Water;case 67:case 69:return B.Heat;case 128:return B.HCA;case 240:return B.Smoke;default:throw new Error(`Unkown Techem device type! 0x${e.toString(16)}`)}}function Jt(e,t,r){if(!Kt.includes(r))throw new Error(`Unknown TCH HCA version 0x${r.toString(16)}`);let a=r==148?15:r==105?14:10;if(t+a>e.length)throw new Error("Telegram to short for TCH HCA coding!");let n=Buffer.alloc(29),c=0,l=w(ie(e.readUInt16LE(t+2)));n[c++]=_|64,n[c++]=108,n.writeUInt16LE(l,c),c+=2,n[c++]=_|64,n[c++]=110,n[c++]=e[t+4],n[c++]=e[t+5];let u=w(le(e.readUInt16LE(t+6)));if(n[c++]=_,n[c++]=108,n.writeUInt16LE(u,c),c+=2,n[c++]=_,n[c++]=110,n[c++]=e[t+8],n[c++]=e[t+9],r!=148&&r!=105)return n.subarray(0,16);let m=r===148?t+11:t+10,g=e.readUint16LE(m);n[c++]=_,n[c++]=101,n[c++]=e[m],n[c++]=e[m+1];let x=e.readUint16LE(m+2);n[c++]=_,n[c++]=101,n[c++]=e[m+2],n[c++]=e[m+3];let F=g-x;return n[c++]=U,n[c++]=97,n[c++]=226,n.writeInt16LE(F,c),c+=2,n}function Zt(e,t){let r=Buffer.alloc(21),a=0,n=w(ie(e.readUInt16LE(t+2)));r[a++]=_|64,r[a++]=108,r.writeUInt16LE(n,a),a+=2,r[a++]=_|64,r[a++]=21,r[a++]=e[t+4],r[a++]=e[t+5];let c=e.readUint16LE(t+4),l=w(le(e.readUInt16LE(t+6)));r[a++]=_,r[a++]=108,r.writeUInt16LE(l,a),a+=2,r[a++]=_,r[a++]=21,r[a++]=e[t+8],r[a++]=e[t+9];let u=e.readUint16LE(t+8);return r[a++]=b,r[a++]=21,r.writeUIntLE(u+c,a,3),r}function zt(e,t){let r=Buffer.alloc(24),a=0,n=w(ie(e.readUInt16LE(t+2)));r[a++]=_|64,r[a++]=108,r.writeUInt16LE(n,a),a+=2,r[a++]=b|64,r[a++]=6,r[a++]=e[t+4],r[a++]=e[t+5],r[a++]=e[t+6];let c=e.readUintLE(t+4,3),l=w(Ze(e.readUInt8(t+11),e.readInt8(t+7)));r[a++]=_,r[a++]=108,r.writeUInt16LE(l,a),a+=2,r[a++]=b,r[a++]=6,r[a++]=e[t+8],r[a++]=e[t+9],r[a++]=e[t+10];let u=e.readUint16LE(t+8);return r[a++]=M,r[a++]=6,r.writeUInt32LE(u+c,a),r}function jt(e,t,r,a){switch(e){case B.HCA:return Jt(t,r,a);case B.Water:return Zt(t,r);case B.Heat:return zt(t,r);default:throw new Error("Not yet implemented!")}}function pt(e,t){let{pos:r,data:a}=e,n=$t(t.type),c=jt(n,a,r,t.version),l=Buffer.concat([a.subarray(0,r),c]),u={ci:a[r],offset:r};return{state:{...e,data:l,pos:r+1},applicationLayer:u,linkLayer:t}}function qt(e,t,r){let a=e.readUInt16LE(t);t+=2;let n=e.readUInt16LE(t);return t+=2,{newPos:t,apl:{ci:$,offset:r,headerCrc:a,frameCrc:n}}}function st(e,t,r){let a=e[t++],n=e[t++],c=Xe(n),{config:l,newPos:u}=ir(e,t);return{newPos:u,apl:{ci:K,offset:r,accessNo:a,statusCode:n,status:c,config:l}}}function Qt(e,t,r){let a=e.readUInt32LE(t);t+=4;let n=e.readUInt16LE(t);t+=2;let c=e[t++],l=e[t++],u=st(e,t,r);return{newPos:u.newPos,apl:{...u.apl,ci:k,meterId:a,meterIdString:h(a),meterManufacturer:n,meterManufacturerString:Z(n),meterVersion:c,meterDevice:l,meterDeviceString:C(l)}}}function ue(e){return v(e)?e:{lField:e.lField,mField:0,cField:e.cField,aField:e.aField,version:0,type:0,addressRaw:Buffer.alloc(8),aFieldRaw:Buffer.alloc(6),manufacturer:"AAA",typeString:"unknown",meterId:"00000000"}}function er(e,t){if(v(t))return t;if(e.ci!==k)return ue(t);let r=Buffer.alloc(8);return r.writeUInt32LE(e.meterManufacturer,0),r.writeUInt32LE(e.meterId,2),r[6]=e.meterVersion,r[7]=e.meterDevice,{lField:t.lField,mField:e.meterManufacturer,cField:t.cField,aField:t.aField,version:e.meterVersion,type:e.meterDevice,addressRaw:r,aFieldRaw:r.subarray(2),manufacturer:e.meterManufacturerString,typeString:e.meterDeviceString,meterId:e.meterIdString}}function tr(e,t){return{mode:e,bidirectional:(t&32768)!==0,accessability:(t&16384)!==0,synchronous:(t&8192)!==0,encryptedBlocks:(t&240)>>4,content:(t&12)>>2,hopCounter:t&3}}function rr(e,t,r){return{mode:e,content:(t&49152)>>14,encryptedBlocks:(t&240)>>4,kdfSel:(r&48)>>4,keyid:r&15}}function ar(e,t,r){return{mode:e,content:(t&49152)>>14,encryptedBytes:t&255,protoType:r&15}}function ir(e,t){let r=e.readUInt16LE(t);t+=2;let a=(r&7936)>>8;switch(a){case 0:case 5:return{config:tr(a,r),newPos:t};case 7:return{config:rr(a,r,e[t]),newPos:t+1};case 13:return{config:ar(a,r,e[t]),newPos:t+1};default:throw new s("UNIMPLEMENTED_FEATURE",`Unhandled config mode ${a}`)}}function nr(){return Buffer.alloc(16,0)}function cr(e,t){let r=Buffer.alloc(16,e.accessNo);return e.ci===K?t.addressRaw.copy(r,0):(r.writeUInt16LE(e.meterManufacturer,0),r.writeUInt32LE(e.meterId,2),r.writeUInt8(e.meterVersion,6),r.writeUInt8(e.meterDevice,7)),r}function or(e,t){return v(e)&&e.manufacturer==="DME"&&Ne.includes(t)}function lr(e,t){return v(e)&&e.manufacturer==="TCH"&&Le.includes(t)}async function ft(e,t,r){let a=e.data,n=e.pos,c=n,l=a[n++];if(l===Te||l===Fe)throw new s("UNIMPLEMENTED_FEATURE","Payload is SML encoded. SML decoding is not implemented");if(l===ge)return{state:{...e,pos:n},applicationLayer:{ci:l,offset:c},linkLayer:ue(t)};if(l===$){let{newPos:x,apl:F}=qt(a,n,c);return{state:{...e,pos:x},applicationLayer:F,linkLayer:ue(t)}}let u,m;if(l===K||l===k){let x=l===K?st(a,n,c):Qt(a,n,c);n=x.newPos,u=x.apl,m=er(u,t)}else{if(or(t,l))return lt(e,t);if(lr(t,l))return pt(e,t);throw new s("UNIMPLEMENTED_FEATURE",`Unsupported CI Field 0x${l.toString(16)}
remaining payload is ${a.toString("hex",n)}`)}let g;if(u.config.mode!==0){if(e.key===void 0)throw new s("NO_AES_KEY","Encrypted telegram but no AES key provided");if(u.config.mode===5){let x=u.config.encryptedBlocks*16;g=q(a,e.key,cr(u,m),"aes-128-cbc",n,x)}else if(u.config.mode===7){if(r===void 0)throw new s("UNEXPECTED_STATE","Mode 7 encryption but no AFL found");let x=u.config.encryptedBlocks*16,{kenc:F,kmac:V}=await rt(e.key,{ll:m,afl:r,apl:u}),S=a.subarray(u.offset,n+x);await at(V,S,r),g=q(a,F,nr(),"aes-128-cbc",n,x)}else throw new s("UNIMPLEMENTED_FEATURE",`Encryption mode ${u.config.mode.toString(16)} not implemented`);if(g.readUInt16LE(n)!=12079)throw new s("WRONG_AES_KEY","Decryption failed, wrong key?")}return{state:{...e,pos:n,data:g??a},applicationLayer:u,linkLayer:m}}function pr(e){return e.data[e.pos]===oe}function mt(e){if(!pr(e))return{state:e,authenticationAndFragmentationLayer:void 0};let t=e.data,r=e.pos,a=t[r++];if(a!==oe)throw new s("UNEXPECTED_STATE",`Wrong AFL CI: ${a}`);d.debug("Authentification and Fragmentation Layer");let n=t[r++],c=t.readUInt16LE(r);r+=2;let l={mf:(c&16384)!==0,mclp:(c&8192)!==0,mlp:(c&4096)!==0,mcrp:(c&2048)!==0,macp:(c&1024)!==0,kip:(c&512)!==0,fid:c&255},u,m;l.mclp&&(u=t[r++],m={mlmp:(u&64)!==0,mcmp:(u&32)!==0,kimp:(u&16)!==0,at:u&15});let g,x;l.kip&&(g=t.readUInt16LE(r),r+=2,x={keyVersion:(g&65280)>>8,kdfSelection:(g&48)>>4,keyId:g&15});let F;l.mcrp&&(F=t.readUInt32LE(r),r+=4);let V;if(l.macp){if(m===void 0)throw new s("UNEXPECTED_STATE","AFL MAC should be present but MCL is missing");let A=0;m.at==4?A=4:m.at==5?A=8:m.at==6?A=12:m.at==7&&(A=16),V=Buffer.from(t.subarray(r,r+A)),r+=A,d.debug(`AFL MAC ${V.toString("hex")}`)}let S;if(l.mlp&&(S=t.readUInt16LE(r),r+=2),l.mf)throw new s("UNIMPLEMENTED_FEATURE","Fragmented messages are not supported yet.");return{state:{...e,pos:r},authenticationAndFragmentationLayer:{ci:a,afll:n,fclRaw:c,fcl:l,mclRaw:u,mcl:m,kiRaw:g,ki:x,mcr:F,mac:V,ml:S}}}var E={Number:"Number",BigInt:"BigInt",String:"String",Date:"Date",DateTime:"DateTime",Buffer:"Buffer",Null:"Null"},T={Default:"Default",FD:"FD",FB:"FB",Plain:"Plain",Manufacturer:"Manufacturer"};function ur(e,t){let r={tariff:0,deviceUnit:0,storageNo:0,functionField:0,dataField:0},a=e[t++],n=0;for(r.storageNo=(a&64)>>6,r.functionField=(a&48)>>4,r.dataField=a&15;a&J;){if(t>=e.length)throw new s("UNEXPECTED_STATE","No data but DIF extension bit still set!");if(a=e[t++],n>9)throw new s("UNEXPECTED_STATE","Too many DIF extensions!");r.storageNo|=(a&15)<<n*4+1,r.tariff|=(a&48)>>4<<n*2,r.deviceUnit|=(a&64)>>6<<n,n++}return{newPos:t,dib:r}}function Q(e,t,r){return t===T.Plain?{vif:e&re,table:T.Plain,plainText:r,extensionBitSet:(e&J)!==0}:{vif:e&re,table:t,extensionBitSet:(e&J)!==0}}function sr(e,t){let r=e[t++];if(r==251){let a=e[t++];return{newPos:t,primary:Q(a,T.FB)}}else if(r==253){let a=e[t++];return{newPos:t,primary:Q(a,T.FD)}}else if(r==255){let a=e[t++];return{newPos:t,primary:Q(a,T.Manufacturer)}}else if(r==124||r==252){let a=e[t++];if(a+t>=e.length)throw new s("UNEXPECTED_STATE","Not enough bytes left for plain text VIF!");let n=e.toString("ascii",t,t+a).split("").reverse().join("");return t+=a,{newPos:t,primary:Q(r,T.Plain,n)}}else return{newPos:t,primary:Q(r,T.Default)}}function fr(e,t){let r=sr(e,t),a=r.newPos,n=r.primary.extensionBitSet,c={primary:r.primary,extensions:[]};for(;n;){if(c.extensions.length>11)throw new s("UNEXPECTED_STATE","Too many VIF extensions!");if(a+1>=e.length)throw new s("UNEXPECTED_STATE","No data left but VIF extension bit still set!");let l=e[a++];c.extensions.push(l&re),n=(l&J)!==0}return{newPos:a,vib:c}}function mr(e,t){let r=t,{dib:a,newPos:n}=ur(e,t);if(a.dataField===ke)return d.debug("DIF_SPECIAL_FUNCTIONS found"),n<e.length&&d.debug(`at position ${n} - remaining data: ${e.toString("hex",n)}`),{newPos:e.length,dataRecordHeader:null};let{vib:c,newPos:l}=fr(e,n);return{newPos:l,dataRecordHeader:{dib:a,vib:c,offset:r,length:l-r}}}function yt(e,t,r){switch(r.dib.dataField){case be:return d.debug("DIF_NONE found!"),{newPos:t+1,value:null};case Se:return d.debug("DIF_READOUT found!"),{newPos:t+1,value:null};case Me:return{newPos:t+1,value:P(2,e,t)};case Ue:return{newPos:t+2,value:P(4,e,t)};case he:return{newPos:t+3,value:P(6,e,t)};case we:return{newPos:t+4,value:P(8,e,t)};case Be:return{newPos:t+6,value:P(12,e,t)};case Ce:return{newPos:t+1,value:e.readInt8(t)};case _:return{newPos:t+2,value:e.readUInt16LE(t)};case b:return{newPos:t+3,value:e.readUIntLE(t,3)};case M:return{newPos:t+4,value:e.readUInt32LE(t)};case W:return{newPos:t+6,value:e.readUIntLE(t,6)};case Oe:return{newPos:t+8,value:e.readBigUInt64LE(t)};case Pe:return{newPos:t+4,value:e.readFloatLE(t)};case U:return yr(e,t);default:throw new s("UNIMPLEMENTED_FEATURE",`Unknown DataInformationBlock.dataField type: 0x${r.dib.dataField.toString(16)}`)}}function yr(e,t){let r=e[t++];if(r<=191){let a=e.toString("ascii",t,t+r).split("").reverse().join("");return{newPos:t+=r,value:a}}else if(r<=207){let a=r-192;return{newPos:t+a,value:P(a*2,e,t)}}else if(r<=223){let a=r-208;return{newPos:t+a,value:-1*P(a*2,e,t)}}else if(r<=239){let a=r-224;if(a<=6)return{newPos:t+a,value:e.readIntLE(t,a)};{let n=e.toString("hex",t,t+a);return{newPos:t+a,value:n}}}else{if(r===248)return{newPos:t+8,value:e.readDoubleLE(t)};throw new s("UNIMPLEMENTED_FEATURE",`Unhandled LVAR field 0x${r.toString(16)}`)}}function dr(e,t){let{newPos:r,dataRecordHeader:a}=mr(e,t);if(a===null)return{newPos:r,dr:null};let{newPos:n,value:c}=yt(e,r,a);return{newPos:n,dr:{header:a,value:c}}}function Er(e,t){let r=[],a=e.pos,n=e.data;for(let c of t){let{newPos:l,value:u}=yt(n,a,c);a=l,r.push({header:c,value:u})}return{state:{pos:a,data:n},dataRecords:r}}function dt(e,t){let r=Buffer.alloc(0);for(let a of e){let n=a.header.offset;r=Buffer.concat([r,t.subarray(n,n+a.header.length)])}return te(r,0,r.length)}function Et(e){return e.map(t=>{let r=t.header;return{dib:{...r.dib},vib:{primary:{...r.vib.primary},extensions:[...r.vib.extensions]},offset:r.offset,length:r.length}})}function se(e,t){if(t!==void 0)return Er(e,t);let r=e.data,a=e.pos,n=[];e:for(;a<r.length;){for(;r[a]===ve;)if(a++,a>=r.length)break e;let{newPos:c,dr:l}=dr(r,a);if(l===null)break e;a=c,n.push(l)}return{state:{pos:a,data:r},dataRecords:n}}function p(e,t){if(typeof e=="number")return e*t;if(typeof e=="bigint")return e*BigInt(t);throw new s("UNEXPECTED_STATE","Wrong data types!")}function ee(e,t){if(typeof e=="number")return e+t;if(typeof e=="bigint")return e+BigInt(t);throw new s("UNEXPECTED_STATE","Wrong data types!")}function o(e,t){if(typeof e=="number")return e/t;if(typeof e=="bigint")return Number(e*BigInt(t)/BigInt(t))/t;throw new s("UNEXPECTED_STATE","Unexpected type!")}function xt(e){return e===null?"<null>":Buffer.isBuffer(e)?e.toString("hex"):e.toString()}function _t(e,t){switch(t.header.dib.functionField){case Re:return e;case De:return e.description+=" (maximum value)",e;case Ve:return e.description+=" (minimum value)",e;case Ae:return e.description+=" (during error state)",e;default:return e}}function O(e,t){return{legacyVif:e.legacyName,tariff:t.header.dib.tariff,deviceUnit:t.header.dib.deviceUnit,storageNo:t.header.dib.storageNo}}function R(e,t){return{value:xt(t.value),unit:e.unit,description:e.description,type:E.String,info:O(e,t)}}function I(e,t){if(typeof t.value!="number"&&typeof t.value!="bigint")return{value:xt(t.value),unit:e.unit,description:e.description,type:E.String,info:O(e,t)};let r=typeof t.value=="bigint"?E.BigInt:E.Number;return{value:t.value,unit:e.unit,description:e.description,type:r,info:O(e,t)}}function i(e,t){let r=typeof t.value=="bigint"?E.BigInt:E.Number;return{value:e.calc(t.value),unit:e.unit,description:e.description,type:r,info:O(e,t)}}function It(e,t){if(typeof t.value!="number")throw new s("UNEXPECTED_STATE","Unexpected type!");return t.header.dib.dataField===_?fe(e,t):H(e,t)}function fe(e,t){if(typeof t.value!="number")throw new s("UNEXPECTED_STATE","Unexpected type!");return{value:$e(t.value),unit:"",description:e.description,type:E.Date,info:O(e,t)}}function H(e,t){if(typeof t.value!="number")throw new s("UNEXPECTED_STATE","Unexpected type!");return{value:t.header.dib.dataField===W?Je(t.value):Ke(t.value),unit:"",description:e.description,type:E.DateTime,info:O(e,t)}}function D(e,t){if(Buffer.isBuffer(t.value))return{value:t.value,unit:e.unit,description:e.description,type:E.Buffer,info:O(e,t)};if(typeof t.value!="number"&&typeof t.value!="bigint")throw new s("UNEXPECTED_STATE","Unexpected type");let r=typeof t.value=="bigint"?E.BigInt:E.Number;return{value:t.value,unit:e.unit,description:e.description,type:r,info:O(e,t)}}function gt(e,t,r){return r}function N(e,t,r){if(r.type!==E.BigInt&&r.type!==E.Number)throw new s("UNEXPECTED_STATE","Unexpected type");return r.value=e.calc(r.value),r}function f(e,t,r){if(e.description===void 0)throw new s("UNEXPECTED_STATE","Description is missing!");return r.description+=`; ${e.description}`,r}function L(e,t,r){if(e.description===void 0)throw new s("UNEXPECTED_STATE","Description is missing!");let a=t.header.dib.dataField===_?e.description.replace("date or time","date"):e.description.replace("date or time","time");return r.description+=`; ${a}`,r}function y(e,t,r){if(e.unit===void 0)throw new s("UNEXPECTED_STATE","Unit is missing!");return r.unit+=` ${e.unit}`,r}var Tt=[{vif:0,legacyName:"VIF_ENERGY_WATT",unit:"Wh",description:"Energy",calc:e=>o(e,1e3),apply:i},{vif:1,legacyName:"VIF_ENERGY_WATT",unit:"Wh",description:"Energy",calc:e=>o(e,100),apply:i},{vif:2,legacyName:"VIF_ENERGY_WATT",unit:"Wh",description:"Energy",calc:e=>o(e,10),apply:i},{vif:3,legacyName:"VIF_ENERGY_WATT",unit:"Wh",description:"Energy",calc:e=>e,apply:i},{vif:4,legacyName:"VIF_ENERGY_WATT",unit:"Wh",description:"Energy",calc:e=>p(e,10),apply:i},{vif:5,legacyName:"VIF_ENERGY_WATT",unit:"Wh",description:"Energy",calc:e=>p(e,100),apply:i},{vif:6,legacyName:"VIF_ENERGY_WATT",unit:"Wh",description:"Energy",calc:e=>p(e,1e3),apply:i},{vif:7,legacyName:"VIF_ENERGY_WATT",unit:"Wh",description:"Energy",calc:e=>p(e,1e4),apply:i},{vif:8,legacyName:"VIF_ENERGY_JOULE",unit:"J",description:"Energy",calc:e=>e,apply:i},{vif:9,legacyName:"VIF_ENERGY_JOULE",unit:"J",description:"Energy",calc:e=>p(e,10),apply:i},{vif:10,legacyName:"VIF_ENERGY_JOULE",unit:"J",description:"Energy",calc:e=>p(e,100),apply:i},{vif:11,legacyName:"VIF_ENERGY_JOULE",unit:"J",description:"Energy",calc:e=>p(e,1e3),apply:i},{vif:12,legacyName:"VIF_ENERGY_JOULE",unit:"J",description:"Energy",calc:e=>p(e,1e4),apply:i},{vif:13,legacyName:"VIF_ENERGY_JOULE",unit:"J",description:"Energy",calc:e=>p(e,1e5),apply:i},{vif:14,legacyName:"VIF_ENERGY_JOULE",unit:"J",description:"Energy",calc:e=>p(e,1e6),apply:i},{vif:15,legacyName:"VIF_ENERGY_JOULE",unit:"J",description:"Energy",calc:e=>p(e,1e7),apply:i},{vif:16,legacyName:"VIF_VOLUME",unit:"m\xB3",description:"Volume",calc:e=>o(e,1e6),apply:i},{vif:17,legacyName:"VIF_VOLUME",unit:"m\xB3",description:"Volume",calc:e=>o(e,1e5),apply:i},{vif:18,legacyName:"VIF_VOLUME",unit:"m\xB3",description:"Volume",calc:e=>o(e,1e4),apply:i},{vif:19,legacyName:"VIF_VOLUME",unit:"m\xB3",description:"Volume",calc:e=>o(e,1e3),apply:i},{vif:20,legacyName:"VIF_VOLUME",unit:"m\xB3",description:"Volume",calc:e=>o(e,100),apply:i},{vif:21,legacyName:"VIF_VOLUME",unit:"m\xB3",description:"Volume",calc:e=>o(e,10),apply:i},{vif:22,legacyName:"VIF_VOLUME",unit:"m\xB3",description:"Volume",calc:e=>e,apply:i},{vif:23,legacyName:"VIF_VOLUME",unit:"m\xB3",description:"Volume",calc:e=>p(e,10),apply:i},{vif:24,legacyName:"VIF_MASS",unit:"kg",description:"Mass",calc:e=>o(e,1e3),apply:i},{vif:25,legacyName:"VIF_MASS",unit:"kg",description:"Mass",calc:e=>o(e,100),apply:i},{vif:26,legacyName:"VIF_MASS",unit:"kg",description:"Mass",calc:e=>o(e,10),apply:i},{vif:27,legacyName:"VIF_MASS",unit:"kg",description:"Mass",calc:e=>e,apply:i},{vif:28,legacyName:"VIF_MASS",unit:"kg",description:"Mass",calc:e=>p(e,10),apply:i},{vif:29,legacyName:"VIF_MASS",unit:"kg",description:"Mass",calc:e=>p(e,100),apply:i},{vif:30,legacyName:"VIF_MASS",unit:"kg",description:"Mass",calc:e=>p(e,1e3),apply:i},{vif:31,legacyName:"VIF_MASS",unit:"kg",description:"Mass",calc:e=>p(e,1e4),apply:i},{vif:32,legacyName:"VIF_ON_TIME",unit:"s",description:"On Time",calc:e=>e,apply:i},{vif:33,legacyName:"VIF_ON_TIME",unit:"min",description:"On Time",calc:e=>e,apply:i},{vif:34,legacyName:"VIF_ON_TIME",unit:"h",description:"On Time",calc:e=>e,apply:i},{vif:35,legacyName:"VIF_ON_TIME",unit:"d",description:"On Time",calc:e=>e,apply:i},{vif:36,legacyName:"VIF_OP_TIME",unit:"s",description:"Operating Time",calc:e=>e,apply:i},{vif:37,legacyName:"VIF_OP_TIME",unit:"min",description:"Operating Time",calc:e=>e,apply:i},{vif:38,legacyName:"VIF_OP_TIME",unit:"h",description:"Operating Time",calc:e=>e,apply:i},{vif:39,legacyName:"VIF_OP_TIME",unit:"d",description:"Operating Time",calc:e=>e,apply:i},{vif:40,legacyName:"VIF_ELECTRIC_POWER",unit:"W",description:"Power",calc:e=>o(e,1e3),apply:i},{vif:41,legacyName:"VIF_ELECTRIC_POWER",unit:"W",description:"Power",calc:e=>o(e,100),apply:i},{vif:42,legacyName:"VIF_ELECTRIC_POWER",unit:"W",description:"Power",calc:e=>o(e,10),apply:i},{vif:43,legacyName:"VIF_ELECTRIC_POWER",unit:"W",description:"Power",calc:e=>e,apply:i},{vif:44,legacyName:"VIF_ELECTRIC_POWER",unit:"W",description:"Power",calc:e=>p(e,10),apply:i},{vif:45,legacyName:"VIF_ELECTRIC_POWER",unit:"W",description:"Power",calc:e=>p(e,100),apply:i},{vif:46,legacyName:"VIF_ELECTRIC_POWER",unit:"W",description:"Power",calc:e=>p(e,1e3),apply:i},{vif:47,legacyName:"VIF_ELECTRIC_POWER",unit:"W",description:"Power",calc:e=>p(e,1e4),apply:i},{vif:48,legacyName:"VIF_THERMAL_POWER",unit:"J/h",description:"Power",calc:e=>e,apply:i},{vif:49,legacyName:"VIF_THERMAL_POWER",unit:"J/h",description:"Power",calc:e=>p(e,10),apply:i},{vif:50,legacyName:"VIF_THERMAL_POWER",unit:"J/h",description:"Power",calc:e=>p(e,100),apply:i},{vif:51,legacyName:"VIF_THERMAL_POWER",unit:"J/h",description:"Power",calc:e=>p(e,1e3),apply:i},{vif:52,legacyName:"VIF_THERMAL_POWER",unit:"J/h",description:"Power",calc:e=>p(e,1e4),apply:i},{vif:53,legacyName:"VIF_THERMAL_POWER",unit:"J/h",description:"Power",calc:e=>p(e,1e5),apply:i},{vif:54,legacyName:"VIF_THERMAL_POWER",unit:"J/h",description:"Power",calc:e=>p(e,1e6),apply:i},{vif:55,legacyName:"VIF_THERMAL_POWER",unit:"J/h",description:"Power",calc:e=>p(e,1e7),apply:i},{vif:56,legacyName:"VIF_VOLUME_FLOW",unit:"m\xB3/h",description:"Volume Flow",calc:e=>o(e,1e6),apply:i},{vif:57,legacyName:"VIF_VOLUME_FLOW",unit:"m\xB3/h",description:"Volume Flow",calc:e=>o(e,1e5),apply:i},{vif:58,legacyName:"VIF_VOLUME_FLOW",unit:"m\xB3/h",description:"Volume Flow",calc:e=>o(e,1e4),apply:i},{vif:59,legacyName:"VIF_VOLUME_FLOW",unit:"m\xB3/h",description:"Volume Flow",calc:e=>o(e,1e3),apply:i},{vif:60,legacyName:"VIF_VOLUME_FLOW",unit:"m\xB3/h",description:"Volume Flow",calc:e=>o(e,100),apply:i},{vif:61,legacyName:"VIF_VOLUME_FLOW",unit:"m\xB3/h",description:"Volume Flow",calc:e=>o(e,10),apply:i},{vif:62,legacyName:"VIF_VOLUME_FLOW",unit:"m\xB3/h",description:"Volume Flow",calc:e=>p(e,1),apply:i},{vif:63,legacyName:"VIF_VOLUME_FLOW",unit:"m\xB3/h",description:"Volume Flow",calc:e=>p(e,10),apply:i},{vif:64,legacyName:"VIF_VOLUME_FLOW_EXT1",unit:"m\xB3/min",description:"Volume Flow ext.",calc:e=>o(e,1e7),apply:i},{vif:65,legacyName:"VIF_VOLUME_FLOW_EXT1",unit:"m\xB3/min",description:"Volume Flow ext.",calc:e=>o(e,1e6),apply:i},{vif:66,legacyName:"VIF_VOLUME_FLOW_EXT1",unit:"m\xB3/min",description:"Volume Flow ext.",calc:e=>o(e,1e5),apply:i},{vif:67,legacyName:"VIF_VOLUME_FLOW_EXT1",unit:"m\xB3/min",description:"Volume Flow ext.",calc:e=>o(e,1e4),apply:i},{vif:68,legacyName:"VIF_VOLUME_FLOW_EXT1",unit:"m\xB3/min",description:"Volume Flow ext.",calc:e=>o(e,1e3),apply:i},{vif:69,legacyName:"VIF_VOLUME_FLOW_EXT1",unit:"m\xB3/min",description:"Volume Flow ext.",calc:e=>o(e,100),apply:i},{vif:70,legacyName:"VIF_VOLUME_FLOW_EXT1",unit:"m\xB3/min",description:"Volume Flow ext.",calc:e=>o(e,10),apply:i},{vif:71,legacyName:"VIF_VOLUME_FLOW_EXT1",unit:"m\xB3/min",description:"Volume Flow ext.",calc:e=>e,apply:i},{vif:72,legacyName:"VIF_VOLUME_FLOW_EXT2",unit:"m\xB3/s",description:"Volume Flow ext.",calc:e=>o(e,1e9),apply:i},{vif:73,legacyName:"VIF_VOLUME_FLOW_EXT2",unit:"m\xB3/s",description:"Volume Flow ext.",calc:e=>o(e,1e8),apply:i},{vif:74,legacyName:"VIF_VOLUME_FLOW_EXT2",unit:"m\xB3/s",description:"Volume Flow ext.",calc:e=>o(e,1e7),apply:i},{vif:75,legacyName:"VIF_VOLUME_FLOW_EXT2",unit:"m\xB3/s",description:"Volume Flow ext.",calc:e=>o(e,1e6),apply:i},{vif:76,legacyName:"VIF_VOLUME_FLOW_EXT2",unit:"m\xB3/s",description:"Volume Flow ext.",calc:e=>o(e,1e5),apply:i},{vif:77,legacyName:"VIF_VOLUME_FLOW_EXT2",unit:"m\xB3/s",description:"Volume Flow ext.",calc:e=>o(e,1e4),apply:i},{vif:78,legacyName:"VIF_VOLUME_FLOW_EXT2",unit:"m\xB3/s",description:"Volume Flow ext.",calc:e=>o(e,1e3),apply:i},{vif:79,legacyName:"VIF_VOLUME_FLOW_EXT2",unit:"m\xB3/s",description:"Volume Flow ext.",calc:e=>o(e,100),apply:i},{vif:80,legacyName:"VIF_MASS_FLOW",unit:"kg/h",description:"Mass Flow",calc:e=>o(e,1e3),apply:i},{vif:81,legacyName:"VIF_MASS_FLOW",unit:"kg/h",description:"Mass Flow",calc:e=>o(e,100),apply:i},{vif:82,legacyName:"VIF_MASS_FLOW",unit:"kg/h",description:"Mass Flow",calc:e=>o(e,10),apply:i},{vif:83,legacyName:"VIF_MASS_FLOW",unit:"kg/h",description:"Mass Flow",calc:e=>e,apply:i},{vif:84,legacyName:"VIF_MASS_FLOW",unit:"kg/h",description:"Mass Flow",calc:e=>p(e,10),apply:i},{vif:85,legacyName:"VIF_MASS_FLOW",unit:"kg/h",description:"Mass Flow",calc:e=>p(e,100),apply:i},{vif:86,legacyName:"VIF_MASS_FLOW",unit:"kg/h",description:"Mass Flow",calc:e=>p(e,1e3),apply:i},{vif:87,legacyName:"VIF_MASS_FLOW",unit:"kg/h",description:"Mass Flow",calc:e=>p(e,1e4),apply:i},{vif:88,legacyName:"VIF_FLOW_TEMP",unit:"\xB0C",description:"Flow Temperature",calc:e=>o(e,1e3),apply:i},{vif:89,legacyName:"VIF_FLOW_TEMP",unit:"\xB0C",description:"Flow Temperature",calc:e=>o(e,100),apply:i},{vif:90,legacyName:"VIF_FLOW_TEMP",unit:"\xB0C",description:"Flow Temperature",calc:e=>o(e,10),apply:i},{vif:91,legacyName:"VIF_FLOW_TEMP",unit:"\xB0C",description:"Flow Temperature",calc:e=>e,apply:i},{vif:92,legacyName:"VIF_RETURN_TEMP",unit:"\xB0C",description:"Return Temperature",calc:e=>o(e,1e3),apply:i},{vif:93,legacyName:"VIF_RETURN_TEMP",unit:"\xB0C",description:"Return Temperature",calc:e=>o(e,100),apply:i},{vif:94,legacyName:"VIF_RETURN_TEMP",unit:"\xB0C",description:"Return Temperature",calc:e=>o(e,10),apply:i},{vif:95,legacyName:"VIF_RETURN_TEMP",unit:"\xB0C",description:"Return Temperature",calc:e=>e,apply:i},{vif:96,legacyName:"VIF_TEMP_DIFF",unit:"K",description:"Temperature Difference",calc:e=>o(e,1e3),apply:i},{vif:97,legacyName:"VIF_TEMP_DIFF",unit:"K",description:"Temperature Difference",calc:e=>o(e,100),apply:i},{vif:98,legacyName:"VIF_TEMP_DIFF",unit:"K",description:"Temperature Difference",calc:e=>o(e,10),apply:i},{vif:99,legacyName:"VIF_TEMP_DIFF",unit:"K",description:"Temperature Difference",calc:e=>e,apply:i},{vif:100,legacyName:"VIF_EXTERNAL_TEMP",unit:"\xB0C",description:"External Temperature",calc:e=>o(e,1e3),apply:i},{vif:101,legacyName:"VIF_EXTERNAL_TEMP",unit:"\xB0C",description:"External Temperature",calc:e=>o(e,100),apply:i},{vif:102,legacyName:"VIF_EXTERNAL_TEMP",unit:"\xB0C",description:"External Temperature",calc:e=>o(e,10),apply:i},{vif:103,legacyName:"VIF_EXTERNAL_TEMP",unit:"\xB0C",description:"External Temperature",calc:e=>p(e,1),apply:i},{vif:104,legacyName:"VIF_PRESSURE",unit:"bar",description:"Pressure",calc:e=>o(e,1e3),apply:i},{vif:105,legacyName:"VIF_PRESSURE",unit:"bar",description:"Pressure",calc:e=>o(e,100),apply:i},{vif:106,legacyName:"VIF_PRESSURE",unit:"bar",description:"Pressure",calc:e=>o(e,10),apply:i},{vif:107,legacyName:"VIF_PRESSURE",unit:"bar",description:"Pressure",calc:e=>p(e,1),apply:i},{vif:108,legacyName:"VIF_TIME_POINT_DATE",unit:"",description:"Time point",calc:e=>e,apply:fe},{vif:109,legacyName:"VIF_TIME_POINT_DATE_TIME",unit:"",description:"Time point",calc:e=>e,apply:H},{vif:110,legacyName:"VIF_HCA",unit:"",description:"Units for H.C.A.",calc:e=>e,apply:i},{vif:112,legacyName:"VIF_AVERAGING_DURATION",unit:"s",description:"Averaging Duration",calc:e=>e,apply:i},{vif:113,legacyName:"VIF_AVERAGING_DURATION",unit:"min",description:"Averaging Duration",calc:e=>e,apply:i},{vif:114,legacyName:"VIF_AVERAGING_DURATION",unit:"h",description:"Averaging Duration",calc:e=>e,apply:i},{vif:115,legacyName:"VIF_AVERAGING_DURATION",unit:"d",description:"Averaging Duration",calc:e=>e,apply:i},{vif:116,legacyName:"VIF_ACTUALITY_DURATION",unit:"s",description:"Actuality Duration",calc:e=>e,apply:i},{vif:117,legacyName:"VIF_ACTUALITY_DURATION",unit:"min",description:"Actuality Duration",calc:e=>e,apply:i},{vif:118,legacyName:"VIF_ACTUALITY_DURATION",unit:"h",description:"Actuality Duration",calc:e=>e,apply:i},{vif:119,legacyName:"VIF_ACTUALITY_DURATION",unit:"d",description:"Actuality Duration",calc:e=>e,apply:i},{vif:120,legacyName:"VIF_FABRICATION_NO",unit:"",description:"Fabrication No",calc:e=>e,apply:I},{vif:121,legacyName:"VIF_OWNER_NO",unit:"",description:"Owner No",calc:e=>e,apply:I},{vif:122,legacyName:"VIF_BUS_ADDRESS",unit:"",description:"Bus Address",calc:e=>e,apply:I}];var Ft=[{vif:0,legacyName:"VIF_ENERGY_MWH",unit:"MWh",description:"Energy",calc:e=>o(e,10),apply:i},{vif:1,legacyName:"VIF_ENERGY_MWH",unit:"MWh",description:"Energy",calc:e=>e,apply:i},{vif:8,legacyName:"VIF_ENERGY_GJ",unit:"GJ",description:"Energy",calc:e=>o(e,10),apply:i},{vif:9,legacyName:"VIF_ENERGY_GJ",unit:"GJ",description:"Energy",calc:e=>e,apply:i},{vif:16,legacyName:"VIF_VOLUME_CM",unit:"m\xB3",description:"Volume",calc:e=>p(e,100),apply:i},{vif:17,legacyName:"VIF_VOLUME_CM",unit:"m\xB3",description:"Volume",calc:e=>p(e,1e3),apply:i},{vif:24,legacyName:"VIF_MASS_T",unit:"t",description:"Mass",calc:e=>p(e,100),apply:i},{vif:25,legacyName:"VIF_MASS_T",unit:"t",description:"Mass",calc:e=>p(e,1e3),apply:i},{vif:33,legacyName:"VIF_VOLUME_CFEET",unit:"ft\xB3",description:"Volume",calc:e=>o(e,10),apply:i},{vif:34,legacyName:"VIF_VOLUME_GALLON",unit:"gal",description:"Volume",calc:e=>o(e,10),apply:i},{vif:35,legacyName:"VIF_VOLUME_GALLON_L",unit:"gal",description:"Volume",calc:e=>e,apply:i},{vif:36,legacyName:"VIF_VOLUME_FLOW_GALLON_L",unit:"gal/min",description:"Volume flow",calc:e=>o(e,1e3),apply:i},{vif:37,legacyName:"VIF_VOLUME_FLOW_GALLON",unit:"gal/min",description:"Volume flow",calc:e=>e,apply:i},{vif:38,legacyName:"VIF_VOLUME_FLOW_GALLON_H",unit:"gal/h",description:"Volume flow",calc:e=>e,apply:i},{vif:40,legacyName:"VIF_POWER_MW",unit:"MW",description:"Power",calc:e=>o(e,10),apply:i},{vif:41,legacyName:"VIF_POWER_MW",unit:"MW",description:"Power",calc:e=>e,apply:i},{vif:48,legacyName:"VIF_POWER_GJH",unit:"GJ/h",description:"Power",calc:e=>o(e,10),apply:i},{vif:49,legacyName:"VIF_POWER_GJH",unit:"GJ/h",description:"Power",calc:e=>e,apply:i},{vif:88,legacyName:"VIF_TEMPERATURE_FLOW_F",unit:"\xB0F",description:"Flow Temperature",calc:e=>o(e,1e3),apply:i},{vif:89,legacyName:"VIF_TEMPERATURE_FLOW_F",unit:"\xB0F",description:"Flow Temperature",calc:e=>o(e,100),apply:i},{vif:90,legacyName:"VIF_TEMPERATURE_FLOW_F",unit:"\xB0F",description:"Flow Temperature",calc:e=>o(e,10),apply:i},{vif:91,legacyName:"VIF_TEMPERATURE_FLOW_F",unit:"\xB0F",description:"Flow Temperature",calc:e=>e,apply:i},{vif:92,legacyName:"VIF_TEMPERATURE_RETURN_F",unit:"\xB0F",description:"Return Temperature",calc:e=>o(e,1e3),apply:i},{vif:93,legacyName:"VIF_TEMPERATURE_RETURN_F",unit:"\xB0F",description:"Return Temperature",calc:e=>o(e,100),apply:i},{vif:94,legacyName:"VIF_TEMPERATURE_RETURN_F",unit:"\xB0F",description:"Return Temperature",calc:e=>o(e,10),apply:i},{vif:95,legacyName:"VIF_TEMPERATURE_RETURN_F",unit:"\xB0F",description:"Return Temperature",calc:e=>e,apply:i},{vif:96,legacyName:"VIF_TEMPERATURE_DIFF_F",unit:"\xB0F",description:"Temperature Difference",calc:e=>o(e,1e3),apply:i},{vif:97,legacyName:"VIF_TEMPERATURE_DIFF_F",unit:"\xB0F",description:"Temperature Difference",calc:e=>o(e,100),apply:i},{vif:98,legacyName:"VIF_TEMPERATURE_DIFF_F",unit:"\xB0F",description:"Temperature Difference",calc:e=>o(e,10),apply:i},{vif:99,legacyName:"VIF_TEMPERATURE_DIFF_F",unit:"\xB0F",description:"Temperature Difference",calc:e=>e,apply:i},{vif:100,legacyName:"VIF_TEMPERATURE_EXT_F",unit:"\xB0F",description:"External Temperature",calc:e=>o(e,1e3),apply:i},{vif:101,legacyName:"VIF_TEMPERATURE_EXT_F",unit:"\xB0F",description:"External Temperature",calc:e=>o(e,100),apply:i},{vif:102,legacyName:"VIF_TEMPERATURE_EXT_F",unit:"\xB0F",description:"External Temperature",calc:e=>o(e,10),apply:i},{vif:103,legacyName:"VIF_TEMPERATURE_EXT_F",unit:"\xB0F",description:"External Temperature",calc:e=>e,apply:i},{vif:112,legacyName:"VIF_COLD_WARM_LIMIT_F",unit:"\xB0F",description:"Cold / Warm Temperature Limit",calc:e=>o(e,1e3),apply:i},{vif:113,legacyName:"VIF_COLD_WARM_LIMIT_F",unit:"\xB0F",description:"Cold / Warm Temperature Limit",calc:e=>o(e,100),apply:i},{vif:114,legacyName:"VIF_COLD_WARM_LIMIT_F",unit:"\xB0F",description:"Cold / Warm Temperature Limit",calc:e=>o(e,10),apply:i},{vif:115,legacyName:"VIF_COLD_WARM_LIMIT_F",unit:"\xB0F",description:"Cold / Warm Temperature Limit",calc:e=>e,apply:i},{vif:116,legacyName:"VIF_COLD_WARM_LIMIT_C",unit:"\xB0C",description:"Cold / Warm Temperature Limit",calc:e=>o(e,1e3),apply:i},{vif:117,legacyName:"VIF_COLD_WARM_LIMIT_C",unit:"\xB0C",description:"Cold / Warm Temperature Limit",calc:e=>o(e,100),apply:i},{vif:118,legacyName:"VIF_COLD_WARM_LIMIT_C",unit:"\xB0C",description:"Cold / Warm Temperature Limit",calc:e=>o(e,10),apply:i},{vif:119,legacyName:"VIF_COLD_WARM_LIMIT_C",unit:"\xB0C",description:"Cold / Warm Temperature Limit",calc:e=>e,apply:i},{vif:120,legacyName:"VIF_CUMUL_COUNT_MAX_POWER",unit:"W",description:"cumul. count max power",calc:e=>o(e,1e3),apply:i},{vif:121,legacyName:"VIF_CUMUL_COUNT_MAX_POWER",unit:"W",description:"cumul. count max power",calc:e=>o(e,100),apply:i},{vif:122,legacyName:"VIF_CUMUL_COUNT_MAX_POWER",unit:"W",description:"cumul. count max power",calc:e=>o(e,10),apply:i},{vif:123,legacyName:"VIF_CUMUL_COUNT_MAX_POWER",unit:"W",description:"cumul. count max power",calc:e=>e,apply:i},{vif:124,legacyName:"VIF_CUMUL_COUNT_MAX_POWER",unit:"W",description:"cumul. count max power",calc:e=>p(e,10),apply:i},{vif:125,legacyName:"VIF_CUMUL_COUNT_MAX_POWER",unit:"W",description:"cumul. count max power",calc:e=>p(e,100),apply:i},{vif:126,legacyName:"VIF_CUMUL_COUNT_MAX_POWER",unit:"W",description:"cumul. count max power",calc:e=>p(e,1e3),apply:i},{vif:127,legacyName:"VIF_CUMUL_COUNT_MAX_POWER",unit:"W",description:"cumul. count max power",calc:e=>p(e,1e4),apply:i}];var Nt=[{vif:0,legacyName:"VIF_CREDIT",unit:"\u20AC",description:"Credit",calc:e=>o(e,1e3),apply:i},{vif:1,legacyName:"VIF_CREDIT",unit:"\u20AC",description:"Credit",calc:e=>o(e,100),apply:i},{vif:2,legacyName:"VIF_CREDIT",unit:"\u20AC",description:"Credit",calc:e=>o(e,10),apply:i},{vif:3,legacyName:"VIF_CREDIT",unit:"\u20AC",description:"Credit",calc:e=>e,apply:i},{vif:4,legacyName:"VIF_DEBIT",unit:"\u20AC",description:"Debit",calc:e=>o(e,1e3),apply:i},{vif:5,legacyName:"VIF_DEBIT",unit:"\u20AC",description:"Debit",calc:e=>o(e,100),apply:i},{vif:6,legacyName:"VIF_DEBIT",unit:"\u20AC",description:"Debit",calc:e=>o(e,10),apply:i},{vif:7,legacyName:"VIF_DEBIT",unit:"\u20AC",description:"Debit",calc:e=>e,apply:i},{vif:8,legacyName:"VIF_ACCESS_NO",unit:"",description:"Access number (transmission count)",calc:e=>e,apply:i},{vif:9,legacyName:"VIF_MEDIUM",unit:"",description:"Device type",calc:e=>e,apply:R},{vif:10,legacyName:"VIF_MANUFACTURER",unit:"",description:"Manufacturer",calc:e=>e,apply:R},{vif:11,legacyName:"VIF_PARAM_SET_IDENTIFICATION",unit:"",description:"Parameter set identification",calc:e=>e,apply:I},{vif:12,legacyName:"VIF_MODEL_VERSION",unit:"",description:"Model / Version",calc:e=>e,apply:I},{vif:13,legacyName:"VIF_HARDWARE_VERSION",unit:"",description:"Hardware version #",calc:e=>e,apply:I},{vif:14,legacyName:"VIF_FIRMWARE_VERSION",unit:"",description:"Firmware version #",calc:e=>e,apply:I},{vif:15,legacyName:"VIF_SOFTWARE_VERSION",unit:"",description:"Software version #",calc:e=>e,apply:I},{vif:16,legacyName:"VIF_CUSTOMER_LOCATION",unit:"",description:"Customer location",calc:e=>e,apply:I},{vif:17,legacyName:"VIF_CUSTOMER",unit:"",description:"Customer",calc:e=>e,apply:I},{vif:18,legacyName:"VIF_ACCESS_CODE_USER",unit:"",description:"Access Code User",calc:e=>e,apply:I},{vif:19,legacyName:"VIF_ACCESS_CODE_OPERATOR",unit:"",description:"Access Code Operator",calc:e=>e,apply:I},{vif:20,legacyName:"VIF_ACCESS_CODE_SYS_OPERATOR",unit:"",description:"Access Code Sytem Operator",calc:e=>e,apply:I},{vif:21,legacyName:"VIF_ACCESS_CODE_DEVELOPER",unit:"",description:"Access Code Developer",calc:e=>e,apply:I},{vif:22,legacyName:"VIF_PASSWORD",unit:"",description:"Password",calc:e=>e,apply:I},{vif:23,legacyName:"VIF_ERROR_FLAGS",unit:"",description:"Error flags (binary)",calc:e=>e,apply:D},{vif:24,legacyName:"VIF_ERROR_MASK",unit:"",description:"Error mask",calc:e=>e,apply:D},{vif:26,legacyName:"VIF_DIGITAL_OUTPUT_BINARY",unit:"",description:"Digital Output (binary)",calc:e=>e,apply:D},{vif:27,legacyName:"VIF_DIGITAL_INPUT_BINARY",unit:"",description:"Digital Input (binary) ",calc:e=>e,apply:D},{vif:28,legacyName:"VIF_BAUDRATE",unit:"baud",description:"Baudrate",calc:e=>e,apply:i},{vif:29,legacyName:"VIF_RESPONSE_DELAY_TIME",unit:"bittimes",description:"Response delay time",calc:e=>e,apply:i},{vif:30,legacyName:"VIF_RETRY",unit:"",description:"Retry",calc:e=>e,apply:D},{vif:32,legacyName:"VIF_FIRST_STORAGE_CYCLIC",unit:"",description:"First storage # for cyclic storage",calc:e=>e,apply:i},{vif:33,legacyName:"VIF_LAST_STORAGE_CYCLIC",unit:"",description:"Last storage # for cyclic storage",calc:e=>e,apply:i},{vif:34,legacyName:"VIF_SIZE_STORAGE_BLOCK",unit:"",description:"Size of storage block",calc:e=>e,apply:i},{vif:36,legacyName:"VIF_SIZE_STORAGE_INTERVAL",unit:"s",description:"Storage interval ",calc:e=>e,apply:i},{vif:37,legacyName:"VIF_SIZE_STORAGE_INTERVAL",unit:"min",description:"Storage interval ",calc:e=>e,apply:i},{vif:38,legacyName:"VIF_SIZE_STORAGE_INTERVAL",unit:"h",description:"Storage interval ",calc:e=>e,apply:i},{vif:39,legacyName:"VIF_SIZE_STORAGE_INTERVAL",unit:"d",description:"Storage interval ",calc:e=>e,apply:i},{vif:40,legacyName:"VIF_SIZE_STORAGE_INTERVAL",unit:"month",description:"Storage interval",calc:e=>e,apply:i},{vif:41,legacyName:"VIF_SIZE_STORAGE_INTERVAL",unit:"year",description:"Storage interval",calc:e=>e,apply:i},{vif:44,legacyName:"VIF_DURATION_SINCE_LAST_READ",unit:"s",description:"Duration since last readout",calc:e=>e,apply:i},{vif:45,legacyName:"VIF_DURATION_SINCE_LAST_READ",unit:"min",description:"Duration since last readout",calc:e=>e,apply:i},{vif:46,legacyName:"VIF_DURATION_SINCE_LAST_READ",unit:"h",description:"Duration since last readout",calc:e=>e,apply:i},{vif:47,legacyName:"VIF_DURATION_SINCE_LAST_READ",unit:"d",description:"Duration since last readout",calc:e=>e,apply:i},{vif:48,legacyName:"VIF_TARIFF_START",unit:"",description:"Start of tariff",calc:e=>e,apply:It},{vif:49,legacyName:"VIF_TARIFF_DURATION",unit:"min",description:"Duration of tariff",calc:e=>e,apply:i},{vif:50,legacyName:"VIF_TARIFF_DURATION",unit:"h",description:"Duration of tariff",calc:e=>e,apply:i},{vif:51,legacyName:"VIF_TARIFF_DURATION",unit:"d",description:"Duration of tariff",calc:e=>e,apply:i},{vif:52,legacyName:"VIF_TARIFF_PERIOD",unit:"s",description:"Period of tariff",calc:e=>e,apply:i},{vif:53,legacyName:"VIF_TARIFF_PERIOD",unit:"min",description:"Period of tariff",calc:e=>e,apply:i},{vif:54,legacyName:"VIF_TARIFF_PERIOD",unit:"h",description:"Period of tariff",calc:e=>e,apply:i},{vif:55,legacyName:"VIF_TARIFF_PERIOD",unit:"d",description:"Period of tariff",calc:e=>e,apply:i},{vif:56,legacyName:"VIF_TARIFF_PERIOD",unit:"months",description:"Period of tariff",calc:e=>e,apply:i},{vif:57,legacyName:"VIF_TARIFF_PERIOD",unit:"years",description:"Period of tariff",calc:e=>e,apply:i},{vif:58,legacyName:"VIF_DIMENSIONLESS",unit:"",description:"Dimensionless",calc:e=>e,apply:i},{vif:64,legacyName:"VIF_ELECTRICAL_VOLTAGE",unit:"V",description:"Voltage",calc:e=>o(e,1e9),apply:i},{vif:65,legacyName:"VIF_ELECTRICAL_VOLTAGE",unit:"V",description:"Voltage",calc:e=>o(e,1e8),apply:i},{vif:66,legacyName:"VIF_ELECTRICAL_VOLTAGE",unit:"V",description:"Voltage",calc:e=>o(e,1e7),apply:i},{vif:67,legacyName:"VIF_ELECTRICAL_VOLTAGE",unit:"V",description:"Voltage",calc:e=>o(e,1e6),apply:i},{vif:68,legacyName:"VIF_ELECTRICAL_VOLTAGE",unit:"V",description:"Voltage",calc:e=>o(e,1e5),apply:i},{vif:69,legacyNa