Skip to content

Commit

Permalink
Merge pull request #4 from jsoika/features/docker_support
Browse files Browse the repository at this point in the history
Added files for docker support
scosman authored Sep 26, 2023
2 parents aecced6 + 55b241f commit dc8a800
Showing 4 changed files with 102 additions and 0 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -41,6 +41,22 @@ Releases have several builds/binaries, and you'll need to download the right one
- Windows with arm processor: I don't believe you
- Linux: you got this

## Docker

In case you don't want to use the binaries you can use docker.

Building the docker image

``` docker build -t airplay-music-watcher:latest . ```

Running the container

```docker run -v $(pwd)/config.json:/config/config.json --net=host airplay-music-watcher:latest```

or use the docker-compose.yaml file and simply call

``` docker compose up```

## JSON Config File Example

This example json file would say "stereo starting" when the Airplay device named "Stereo" starts playing, and say "stereo stopping" when it stops.
@@ -74,6 +90,30 @@ This project can be used to run any command line utility, and isn't tied to home

However, use with homebridge is the most common use case. For homebridge users, this works well with the [homebridge-config-ui-x API](https://github.com/oznu/homebridge-config-ui-x/wiki/API-Reference). Use the curl commands generated in the API UI in your JSON file; these commands will call the API and enable/disable smart home devices (like a smart plug controlling your vintage audio amp). You'll need to extend the [sessionTimeout](https://github.com/oznu/homebridge-config-ui-x/wiki/Config-Options) config option so your auth tokens don't expire.

## Home Assistant usage
To integrate with home assistant [web hooks](https://www.home-assistant.io/docs/automation/trigger/#webhook-trigger) can be easily used and called via curl.

The following example can be used in the config.json as command which will be called in case of an event

```curl -X POST -d 'device=my_device_name&action=playing' http://my_home_assistant_ip:8123/api/webhook/airplay-watcher-webhook```

An automation which handles these events could look like this
```
- id: airplay-watcher
alias: Airplay Watcher Automation
trigger:
- platform: webhook
webhook_id: "airplay-watcher-webhook"
allowed_methods:
- POST
local_only: true
action:
- service: notify.my_device_name
data:
title: Airplay Trigger
message: 'Airplay device {{ trigger.data.device}} changed to {{ trigger.data.action}} '
```
## How this works

This process monitors UDP traffic on your network for MDNS records from Airplay devices. Certain Airplay MDNS TXT records include a [bitmask](https://github.com/openairplay/airplay-spec/blob/master/src/status_flags.md) which let us infer the device's state (playing or not).
14 changes: 14 additions & 0 deletions config.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"actions": [
{
"device_name": "Stereo",
"action": "start_playing",
"command": "say 'stereo starting'"
},
{
"device_name": "Stereo",
"action": "end_playing",
"command": "say 'stereo stopping'"
}
]
}
10 changes: 10 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "3"
services:
airplay-music-watcher:
container_name: airplay-music-watcher
image: airplay-music-watcher:latest
network_mode: host
volumes:
- ./config.json:/config/config.json
restart: unless-stopped

38 changes: 38 additions & 0 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
FROM golang:1.20 AS build-stage

# Set destination for COPY
WORKDIR /

# Download Go modules
COPY go.mod go.sum ./
RUN go mod download

# Copy the source code. Note the slash at the end, as explained in
# https://docs.docker.com/engine/reference/builder/#copy
COPY . ./

# Build
RUN CGO_ENABLED=0 GOOS=linux go build -o /airplay-music-watcher

# Deploy the application binary into a lean image
FROM alpine/curl AS build-release-stage

WORKDIR /

COPY --from=build-stage /airplay-music-watcher /airplay-music-watcher

# Optional:
# To bind to a TCP port, runtime parameters must be supplied to the docker command.
# But we can document in the Dockerfile what ports
# the application is going to listen on by default.
# https://docs.docker.com/engine/reference/builder/#expose
EXPOSE 5353

# Run
USER root:root
VOLUME [ "/config" ]

ENTRYPOINT ["/airplay-music-watcher", "/config/config.json"]



0 comments on commit dc8a800

Please sign in to comment.