UNPKG

@redplc/node-red-rpi-ads1115

Version:

Node-RED node for ads1115 16bit adc using with redPlc nodes.

122 lines (103 loc) 3.24 kB
/** * Copyright 2024 Derya Y. (iiot2k@gmail.com). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use node 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"; const fs = require('fs'); /** * Outputs error on status and error log. * @param node - the node object * @param errShort - the error text display on status * @param errLong - the error text display on log * @returns true for set node.error */ function outError(node, errShort, errLong) { if (node.save_txt !== errShort) { node.save_txt = errShort; node.save_color = "red"; node.status({fill: "red", shape: "dot", text: errShort}); if (errLong) node.error(errLong); } return true; } module.exports.outError = outError; /** * Sets status text and icon. * @param node - the node object * @param txt - the text display on status * @param color - the color of icon * @returns true for set node.error */ module.exports.setStatus = function(node, txt = "", color = "green") { if ((node.save_txt !== txt) || (node.save_color !== color)) { node.save_txt = txt; node.save_color = color; node.status({ fill: color, shape: "dot", text: txt }); } return true; } /** * Create Variable. * @param node - the node object * @param varname - the variable name * @param ctxvar - the variable object * @returns true is variable initialized, false already exist */ module.exports.createVariable = function(node, varname, ctxvar) { const store = node.context().global; if (store.get(varname) !== undefined) return false; store.set(varname, ctxvar); return true; } /** * Delete variable. * @param node - the node object * @param varname - the variable name */ module.exports.deleteVariable = function(node, varname) { const store = node.context().global; if (store.get(varname) === undefined) return false; node.context().global.set(varname, undefined); } /** * Returns module extension depends on platform. * @returns module extension */ function getModuleExtension() { if (process.platform !== 'linux') return undefined; if (process.arch === "arm64") return "_arm64.node"; else if (process.arch === "arm") return "_arm32.node"; return undefined; } /** * Loads module depends on platform. * @param module - the module name * @returns module object */ module.exports.LoadModule = function(module) { try { var modExtension = getModuleExtension(); if (modExtension !== undefined) return require("./" + module + modExtension); } catch (e) { console.log(e); } return undefined; }