def build_console(): global msgs page = get_page() page = page.replace("{nerodia}", str(Nerodia)) page = page.replace("{message}", "<br>".join(msgs)) msgs = [] return build_response(body=page)
def tare(self, request): reading = self.get_scale_reading(samples=20) print("Read {}. Setting as new tare point".format(reading)) self.tare_point(new=reading) print("New tare point: {}".format(self.tare_point())) return build_response(body=self.build_configuration_page())
def scale_reader(self, request): with open('webpages/scale_reader.html', 'r') as f: page = f.read() page = page.replace("{current_value}", "{:.2f}".format(self.get_current_reading())) page = page.replace("{calibration_unit}", str(self.calibration_unit())) return build_response(body=page)
def remember(self, request): qs = unquote(request['query_parameters']).decode() query_parameters = parse_querystring(qs) tree = query_parameters['tree'] trunk = query_parameters['trunk'] if 'trunk' in query_parameters else None memories = self.woodwell.remember(tree=tree, trunk=trunk) return build_response(body=json.dumps(memories))
def aggregate_tare(self, reqeust): try: scale_reading = self.get_scale_reading(samples=20) self.tare_point(new=scale_reading) except Exception as e: print("Failed to tare device (aggregate)") print_exception(e) return build_response(body=self.build_aggregate_page())
def aggregate_next(self, request): print("Original Aggregated: {}".format(self.aggregated())) aggregated = self.aggregated() cv = self.get_current_reading() aggregated += cv self.aggregated(new=aggregated) print("New Aggregated: {}".format(self.aggregated())) return build_response(body=self.build_aggregate_page())
def mode(self, request, msg=""): print("Mode Received Request:\n{}".format(request)) try: mode = self.mode_from_body(request['body']) if 'configuration' == mode: page = self.build_configuration_page() elif 'aggregate' == mode: page = self.build_aggregate_page() else: page = self.build_scale_page() return build_response(body=page) except Exception as e: print("Mode Endpoint Failed") print_exception(e) return failure
def calibrate(self, request): req_body = request['body'] print("Calibrating with {}".format(req_body)) try: value, unit = req_body.split("&") value = float(value.split("=")[1]) unit = unit.split("=")[1] self.calibration_unit(new=unit) current_scale = self.get_scale_reading() tare = self.tare_point() current_value = current_scale - tare factor = current_value / value print("Found Factor to be: {}".format(factor)) self.calibration_factor(new=factor) except Exception as e: print("Failed to store calibration values") print_exception(e) return build_response(body=self.build_configuration_page())
def http_daemon(self, log=print): path_to_handler = { "/": self.mode, "/mode": self.mode, "/favicon.ico": lambda req: success, # Aggregation Functional Endpoints "/aggregate_next": self.aggregate_next, "/aggregate_clear": self.aggregate_clear, "/aggregate_tare": self.aggregate_tare, # Configuration Functional Endpoints "/adjustment_menu.html": self.adjustment_menu, "/mode_menu.html": self.mode_menu, "/scale_reader.html": self.scale_reader, "/update": self.update, "/calibrate": self.calibrate, "/tare": self.tare, # Functional Endpoints "/remember": self.remember, "/scale": self.current_scale_value, } error_page = self.get_page(path='webpages/error.html') error_res = build_response( status_code=500, body=error_page ) http_daemon( log=log, path_to_handler=path_to_handler, error_response=error_res )
def provide_jquery(self, request): js = self.get_page("webpage/jquery.min.js") return build_response( body=js, content_type="text/javascript")
def current_scale_value(self, request): return build_response(body=str(self.get_scale_reading()))
def aggregate_clear(self, request): self.aggregated(new=0) return build_response(body=self.build_aggregate_page())
def update(self, request): print("Not implemented") return build_response(body=self.build_configuration_page())
def mode_menu(self, request): with open('webpages/mode_menu.html', 'r') as f: page = f.read() return build_response(body=page)