Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  /**
  2.  * Copyright 2020 Nemanja Vukmirovic
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  * http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  **/
  16. module.exports = function(RED) {
  17.     function send(config) {
  18.        
  19.         const { exec } = require("child_process");
  20.        
  21.         RED.nodes.createNode(this,config);
  22.         var node = this;
  23.         var options;
  24.         var baudmode = config.baudmode;
  25.         var autocarrier = config.autocarrier;
  26.         var inverted = config.inverted;
  27.         var bandwith = config.bandwith;
  28.         var mark = config.mark;
  29.         var space =config.space;
  30.         var startbits = config.startbits;
  31.         var stopbits = config.stopbits;
  32.         var invertstartstop = config.invertstartstop;
  33.         var syncbyte = config.syncbyte;
  34.         var quiet = config.quiet;
  35.         var samplerate = config.samplerate;
  36.         var floatsamples= config.floatsamples;
  37.  
  38.         var bandwithval = config.bandwithval;
  39.         var markval = config.markval;
  40.         var spaceval = config.spaceval;
  41.         var startbitsval = config.startbitsval;
  42.         var stopbitsval = config.stopbitsval;
  43.         var syncbyteval = config.syncbyteval;
  44.         var samplerateval = config.samplerateval;
  45.  
  46.         var repeat = config.repeat;
  47.         var repeatdelay = config.repeatdelay;
  48.         var repeatxtimes = config.repeatxtimes;
  49.         var installed =false
  50.         var repeatdelaytoint
  51.         var repeatxtimestoint
  52.         var iexecuted
  53.         checkifinstalled()
  54.         function checkifinstalled(){
  55.             exec('minimodem -V',(error, stdout, stderr) =>{
  56.                 if (error) {
  57.                     console.log("\x1b[31m",`error: ${error.message}`);
  58.                     console.log(`Please install minimodem from your package manager!`);
  59.                     console.log( "\x1b[0m")
  60.                     installed = false  
  61.                     return;
  62.             }
  63.             if (stderr) {
  64.                 console.error(`stderr: ${stderr}`);
  65.                 return;
  66.             }
  67.             console.log("\x1b[34m",'Minimodem installed! :) ')
  68.             console.log( "\x1b[0m")
  69.             installed = true
  70.            
  71.             } )
  72.         }
  73.         function sleep(ms) {
  74.             return new Promise(resolve => setTimeout(resolve, ms));
  75.           }
  76.         node.on('input', function(msg, send, done) {
  77.             iexecuted = 0
  78.             if (installed){
  79.                 if (typeof(bandwithval) != "undefined" || typeof(markval) != "undefined" || typeof(spaceval) != "undefined" || typeof(startbitsval) != "undefined" || typeof(stopbitsval) != "undefined" || typeof(syncbyteval) != "undefined" || typeof(samplerateval) != "undefined" );
  80.                 {
  81.                       options = formatexecutecmd()  
  82.                       checkrepeat()
  83.                    
  84.                 }
  85.                
  86.                
  87.          
  88.                
  89.             }
  90.            
  91.             else if(!installed){
  92.                 node.error('Minimodem is not installed!')
  93.                 node.error('Please install minimodem from your package manager!')
  94.             }
  95.                
  96.              
  97.  
  98.          
  99.         function formatexecutecmd(){
  100.             var output =''
  101.             if (autocarrier){output = output + ' --auto-carrier '}
  102.             if (inverted){output = output + ' --inverted'}
  103.             if (bandwith){output = output + ' --bandwidth ' + bandwithval + ' '}
  104.             if (mark){output = output + ' --mark ' + markval + ' '}
  105.             if (space){output = output + ' --space ' + spaceval + ' '}
  106.             if (startbits){output = output + ' --startbits ' + startbitsval + ' '}
  107.             if (stopbits){output = output + ' --stopbits ' + stopbitsval + ' '}
  108.             if (invertstartstop){output = output + ' --invert-start-stop '}
  109.             if (syncbyte){output = output + ' --sync-byte ' + syncbyteval + ' '}
  110.             if (quiet){output = output + ' --quiet '}
  111.             if (samplerate){output = output + ' --samplerate ' + samplerateval + ' '}
  112.             if (floatsamples){output = output + ' --float-samples '}
  113.             return output;
  114.         }
  115.        
  116.         function checkrepeat(){
  117.             if (repeat == true){
  118.             try {repeatdelaytoint = parseInt(repeatxtimes)
  119.                 repeatxtimestoint = parseInt(repeatxtimes)
  120.                 repeat(repeatdelaytoint)
  121.             }            
  122.             catch(err){
  123.                 console.log("Error occured while trying to pass repeat info, please double check the node settings.")
  124.                 node.error("Error occured while trying to pass repeat info, please double check the node settings.")
  125.                
  126.             }
  127.  
  128.  
  129.             }
  130.             else {
  131.                 exec('echo ' + msg.payload + ' | minimodem --tx ' + options + ' ' + baudmode, (error, stdout, stderr) => {
  132.  
  133.                     if (error) {
  134.                         console.log(`error: ${error.message}`);
  135.                         return;
  136.                     }
  137.                     if (stderr) {
  138.                         console.log(`stderr: ${stderr}`);
  139.                         return;
  140.                     }
  141.                     console.log(`Sent!`);
  142.                    
  143.                 });
  144.                 }
  145.            
  146.         }
  147.         function repeat(time) {
  148.             repeatxtimestoint -= 1;
  149.             setTimeout(()=>{
  150.                 executesend();
  151.             },time);
  152.             return;
  153.         }
  154.  
  155.         async function executesend(){
  156.            
  157.                 console.log(repeatdelay + ' DELAY')
  158.                 console.log(repeatxtimes + 'X')
  159.                 exec('echo ' + msg.payload + ' | minimodem --tx ' + options + ' ' + baudmode, (error, stdout, stderr) => {
  160.  
  161.                     if (error) {
  162.                         console.log(`error: ${error.message}`);
  163.                        
  164.                     }
  165.                     if (stderr) {
  166.                         console.log(`stderr: ${stderr}`);
  167.                        
  168.                     }
  169.                     console.log(`Sent!`);
  170.                    
  171.                             if (repeatxtimestoint > 0) { repeat(repeatdelaytoint) }
  172.                        
  173.                    
  174.                 });
  175.            
  176.         };
  177.           })  
  178.     }
  179.     RED.nodes.registerType("send",send);
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement