Commit 074e2727 authored by Shaun Case's avatar Shaun Case

Initial version of AJA REST API bash and python scripts for HELO

parent 03dfaf03
<head>
<meta charset="utf-8">
<title>[AJA REST API - Code - Bash Example Scripts](https://gitlab.aja.com/pub/rest_api/tree/master/code/bash)</title>
</head>
# Directory Contents:
Example bash scripts to interact with the AJA HELO via its REST API.
# Requirements:
* All scripts are written in the 'bash' shell scripting language and were
tested on Linux using bash v 4.2
* All scripts require the [cURL](https://curl.haxx.se/) command line utility
* Only the <samp>helo_get_param.sh</samp> script requires [jq](https://stedolan.github.io/jq/).
All others do not require it.
# License
All scripts are made available by AJA under the Apache License v2.0.
# Example Scripts
* <samp>helo_get_param.sh</samp> - Standalone script to get any HELO parameter by name. Requires [jq](https://stedolan.github.io/jq/)
* <samp>helo_get_recording_state.sh</samp> - Check to see if HELO is recording
* <samp>helo_get_streaming_state.sh</samp> - Check to see if HELO is streaming
* <samp>helo_set_recording_name.sh</samp> - Set HELO's recording filename
* <samp>helo_set_recording_profile.sh</samp> - Select a recording profile
* <samp>helo_set_streaming_profile.sh</samp> - Select a streaming profile
* <samp>helo_start_recording.sh</samp> - Tell HELO to begin recording
* <samp>helo_start_streaming.sh</samp> - Tell HELO to begin streaming
* <samp>helo_stop_recording.sh</samp> - Tell HELO to cease recording
* <samp>helo_stop_streaming.sh</samp> - Tell HELO to cease streaming
#!/bin/bash
###############################################################################
#
# helo_get_param - bash shell script to get the value of a parameter from a
# HELO, by value or by name, using the cURL(1) utility.
#
# GETs the param and uses the jq(2) utility to return either the param value
# or the associated valuename
#
#
# Examples:
#
# $ # 1) Get current replicator record state name
# $ helo_get_param.sh -u 10.3.36.88 -p eParamID_ReplicatorRecordState -n
# eRRSRecording
#
# $ 2) Is replicator record state equal to eRRSIdle?
# $ helo_get_param.sh -u 10.3.36.88 -p eParamID_ReplicatorRecordState -i eRRSIdle -n
# $ echo \$?
# 1
# $ # It's not. Unix convention, 0 == success, anything else is false or error
#
#---------------------------------------------------
#
# References:
#
# 1) curl: https://curl.haxx.se/
# 2) jq: https://stedolan.github.io/jq/
#
###############################################################################
#
# Copyright 2017 AJA Video Systems, Inc.
#
# 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.
#
###############################################################################
usage()
{
echo "Usage: $0 -u <url_string> -p <paramid> [-i <match_string>] [-n | -j]"
echo ""
echo "Options:"
echo ""
echo " -i is; returns 1 if match, 0 if not. Default is match by value."
echo " Use -n to match by name."
echo " -j show response as pretty-printed JSON "
echo " -n output value name instead of value"
echo " -p parameter identifer"
echo " -u URL of AJA device"
echo ""
echo "Examples:"
echo ""
echo "$ # Get current replicator record state name"
echo "$ helo_get_param.sh -u 10.3.36.88 -p eParamID_ReplicatorRecordState -n"
echo "eRRSRecording"
echo ""
echo "$ # Is replicator record state equal to eRRSIdle?"
echo "$ helo_get_param.sh -u 10.3.36.88 -p eParamID_ReplicatorRecordState -i eRRSIdle -n"
echo "$ echo \$?"
echo "1"
echo "$ # It's not. Unix convention, 0 == success, anything else is false or error"
echo ""
exit 1;
}
#####
##### main
# Check dependencies
# Ensure jq is available. More at https://stedolan.github.io/jq/
hash jq 2>/dev/null || { echo >&2 "I require jq, a lightweight command-line JSON processor but I can't find it. Aborting."; exit 1; }
# Ensure curl is available. More at https://curl.haxx.se/
hash curl 2>/dev/null || { echo >&2 "I require curl, a command-line tool for transferring data with URLs, but I can't find it. Aborting."; exit 1; }
# Option control variables
matchString=""
jsonField=".value"
prettyPrint=false
# Parse command line options
while getopts ":i:jnp:u:" opt; do
case "${opt}" in
i)
matchString=${OPTARG}
;;
n)
jsonField=".value_name"
;;
j)
prettyPrint=true;
;;
p)
paramId=${OPTARG}
;;
u)
deviceUrl=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
# Ensure we recived all required command line arguments
if [ -z "${deviceUrl}" ] || [ -z "${paramId}" ]; then
echo "Missing arguments"
usage
fi
# Use cURL to get the current value of parameter specified by paramId
httpResp=$(curl -s "${deviceUrl}/config?action=get&paramid=${paramId}")
# Pretty Print full JSON response if requested; useful for debugging
if [ "${prettyPrint}" = true ] ; then
echo "$httpResp" | jq -r .
fi
# Parse JSON response and check match if requested; exit with 0 for match.
# Otherwise output value
jqResp=$(echo "$httpResp" | jq -r "${jsonField}")
if [ "${matchString}" != "" ]; then
[ "${matchString}" = "${jqResp}" ]
exit $?
else
echo "${jqResp}"
fi
exit 0
#!/bin/bash
###############################################################################
#
# helo_verify_recording.sh - bash shell script to check if a HELO is
# recording, using the using the cURL utility.
#
# GETs the ReplicatorRecordState, prints the numeric value.
# Exits with
# 0 if recording
# 1 if not recording
# 255 on error
#
###############################################################################
#
# Copyright 2017 AJA Video Systems, Inc.
#
# 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.
#
###############################################################################
usage() { echo "Usage: $0 -u <url_string>"; exit 255; }
function jsonval {
local json=$1
local key=$2
temp=`echo $json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $key`
echo ${temp##*|}
return ${temp##*|}
}
# Get recording state, exit with 0 (true) if recording, 1 otherwise
function get_record_state
{
local IS_RECORDING=1 # Assume False
local DEVICE_URL=$1
local VALUE=2 # 2 is the value for replicator record state recording
echo "Checking record state on HELO at ${DEVICE_URL}"
local PARAM_ID="eParamID_ReplicatorRecordState"
echo "curling with URL ${DEVICE_URL}/config?action=get&paramid=${PARAM_ID}"
local HTTP_RESPONSE=$(curl -sS "${DEVICE_URL}/config?action=get&paramid=${PARAM_ID}")
# Call the quick-and-dirty JSON parsing function above.
# For something more sophisticated, see helo_get_param.sh, which uses jq.
jsonval $HTTP_RESPONSE 'value'
[ $? -eq $VALUE ]
IS_RECORDING=$?
if [ "$IS_RECORDING" -eq 0 ]; then
echo "${DEVICE_URL} is recording."
else
echo "${DEVICE_URL} is not recording."
fi
exit ${IS_RECORDING}
} # end of get_record_state
#####
##### main
# Check dependencies
# Ensure curl is available. More at https://curl.haxx.se/
hash curl 2>/dev/null || { echo >&2 "I require curl, a command-line tool for transferring data with URLs, but I can't find it. Aborting."; exit 1; }
# Parse command line options
while getopts ":u:" opt; do
case "${opt}" in
u)
u=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
# Ensure we recived all required command line arguments
if [ -z "${u}" ]; then
usage
fi
echo "Device URL = ${u}"
# Process command-line arguments
# Trim off trailing backslash if there is one so we don't end up with two;
# curl errors out on URLs like http://192.168.0.1//..."
DEVICE_URL=${u%/}
# Call the get_record_state function
get_record_state "${DEVICE_URL}"
# get_record_state exits, so we should never get here.
# In case we do, return error.
exit 255
#!/bin/bash
###############################################################################
#
# helo_verify_streaming.sh - bash shell script to check if a HELO is
# streaming, using the using the cURL utility.
#
# GETs the ReplicatorStreamState, prints the numeric value.
# Exits with
# 0 if streaming
# 1 if not streaming
# 255 on error
#
###############################################################################
#
# Copyright 2017 AJA Video Systems, Inc.
#
# 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.
#
###############################################################################
usage() { echo "Usage: $0 -u <url_string>"; exit 255; }
function jsonval {
local json=$1
local key=$2
temp=`echo $json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $key`
echo ${temp##*|}
return ${temp##*|}
}
# Get streaming state, exit with 0 (true) if recording, 1 otherwise
function get_stream_state
{
local IS_STREAMING=1 # Assume False
local DEVICE_URL=$1 # Function's first argument is the HELO's URL
local VALUE=2 # 2 is the value for replicator stream state "streaming"
echo "Checking stream state on HELO at ${DEVICE_URL}"
local PARAM_ID="eParamID_ReplicatorStreamState"
echo "curling with URL ${DEVICE_URL}/config?action=get&paramid=${PARAM_ID}"
local HTTP_RESPONSE=$(curl -sS "${DEVICE_URL}/config?action=get&paramid=${PARAM_ID}")
# Call the quick-and-dirty JSON parsing function above.
# For something more sophisticated, see helo_get_param.sh, which uses jq.
jsonval $HTTP_RESPONSE 'value'
[ $? -eq $VALUE ]
IS_STREAMING=$?
if [ "$IS_STREAMING" -eq 0 ]; then
echo "${DEVICE_URL} is streaming."
else
echo "${DEVICE_URL} is not streaming."
fi
exit ${IS_STREAMING}
} # end of get_stream_state
#####
##### main
# Check dependencies
# Ensure curl is available. More at https://curl.haxx.se/
hash curl 2>/dev/null || { echo >&2 "I require curl, a command-line tool for transferring data with URLs, but I can't find it. Aborting."; exit 1; }
# Parse command line options
while getopts ":u:" opt; do
case "${opt}" in
u)
u=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
# Ensure we recived all required command line arguments
if [ -z "${u}" ]; then
usage
fi
echo "Device URL = ${u}"
# Process command-line arguments
# Trim off trailing backslash if there is one so we don't end up with two;
# curl errors out on URLs like http://192.168.0.1//..."
DEVICE_URL=${u%/}
# Call the get_stream_state function
get_stream_state "${DEVICE_URL}"
exit 255
#!/bin/bash
###############################################################################
#
# helo_set_recording_name - bash shell script to set the Filename Prefix for
# recordings, using the cURL utility.
#
###############################################################################
#
# Copyright 2017 AJA Video Systems, Inc.
#
# 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.
#
###############################################################################
usage()
{
echo "Usage: $0 -u <url_string> -p <prefix_string>"
exit 1
}
# 'main' set_recording_name function
function set_recording_name
{
local DEVICE_URL=$1
local RECORDING_NAME=$2
echo "Selecting Filename Prefix ${RECORDING_NAME} on HELO at ${DEVICE_URL}"
local PARAM_ID="eParamID_FilenamePrefix"
echo "curling with URL ${DEVICE_URL}/config?action=set&paramid=${PARAM_ID}&value=${RECORDING_NAME}"
# Declaring and assigning the local variable here in one statement will hide the exit code from cURL
local RESPONSE
RESPONSE=$(curl "${DEVICE_URL}/config?action=set&paramid=${PARAM_ID}&value=${RECORDING_NAME}")
local CURL_RESULT=$?
echo "Response: ${RESPONSE}"
return ${CURL_RESULT}
} # end of set_recording_name
#####
##### main
# Check dependencies
# Ensure curl is available. More at https://curl.haxx.se/
hash curl 2>/dev/null || { echo >&2 "I require curl, a command-line tool for transferring data with URLs, but I can't find it. Aborting."; exit 1; }
# Parse command line options
while getopts ":u:p:" opt; do
case "${opt}" in
u)
u=${OPTARG}
;;
p)
p=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
# Ensure we recived all required command line arguments
if [ -z "${u}" ]; then
usage
fi
if [ -z "${p}" ]; then
usage
fi
echo "Device URL = ${u}"
echo "Filename Prefix = ${p}"
# Process command-line arguments
DEVICE_URL=${u}
RECORDING_NAME=${p}
# Call the set_recording_name function
set_recording_name "${DEVICE_URL}" "${RECORDING_NAME}"
exit $?
#!/bin/bash
###############################################################################
#
# helo_set_recording_profile - bash shell script to select a Recording Profile
# on a HELO, using the cURL utility.
#
###############################################################################
#
# Copyright 2017 AJA Video Systems, Inc.
#
# 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.
#
###############################################################################
usage()
{
echo "Usage: $0 -u <url_string> -p <profile_num>"
exit 1
}
# 'main' select_recording_profile function
function select_recording_profile
{
local DEVICE_URL=$1
local PROFILE_NUM=$2
echo "Selecting Recording Profile ${PROFILE_NUM} on HELO at ${DEVICE_URL}"
local PARAM_ID="eParamID_RecordingProfileSel"
echo "curling with URL ${DEVICE_URL}/config?action=set&paramid=${PARAM_ID}&value=${PROFILE_NUM}"
# Declaring and assigning the local variable here in one statement will hide the exit code from cURL
local RESPONSE
RESPONSE=$(curl "${DEVICE_URL}/config?action=set&paramid=${PARAM_ID}&value=${PROFILE_NUM}")
local CURL_RESULT=$?
echo "Response: ${RESPONSE}"
# The Descriptor for eParamID_RecordingProfileSel indicates that it is an "enum" type parameter,
# but its 'enum_values' list is empty, which may cause some consternation.
echo 'NOTE: Response may indicate "value_name":"Invalid". This is normal.'
return ${CURL_RESULT}
} # end of select_recording_profile
#####
##### main
# Check dependencies
# Ensure curl is available. More at https://curl.haxx.se/
hash curl 2>/dev/null || { echo >&2 "I require curl, a command-line tool for transferring data with URLs, but I can't find it. Aborting."; exit 1; }
# Parse command line options
while getopts ":u:p:" opt; do
case "${opt}" in
u)
u=${OPTARG}
;;
p)
p=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
# Ensure we recived all required command line arguments
if [ -z "${u}" ]; then
usage
fi
if [ -z "${p}" ]; then
usage
fi
echo "Device URL = ${u}"
echo "Recording Profile # = ${p}"
# Process command-line arguments
DEVICE_URL=${u}
PROFILE_NUM=${p}
# Call the select_recording_profile function
select_recording_profile "${DEVICE_URL}" "${PROFILE_NUM}"
exit $?
#!/bin/bash
###############################################################################
#
# helo_set_streaming_profile - bash shell script to select a Streaming Profile
# on a HELO, using the cURL utility.
#
###############################################################################
#
# Copyright 2017 AJA Video Systems, Inc.
#
# 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.
#
###############################################################################