| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | from flask import render_template, flash, redirect, request, Response | 
					
						
							|  |  |  | from app import app | 
					
						
							|  |  |  | from app.forms import LoginForm | 
					
						
							|  |  |  | import json | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from inkycal import Display | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from .config_loader import get_all_config | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | settings = get_all_config() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Home | 
					
						
							|  |  |  | @app.route('/') | 
					
						
							|  |  |  | @app.route('/index') | 
					
						
							|  |  |  | def index(): | 
					
						
							|  |  |  |     return render_template('index.html', title='Home') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Wifi-setup | 
					
						
							|  |  |  | @app.route('/setup_wifi') | 
					
						
							|  |  |  | def wifi_setup(): | 
					
						
							|  |  |  |     return render_template('wifi.html', title='Wifi-setup') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Inkycal-setup | 
					
						
							| 
									
										
										
										
											2020-11-21 16:28:05 +01:00
										 |  |  | @app.route('/inkycal-config-v2-0-0', methods=['GET', 'POST']) | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | def inkycal_config(): | 
					
						
							|  |  |  |     form = LoginForm() | 
					
						
							|  |  |  |     if form.validate_on_submit(): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # General epaper settings | 
					
						
							|  |  |  |         model = request.form.get('model') | 
					
						
							|  |  |  |         update_interval = int(request.form.get('update_interval')) | 
					
						
							|  |  |  |         calibration_hour_1 = int(request.form.get('calibration_hour_1')) | 
					
						
							|  |  |  |         calibration_hour_2 = int(request.form.get('calibration_hour_2')) | 
					
						
							|  |  |  |         calibration_hour_3 = int(request.form.get('calibration_hour_3')) | 
					
						
							|  |  |  |         orientation: int(request.form.get('orientation')) | 
					
						
							|  |  |  |         language = request.form.get('language') | 
					
						
							|  |  |  |         info_section = True if (request.form.get('info_section') == "on") else False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-21 16:28:05 +01:00
										 |  |  |         info_height = int(request.form.get('info_section_height')) if info_section == True else None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  |         # template for basic settings | 
					
						
							|  |  |  |         template = { | 
					
						
							|  |  |  |             "model": model, | 
					
						
							|  |  |  |             "update_interval": update_interval, | 
					
						
							|  |  |  |             "orientation": int(request.form.get('orientation')), | 
					
						
							|  |  |  |             "info_section": info_section, | 
					
						
							| 
									
										
										
										
											2020-11-21 16:28:05 +01:00
										 |  |  |             "info_section_height": info_height, | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  |             "calibration_hours": [calibration_hour_1, calibration_hour_2, calibration_hour_3], | 
					
						
							|  |  |  |             "modules": [], | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-21 16:28:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  |         # common module config (shared by all modules) | 
					
						
							|  |  |  |         padding_x = int(request.form.get('padding_x')) | 
					
						
							|  |  |  |         padding_y = int(request.form.get('padding_y')) | 
					
						
							|  |  |  |         fontsize = int(request.form.get('fontsize')) | 
					
						
							|  |  |  |         language = request.form.get('language') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-12 10:26:58 +01:00
										 |  |  |         common_settings = {"padding_x":padding_x, "padding_y":padding_y, "fontsize":fontsize, "language":language} | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # loop over the modules, add their config data based on user selection, merge the common_settings into each module's config | 
					
						
							| 
									
										
										
										
											2020-11-21 16:28:05 +01:00
										 |  |  |         no_of_modules = int(request.form.get("module_counter")) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # display size ---- Since Inkycal works in vertical mode (only), the width and height have to be flipped here | 
					
						
							|  |  |  |         display_size = Display.get_display_size(model) # returns width,height but flipping these for vertical mode | 
					
						
							|  |  |  |         height, width = int(display_size[0]), int(display_size[1]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # If info section was active, substract the height of the info section from the display height | 
					
						
							|  |  |  |         if info_section == True: | 
					
						
							|  |  |  |             height = height-info_height | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # get all module heights, calculate single part | 
					
						
							|  |  |  |         module_sizes = [int(request.form.get("module"+str(i)+"_height")) for i in range(1, no_of_modules+1)] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if sum(module_sizes) != 0: | 
					
						
							|  |  |  |             single_part = height / sum(module_sizes) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for i in range(1, no_of_modules+1): | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  |             conf = {} | 
					
						
							| 
									
										
										
										
											2020-11-21 16:28:05 +01:00
										 |  |  |             module = 'selected_module'+str(i) | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-21 16:28:05 +01:00
										 |  |  |             if request.form.get(module) != "None": | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  |                 conf = {"position":i , "name": request.form.get(module), "config":{}} | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 for modules in settings: | 
					
						
							|  |  |  |                     if modules['name'] == request.form.get(module): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-21 16:28:05 +01:00
										 |  |  |                         module_height = int( request.form.get("module"+str(i)+"_height") ) | 
					
						
							|  |  |  |                         conf['config']['size'] = (width, int(single_part*module_height) ) | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  |                         # Add required fields to the config of the module in question | 
					
						
							| 
									
										
										
										
											2020-11-12 10:26:58 +01:00
										 |  |  |                         # True/False choices are converted to string for some reason, leading to incorrect values | 
					
						
							|  |  |  |                         # Convert "True" to True, "False" to False and empty input to None | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  |                         if 'requires' in modules: | 
					
						
							|  |  |  |                             for key in modules['requires']: | 
					
						
							| 
									
										
										
										
											2020-11-21 16:28:05 +01:00
										 |  |  |                                 val = request.form.get('module'+str(i)+'_'+key).replace(" ", "") | 
					
						
							| 
									
										
										
										
											2020-11-12 10:26:58 +01:00
										 |  |  |                                 if val == "True": | 
					
						
							|  |  |  |                                     val = True | 
					
						
							|  |  |  |                                 elif val == "False": | 
					
						
							|  |  |  |                                     val = False | 
					
						
							|  |  |  |                                 elif val == "": | 
					
						
							|  |  |  |                                         val = None | 
					
						
							|  |  |  |                                 conf['config'][key] = val | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                         # For optional fields, check if user entered/selected something. If not, and a default value was given, | 
					
						
							|  |  |  |                         # use the default value, else set the value of that optional key as None | 
					
						
							| 
									
										
										
										
											2020-11-12 10:26:58 +01:00
										 |  |  |                         # True/False choices are converted to string for some reason, leading to incorrect values | 
					
						
							|  |  |  |                         # Convert "True" to True, "False" to False and empty input to None | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  |                         if 'optional' in modules: | 
					
						
							|  |  |  |                             for key in modules['optional']: | 
					
						
							|  |  |  |                                 if request.form.get(module+'_'+key): | 
					
						
							| 
									
										
										
										
											2020-11-21 16:28:05 +01:00
										 |  |  |                                     val = request.form.get('module'+str(i)+'_'+key).replace(" ", "") | 
					
						
							| 
									
										
										
										
											2020-11-12 10:26:58 +01:00
										 |  |  |                                     if val == "True": | 
					
						
							|  |  |  |                                         val = True | 
					
						
							|  |  |  |                                     elif val == "False": | 
					
						
							|  |  |  |                                         val = False | 
					
						
							|  |  |  |                                     elif val == "": | 
					
						
							|  |  |  |                                         val = None | 
					
						
							|  |  |  |                                     conf['config'][key] = val | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  |                                 else: | 
					
						
							|  |  |  |                                     if "default" in modules["optional"][key]: | 
					
						
							|  |  |  |                                         conf['config'][key] = modules["optional"][key]["default"] | 
					
						
							|  |  |  |                                     else: | 
					
						
							|  |  |  |                                         conf['config'][key] = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 # update the config dictionary | 
					
						
							| 
									
										
										
										
											2020-11-10 22:48:04 +01:00
										 |  |  |                 conf["config"].update(common_settings) | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  |                 template['modules'].append(conf) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Send the data back to the server side in json dumps and convert the response to a downloadable settings.json file | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2020-11-12 10:26:58 +01:00
										 |  |  |             user_settings = json.dumps(template, indent=4).encode('utf-8') | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  |             response = Response(user_settings, mimetype="application/json", direct_passthrough=True) | 
					
						
							|  |  |  |             response.headers['Content-Disposition'] = 'attachment; filename=settings.json' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return response | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         except Exception as e: | 
					
						
							|  |  |  |             flash(str(e)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-21 16:28:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return render_template('inkycal-config-v2-0-0.html', title='Inkycal-Setup', conf=settings, form=form) | 
					
						
							|  |  |  | 
 |