Giter Site home page Giter Site logo

paoliniluis / weewx-customapi Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 26 KB

WeeWX Extension for sending your data to a custom REST API

License: MIT License

Python 100.00%
weewx weewx-software weewx-pusher weewx-service weather-api weather-station

weewx-customapi's Introduction

WeeWX Extension for sending your data to a custom REST API

DISCLAIMER: I'm not responsible to anything that happens to your weather station, or WeeWX installation or anything in between. This is just a bit of code I added to my WeeWX installation as I wanted to send the data that was sent to Weather Underground to a custom API that I built.

Steps to make this work:

  1. install WeeWX
  2. Make sure you're currently sending data to any sort of destination (like WU or AWEKAS)
  3. include the following code in your "restx.py" file (depending on the distro where you installed can be in different locations. If you don't know where the file is just run the following command "cd / && find | grep restx.py" without quotes):
class StdCustomApi(StdRESTful):
    """Specialized version of the Ambient protocol for your custom API.
    """

    def __init__(self, engine, config_dict):

        super(StdCustomApi, self).__init__(engine, config_dict)

        _ambient_dict = get_site_dict(
            config_dict, 'CustomAPI', 'station', 'password')
        if _ambient_dict is None:
            return

        _essentials_dict = search_up(config_dict['StdRESTful']['CustomAPI'], 'Essentials', {})

        syslog.syslog(syslog.LOG_DEBUG, "restx: CustomAPI essentials: %s" % _essentials_dict)

        # Get the manager dictionary:
        _manager_dict = weewx.manager.get_manager_dict_from_config(
            config_dict, 'wx_binding')

        # The default is to not do an archive post if a rapidfire post
        # has been specified, but this can be overridden
        do_rapidfire_post = to_bool(_ambient_dict.pop('rapidfire', False))
        do_archive_post = to_bool(_ambient_dict.pop('archive_post',
                                                    not do_rapidfire_post))

        # Get the URL of the custom API
        apiURLConfig = _ambient_dict.pop('apiURL')

        if do_archive_post:
            _ambient_dict.setdefault('server_url', apiURLConfig)
            self.archive_queue = Queue.Queue()
            self.archive_thread = AmbientThread(
                self.archive_queue,
                _manager_dict,
                protocol_name="CustomAPI",
                essentials=_essentials_dict,
                **_ambient_dict)
            self.archive_thread.start()
            self.bind(weewx.NEW_ARCHIVE_RECORD, self.new_archive_record)
            syslog.syslog(syslog.LOG_INFO, "restx: CustomAPI: "
                                           "Data for station %s will be posted" %
                          _ambient_dict['station'])

        if do_rapidfire_post:
            _ambient_dict.setdefault('server_url', apiURLConfig)
            _ambient_dict.setdefault('log_success', False)
            _ambient_dict.setdefault('log_failure', False)
            _ambient_dict.setdefault('max_backlog', 0)
            _ambient_dict.setdefault('max_tries', 1)
            _ambient_dict.setdefault('rtfreq',  2.5)
            self.cached_values = CachedValues()
            self.loop_queue = Queue.Queue()
            self.loop_thread = AmbientLoopThread(
                self.loop_queue,
                _manager_dict,
                protocol_name="CustomAPI",
                essentials=_essentials_dict,
                **_ambient_dict)
            self.loop_thread.start()
            self.bind(weewx.NEW_LOOP_PACKET, self.new_loop_packet)
            syslog.syslog(syslog.LOG_INFO, "restx: CustomAPI: "
                                           "Data for station %s will be posted" %
                          _ambient_dict['station'])

    def new_loop_packet(self, event):
        """Puts new LOOP packets in the loop queue"""
        if weewx.debug >= 3:
            syslog.syslog(syslog.LOG_DEBUG, "restx: raw packet: %s" % to_sorted_string(event.packet))
        self.cached_values.update(event.packet, event.packet['dateTime'])
        if weewx.debug >= 3:
            syslog.syslog(syslog.LOG_DEBUG, "restx: cached packet: %s" %
                          to_sorted_string(self.cached_values.get_packet(event.packet['dateTime'])))
        self.loop_queue.put(
            self.cached_values.get_packet(event.packet['dateTime']))

    def new_archive_record(self, event):
        """Puts new archive records in the archive queue"""
        self.archive_queue.put(event.record)
  1. include the following code in your weewx.conf file (same as above): Section [StdRESTful]
    [[CustomAPI]]
        # This section is for configuring posts to your own custom REST API.

        # If you wish to do this, set the option 'enable' to true,
        # and specify a station (e.g., 'KORHOODR3') and password, if you have any.
        # To guard against parsing errors, put the password in quotes.
        enable = false
        apiURL= "http://myCustomApiURL"
        station  = replace_me
        password = "replace_me"

        # Set the following to True to have weewx pull the values every one second and hit your API with them
        # Not all hardware can support it. See the User's Guide.
        rapidfire = False

Section [Engine]

restful_services = weewx.restx.StdStationRegistry, weewx.restx.StdWunderground, weewx.restx.StdPWSweather, weewx.restx.StdCWOP, weewx.restx.StdWOW, weewx.restx.StdAWEKAS, weewx.restx.StdCustomAPI

In the [[CustomAPI]] section in weewx.conf you'll be able to include the address of the api you want to send the info to. As I didn't tinker much with how the platform sends the data, it is currently sending the data through a GET or POST request (I forgot the method) and values as parameters in a querystring (yup, you figured it out)

Enjoy!

weewx-customapi's People

Contributors

paoliniluis avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.