SIP
Push Notification - Printable Version

+- SIP (https://nosack.com/sipforum)
+-- Forum: SIP (Sustainable Irrigation Platform) (https://nosack.com/sipforum/forumdisplay.php?fid=1)
+--- Forum: Plugin suggestions (https://nosack.com/sipforum/forumdisplay.php?fid=7)
+--- Thread: Push Notification (/showthread.php?tid=72)



Push Notification - chrisbaer10 - 2017 Apr 19

I'm going to try to start working on a push notification plugin... has anyone else done this?  I use Pushover (there are many other options) and they have an API and python library, so just have to put it all together...

https://github.com/Thibauth/python-pushover


RE: Push Notification - dan - 2017 Apr 19

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


RE: Push Notification - chrisbaer10 - 2017 Apr 19

(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


RE: Push Notification - dan - 2017 Apr 20

Cool!
I'll take a look and get back to you.

I will be traveling for a couple of days so it won't be till I get back.

Dan


RE: Push Notification - dan - 2017 Apr 23

I loaded your plugin and was able to access the set-up page but only after a couple of tweaks.

Initially python would not load "from pushover import Client". I'm not sure what the problem is since I could load it from the Python command line.
I changed the import to "import pushover" and changed each "Client" reference to "pushover.Client" then it worked.

Also in the "urls.extend" function on line 16 of pushover.py I had to change both references from "pushoverNotif" to "pushover" to get the setup page to load.

It looks like you are off to a great start and I think this will be a very useful plugin.

I will add the "station_completed" blinker event to the main SIP branch on GitHub. Let me know if there any other events that would be useful.
Perhaps the config for your plugin could offer a list of events that a user could select to be notified about. Maybe a list of active stations with check boxes for the available events.

When you get things a little farther along I will purchase the android PushOver app for testing.

It might be good if you get a free GitHub account and post the files there. It would be easier to update.
If you are willing to share the plugin with the SIP community I will be happy to add it to the SIP plugin repository when you are ready.

Dan


RE: Push Notification - msev - 2017 Jun 27

For some other project (Mycroft virtual assistant) I made a Pushetta "skill". Would be cool if Pushetta would also be an option in SIP. The bonus of Pushetta is that it is free...I will take a look at how these plugins work, if I manage to write one...