@candy-line/node-red-contrib-omron-2jcie-bu
Version:
OMRON 2JCIE-BU Environment Sensor (USB Type) Serial Data Translator
17 lines • 5.01 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Omron2jcieBuPacketParser=exports.Omron2jcieBuPacketBuilder=void 0,require("source-map-support/register");const HEADER=16978,RESPONSE_STATUS={1:"Read OK",2:"Write OK",129:"Read Error",130:"Write Error",255:"Unkown"},ERROR_STATUS={1:"CRC Error",2:"Command Error",3:"Address Error",4:"Length Error",5:"Data Error",6:"Busy"},DISPLAY_RULES={OFF:0,ON:1,Temperature:2,Humidity:3,Illuminance:4,"Barometric Pressure":5,"Sound Noise":6,eTVOC:7,Vibration:8},crc16=e=>{let r=65535;for(let t=0;t<e.length;t++){r^=e[t];for(let e=0;e<8;e++){const e=1&r;r=r>>1&524287,e&&(r^=40961)}}return r};
/**
* @license
* Copyright (c) 2019 CANDY LINE INC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/exports.Omron2jcieBuPacketBuilder=class{constructor(){}buildRequest(e){let r=Buffer.alloc(4);r.writeUInt16LE(HEADER,0),r.writeUInt16LE(e.length+2,2),r=Buffer.concat([r,e]);const t=crc16(r),a=Buffer.alloc(2);return a.writeUInt16LE(t,0),r=Buffer.concat([r,a])}buildReadLatestDataLongRequest(){const e=Buffer.alloc(3);return e[0]=1,e.writeUInt16LE(20513,1),this.buildRequest(e)}buildReadMountingOrientationRequest(){const e=Buffer.alloc(3);return e[0]=1,e.writeUInt16LE(21506,1),this.buildRequest(e)}buildReadDeviceInformationRequest(){const e=Buffer.alloc(3);return e[0]=1,e.writeUInt16LE(6154,1),this.buildRequest(e)}buildWriteLEDSettingsRequest({displayRule:e,color:r}){const t=Buffer.alloc(8);if(t[0]=2,t.writeUInt16LE(20753,1),t.writeUInt16LE(this.resolveDisplayRule(e),3),"ON"===e){const e=this.resolveColor(r);t.writeUInt8(e>>16&255,5),t.writeUInt8(e>>8&255,6),t.writeUInt8(255&e,7)}return this.buildRequest(t)}resolveDisplayRule(e){const r=DISPLAY_RULES[e];return r||0}resolveColor(e){switch(typeof e){case"string":return 0===e.indexOf("#")?parseInt(e.substring(1),16):parseInt(e,16);case"number":return e;default:return 0}}};exports.Omron2jcieBuPacketParser=class{constructor(e){this.bufferArray=null,this.expectedLength=0,this.lastBuffered=0,this.ttl=e}reset(){this.bufferArray=null,this.expectedLength=0,this.lastBuffered=0}parseResponse(e){if(this.expectedLength>0&&this.ttl&&Date.now()-this.lastBuffered>=this.ttl&&this.reset(),this.expectedLength>0)this.bufferArray.push(e),this.lastBuffered=Date.now();else{if(!(Array.isArray(e)||e instanceof Buffer)){if(!e||"Buffer"!==e.type||!Array.isArray(e.data))throw Error("[FormatError] The passed data buffer must be either int array or Buffer.");e=Buffer.from(e.data)}if(Array.isArray(e)&&(e=Buffer.from(e)),e.readUInt16LE(0)!==HEADER)throw Error("[FormatError] Cannot parse the given data. Unsupported format.");this.expectedLength=e.readUInt16LE(2)+4,this.bufferArray=[e],this.lastBuffered=Date.now()}if(this.bufferArray.reduce((e,r)=>e+r.length,0)===this.expectedLength){const e=Buffer.concat(this.bufferArray);this.reset();const r=e.length-2,t=e.slice(4,r),a=e.readUInt16LE(r),s=crc16(e.slice(0,r));if(s!==a)throw Error(`[FormatError] CRC16 Check Failed! Expected:0x${s.toString(16)}, Actual:0x${a.toString(16)}`);const i=this.parsePayloadFrame(t);return i.finished=!0,i}return{finished:!1}}parsePayloadFrame(e){const r=e[0],t=e.readUInt16LE(1),a={status:RESPONSE_STATUS[r],statusCode:r,address:t,data:{}};if(128&r){const r=e[3];return a.error=ERROR_STATUS[r],a.errorCode=r,a}switch(t){case 20513:this.readLatestDataLongFrame(e,a.data);break;case 21506:this.readMountingOrientationFrame(e,a.data);break;case 6154:this.readDeviceInformationFrame(e,a.data);break;default:if(1===r)throw Error("[IllegalDataError] Unsupported Address.")}return a}readLatestDataLongFrame(e,r){r.id=e.readUInt8(3),r.temperature=e.readInt16LE(4)/100,r.humidity=e.readInt16LE(6)/100,r.illuminance=e.readInt16LE(8),r.barometricPressure=e.readInt32LE(10)/1e3,r.soundNoise=e.readInt16LE(14)/100,r.etvoc=e.readInt16LE(16),r.eco2=e.readInt16LE(18),r.discomfortIndex=e.readInt16LE(20)/100,r.heatStroke=e.readInt16LE(22)/100,r.vibrationStatus=this.resolveVibrationStatus(e.readUInt8(24)),r.spectralIntensity=e.readInt16LE(25)/10,r.seismicIntensity=e.readInt16LE(29)/1e3}resolveVibrationStatus(e){switch(e){case 0:return"None";case 1:return"Vibration Detected";case 2:return"Earthquake Detected";default:return"Unknown"}}readMountingOrientationFrame(e,r){r.position=e.readUInt8(3)}readDeviceInformationFrame(e,r){r.model=e.slice(3,13).toString(),r.serialNumber=e.slice(13,23).toString(),r.firmwareVersion=e.slice(23,28).toString(),r.hardwareVersion=e.slice(28,33).toString(),r.manufacturer=e.slice(33,38).toString()}};