Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # CONFIG
- PortName="ttyUSB0"
- BaudRate=9600
- # The app begins.
- MODULE="AOSRService"
- # Check if some commands exist.
- for CMDAPP in "stty" "echo" "hash" "date" "cat" "ps" "grep" "awk" "xargs" "kill"
- do
- if ! hash "$CMDAPP" 2>/dev/null
- then
- echo "`date`: $MODULE ended without '$CMDAPP' command." >> ERROR.log
- echo "`date`: $MODULE ended without '$CMDAPP' command."
- exit
- fi
- done
- # Call function to verify the output of commands.
- CheckOutput() {
- if [ $? -eq 0 ]
- then
- echo "`date`: $1" >> ACTIONS.log
- # Search and kill listen process's.
- ps -ef | grep cat | grep -v "grep" | awk '{print $2}' | xargs kill;
- else
- echo "`date`: $1" >> ERROR.log
- fi
- }
- # Connect to the specified device instead of stdin.
- stty -F /dev/$PortName $BaudRate cs8 -cstopb -parenb -icanon min 1 time 1
- # Run in background the process that will listen to the output.
- cat -v /dev/$PortName > output 2>&1 &
- # Turn on/off and receive status of the relays.
- case "$1" in
- "1:1")
- echo -e '\xff\x01\x01' > /dev/$PortName
- CheckOutput $1
- ;;
- "1:0")
- echo -e '\xff\x01\x00' > /dev/$PortName
- CheckOutput $1
- ;;
- "2:1")
- echo -e '\xff\x02\x01' > /dev/$PortName
- CheckOutput $1
- ;;
- "2:0")
- echo -e '\xff\x02\x00' > /dev/$PortName
- CheckOutput $1
- ;;
- "3:1")
- echo -e '\xff\x03\x01' > /dev/$PortName
- CheckOutput $1
- ;;
- "3:0")
- echo -e '\xff\x03\x00' > /dev/$PortName
- CheckOutput $1
- ;;
- "4:1")
- echo -e '\xff\x04\x01' > /dev/$PortName
- CheckOutput $1
- ;;
- "4:0")
- echo -e '\xff\x04\x00' > /dev/$PortName
- CheckOutput $1
- ;;
- "5:1")
- echo -e '\xff\x05\x01' > /dev/$PortName
- CheckOutput $1
- ;;
- "5:0")
- echo -e '\xff\x05\x00' > /dev/$PortName
- CheckOutput $1
- ;;
- "6:1")
- echo -e '\xff\x06\x01' > /dev/$PortName
- CheckOutput $1
- ;;
- "6:0")
- echo -e '\xff\x06\x00' > /dev/$PortName
- CheckOutput $1
- ;;
- "7:1")
- echo -e '\xff\x07\x01' > /dev/$PortName
- CheckOutput $1
- ;;
- "7:0")
- echo -e '\xff\x07\x00' > /dev/$PortName
- CheckOutput $1
- ;;
- "8:1")
- echo -e '\xff\x08\x01' > /dev/$PortName
- CheckOutput $1
- ;;
- "8:0")
- echo -e '\xff\x08\x00' > /dev/$PortName
- CheckOutput $1
- ;;
- "status-all")
- echo -e '\xff\x09\x00' > /dev/$PortName
- CheckOutput $1
- ;;
- *)
- echo "Select Case problem."
- echo "Usage: $MODULE <param>"
- echo "`date`: Select Case problem." >> ERROR.log
- exit 1
- esac
- # Exit app.
- exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement