# Elgato from the shell

Once the lamp joined my local WLAN network (and I know the IP address), I use this script for controlling it.

## Requirements

* You must have `cURL` and `jq` installed. (`sudo apt-get install jq curl`)
* The script sets a local `ELGATO_KEY_LIGHT_IPADDRESS` variable. Tweak that part to your needs.

## Manual

* `e` turns it on or off
* Brightness
  * `e +` increases the brightness
  * `e -` decreases the brightness
* Color tempreature
  * `e 1` selects the coldest color temperature
  * `e 2` selects a medium color temperature
  * `e 3` selects the warmest color temperature
* Identify the light
  * `e i` makes the light blink three times

## Contents of `/usr/local/bin/e`

```bash
#!/bin/bash

function _elgatoGet {
  local ip="$1"
  local path="$2"

  curl --silent \
      --url "http://${ip}:9123/elgato/${path}"
}

function _elgatoPut {
  local ip="$1"
  local path="$2"
  local msg="$3"

  echo "${msg}" | curl --silent --request PUT \
      --header "Content-Type: application/json" \
      --url "http://${ip}:9123/elgato/${path}" \
      --data @-
}

function _elgatoIdentify {
  local ip="$1"

  local displayName="$( _elgatoGet "${ip}" "accessory-info" | jq -r ".displayName" )"

  echo "Light ${displayName} is blinking"

  # empty POST
  curl --silent \
    --request POST\
    --url "http://${ip}:9123/elgato/identify"
}

function _elgatoTurnOnOff {
  local ip="$1"
	local current="$( _elgatoGet "${ip}" "lights" | jq ".lights[0].on" )"
	local new="$(( 1-${current} ))"
	local state="$( _elgatoPut "${ip}" "lights" "{ \"lights\" : [ { \"on\" : ${new} } ] }" | jq ".lights[0].on" )"
  local displayName="$( _elgatoGet "${ip}" "accessory-info" | jq -r ".displayName" )"

  echo "$( if [[ "${state}" == "0" ]] ; then echo "Light \"${displayName}\" turned off" ; else echo "Light \"${displayName}\" turned on" ; fi ; )"
}

function _elgatoTemperature {
  local ip="$1"
  local temperature="$2"

  # temperature must be in range [143..344]
  if [[ ${temperature} -lt 143 ]] ; then 
    temperature="143" ;
  elif [[ ${temperature} -gt 344 ]] ; then 
    temperature="344" ;
  fi
  
  echo "Temperature: $( _elgatoPut "${ip}" lights \
    "{\"lights\":[{ \"on\": 1 , \"temperature\": ${temperature} }]}" \
    | jq '.lights[0].temperature' )"
}

function _elgatoBrightness {
  local ip="$1"
  local brightnessDelta="$2"

  local currentBrightness="$( _elgatoGet "${ip}" lights | jq ".lights[0].brightness" )"
  local newBrightness="$(( $currentBrightness + $brightnessDelta ))"

  # brightness must be in range [3..100]
  if [[ ${newBrightness} -lt 3 ]] ; then 
    newBrightness="3" ;
  elif [[ ${newBrightness} -gt 100 ]] ; then 
    newBrightness="100" ;
  fi
  
  echo "Brightness: $( _elgatoPut "${ip}" lights \
    "{\"lights\":[{ \"on\": 1 , \"brightness\": ${newBrightness} }]}" \
    | jq '.lights[0].brightness' )"
}

# ELGATO_KEY_LIGHT_IPADDRESS="192.168.0.106"
ELGATO_KEY_LIGHT_IPADDRESS="elgatoip"

if [[ $# -eq 0 ]] ; then 
  echo "$( _elgatoTurnOnOff  "${ELGATO_KEY_LIGHT_IPADDRESS}" )"
elif [[ $# -eq 1 ]] ; then
  command=$1
  if [[ "${command}" == "+" ]] ; then
    echo "$( _elgatoBrightness  "${ELGATO_KEY_LIGHT_IPADDRESS}" 20 )"
  elif [[ "${command}" == "-" ]] ; then
    echo "$( _elgatoBrightness  "${ELGATO_KEY_LIGHT_IPADDRESS}" -20 )"
  elif [[ "${command}" == "1" ]] ; then
    echo "$( _elgatoTemperature "${ELGATO_KEY_LIGHT_IPADDRESS}" 143 )"
  elif [[ "${command}" == "2" ]] ; then
    echo "$( _elgatoTemperature "${ELGATO_KEY_LIGHT_IPADDRESS}" 243 )"
  elif [[ "${command}" == "3" ]] ; then
    echo "$( _elgatoTemperature "${ELGATO_KEY_LIGHT_IPADDRESS}" 344 )"
  elif [[ "${command}" == "i" ]] ; then
    echo "$( _elgatoIdentify "${ELGATO_KEY_LIGHT_IPADDRESS}" )"
  fi
fi
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cookbook.geuer-pollmann.de/productivity/elgato.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
