Marcelo Ávila de Oliveira
Published © GPL3+

Smart Basketball Scoreboard Prototype

What if the Avnet SmartEdge Agile was used to prototype a solution to track my basketball training workouts? Let's go for it!

IntermediateFull instructions provided24 hours1,041
Smart Basketball Scoreboard Prototype

Things used in this project

Hardware components

SmartEdge Agile Brainium
Avnet SmartEdge Agile Brainium
×1

Software apps and online services

Avnet Brainium Portal
Avnet Brainium Gateway

Story

Read more

Code

show-recordings.sh

SH
Show available project recordings in the Brainium portal
#!/bin/bash
#------------------------------------------------------------------------------#
# Filename    : show-recordings.sh                                             #
# Description : Show available project recordings                              #
# Author      : Marcelo Avila de Oliveira <marceloavilaoliveira@gmail.com>     #
#------------------------------------------------------------------------------#

#------------------------------------------------------------------------------#
# VARIABLES                                                                    #
#------------------------------------------------------------------------------#

rest_path=https://ns01-api.brainium.com/api/v1
project_id=893
access_token=xxxxx

#------------------------------------------------------------------------------#
# MAIN                                                                         #
#------------------------------------------------------------------------------#

echo ""
echo "=> Showing recordings"
echo ""

echo "Date/Time                  Id"
echo "-----------------------    ------------------------"

curl --silent --request POST "$rest_path/recordings/api/find" --header "Content-Type: application/json" --header "Authorization: Bearer $access_token" --data "
{
    \"projectIds\" : [ \"$project_id\" ]
}" | jq --raw-output '.content[] | .startedAt + " => " + .id'

echo ""

get-recording.sh

SH
Download a recording file from Brainium portal
#!/bin/bash
#------------------------------------------------------------------------------#
# Filename    : get-recording.sh                                               #
# Description : Download a recording file                                      #
# Author      : Marcelo Avila de Oliveira <marceloavilaoliveira@gmail.com>     #
#------------------------------------------------------------------------------#

#------------------------------------------------------------------------------#
# VARIABLES                                                                    #
#------------------------------------------------------------------------------#

rest_path=https://ns01-api.brainium.com/api/v1
access_token=xxxxx

#------------------------------------------------------------------------------#
# MAIN                                                                         #
#------------------------------------------------------------------------------#

echo ""
echo "=> Getting the recording file URL"
echo ""

file_url=$(curl --silent --request GET "$rest_path/recordings/$1" --header "Content-Type: application/json" --header "Authorization: Bearer $access_token" | jq --raw-output '.linkOnExportedFile')

echo ""
echo "=> Downloading the recording file"
echo ""

wget --output-document recording_session.csv "$file_url"

echo ""

scoreboard.sh

SH
Smart basketball training tracker
#!/bin/bash
#------------------------------------------------------------------------------#
# Filename    : scoreboard.sh                                                  #
# Description : Smart basketball training tracker                              #
# Author      : Marcelo Avila de Oliveira <marceloavilaoliveira@gmail.com>     #
#------------------------------------------------------------------------------#

#------------------------------------------------------------------------------#
# VARIABLES                                                                    #
#------------------------------------------------------------------------------#

source=
timestamp=0
value=0

date_ini=
date_end=

shots_num=0
scores_num=0

shots_wait=0
scores_wait=0

temperature_tot=0
humidity_tot=0
lightness_tot=0
pressure_tot=0

temperature_num=0
humidity_num=0
lightness_num=0
pressure_num=0

#------------------------------------------------------------------------------#
# MAIN                                                                         #
#------------------------------------------------------------------------------#

echo ""
echo "=> Calculating statistics"
echo ""

# The recording_session.csv file is formated as following:
#
# devicePublicId,dataSource,timestamp,scalar,vector
# TO136-020210000100112A,HUMIDITY,1560712053812,51,
# TO136-020210000100112A,ACCELERATION_NORM,1560712053219,9.831536293029785,
# TO136-020210000100112A,PRESSURE,1560712053809,94928,
# TO136-020210000100112A,PROXIMITY,1560712053221,200,
# TO136-020210000100112A,HUMIDITY_TEMPERATURE,1560712053812,23.492218017578125,
# TO136-020210000100112A,VISIBLE_SPECTRUM_LIGHTNESS,1560712053931,201,
# ...

# For each line of the recording_session.csv file
while read -r line
do
    # Get the dataSource (HUMIDITY, ACCELERATION_NORM, etc) and the scalar value
    source=$(echo $line | cut -d "," -f 2)
    timestamp=$(echo $line | cut -d "," -f 3)
    value=$(echo $line | cut -d "," -f 4)

    if [[ $date_ini = "" ]]
    then
        # Keep the initial date/time
        date_ini=$(date "+%Y-%m-%d %H:%M:%S" -d @${timestamp:0:10})
    fi

    case $source in
        # Check the acceleration (shots)
        ACCELERATION_NORM)
            if [[ $shots_wait -eq 0 ]]
            then
                if (( $(echo "$value > 10" | bc -l) || $(echo "$value < 9.6" | bc -l) ))
                then
                    # An acceleration was found
                    # Increment the number of shots
                    shots_num=$(echo "$shots_num+1" | bc)
                    # Start a wait time
                    shots_wait=1
                fi
            elif [[ $shots_wait -lt 50 ]]
            then
                shots_wait=$(echo "$shots_wait+1" | bc)
            else
                # Wait time finished
                shots_wait=0
            fi;;
        # Check the proximity (scores)
        PROXIMITY)
            if [[ $scores_wait -eq 0 ]]
            then
                if (( $(echo "$value < 100" | bc -l) ))
                then
                    # a proximity was found
                    # Increment the number of scores
                    scores_num=$(echo "$scores_num+1" | bc)
                    # Start a wait time
                    scores_wait=1
                fi
            elif [[ $scores_wait -lt 30 ]]
            then
                scores_wait=$(echo "$scores_wait+1" | bc)
            else
                # Wait time finished
                scores_wait=0
            fi;;
        # Check the temperature
        HUMIDITY_TEMPERATURE)
            temperature_num=$(echo "$temperature_num+1" | bc)
            temperature_tot=$(echo "$temperature_tot+$value" | bc );;
        # Check the humidity
        HUMIDITY)
            humidity_num=$(echo "$humidity_num+1" | bc)
            humidity_tot=$(echo "$humidity_tot+$value" | bc );;
        # Check the lightness
        VISIBLE_SPECTRUM_LIGHTNESS)
            lightness_num=$(echo "$lightness_num+1" | bc)
            lightness_tot=$(echo "$lightness_tot+$value" | bc );;
        # Check the pressure
        PRESSURE)
            pressure_num=$(echo "$pressure_num+1" | bc)
            pressure_tot=$(echo "$pressure_tot+$value" | bc );;
    esac
done < <(cat recording_session.csv | sed '1d' | sort --field-separator=, --key=3,3n)
# cat recording_session.csv => List recording_session.csv file
# sed '1d'                  => Remove the header line
# sort                      => Sort the file
#     --field-separator=,   => Use "-" as the field separator
#     --key=3,3n            => Sort numerically by the third field (timestamp)

# Keep the final date/time
date_end=$(date "+%Y-%m-%d %H:%M:%S" -d @${timestamp:0:10})

echo ""
echo "=> Showing statistics"
echo ""

echo "Start       = $date_ini"
echo "Finish      = $date_end"
echo ""
echo "Shots       = $shots_num (100%)"
echo "Scores      = $scores_num ($(echo "100*$scores_num/$shots_num" | bc)%)"
echo "Misses      = $(echo "$shots_num-$scores_num" | bc) ($(echo "100*($shots_num-$scores_num)/$shots_num" | bc)%)"
echo ""
echo "Temperature = $(echo "$temperature_tot/$temperature_num" | bc) C"
echo "Humidity    = $(echo "$humidity_tot/$humidity_num" | bc) %"
echo "Lightness   = $(echo "$lightness_tot/$lightness_num" | bc) lux"
echo "Pressure    = $(echo "$pressure_tot/$pressure_num" / 100 | bc) hPa"
echo ""

Credits

Marcelo Ávila de Oliveira

Marcelo Ávila de Oliveira

5 projects • 9 followers

Comments