matter-history
Version:
Matterbridge history module
1 lines • 10.1 kB
JavaScript
import{db as a,dn as s,or as o,rs as n}from"matterbridge/logger";import{History as t}from"./history.js";import{EveHistory as r}from"./eveHistoryCluster.js";import{EveHistoryServer as i}from"./eveHistoryBehavior.js";class h extends t{constructor(t,e,r){super(t,e,r),this.log.debug(`Creating MatterHistory for ${o}${e}${a} with enableDebug: ${this.params.enableDebug} historySize: ${this.params.historySize} timerMinutes: ${this.params.timerMinutes} fileName: ${this.params.fileName} filePath: `+this.params.filePath)}static uint8ArrayFromHex(e){if(e.length%2!=0)throw new Error("Hex string must have an even length");var r=new Uint8Array(e.length/2);for(let t=0;t<e.length;t+=2)r[t/2]=parseInt(e.substr(t,2),16);return r}static uint8ArrayToHex(t){return Array.from(t).map(t=>t.toString(16).padStart(2,"0")).join("")}setMatterHystoryType(t,e){"door"===t?this.configData=Buffer.from("00014d0b0200000302fa19040c515632324d314d30313139329c0100ff04010207c4f90101","hex"):"motion"===t?this.configData=Buffer.from("0001590b0200000302fa19040c5a5633384c314131313937349c0100ff040102012cf901017001017102090072040000ffff","hex"):"energy"===t?this.configData=Buffer.from("0001500b0200000302fb19040c525631354d314d30363931389c0100ff04010200b4f90101600164d00400000000450505000000004609050000000e000042064906050c081080014411051c0005033c00000075df36428199de404711052a21fc2196226823780000003c00000048060500000000004a06050000000000","hex"):"weather"===t?(this.configData=Buffer.from("0b0200009c0100ff04010207c4f901014b043408d007","hex"),e&&this.setConfigDataTags(87,2996,e)):"room"===t?(this.configData=Buffer.from("0b0200009c0100ff04010207c4f901014b043408d007","hex"),e&&this.setConfigDataTags(39,1416,e)):(this.log.error(`MatterHistory: unknown type ${t} setting default to door type`),this.configData=Buffer.from("00014d0b0200000302fa19040c515632324d314d30313139329c0100ff04010207c4f90101","hex")),this.log.debug("*"+this.decodeConfigData(this.configData,this.tlvConfigDataGet))}createRoomEveHistoryClusterServer(t,e){this.setMatterHystoryType("room",t.serialNumber),t.behaviors.require(i.with(r.Feature.EveRoom),{configDataGet:h.uint8ArrayFromHex(""),configDataSet:h.uint8ArrayFromHex(""),historyStatus:h.uint8ArrayFromHex(""),historyEntries:h.uint8ArrayFromHex(""),historyRequest:h.uint8ArrayFromHex(""),historySetTime:h.uint8ArrayFromHex(""),lastEvent:0,resetTotal:0,temperatureDisplayUnits:0,rLoc:46080,history:this,historyType:"room"})}createWeatherEveHistoryClusterServer(t,e){this.setMatterHystoryType("weather",t.serialNumber),t.behaviors.require(i.with(r.Feature.EveWeather),{configDataGet:h.uint8ArrayFromHex(""),configDataSet:h.uint8ArrayFromHex(""),historyStatus:h.uint8ArrayFromHex(""),historyEntries:h.uint8ArrayFromHex(""),historyRequest:h.uint8ArrayFromHex(""),historySetTime:h.uint8ArrayFromHex(""),lastEvent:0,resetTotal:0,elevation:0,airPressure:1e3,weatherTrend:1,temperatureDisplayUnits:0,rLoc:46080,history:this,historyType:"weather"})}createEnergyEveHistoryClusterServer(t,e){this.setMatterHystoryType("energy"),t.behaviors.require(i.with(r.Feature.EveEnergy),{configDataGet:h.uint8ArrayFromHex(""),configDataSet:h.uint8ArrayFromHex(""),historyStatus:h.uint8ArrayFromHex(""),historyEntries:h.uint8ArrayFromHex(""),historyRequest:h.uint8ArrayFromHex(""),historySetTime:h.uint8ArrayFromHex(""),lastEvent:0,resetTotal:0,voltage:0,current:0,consumption:0,totalConsumption:0,energyUnknown:1,childLock:!1,rLoc:46080,history:this,historyType:"energy"})}createMotionEveHistoryClusterServer(t,e){this.setMatterHystoryType("motion"),t.behaviors.require(i.with(r.Feature.EveMotion),{configDataGet:h.uint8ArrayFromHex(""),configDataSet:h.uint8ArrayFromHex(""),historyStatus:h.uint8ArrayFromHex(""),historyEntries:h.uint8ArrayFromHex(""),historyRequest:h.uint8ArrayFromHex(""),historySetTime:h.uint8ArrayFromHex(""),lastEvent:0,motionSensitivity:0,rLoc:46080,history:this,historyType:"motion"})}createDoorEveHistoryClusterServer(t,e){this.setMatterHystoryType("door"),t.behaviors.require(i.with(r.Feature.EveDoor),{configDataGet:h.uint8ArrayFromHex(""),configDataSet:h.uint8ArrayFromHex(""),historyStatus:h.uint8ArrayFromHex(""),historyEntries:h.uint8ArrayFromHex(""),historyRequest:h.uint8ArrayFromHex(""),historySetTime:h.uint8ArrayFromHex(""),lastEvent:0,timesOpened:0,resetTotal:0,rLoc:46080,history:this,historyType:"door"})}setConfigDataTags(e,t,r){if(this.log.debug(`*setConfigDataTags(...) productId: ${o}0x${e.toString(16).padStart(2,"0")}${a} softwareVersion: ${o}${t}${a} serialNumber: `+o+r+a),this.log.debug("*1"+this.decodeConfigData(this.configData,this.tlvConfigDataGet)),36===e){let t=Buffer.from("0000","hex");t.writeUint16LE(e),this.tlvConfigDataGet.push({field:0,name:"productId",length:2,data:t,updated:!1,value:e})}else{let t=Buffer.from("00","hex");t.writeUInt8(e),this.tlvConfigDataGet.push({field:0,name:"productId",length:1,data:t,updated:!1,value:e})}let i=Buffer.from("0000","hex");i.writeUInt16LE(t),this.tlvConfigDataGet.push({field:3,name:"firmwareVersion",length:2,data:i,updated:!1,value:t}),i=Buffer.from(r,"ascii"),this.tlvConfigDataGet.push({field:4,name:"serialNumber",length:i.length,data:i,updated:!1,value:r}),this.configData=this.encodeConfigData(this.tlvConfigDataGet),this.log.debug("*2"+this.decodeConfigData(this.configData,this.tlvConfigDataGet)),this.log.debug("*3"+this.configDataToString(this.tlvConfigDataGet))}autoPilot(t){let i=[{clusterName:"onOff",entryName:"status"},{clusterName:"airQuality",entryName:"airquality"},{clusterName:"tvocMeasurement",entryName:"voc"},{clusterName:"temperatureMeasurement",entryName:"temperature"},{clusterName:"relativeHumidityMeasurement",entryName:"humidity"},{clusterName:"pressureMeasurement",entryName:"pressure"},{clusterName:"booleanState",entryName:"contact"},{clusterName:"occupancySensing",entryName:"motion"},{clusterName:"illuminanceMeasurement",entryName:"lux"}],a=(this.log.info(`Starting history autoPilot on endpoint ${t.maybeNumber} (${t.name})`),0);this.log.debug(`-Cluster servers for: ${t.maybeNumber} (${t.name})`),Object.keys(t.behaviors.supported).forEach(e=>{var t,r;this.log.debug("--clusterServer: "+e),"onOff"===e?(this.sigTable.push({tag:11,length:2,factor:10,precision:2,bitmask:Math.pow(2,a++),entryName:"power"}),this.sigTable.push({tag:12,length:2,factor:1,precision:2,bitmask:Math.pow(2,a++),entryName:"voltage"}),this.sigTable.push({tag:13,length:2,factor:1,precision:2,bitmask:Math.pow(2,a++),entryName:"current"}),this.sigTable.push({tag:7,length:2,factor:10,precision:2,bitmask:Math.pow(2,a++),entryName:"consumption"}),this.sigTable.push({tag:14,length:1,factor:1,precision:0,bitmask:Math.pow(2,a++),entryName:"status"}),this.sigTable.push({tag:1,length:2,factor:100,precision:2,bitmask:Math.pow(2,a++),entryName:"temperature"})):(t=i.find(t=>t.clusterName===e))&&(r=this.entryNameMap.get(t.entryName))&&this.sigTable.push({tag:r.tag,length:r.length,factor:r.factor,precision:r.precision,bitmask:Math.pow(2,a++),entryName:t.entryName})});let r=0;this.signature=Buffer.alloc(15),this.signature.writeUInt8(this.sigTable.length,r),r+=1,this.sigTable.forEach((t,e)=>{this.log.debug(` ${e} - `+this.entryStringify(t)+n),this.signature.writeUInt8(t.tag,r),r+=1,this.signature.writeUInt8(t.length,r),r+=1}),this.signature=this.signature.subarray(0,r),this.log.info(`History found ${this.sigTable.length} signatures [${this.signature.toString("hex")}]`),this.readHistoryFile(),this.logHistory()}OnGetConfigData(t){return!1===t?Uint8Array.from(Buffer.from("","hex")):(this.log.debug("*"+this.decodeConfigData(this.configData,this.tlvConfigDataGet)),Uint8Array.from(this.configData))}OnSetConfigData(t){return this.log.debug("this.decodeConfigData(this.configData, this.tlvConfigDataGet)"),this.decodeConfigData(this.configData,this.tlvConfigDataGet),this.log.debug("this.decodeConfigData(Buffer.from(value), this.tlvConfigDataSet)"),this.decodeConfigData(Buffer.from(t),this.tlvConfigDataSet),this.log.debug("this.decodeConfigData(Buffer.from(value), this.tlvConfigDataGet)"),this.decodeConfigData(Buffer.from(t),this.tlvConfigDataGet),this.log.debug("this.configData = this.encodeConfigData(this.tlvConfigDataGet)"),this.configData=this.encodeConfigData(this.tlvConfigDataGet),this.log.debug("*"+this.configDataToString(this.tlvConfigDataSet)),!0}OnGetHistoryStatus(t){return!1===t?Uint8Array.from(Buffer.from("","hex")):(t=this.encodeHistoryStatus(),this.log.debug("*"+this.historyStatusToString(t)),Uint8Array.from(t))}OnGetHistoryEntries(){var t;if(-1===this.currentEntry||this.currentEntry>this.lastEntry)return this.currentEntry=-1,t=Buffer.from("00","hex"),this.log.debug(`OnGetHistoryEntries(...) [${t.toString("hex")}] currentEntry: ${this.currentEntry} for `+s+this.name+n),Uint8Array.from(t);this.log.debug(`OnGetHistoryEntries(...) entry: ${o}${this.currentEntry}${a}-${o}${Math.min(this.currentEntry+20-1,this.lastEntry)}${a} of ${o}${this.firstEntry}${a}-${o}${this.lastEntry}${a} for `+s+this.name+n);let e=0,r=Buffer.alloc(4096);for(let t=e=0;t<20;t++){var i=this.encodeHistoryEntry(this.currentEntry);if(this.log.debug(this.historyEntryToString(i)),i.copy(r,e),e+=i.readUInt8(0),this.currentEntry++,this.currentEntry>this.lastEntry){this.currentEntry=-1;break}}return r=r.subarray(0,e),this.log.debug("*"+this.historyEntriesToString(r)),Uint8Array.from(r)}OnSetHistorySetTime(t){return this.log.debug("*"+this.decodeHistorySetTime(Buffer.from(t))),!0}OnSetHistoryRequest(t){return this.log.debug("*"+this.decodeHistoryRequest(Buffer.from(t))),!0}OnGetimesOpened(){return this.log.debug("*TimesOpened: "+this.timesOpened),this.timesOpened}OnGetLastEvent(){return this.log.debug(`*LastEvent: ${this.lastEvent}=`+this.secsToDateString(this.lastEvent+this.initialTime-this.timeOffset)),this.lastEvent}OnGetLastReset(){return this.log.debug(`*LastReset: ${this.resetTotal}=`+this.secsToDateStringSinceEveEpoch(this.resetTotal)),this.resetTotal}OnSetLastReset(t){return this.resetTotal=t,this.timesOpened=0,this.log.debug(`*LastReset: ${this.resetTotal}=`+this.secsToDateStringSinceEveEpoch(this.resetTotal)),!0}}export{h as MatterHistory};