#!/bin/bash OPENHAB="10.0.10.231" # Your openHab Server ROKU="10.0.10.242" TOKEN="SuperSecret" # Your OpenHab API/Token TARGETFUNC=${1^^} TARGETITEM=$2 TARGETPAYLOAD=$3 if ! command -v jq &> /dev/null then echo "jq could not be found" exit 2 fi function postItem { ITEM=$1 PAYLOAD=$2 RESULT=$(curl -s -X POST "http://${OPENHAB}:8080/rest/items/${ITEM}" -H "accept: */*" -H "Content-Type: text/plain" -H "Authorization: Bearer ${TOKEN}" -d "${PAYLOAD}") } function GetItem { ITEM=$1 KEY=$2 RESULT=$(curl -s -X GET "http://${OPENHAB}:8080/rest/items/${ITEM}" -H "accept: application/json" -H "Authorization: Bearer ${TOKEN}" | jq -r .${KEY}) echo "${RESULT}" } function ShowHelp { echo -e "Missing Parms Usage: api.sh [GETSTATE/GETALL/PUT] [OH ITEM] [PAYLOAD(opt)] api.sh getstate oh_door21 api.sh getall oh_door21 napi.sh put oh_door21 CLOSED " exit 2 } if [ -z "$TARGETFUNC" ]; then ShowHelp; fi if [ -z "$TARGETITEM" ]; then ShowHelp; fi case $TARGETFUNC in PUT) postItem "${TARGETITEM}" "${TARGETPAYLOAD}" ;; GETSTATE) RESULT=$(GetItem "${TARGETITEM}" | jq -r .state) echo -e "${RESULT}" ;; GETALL) RESULT=$(GetItem "${TARGETITEM}") echo -e "${RESULT}" ;; *) ShowHelp ;; esac