Beispiel #1
0
class Application:
    def __init__(self):
        self.is_data_posted = False
        self.log = Log()  # log file to test and debug (writes debug messages)
        self.log_data = LogData()  # sensor data log (if no internet available)
        self.api = SamsApi()  # https://sams.science.itf.llu.lv/ Data Warehouse Plugin to send the data
        self.config = Config()  # Configurations (/config/config.ini)
        self.config_data = self.config.get_config_data()
        self.repost_seconds = int(self.config_data['INTERVAL']['repost_seconds'])
        self.app_wait_time = int(self.config_data['INTERVAL']['app_wait_seconds'])
        self.dataset_taken = False
        self.dataset_taken_counter = 0

        self.data = Dataset()  # collect all the data from sensors
        self.dataset = ""

    def take_dataset(self):
        self.dataset = ""  # empty the dataset before take new data
        self.dataset = self.data.get_dataset()

    def start(self):
        while True:
            try:
                self.log.write_log("take dataset")
                self.take_dataset()
                # if stored data (/log/*.json) available, then try to send this data to the data warehouse
                if self.log_data.has_log_files():
                    self.log.write_log("has log files")
                    self.log_data.post_log_files(self.dataset)
                # if not, take a new dataset to post
                else:
                    response = self.api.call(self.dataset)
                    # try to post data. If api status is 200 then everything is right
                    if response == 200:
                        self.log.write_log("dataset posted")
                    # if no internet connection or the api do not allow to send, then store the data
                    # if the status code from api is 500 then the log function will delete the file
                    else:
                        self.log.write_log("dataset posting failed. Statuscode: {0}".format(response))
                        self.is_data_posted = False  # data where not posted
                        self.log.write_log("log dataset")
                        self.log_data.insert(self.dataset)  # write new log file with the dataset
                        # try to post every X seconds while the data is not posted (no internet connection)
                        while not self.is_data_posted:
                            self.is_data_posted = self.api.call(self.dataset)
                            time.sleep(self.repost_seconds)
                self.log.write_log("wait: {}".format(self.app_wait_time))
                self.dataset_taken = False
                time.sleep(int(self.app_wait_time))  # sleep X seconds before collecting the new data
            except Exception as e:
                print(e)
                self.log.write_log(e)
Beispiel #2
0
    def __init__(self):
        self.is_data_posted = False
        self.log = Log()  # log file to test and debug (writes debug messages)
        self.log_data = LogData()  # sensor data log (if no internet available)
        self.api = SamsApi()  # https://sams.science.itf.llu.lv/ Data Warehouse Plugin to send the data
        self.config = Config()  # Configurations (/config/config.ini)
        self.config_data = self.config.get_config_data()
        self.repost_seconds = int(self.config_data['INTERVAL']['repost_seconds'])
        self.app_wait_time = int(self.config_data['INTERVAL']['app_wait_seconds'])
        self.dataset_taken = False
        self.dataset_taken_counter = 0

        self.data = Dataset()  # collect all the data from sensors
        self.dataset = ""
    def __init__(self):
        self.config = Config()
        self.config_data = self.config.get_config_data()
        try:
            self.dht22 = DHT22(int(self.config_data['DHT22']['pin']))
        except Exception as e:
            self.log.write_log("Failed to initialize DHT22: {}".format(e))
        try:
            self.scale = Scale()
        except Exception as e:
            self.log.write_log("Failed to initialize scale: {}".format(e))

        try:
            self.DS18B20 = DS18B20()
        except Exception as e:
            self.log.write_log("Failed to initialize DS18B20: {}".format(e))

        self.api = SamsApi()
        self.log = Log()

        self.median_interval = int(self.config_data['INTERVAL']['median'])
        self.wait_time = int(self.config_data['INTERVAL']['wait_time_seconds'])

        self.dataset = []
        self.temp = []
        self.hum = []
        self.weight = []
        self.ds_temp = []

        self.median_temp = 0
        self.median_hum = 0
        self.median_weight = 0
        self.median_ds_temp = 0

        self.duration = int(self.config_data['AUDIO']['duration'])
 def __init__(self):
     self.path = '/var/www/upload/data_logger/log/'
     self.api = SamsApi()
     self.status = []
     self.files = os.listdir(self.path)
     self.log = Log()
class LogData:
    def __init__(self):
        self.path = '/var/www/upload/data_logger/log/'
        self.api = SamsApi()
        self.status = []
        self.files = os.listdir(self.path)
        self.log = Log()

    def insert(self, json_data):
        files = os.listdir(self.path)
        if len(files) != 0:
            file = int(
                len([
                    name for name in os.listdir(self.path)
                    if os.path.isfile(os.path.join(self.path, name))
                ])) + 1
        else:
            file = int(1)
        try:
            with open(self.path + str(file) + ".json", 'w') as f:
                json.dump(json_data, f)
                f.close()
        except Exception as e:
            print(e)

    def list_dir(self):
        self.files = os.listdir(self.path)
        self.files.sort()

    @staticmethod
    def read_file(path):
        with open(path) as json_file:
            data = json.load(json_file)

        return data

    def has_log_files(self):
        if not os.listdir(self.path):
            return False
        else:
            return True

    def post_log_files(self, dataset):
        try:
            self.log.write_log("log dataset...")
            self.insert(dataset)
            while self.has_log_files():
                self.list_dir()
                for x in self.files:
                    file = self.read_file(self.path + str(x))
                    self.log.write_log("try to post data")
                    if self.api.call(file) == 200:
                        self.log.write_log("status code ok! Delete file...")
                        os.remove(self.path + str(x))
                    if self.api.call(file) == 500:
                        self.log.write_log("File corrupted! Delete file...")
                        os.remove(self.path + str(x))
                    time.sleep(5)
            return True

        except Exception as e:
            print(e)
            self.log.write_log(e)
Beispiel #6
0
 def __init__(self):
     self.path = Credentials.log_path
     self.api = SamsApi()
     self.status = []
     self.files = os.listdir(self.path)
     self.log = Log()
Beispiel #7
0
 def __init__(self):
     self.path = '/home/pi/sams/data_logger/log/'
     self.api = SamsApi()
     self.status = []
     self.files = os.listdir(self.path)
     self.log = Log()