2022-07-29 19:49:14 -05:00
|
|
|
# cherry-sun - periodically publishes sun attributes to mqtt
|
|
|
|
# Copyright (C) 2021 Bob Carroll <bob.carroll@alum.rit.edu>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import asyncio
|
2022-07-29 19:49:25 -05:00
|
|
|
import logging
|
|
|
|
from datetime import datetime
|
2022-07-29 19:49:14 -05:00
|
|
|
|
|
|
|
import yaml
|
|
|
|
from dns.asyncresolver import resolve
|
|
|
|
from asyncio_mqtt import Client
|
2024-01-17 17:06:34 -06:00
|
|
|
import umsgpack
|
2022-07-29 19:49:14 -05:00
|
|
|
|
|
|
|
from astral import LocationInfo
|
|
|
|
from astral.location import Location
|
|
|
|
|
2022-07-29 19:49:25 -05:00
|
|
|
from dateutil.tz import gettz
|
2022-07-29 19:49:14 -05:00
|
|
|
|
|
|
|
|
2024-01-17 17:06:34 -06:00
|
|
|
async def compute_direction(location):
|
2022-07-29 19:49:14 -05:00
|
|
|
"""
|
2022-07-29 19:49:25 -05:00
|
|
|
Determines if the sun is rising or setting.
|
2022-07-29 19:49:14 -05:00
|
|
|
|
2022-07-29 19:49:25 -05:00
|
|
|
:param location: astral Location object
|
|
|
|
:returns: 1 if rising, 0 if setting
|
|
|
|
"""
|
2023-01-02 09:20:58 -06:00
|
|
|
now = datetime.now().astimezone(gettz(location.timezone))
|
2022-07-29 19:49:25 -05:00
|
|
|
return int(location.midnight() <= now <= location.noon())
|
2022-07-29 19:49:14 -05:00
|
|
|
|
|
|
|
|
2022-07-29 19:49:25 -05:00
|
|
|
async def get_location(config):
|
2022-07-29 19:49:14 -05:00
|
|
|
"""
|
|
|
|
Gets a location object from the given configuration.
|
|
|
|
|
|
|
|
:param config: configuration dictionary
|
|
|
|
:returns: astral Location object
|
|
|
|
"""
|
|
|
|
info = config.get('location', {})
|
|
|
|
return Location(LocationInfo(info.get('name'), '', info.get('timezone'),
|
|
|
|
info.get('latitude'), info.get('longitude')))
|
|
|
|
|
|
|
|
|
2022-07-29 19:49:25 -05:00
|
|
|
async def get_broker(config):
|
|
|
|
"""
|
|
|
|
Gets the mqtt broker address from an SRV record.
|
|
|
|
|
|
|
|
:param config: configuration dictionary
|
|
|
|
:returns: the broker address
|
|
|
|
"""
|
|
|
|
broker = config.get('mqtt', {}).get('broker')
|
|
|
|
if broker is not None:
|
|
|
|
return broker
|
|
|
|
|
|
|
|
answer = await resolve('_mqtt._tcp', 'SRV', search=True)
|
|
|
|
return next((x.target.to_text() for x in answer))
|
|
|
|
|
|
|
|
|
2022-07-29 19:49:14 -05:00
|
|
|
async def update(config):
|
|
|
|
"""
|
|
|
|
Publishes sun update events.
|
|
|
|
|
|
|
|
:param config: configuration dictionary
|
|
|
|
"""
|
|
|
|
async with Client(await get_broker(config), client_id='cherry-sun') as client:
|
2022-07-29 19:49:25 -05:00
|
|
|
logging.info('Connected to mqtt broker')
|
|
|
|
location = await get_location(config)
|
|
|
|
|
|
|
|
logging.debug(f'City: {location.name}')
|
|
|
|
await client.publish('sun/city', location.name, retain=True)
|
|
|
|
|
|
|
|
logging.debug(f'Latitude: {location.latitude}')
|
|
|
|
await client.publish('sun/latitude', location.latitude, retain=True)
|
|
|
|
|
|
|
|
logging.debug(f'Longitude: {location.longitude}')
|
|
|
|
await client.publish('sun/longitude', location.longitude, retain=True)
|
|
|
|
|
|
|
|
logging.debug(f'Timezone: {location.timezone}')
|
|
|
|
await client.publish('sun/timezone', location.timezone, retain=True)
|
2022-07-29 19:49:14 -05:00
|
|
|
|
|
|
|
while True:
|
2022-07-29 19:49:25 -05:00
|
|
|
elevation = location.solar_elevation()
|
|
|
|
logging.debug(f'Elevation: {elevation}')
|
|
|
|
await client.publish('sun/elevation', elevation, retain=True)
|
|
|
|
|
|
|
|
midnight = str(location.midnight())
|
|
|
|
logging.debug(f'Midnight: {midnight}')
|
|
|
|
await client.publish('sun/midnight', midnight, retain=True)
|
|
|
|
|
|
|
|
noon = str(location.noon())
|
|
|
|
logging.debug(f'Noon: {noon}')
|
|
|
|
await client.publish('sun/noon', noon, retain=True)
|
|
|
|
|
2024-01-17 17:06:34 -06:00
|
|
|
direction = await compute_direction(location)
|
|
|
|
logging.debug(f'Direction: {direction}')
|
|
|
|
await client.publish('sun/direction', direction, retain=True)
|
|
|
|
|
|
|
|
solar_state = {'elevation': elevation,
|
|
|
|
'direction': direction,
|
|
|
|
'noon': location.solar_elevation(location.noon())}
|
|
|
|
logging.debug(f'Solar State: {solar_state}')
|
|
|
|
await client.publish('sun/state', umsgpack.packb(solar_state), retain=True)
|
2022-07-29 19:49:25 -05:00
|
|
|
|
|
|
|
sunrise = str(location.sunrise())
|
|
|
|
logging.debug(f'Sunrise: {sunrise}')
|
|
|
|
await client.publish('sun/sunrise', sunrise, retain=True)
|
|
|
|
|
|
|
|
sunset = str(location.sunset())
|
|
|
|
logging.debug(f'Sunset: {sunset}')
|
|
|
|
await client.publish('sun/sunset', sunset, retain=True)
|
|
|
|
|
2022-07-29 19:49:14 -05:00
|
|
|
await asyncio.sleep(60)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""
|
|
|
|
CLI entry point.
|
|
|
|
"""
|
|
|
|
if len(sys.argv) != 2:
|
|
|
|
print('USAGE: cherry-sun <config file>')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
with open(sys.argv[1], 'r') as f:
|
|
|
|
config = yaml.safe_load(f)
|
|
|
|
|
2022-07-29 19:49:25 -05:00
|
|
|
log = config.get('log', {})
|
|
|
|
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s',
|
|
|
|
level=log.get('level', logging.ERROR))
|
|
|
|
|
2022-07-29 19:49:14 -05:00
|
|
|
try:
|
|
|
|
asyncio.run(update(config))
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|