Ejemplo n.º 1
0
    def post_log_files(self):
        try:
            post_logs = self.has_log_files()  # check if app has log files
            while post_logs:
                self.list_dir()
                for x in self.files:
                    file = self.read_file(self.log_path + str(x))
                    response = self.data_api.send_data(file)
                    if response:
                        os.remove(self.log_path + str(x))  # delete file
                        post_logs = self.has_log_files(
                        )  # ask for more log files
                    if response == "delete":
                        send_log("File corrupted! Delete file", "warning")
                        self.dataset_corrupted_counter += 1
                        os.remove(self.log_path + str(x))
                        post_logs = self.has_log_files()
                    if not response:
                        post_logs = False

            return True

        except Exception as e:
            send_log("Failed to post log files: ".format(e), "error")
            return False
Ejemplo n.º 2
0
 def read_file(file_path):
     try:
         with open(file_path) as json_file:
             file_data = json.load(json_file)
         return file_data
     except Exception as e:
         send_log("Failed to read log files: ".format(e), "error")
         return False
Ejemplo n.º 3
0
    def sensor_error(self, sensor):  # sensor is offline or sends no valid data
        self.attempts += 1
        self.failed_sensor = str(sensor)
        if os.path.exists(mapping.witty_pi):
            self.error_helper.set_sensor_with_error(sensor)
            self.error_helper.set_sensor_restarted(sensor)
        else:
            self.error_helper.set_sensor_with_error(sensor)

        send_log(f'{sensor} failed!', "error")
Ejemplo n.º 4
0
    def sync_config(self):
        try:
            if self.online_config.get_config_data():
                self.local_config.set_config_data("DEFAULT", "group",
                                                  self.online_config.groupname)
                self.local_config.set_config_data(
                    "DEFAULT", "ignore_error", self.online_config.ignore_error)
                self.local_config.set_config_data("DEFAULT", "is_dht22",
                                                  self.online_config.is_dht22)
                self.local_config.set_config_data("DEFAULT", "is_scale",
                                                  self.online_config.is_scale)
                self.local_config.set_config_data(
                    "DEFAULT", "is_microphone",
                    self.online_config.is_microphone)
                self.local_config.set_config_data(
                    "DEFAULT", "is_ds18b20", self.online_config.is_ds18b20)
                self.local_config.set_config_data(
                    "DEFAULT", "auto_update", self.online_config.auto_update)
                self.local_config.set_config_data(
                    "DEFAULT", "auto_shutdown",
                    self.online_config.auto_shutdown)
                self.local_config.set_config_data("DEFAULT", "timezone",
                                                  self.online_config.timezone)
                self.local_config.set_config_data("DEFAULT", "debug",
                                                  self.online_config.debug)
                self.local_config.set_config_data(
                    "INTERVAL", "median", self.online_config.interval_median)
                self.local_config.set_config_data(
                    "INTERVAL", "app_wait_seconds",
                    self.online_config.interval_app_wait_seconds)
                self.local_config.set_config_data(
                    "INTERVAL", "attempts_before_restart",
                    self.online_config.interval_attempts_before_restart)
                self.local_config.set_config_data("DHT22", "dht22_pin",
                                                  self.online_config.dht22_pin)
                self.local_config.set_config_data(
                    "AUDIO", "duration", self.online_config.audio_duration)
                self.local_config.set_config_data("AUDIO", "fs",
                                                  self.online_config.audio_fs)

            if self.local_config.get_config_data():
                return True
            else:
                return False

        except Exception as e:
            send_log("Sync config Error: {}".format(e), "warning")
Ejemplo n.º 5
0
    def insert(self, json_data):
        files = os.listdir(self.log_path)
        if range(len(files)) != 0:
            file = int(
                len([
                    name for name in os.listdir(self.log_path)
                    if os.path.isfile(os.path.join(self.log_path, name))
                ])) + 1
        else:
            file = int(1)
        try:
            path = '/'
            bytes_avail = psutil.disk_usage(path).free
            gigabytes_avail = bytes_avail / 1024 / 1024 / 1024

            if gigabytes_avail >= 0.5:
                with open(self.log_path + str(file) + ".json", 'w') as f:
                    json.dump(json_data, f)
                    f.close()

            else:
                send_log(
                    "No more space available: {} GB".format(
                        round(gigabytes_avail, 2)), "warning")
                files = []
                for name in os.listdir(self.log_path):
                    file_number = name.replace(".json", "")
                    files.append(int(file_number))

                files.sort()
                for logfile in files:
                    if int(logfile) % 2:
                        pass
                    else:
                        last_number = files[-1]
                        last_number = int(last_number) + 1
                        with open(self.log_path + str(last_number) + ".json",
                                  'w') as f:
                            json.dump(json_data, f)
                            f.close()
                        os.remove(self.log_path + str(logfile) + ".json")
                        break

        except Exception as e:
            send_log("Failed to insert log files: ".format(e), "error")
Ejemplo n.º 6
0
    def __init__(self):
        self.config = LocalConfig()
        self.user_credentials = UserConfig()
        self.config.get_config_data()
        try:
            self.dht22 = DHT22(int(self.config.dht22_pin))
        except Exception as e:
            send_log("Failed to initialize DHT22: {}".format(e), "error")

        try:
            self.scale = Scale()
        except Exception as e:
            send_log("Failed to initialize scale: {}".format(e), "error")

        try:
            self.DS18B20 = DS18B20()
        except Exception as e:
            send_log("Failed to initialize DS18B20: {}".format(e), "error")

        self.last_measured_weight = 0
        self.median_weight = 0
Ejemplo n.º 7
0
    def start(self):
        while True:
            try:
                # before do anything, handle on / offline status and try to sync config
                self.handle_online_status()
                sensors = []
                if not self.app_config.local_config.ignore_error:  # if not ignore error
                    if self.app_config.local_config.is_ds18b20 and not self.error_helper.has_error(
                            "DS18B20"):
                        sensors.append("ds18b20")  # TEMP SENSOR
                    if self.app_config.local_config.is_dht22 and not self.error_helper.has_error(
                            "DHT22"):
                        sensors.append("dht22")  # TEMP and HUMIDITY SENSOR
                    if self.app_config.local_config.is_scale and not self.error_helper.has_error(
                            "SCALE"):
                        sensors.append("scale")
                    if self.app_config.local_config.is_microphone and not self.error_helper.has_error(
                            "MICROPHONE"):
                        sensors.append("microphone")
                else:  # if ignore all errors
                    if self.app_config.local_config.is_ds18b20:
                        sensors.append("ds18b20")  # TEMP SENSOR
                    if self.app_config.local_config.is_dht22:
                        sensors.append("dht22")  # TEMP and HUMIDITY SENSOR
                    if self.app_config.local_config.is_scale:
                        sensors.append("scale")
                    if self.app_config.local_config.is_microphone:
                        sensors.append("microphone")

                # START GET AND SEND DATASET BLOCK
                for sensor in sensors:
                    dataset = self.dataset.get_data(
                        sensor)  # get dataset from sensor
                    if dataset:
                        for x in range(len(dataset)):
                            if not dataset[x] or not hasattr(
                                    dataset[x], '__len__'):
                                self.sensor_error(sensor.upper())
                            else:
                                response = self.dwh_api.send_data(dataset[x])
                                if not response:
                                    self.dataset_helper.insert(
                                        dataset[x])  # save data
                    else:
                        self.sensor_error(sensor.upper())

                # END DATASET BLOCK ###

                # START POST LOG FILES ####
                if self.wifi_helper.is_online():
                    response = self.dataset_helper.post_log_files()
                    if not response:
                        self.attempts += 1
                # END POST LOG FILES ####

                # START CHECKING FAILED ATTEMPTS BLOCK
                if int(self.attempts) >= int(self.app_config.local_config.
                                             interval_attempts_before_restart):
                    self.error_helper.set_sensor_restarted(self.failed_sensor)
                    self.restart_hive("Too many errors: reboot system!",
                                      "error")
                # END FAILED ATTEMPTS BLOCK ###

                # START CHECKING UPDATE
                if self.wifi_helper.is_online(
                ) and self.app_config.local_config.auto_update:
                    self.update()
                # END CHECKING UPDATE

                # START AUTO SHUTDOWN BLOCK
                if self.app_config.local_config.auto_shutdown:
                    send_log(f'Power off. System time: {str(get_time())}',
                             "debug")
                    time.sleep(30)
                    os.system("sudo poweroff")
                # END AUTO SHUTDOWN BLOCK ###

                # WAIT BEFORE TAKE NEW DATASET
                time.sleep(
                    int(self.app_config.local_config.interval_app_wait_seconds)
                )
                # END WAIT ###

            except Exception as e:
                print(f'app crashed: {e}')
                self.restart_hive(
                    f'Application crashed! Error: {e}. Reboot System', "error")
Ejemplo n.º 8
0
    def __init__(self):
        self.dataset_helper = DatasetLogHelper(
        )  # saving and sends the dataset
        self.dataset = Dataset()  # dataset for take sensor data
        self.app_config = ApplicationConfig(
        )  # configuration data (on- and offline)
        self.wifi_helper = WifiHelper(
        )  # gets signal strength for debug purpose
        self.attempts = 0
        self.dwh_api = DataApi()
        self.token_handler = TokenHandler()
        self.error_helper = ErrorHelper()
        self.failed_sensor = ""
        self.handle_online_status()
        self.checker = SelfChecker()

        # send status:
        try:
            send_log(
                f'Start Application: {self.app_config.local_config.version}',
                "debug")
            send_log(f'Config Name: {self.app_config.local_config.group}',
                     "debug")
            send_log(
                f'Signal Strength: {self.wifi_helper.get_signal_strength()}',
                "debug")
            if self.current_volt():
                send_log(f'Voltage: {self.current_volt()}', "debug")

            set_timezone(self.app_config.local_config.timezone)

            for file, status in self.checker.check_files().items():
                send_log(f"created file: {file}.", "warning")
            for failed_sensor in self.error_helper.get_sensors_with_errors():
                send_log(
                    f'Please check {str(failed_sensor)} and reset all errors to reactivate the sensor.',
                    "warning")
        except Exception as e:
            print(e)
Ejemplo n.º 9
0
 def restart_hive(message, level):
     send_log(message, level)
     time.sleep(120)
     os.system('sudo reboot')