-
Notifications
You must be signed in to change notification settings - Fork 22
website_handler
The handler script is imported by host_webpage.py and used to respond to messages sent from the webpage, this can be used to enable buttons or controls on the webpage to perform actions on the pigrow such as running info modules, creating datawalls or activating devices. It's designed so that it can be upgraded and modified to use with custom webpages without having to rework host_webpage.py.
the basic handler has three basic functions;
info:<info module name>
info:boxname
this runs an info module from the info_module folder and returns the output.
log:<log name / path>
log:selflog.txt
reads the last five entries of a log file from the pigrow/logs folder or from the full path of the file when supplied.
datawall:<preset>+<module>
datawall:basic_weekly
datawall:basic_weekly+basic_info
creates a datawall and returns the name of the file as found in the pigrow/graphs folder. When no module is supplied it creates the graphs listed in the preset and returns the name of the first graph created (datawall_graph_0.png)
The script used for hosting a webpages uses Flask to collect text strings sent from the webpage, this text is then sent to a function in the imported handler called call_command which takes the text string, parses it and returns the response as a text string, which may include the path to an image.
@app.route('/process_text', methods=['POST'])
def process_text():
text = request.json['text']
response = Handler.call_command(text)
return response
the script also uses send_from_directory to allow use of files from the pigrow graphs folder.
@app.route('/graphs/<filename>')
def graphs_image(filename):
graphs_folder = os.path.join(os.path.expanduser('~'), 'Pigrow/graphs/')
return send_from_directory(graphs_folder, filename)
To load an image from the Pigrow graphs use the web path /graphs/ followed by the filename of the image.
The basic example handler is very simple, it has a class called Handler which contains call_command, this checks for a : and if present splits either side of it to create a key:value pair, if no : is present it uses the whole thing as the key - it then checks if that key is present in a list of commands, each command is associated with a function that performs the required action and returns a text string which is then passed back to the webpage.
To add a new command you can simply add an extra entry to the commands dict
commands = {
"info": self.info_function,
"log": self.showlog_function,
"datawall": self.create_datawall
}
if the command log: is sent to the handler it'll run the showlog_function, here's a simplified version;
def showlog_function(self, value):
log_to_load = value
cmd = "tail -n 5 " + log_to_load
return self.run_local(cmd)
it just uses the function run_local to run a shell command on the pi and return the output.
note - the actual function will expand a filename to include the full path of the pigrow logs folder if no path is supplied.