def _check_time_to_power_save(cls, wake_up): #micropython.mem_info(False) go_to_power_save = False try: #to do not be _check_time_to_power_save interupted due to a BLE issue if wake_up: PowerMgmt.block_power_save() if not cls._time_to_power_save: #can be reset from constructor cls._time_to_power_save = cls._running_time_up cls._start_ble() #cls._advertise() #just for sure it is turned on print("BLE power-save blocked") else: if cls._time_to_power_save: #if time was not reset externally (e.g. ble connection) cls._time_to_power_save -= 1 if cls._time_to_power_save == 0: #if time has been reset by decreasing go_to_power_save = True # ble is disabled automatically when power save is activated and is enabled again when the program runs again # but it is not reliable (advertisement si not started) - lets do it manually #cls.disconnect() cls._stop_ble() PowerMgmt.unblock_power_save() print("BLE power-save allowed") except Exception as error: print("BLE error") sys.print_exception(error) #micropython.mem_info(0) delay = cls._time_down if go_to_power_save else 1 Planner.postpone(delay, cls._check_time_to_power_save, go_to_power_save)
def command_request(self, data): if data and len(data) > 0: command = data[0] if command == _cmd_version: return self._b_true elif command == _cmd_stop_program: print("cmd_stop_program") if self.file_exists(self.events_file_name): print("events_file will be renamed") self.rename_file(self.events_file_name, "." + self.events_file_name) print("events_file renamed") print("reboot planned") Planner.postpone(0.1, self._reboot) return self._b_true elif command == _cmd_start_program: return self._b_true if self._import_events() else self._b_false elif command == _cmd_get_next_file_info: if not self.dir_content: self.dir_content = os.listdir("/") if self.dir_pos >= len(self.dir_content): return self._b_false name = self.dir_content[self.dir_pos] self.dir_pos += 1 return self._get_file_checksum(name) + name.encode("utf-8") elif command == _cmd_remove_file: filename = data[1:] self.remove_file(filename) return self._b_true elif command == _cmd_handle_file: self.handeled_file_path = data[1:] self.new_file = True return self._b_true elif command == _cmd_get_file_checksum: return self._get_file_checksum(self.handeled_file_path) elif command == _cmd_append: data = data[1:] if self.new_file: file = open(self.handeled_file_path, "wb") self.new_file = False else: file = open(self.handeled_file_path, "ab") file.write(data) file.close() return self._b_true else: return None
def reboot(cls): print("rebooting") Ble.disconnect() Planner.postpone(0.1, cls._reboot)