UNPKG

node-red-contrib-adxl-vibration-fft

Version:

Node-RED nodes for Analog Devices' ADXL series FFT(Fast Fourier Transform) stream data parser

17 lines 3.22 kB
/** * @license * Copyright (c) 2018 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. */ "use strict";require("source-map-support/register");var obj,_serialport=(obj=require("serialport"))&&obj.__esModule?obj:{default:obj},_adxlFftClient=require("./adxl-fft-client");module.exports=function(t){const e={},s=()=>{Object.keys(e).forEach(t=>{e[t].shutdown()})};process.on("exit",s),t.settings&&t.settings.exitHandlers&&t.settings.exitHandlers.push(s);t.nodes.registerType("ADXL100x FFT",class{constructor(s){if(t.nodes.createNode(this,s),this.serialport=s.serialport,e[this.serialport])throw Error("Duplicate serialport configuration for ADXL100xFFTNode!");this.identifier=s.identifier,this.enabled=!!s.enabled,this.emitFftValues=!!s.emitFftValues,this.edgeDeviceModel=s.edgeDeviceModel,this.nodes={},["connected","disconnected","error"].forEach(t=>{this.on(t,()=>{try{Object.keys(this.nodes).forEach(e=>{this.nodes[e].emit(t)})}catch(t){this.error(t)}})}),this.on("data",t=>{try{Object.keys(this.nodes).forEach(e=>{if(this.nodes[e].input){const s=parseInt(this.nodes[e].numOfPeaks||0),{payloadFormat:i}=this.nodes[e];this.nodes[e].send(this.client.format(t,this.identifier,s,i))}})}catch(t){this.error(t)}}),this.on("close",t=>{delete e[this.serialport],this.client.shutdown().then(()=>{t()}).catch(e=>{this.log(e),t()})}),this.operations={register:t=>!!t&&!this.nodes[t.id]&&(this.nodes[t.id]=t,this.client.closed?t.emit("disconnected"):t.emit("connected"),!0),remove:t=>!(!t||!this.nodes[t.id]||(delete this.nodes[t.id],0)),shutdown:()=>this.client.shutdown()},this.client=new _adxlFftClient.ADXL100xFFTClient(this),this.enabled?(e[this.serialport]=this.client,this.client.start().catch(t=>{this.error(t),this.emit("error")})):process.nextTick(()=>{this.emit("disconnected")})}});t.nodes.registerType("ADXL100x FFT in",class{constructor(e){t.nodes.createNode(this,e),this.name=e.name,this.input=!0,this.payloadFormat=e.payloadFormat,this.numOfPeaks=e.numOfPeaks,this.adxl100xFFTNodeId=e.adxl100xFFT,this.adxl100xFFTNode=t.nodes.getNode(this.adxl100xFFTNodeId),this.adxl100xFFTNode&&(this.on("connected",()=>{const e=0<arguments.length&&null!==arguments[0]?arguments[0]:0;this.status({fill:"green",shape:"dot",text:t._("adxl-fft.status.connected",{n:e})})}),["disconnected","error"].forEach(t=>{this.on(t,()=>{this.status({fill:"red",shape:"ring",text:"adxl-fft.status."+t})})}),this.on("close",()=>{this.adxl100xFFTNode&&this.adxl100xFFTNode.operations.remove(this)}),this.adxl100xFFTNode.operations.register(this))}}),t.httpAdmin.get("/adxl-fft-ports",t.auth.needsPermission("serial.read"),(t,e)=>{_serialport.default.list().then(t=>{e.json(t)}).catch(t=>{this.error(t),e.sendStatus(500)})})};