Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Push Notification
#3
(2017 Apr 19, 02:42 PM)dan Wrote: Hi Chris,

Sounds interesting. I'm not aware of anyone else using pushover with SIP. How would it be used?

There is already blinker included with SIP that provides notifications as demonstrated in the signaling_examples.py in the plugins directory.  There is also an MQTT suite of plugins that can provide communication between different devices and even with a JavaScript client using websockets.

Dan
Hey Dan - 
I put a few files together following your proto example, but also needed to add a new blinker event for "station_completed" in helpers.py:
station_completed = signal('station_completed')
def report_station_completed(txt=None):
    """
    Send blinker signal indicating that station has completed.
    """
    station_completed.send()
Then I added the report_station_completed function in the section that adds the logging.
        lines = []
        report_station_completed()
        lines.append(logline + '\n')

Attached are the new plugin python file and the html template.

Edit: Can't figure out how to attach python file. Here's the contents:

Code:
# !/usr/bin/env python

import web  # web.py framework
import gv  # Get access to SIP's settings
from urls import urls  # Get access to SIP's URLs
from sip import template_render  #  Needed for working with web.py templates
from webpages import ProtectedPage  # Needed for security
import json  # for working with data file
from blinker import signal
import time
import datetime
from pushover import Client

# Add new URLs to access classes in this plugin.
urls.extend([
   '/pushoverNotif-sp', 'plugins.pushoverNotif.settings',
   '/pushoverNotif-save', 'plugins.pushoverNotif.save_settings'

   ])

# Add this plugin to the PLUGINS menu ['Menu Name', 'URL'], (Optional)
gv.plugin_menu.append(['PushOver Notification', '/pushoverNotif-sp'])

def empty_function():  # Only a place holder
   """
   Functions defined here can be called by classes
   or run when the plugin is loaded. See comment at end.
   """
   pass


class settings(ProtectedPage):
   """
   Load an html page for entering plugin settings.
   """

   def GET(self):
       try:
           with open('./data/pushoverNotif.json', 'r') as f:  # Read settings from json file if it exists
               settings = json.load(f)
       except IOError:  # If file does not exist return empty value
           settings = {}  # Default settings. can be list, dictionary, etc.
       return template_render.pushoverNotif(settings)  # open settings page

class save_settings(ProtectedPage):
   """
   Save user input to json file.
   Will create or update file when SUBMIT button is clicked
   CheckBoxes only appear in qdict if they are checked.
   """

   def GET(self):
       qdict = web.input()  # Dictionary of values returned as query string from settings page.
#        print qdict  # for testing
       with open('./data/pushoverNotif.json', 'w') as f:  # Edit: change name of json file
            json.dump(qdict, f) # save to file
       raise web.seeother('/')  # Return user to home page.

#  Run when plugin is loaded
empty_function()


def timestr(t):
   """
   Convert duration in seconds to string in the form mm:ss.

   @type  t: int
   @param t: duration in seconds
   @rtype:   string
   @return:  duration as "mm:ss"
   """
   return str((t / 60 >> 0) / 10 >> 0) + str((t / 60 >> 0) % 10) + ":" + str((t % 60 >> 0) / 10 >> 0) + str(
       (t % 60 >> 0) % 10)

### Reboot ###
def notify_rebooted(name, **kw):
   Client().send_message("SIP is rebooting")

rebooted = signal('rebooted')
rebooted.connect(notify_rebooted)

### Restart ###
def notify_restart(name, **kw):
   Client().send_message("SIP is restarting")

restart = signal('restart')
restart.connect(notify_restart)

### Station Completed ###
def notify_station_completed(name, **kw):
   if gv.lrun[0] == 2:
       Client().send_message(gv.snames[gv.lrun[0]] + " ran for " + timestr(gv.lrun[2]))
   else:
       print "Station is not station 2... not sending message"

station_completed = signal('station_completed')
station_completed.connect(notify_station_completed)

### rain changed ##
def notify_rain_changed(name, **kw):
   print "Rain changed (from plugin)"
   #  Programs are in gv.pd and /data/programs.json

rain_changed = signal('rain_changed')
rain_changed.connect(notify_rain_changed)


This also requires you do the following steps beforehand:

pip install python-pushover
root@baergarden:~# cat /root/.pushoverrc

[Default]
api_token=<your_api_key>
user_key=<your_user_key>
As you can see in the python file, I was only really interested in getting push notifications from certain stations.  Ideally this would be read from the config file, but I think I would need to add an attribute to a json config in data.
Work in progress... but figured I would show you what I was doing to see if you had any suggestions.
Let me know what you think.
Chris


Attached Files
.html   pushoverNotif.html (Size: 1.89 KB / Downloads: 3)
Reply


Messages In This Thread
Push Notification - by chrisbaer10 - 2017 Apr 19, 02:08 AM
RE: Push Notification - by dan - 2017 Apr 19, 02:42 PM
RE: Push Notification - by chrisbaer10 - 2017 Apr 19, 11:35 PM
RE: Push Notification - by dan - 2017 Apr 20, 12:30 AM
RE: Push Notification - by dan - 2017 Apr 23, 10:00 PM
RE: Push Notification - by msev - 2017 Jun 27, 11:24 AM

Forum Jump:


Users browsing this thread: 2 Guest(s)