OpenSprinkler Forums Hardware Questions OpenSprinkler Pi (OSPi) Custom Additions to Interval Program for OSPi?

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #22497

    djagerif
    Participant

    I want to add some custom commands to the Interval program running on OSPi. The problem I have is that every time I do an update then my additions are gone.

    Is there a way to include a custom file, say called ‘custom.py’, that will never be packaged with a new distribution or overwritten?

    If that is possible, how can I move my new web addition to this new file and still have it working? Let’s take a custom command called ‘/hw’ or ‘hello_world’.

    As far as I can figure you need to update the urls structure to add this new command and then add the ‘hello_world’ method to return your data. If you now want to add variables to the returned data you would just add it in the method and it should work. If this file is now ‘external’ is it still possible and how would one go about doing something like this?

    Thanks,
    Ingo

    #24602

    djagerif
    Participant

    Ok, seeing that I didn’t get any response I went ahead and did my own thing. For starters I modified the ospi.py program to look for a file called custom.py, if found then it imports the classes from that file and also loads a different ‘urls’ declaration. If the file doesn’t exist then it loads the standard ‘urls’ definition and everything is as per normal.

    With the custom file I can add my own stuff to enhance the interval program without messing with the original program. Here are my changes for anyone interested.

    PS. I am sure there are better ways of doing it but hey, no one offered any advice.

    Just below the import statement I added the following and deleted the original ‘urls’ command.

     #### Load Custom Import file if it exists ####
    try:
    with open('./custom.py'):
    import custom
    urls = (
    '/', 'home',
    '/cv', 'change_values',
    '/vo', 'view_options',
    '/co', 'change_options',
    '/vs', 'view_stations',
    '/cs', 'change_stations', # name and master
    '/sn(d+?Z)', 'get_station', # regular expression, accepts any station number
    '/sn(d+?=d(&t=d+?Z)?)', 'set_station', # regular expression, accepts any digits
    '/vr', 'view_runonce',
    '/cr', 'change_runonce',
    '/vp', 'view_programs',
    '/mp', 'modify_program', # open 'Modify program' window
    '/cp', 'change_program',
    '/dp', 'delete_program',
    '/gp', 'graph_programs',
    '/vl', 'view_log',
    '/cl', 'clear_log',
    '/lo', 'log_options',
    '/c1', 'custom.custom_1', # Custom function 1
    '/c2', 'custom.custom_2', # Custom function 2
    '/c3', 'custom.custom_3', # Custom function 3
    )

    except IOError:
    print 'Custom Imports not found.'
    urls = (
    '/', 'home',
    '/cv', 'change_values',
    '/vo', 'view_options',
    '/co', 'change_options',
    '/vs', 'view_stations',
    '/cs', 'change_stations', # name and master
    '/sn(d+?Z)', 'get_station', # regular expression, accepts any station number
    '/sn(d+?=d(&t=d+?Z)?)', 'set_station', # regular expression, accepts any digits
    '/vr', 'view_runonce',
    '/cr', 'change_runonce',
    '/vp', 'view_programs',
    '/mp', 'modify_program', # open 'Modify program' window
    '/cp', 'change_program',
    '/dp', 'delete_program',
    '/gp', 'graph_programs',
    '/vl', 'view_log',
    '/cl', 'clear_log',
    '/lo', 'log_options',
    )

    Then I created a custom.py file containing my code I wish to add. Here is a sample

    #!/usr/bin/python
    """Updated 01/07/2013."""

    import ospi

    class custom_1:
    """View all the options above and also station names."""
    def GET(self):
    custpg = 'n'
    custpg += 'n'
    custpg += 'n'
    custpg += 'n'
    return custpg

    class custom_2:
    """ Custom Script 2 """
    def GET(self):
    custpg = 'n'
    #Insert Custom Code here.
    return custpg

    class custom_3:
    """ Custom Script 3 """
    def GET(self):
    custpg = 'n'
    #Insert Custom Code here.
    return custpg
    #24603

    Andrew
    Participant

    I’m a perl rather than a python programmer so I’m not certain this will work but if you import custom after the urls declaration couldn’t you then push the extra values into the urls array in custom.py? This would mean that the main script doesn’t have to have any of your code in it except the importing of the custom.py script (which I can see as definitely being worthwhile, or at least very easily re-added after any updates).

    #24604

    djagerif
    Participant

    I thought of that but being a .NET programmer I didn’t really know. Let’s see if a Python guru can suggest something that will work better.

    PS. It actually works with the testing I did.

    #24605

    djagerif
    Participant

    Thanks for making me investigate further. By changing the ospi.py urls variable to a ‘list’ variable I am now able to extend the list from the custom.py script.

    The ospi.py script change below, notice just the urls variable change and the addition of the custom script section.


    urls = [
    '/', 'home',
    '/cv', 'change_values',
    '/vo', 'view_options',
    '/co', 'change_options',
    '/vs', 'view_stations',
    '/cs', 'change_stations', # name and master
    '/sn(d+?Z)', 'get_station', # regular expression, accepts any station number
    '/sn(d+?=d(&t=d+?Z)?)', 'set_station', # regular expression, accepts any digits
    '/vr', 'view_runonce',
    '/cr', 'change_runonce',
    '/vp', 'view_programs',
    '/mp', 'modify_program', # open 'Modify program' window
    '/cp', 'change_program',
    '/dp', 'delete_program',
    '/gp', 'graph_programs',
    '/vl', 'view_log',
    '/cl', 'clear_log',
    '/lo', 'log_options1',
    ]

    #### Load Custom Import file if it exists ####
    try:
    with open('./custom.py'):
    import custom

    except IOError:
    print 'Custom Imports not found.'

    And in the custom.py script I have the following:

    import ospi

    ospi.urls.extend()

    class custom_1:
    """View all the options above and also station names."""
    def GET(self):
    custpg = 'n'
    custpg += 'n'
    custpg += 'n'
    custpg += 'n'
    return custpg

    class custom_2:
    """ Custom Script 2 """
    def GET(self):
    custpg = 'n'
    #Insert Custom Code here.
    return custpg

    class custom_3:
    """ Custom Script 3 """
    def GET(self):
    custpg = 'n'
    #Insert Custom Code here.
    return custpg
    #24606

    Dan in CA
    Participant

    djagerif,

    Sorry no one got back to you in a timely manner. Although I wouldn’t have had a good answer for you.

    What you worked out is something I was planning to investigate because I want to add some additional features without modifying the ospi.py program itself. I will add the code for importing a custom module to ospi.py and include a file named ospi_addon.py in the distribution so that anyone who wants to add custom features will have a starting point.

    This is a great addition to the project. Thanks for your contribution.

    Dan

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

OpenSprinkler Forums Hardware Questions OpenSprinkler Pi (OSPi) Custom Additions to Interval Program for OSPi?