Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Copyright 2020 Nemanja Vukmirovic
- *
- * 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.
- **/
- module.exports = function(RED) {
- function send(config) {
- const { exec } = require("child_process");
- RED.nodes.createNode(this,config);
- var node = this;
- var options;
- var baudmode = config.baudmode;
- var autocarrier = config.autocarrier;
- var inverted = config.inverted;
- var bandwith = config.bandwith;
- var mark = config.mark;
- var space =config.space;
- var startbits = config.startbits;
- var stopbits = config.stopbits;
- var invertstartstop = config.invertstartstop;
- var syncbyte = config.syncbyte;
- var quiet = config.quiet;
- var samplerate = config.samplerate;
- var floatsamples= config.floatsamples;
- var bandwithval = config.bandwithval;
- var markval = config.markval;
- var spaceval = config.spaceval;
- var startbitsval = config.startbitsval;
- var stopbitsval = config.stopbitsval;
- var syncbyteval = config.syncbyteval;
- var samplerateval = config.samplerateval;
- var repeat = config.repeat;
- var repeatdelay = config.repeatdelay;
- var repeatxtimes = config.repeatxtimes;
- var installed =false
- var repeatdelaytoint
- var repeatxtimestoint
- var iexecuted
- checkifinstalled()
- function checkifinstalled(){
- exec('minimodem -V',(error, stdout, stderr) =>{
- if (error) {
- console.log("\x1b[31m",`error: ${error.message}`);
- console.log(`Please install minimodem from your package manager!`);
- console.log( "\x1b[0m")
- installed = false
- return;
- }
- if (stderr) {
- console.error(`stderr: ${stderr}`);
- return;
- }
- console.log("\x1b[34m",'Minimodem installed! :) ')
- console.log( "\x1b[0m")
- installed = true
- } )
- }
- function sleep(ms) {
- return new Promise(resolve => setTimeout(resolve, ms));
- }
- node.on('input', function(msg, send, done) {
- iexecuted = 0
- if (installed){
- if (typeof(bandwithval) != "undefined" || typeof(markval) != "undefined" || typeof(spaceval) != "undefined" || typeof(startbitsval) != "undefined" || typeof(stopbitsval) != "undefined" || typeof(syncbyteval) != "undefined" || typeof(samplerateval) != "undefined" );
- {
- options = formatexecutecmd()
- checkrepeat()
- }
- }
- else if(!installed){
- node.error('Minimodem is not installed!')
- node.error('Please install minimodem from your package manager!')
- }
- function formatexecutecmd(){
- var output =''
- if (autocarrier){output = output + ' --auto-carrier '}
- if (inverted){output = output + ' --inverted'}
- if (bandwith){output = output + ' --bandwidth ' + bandwithval + ' '}
- if (mark){output = output + ' --mark ' + markval + ' '}
- if (space){output = output + ' --space ' + spaceval + ' '}
- if (startbits){output = output + ' --startbits ' + startbitsval + ' '}
- if (stopbits){output = output + ' --stopbits ' + stopbitsval + ' '}
- if (invertstartstop){output = output + ' --invert-start-stop '}
- if (syncbyte){output = output + ' --sync-byte ' + syncbyteval + ' '}
- if (quiet){output = output + ' --quiet '}
- if (samplerate){output = output + ' --samplerate ' + samplerateval + ' '}
- if (floatsamples){output = output + ' --float-samples '}
- return output;
- }
- function checkrepeat(){
- if (repeat == true){
- try {repeatdelaytoint = parseInt(repeatxtimes)
- repeatxtimestoint = parseInt(repeatxtimes)
- repeat(repeatdelaytoint)
- }
- catch(err){
- console.log("Error occured while trying to pass repeat info, please double check the node settings.")
- node.error("Error occured while trying to pass repeat info, please double check the node settings.")
- }
- }
- else {
- exec('echo ' + msg.payload + ' | minimodem --tx ' + options + ' ' + baudmode, (error, stdout, stderr) => {
- if (error) {
- console.log(`error: ${error.message}`);
- return;
- }
- if (stderr) {
- console.log(`stderr: ${stderr}`);
- return;
- }
- console.log(`Sent!`);
- });
- }
- }
- function repeat(time) {
- repeatxtimestoint -= 1;
- setTimeout(()=>{
- executesend();
- },time);
- return;
- }
- async function executesend(){
- console.log(repeatdelay + ' DELAY')
- console.log(repeatxtimes + 'X')
- exec('echo ' + msg.payload + ' | minimodem --tx ' + options + ' ' + baudmode, (error, stdout, stderr) => {
- if (error) {
- console.log(`error: ${error.message}`);
- }
- if (stderr) {
- console.log(`stderr: ${stderr}`);
- }
- console.log(`Sent!`);
- if (repeatxtimestoint > 0) { repeat(repeatdelaytoint) }
- });
- };
- })
- }
- RED.nodes.registerType("send",send);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement