Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ambient proxy #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions PWSPublish/ambientproxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import asyncio
import json
import websockets

class Proxy:
def __init__(self, host="ambient-proxy", port=8080, method="ws", verbose=False):
self.uri = f"{method}://{host}:{port}"
self.verbose = verbose

async def run_loop(self, callback):
if self.verbose:
print(f"ambient-proxy: connecting to {self.uri}")
async with websockets.connect(self.uri) as websocket:
if self.verbose:
print(f"ambient-proxy: connected to {self.uri}")
while 1:
data = await websocket.recv()
if self.verbose:
print(f"ambient-proxy: received data - {data}")
callback(json.loads(data))
37 changes: 30 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,29 @@ Your Wunderground API key is found under [member settings](https://www.wundergro

Create an API key and Application Key under your [Ambient Weather account](https://ambientweather.net/account)

You will need to have either `--wunderground` or `--ambient-weather` as arguments, or it won't connect to either.
You will need to have either `--wunderground`, `--ambient-weather`, `--ambient-proxy` as arguments, or it won't connect to anything.

## Ambient Proxy

[ambient-proxy](https://github.com/drakeapps/ambient-proxy) is a simple node websocket server that's just replaying the data received from the Ambient Weather Realtime API. I have continuously had issues with the Ambient Weather socket-io API, but have had zero issues with the node one.

This is a way to use the Node library but maintain all the

There is a docker-compose target, `publish-ambient-proxy`, that will pull and run the proxy. This uses the same environment variables as the normal Ambient Weather container.

## Command Line Arguments

```
python main.py -h
usage: main.py [-h] [--wunderground] --api-key API_KEY --station-id STATION_ID
[--ambient-weather] [--application-key APPLICATION_KEY]
[--influx] [--influx-host INFLUX_HOST]
[--influx-port INFLUX_PORT] [--influx-db INFLUX_DB]
python main.py -h
usage: main.py [-h] [--wunderground] [--api-key API_KEY]
[--station-id STATION_ID] [--ambient-weather]
[--ambient-api-key AMBIENT_API_KEY]
[--application-key APPLICATION_KEY] [--ambient-proxy]
[--ambient-proxy-host AMBIENT_PROXY_HOST]
[--ambient-proxy-port AMBIENT_PROXY_PORT]
[--ambient-proxy-method AMBIENT_PROXY_METHOD] [--influx]
[--influx-host INFLUX_HOST] [--influx-port INFLUX_PORT]
[--influx-db INFLUX_DB]
[--influx-measurement INFLUX_MEASUREMENT] [--websocket]
[--websocket-host WEBSOCKET_HOST]
[--websocket-port WEBSOCKET_PORT] [--mqtt]
Expand All @@ -28,12 +41,22 @@ Wunderground/Ambient Weather PWS Publisher
optional arguments:
-h, --help show this help message and exit
--wunderground enable wunderground api polling
--api-key API_KEY wunderground/ambient weather api key
--api-key API_KEY wunderground weather api key
--station-id STATION_ID
wunderground station id
--ambient-weather enable ambient weather api stream
--ambient-api-key AMBIENT_API_KEY
ambient weather api key
--application-key APPLICATION_KEY
ambient weather application key
--ambient-proxy enable ambient weather api stream using a separate
ambient websocket proxy
--ambient-proxy-host AMBIENT_PROXY_HOST
ambient weather proxy host
--ambient-proxy-port AMBIENT_PROXY_PORT
ambient weather proxy port
--ambient-proxy-method AMBIENT_PROXY_METHOD
ambient weather proxy method (ws or wss)
--influx publish to influxdb
--influx-host INFLUX_HOST
InfluxDB Host
Expand Down
19 changes: 18 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,22 @@ services:
ports:
- 6789:6789
env_file: .env
command: --ambient-api-key ${AMBIENT_API_KEY} --station-id ${STATION_ID} --application-key ${APPLICATION_KEY}--ambient-weather --mqtt --mqtt-broker mqtt.xrho.com --websocket --refresh-rate 60 --influx-host o.xrho.com --verbose --mqtt-prefix ambientWeather --influx --influx-db ambient-weather
command: --ambient-api-key ${AMBIENT_API_KEY} --station-id ${STATION_ID} --application-key ${APPLICATION_KEY} --ambient-weather --mqtt --mqtt-broker mqtt.xrho.com --websocket --websocket-host 0.0.0.0 --refresh-rate 60 --influx-host o.xrho.com --verbose --mqtt-prefix ambientWeather --influx --influx-db ambient-weather
ambient-proxy:
image: ghcr.io/drakeapps/ambient-proxy
restart: unless-stopped
env_file: .env
expose:
- 8080
publish-ambient-proxy:
image: ghcr.io/drakeapps/pws-publish
restart: unless-stopped
build:
context: .
ports:
- 6789:6789
env_file: .env
depends_on:
- ambient-proxy
command: --station-id ${STATION_ID} --ambient-proxy --ambient-proxy-host ambient-proxy --ambient-proxy-port 8080 --mqtt --mqtt-broker mqtt.xrho.com --websocket --websocket-host 0.0.0.0 --refresh-rate 60 --influx-host o.xrho.com --verbose --mqtt-prefix ambientWeather --influx --influx-db ambient-weather

20 changes: 20 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from PWSPublish import wunderground
from PWSPublish import ambient
from PWSPublish import ambientproxy
from PWSPublish import mqtt
from PWSPublish import websocket
from PWSPublish import influxdb
Expand All @@ -19,6 +20,11 @@
parser.add_argument('--ambient-api-key', help="ambient weather api key")
parser.add_argument('--application-key', help="ambient weather application key")

parser.add_argument('--ambient-proxy', help="enable ambient weather api stream using a separate ambient websocket proxy", action='store_true')
parser.add_argument('--ambient-proxy-host', help="ambient weather proxy host", default="ambient-proxy")
parser.add_argument('--ambient-proxy-port', help="ambient weather proxy port", type=int, default=8080)
parser.add_argument('--ambient-proxy-method', help="ambient weather proxy method (ws or wss)", default="ws")

parser.add_argument('--influx', help='publish to influxdb', action='store_true')
parser.add_argument("--influx-host", help="InfluxDB Host", default="localhost")
parser.add_argument("--influx-port", help="InfluxDB Port", type=int, default=8086)
Expand All @@ -44,15 +50,26 @@
args = parser.parse_args()

if args.wunderground:
if args.verbose:
print(f"Wunderground enabled")
wunderground_api = wunderground.API(args.api_key, args.station_id, units=args.units, verbose=args.verbose)
else:
wunderground_api = None

if args.ambient_weather:
if args.verbose:
print(f"Ambient Weather enabled")
ambient_weather_api = ambient.Stream(args.api_key, args.application_key, device=args.station_id, verbose=args.verbose)
else:
ambient_weather_api = None

if args.ambient_proxy:
if args.verbose:
print(f"Ambient Weather proxy enabled")
ambient_proxy = ambientproxy.Proxy(host=args.ambient_proxy_host, port=args.ambient_proxy_port, method=args.ambient_proxy_method, verbose=args.verbose)
else:
ambient_proxy = None

if args.mqtt:
if args.verbose:
print(f"MQTT enabled")
Expand Down Expand Up @@ -114,6 +131,9 @@ async def main():
task = asyncio.create_task(ambient_weather_api.run_loop(publish_data))
tasks.add(task)

if ambient_proxy:
task = asyncio.create_task(ambient_proxy.run_loop(publish_data))

await asyncio.gather(*tasks)


Expand Down