Advertisement
Guest User

Untitled

a guest
Jun 11th, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.21 KB | None | 0 0
  1.  #!/bin/bash
  2.     #
  3.  
  4.     # CONFIG
  5.     PortName="ttyUSB0"
  6.     BaudRate=9600
  7.  
  8.     # The app begins.
  9.     MODULE="AOSRService"
  10.  
  11.     # Check if some commands exist.
  12.     for CMDAPP in "stty" "echo" "hash" "date" "cat" "ps" "grep" "awk" "xargs" "kill"
  13.     do
  14.       if ! hash "$CMDAPP" 2>/dev/null
  15.       then
  16.           echo "`date`: $MODULE ended without '$CMDAPP' command." >> ERROR.log
  17.           echo "`date`: $MODULE ended without '$CMDAPP' command."
  18.           exit
  19.       fi
  20.     done
  21.  
  22.     # Call function to verify the output of commands.
  23.     CheckOutput() {
  24.         if [ $? -eq 0 ]
  25.         then
  26.              echo "`date`: $1" >> ACTIONS.log
  27.  
  28.              # Search and kill listen process's.
  29.              ps -ef | grep cat | grep -v "grep" | awk '{print $2}' | xargs kill;
  30.         else
  31.              echo "`date`: $1" >> ERROR.log
  32.         fi
  33.     }
  34.  
  35.     # Connect to the specified device instead of stdin.
  36.     stty -F /dev/$PortName $BaudRate cs8 -cstopb -parenb -icanon min 1 time 1
  37.  
  38.     # Run in background the process that will listen to the output.
  39.     cat -v /dev/$PortName > output 2>&1 &
  40.  
  41.     # Turn on/off and receive status of the relays.
  42.     case "$1" in
  43.                    "1:1")
  44.                           echo -e '\xff\x01\x01' > /dev/$PortName
  45.                           CheckOutput $1
  46.                           ;;
  47.                    "1:0")
  48.                           echo -e '\xff\x01\x00' > /dev/$PortName
  49.                           CheckOutput $1
  50.                           ;;
  51.                    "2:1")
  52.                           echo -e '\xff\x02\x01' > /dev/$PortName
  53.                           CheckOutput $1
  54.                           ;;
  55.                    "2:0")
  56.                           echo -e '\xff\x02\x00' > /dev/$PortName
  57.                           CheckOutput $1
  58.                           ;;
  59.                    "3:1")
  60.                           echo -e '\xff\x03\x01' > /dev/$PortName
  61.                           CheckOutput $1
  62.                           ;;
  63.                    "3:0")
  64.                           echo -e '\xff\x03\x00' > /dev/$PortName
  65.                           CheckOutput $1
  66.                           ;;
  67.                    "4:1")
  68.                           echo -e '\xff\x04\x01' > /dev/$PortName
  69.                           CheckOutput $1
  70.                           ;;
  71.                    "4:0")
  72.                           echo -e '\xff\x04\x00' > /dev/$PortName
  73.                           CheckOutput $1
  74.                           ;;
  75.                    "5:1")
  76.                           echo -e '\xff\x05\x01' > /dev/$PortName
  77.                           CheckOutput $1
  78.                           ;;
  79.                    "5:0")
  80.                           echo -e '\xff\x05\x00' > /dev/$PortName
  81.                           CheckOutput $1
  82.                           ;;
  83.                    "6:1")
  84.                           echo -e '\xff\x06\x01' > /dev/$PortName
  85.                           CheckOutput $1
  86.                           ;;
  87.                    "6:0")
  88.                           echo -e '\xff\x06\x00' > /dev/$PortName
  89.                           CheckOutput $1
  90.                           ;;
  91.                    "7:1")
  92.                           echo -e '\xff\x07\x01' > /dev/$PortName
  93.                           CheckOutput $1
  94.                           ;;
  95.                    "7:0")
  96.                           echo -e '\xff\x07\x00' > /dev/$PortName
  97.                           CheckOutput $1
  98.                           ;;
  99.                    "8:1")
  100.                           echo -e '\xff\x08\x01' > /dev/$PortName
  101.                           CheckOutput $1
  102.                           ;;
  103.                    "8:0")
  104.                           echo -e '\xff\x08\x00' > /dev/$PortName
  105.                           CheckOutput $1
  106.                           ;;
  107.                    "status-all")
  108.                           echo -e '\xff\x09\x00' > /dev/$PortName
  109.                           CheckOutput $1
  110.                           ;;
  111.                    *)
  112.                           echo "Select Case problem."
  113.                           echo "Usage: $MODULE <param>"
  114.                           echo "`date`: Select Case problem." >> ERROR.log
  115.                           exit 1
  116.     esac
  117.  
  118.     # Exit app.
  119.     exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement