import sys
from functools import partial
from datetime import datetime

from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import sessionmaker

from model import (
    Monitor,
    EnvironmentalSensor,
    engine,
)

from utils import (
    loop_csv_files,
    rpartial,
    safe_cast,
    search_files_in_folder,
)

NUM_DATA_COL = 7


def is_sensor_file(file):
    if file.endswith('st.csv'):
        return True
    return False

def loop_sensors(filename, content, callback):
    _, monitor_id, __ = filename.split('_')
    for row in content[1:]: # Ignore header
        if len(row) == NUM_DATA_COL:
            callback(monitor_id, row)
        else:
            print(f'{filename} has an incomplete row', row)

def insert_sensor(session, monitor_id, row):
    monitor = session.query(Monitor).get(monitor_id)
    try:
        session.add(
            EnvironmentalSensor(
                id=f'{monitor_id}_{row[0]}',
                timestamp=datetime.utcfromtimestamp(int(row[0])),
                time=datetime.strptime(row[1], '%Y/%m/%d %H:%M'),
                temperature=safe_cast(float, row[2]),
                humidity=safe_cast(int, row[3]),
                co2_concentration=safe_cast(int, row[4]),
                sound_pressure=safe_cast(int, row[5]),
                air_pressure=safe_cast(float, row[6]),
                monitor=monitor,
            )
        )
    except IntegrityError:
        session.rollback()


if __name__ == '__main__':
    if len(sys.argv) > 1:
        nilm_folder_path = sys.argv[1]
        DBSession = sessionmaker(bind=engine)
        session = DBSession()

        insert_sensor_with_session = partial(insert_sensor, session)
        loop_csv_files(
            search_files_in_folder(nilm_folder_path, is_sensor_file),
            rpartial(loop_sensors, insert_sensor_with_session)
        )
        session.commit()
        print('done.')
    else:
        print('python insert_enviromental_sensors.py <nilm_folder_path>')

5de7b9311113a3b6a1c2e3170f5b672775389d77
<mask token>


def is_sensor_file(file):
    if file.endswith('st.csv'):
        return True
    return False


<mask token>


def insert_sensor(session, monitor_id, row):
    monitor = session.query(Monitor).get(monitor_id)
    try:
        session.add(EnvironmentalSensor(id=f'{monitor_id}_{row[0]}',
            timestamp=datetime.utcfromtimestamp(int(row[0])), time=datetime
            .strptime(row[1], '%Y/%m/%d %H:%M'), temperature=safe_cast(
            float, row[2]), humidity=safe_cast(int, row[3]),
            co2_concentration=safe_cast(int, row[4]), sound_pressure=
            safe_cast(int, row[5]), air_pressure=safe_cast(float, row[6]),
            monitor=monitor))
    except IntegrityError:
        session.rollback()


<mask token>

<mask token>


def is_sensor_file(file):
    if file.endswith('st.csv'):
        return True
    return False


def loop_sensors(filename, content, callback):
    _, monitor_id, __ = filename.split('_')
    for row in content[1:]:
        if len(row) == NUM_DATA_COL:
            callback(monitor_id, row)
        else:
            print(f'{filename} has an incomplete row', row)


def insert_sensor(session, monitor_id, row):
    monitor = session.query(Monitor).get(monitor_id)
    try:
        session.add(EnvironmentalSensor(id=f'{monitor_id}_{row[0]}',
            timestamp=datetime.utcfromtimestamp(int(row[0])), time=datetime
            .strptime(row[1], '%Y/%m/%d %H:%M'), temperature=safe_cast(
            float, row[2]), humidity=safe_cast(int, row[3]),
            co2_concentration=safe_cast(int, row[4]), sound_pressure=
            safe_cast(int, row[5]), air_pressure=safe_cast(float, row[6]),
            monitor=monitor))
    except IntegrityError:
        session.rollback()


if __name__ == '__main__':
    if len(sys.argv) > 1:
        nilm_folder_path = sys.argv[1]
        DBSession = sessionmaker(bind=engine)
        session = DBSession()
        insert_sensor_with_session = partial(insert_sensor, session)
        loop_csv_files(search_files_in_folder(nilm_folder_path,
            is_sensor_file), rpartial(loop_sensors, insert_sensor_with_session)
            )
        session.commit()
        print('done.')
    else:
        print('python insert_enviromental_sensors.py <nilm_folder_path>')

<mask token>
NUM_DATA_COL = 7


def is_sensor_file(file):
    if file.endswith('st.csv'):
        return True
    return False


def loop_sensors(filename, content, callback):
    _, monitor_id, __ = filename.split('_')
    for row in content[1:]:
        if len(row) == NUM_DATA_COL:
            callback(monitor_id, row)
        else:
            print(f'{filename} has an incomplete row', row)


def insert_sensor(session, monitor_id, row):
    monitor = session.query(Monitor).get(monitor_id)
    try:
        session.add(EnvironmentalSensor(id=f'{monitor_id}_{row[0]}',
            timestamp=datetime.utcfromtimestamp(int(row[0])), time=datetime
            .strptime(row[1], '%Y/%m/%d %H:%M'), temperature=safe_cast(
            float, row[2]), humidity=safe_cast(int, row[3]),
            co2_concentration=safe_cast(int, row[4]), sound_pressure=
            safe_cast(int, row[5]), air_pressure=safe_cast(float, row[6]),
            monitor=monitor))
    except IntegrityError:
        session.rollback()


if __name__ == '__main__':
    if len(sys.argv) > 1:
        nilm_folder_path = sys.argv[1]
        DBSession = sessionmaker(bind=engine)
        session = DBSession()
        insert_sensor_with_session = partial(insert_sensor, session)
        loop_csv_files(search_files_in_folder(nilm_folder_path,
            is_sensor_file), rpartial(loop_sensors, insert_sensor_with_session)
            )
        session.commit()
        print('done.')
    else:
        print('python insert_enviromental_sensors.py <nilm_folder_path>')

import sys
from functools import partial
from datetime import datetime
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import sessionmaker
from model import Monitor, EnvironmentalSensor, engine
from utils import loop_csv_files, rpartial, safe_cast, search_files_in_folder
NUM_DATA_COL = 7


def is_sensor_file(file):
    if file.endswith('st.csv'):
        return True
    return False


def loop_sensors(filename, content, callback):
    _, monitor_id, __ = filename.split('_')
    for row in content[1:]:
        if len(row) == NUM_DATA_COL:
            callback(monitor_id, row)
        else:
            print(f'{filename} has an incomplete row', row)


def insert_sensor(session, monitor_id, row):
    monitor = session.query(Monitor).get(monitor_id)
    try:
        session.add(EnvironmentalSensor(id=f'{monitor_id}_{row[0]}',
            timestamp=datetime.utcfromtimestamp(int(row[0])), time=datetime
            .strptime(row[1], '%Y/%m/%d %H:%M'), temperature=safe_cast(
            float, row[2]), humidity=safe_cast(int, row[3]),
            co2_concentration=safe_cast(int, row[4]), sound_pressure=
            safe_cast(int, row[5]), air_pressure=safe_cast(float, row[6]),
            monitor=monitor))
    except IntegrityError:
        session.rollback()


if __name__ == '__main__':
    if len(sys.argv) > 1:
        nilm_folder_path = sys.argv[1]
        DBSession = sessionmaker(bind=engine)
        session = DBSession()
        insert_sensor_with_session = partial(insert_sensor, session)
        loop_csv_files(search_files_in_folder(nilm_folder_path,
            is_sensor_file), rpartial(loop_sensors, insert_sensor_with_session)
            )
        session.commit()
        print('done.')
    else:
        print('python insert_enviromental_sensors.py <nilm_folder_path>')

import sys
from functools import partial
from datetime import datetime

from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import sessionmaker

from model import (
    Monitor,
    EnvironmentalSensor,
    engine,
)

from utils import (
    loop_csv_files,
    rpartial,
    safe_cast,
    search_files_in_folder,
)

NUM_DATA_COL = 7


def is_sensor_file(file):
    if file.endswith('st.csv'):
        return True
    return False

def loop_sensors(filename, content, callback):
    _, monitor_id, __ = filename.split('_')
    for row in content[1:]: # Ignore header
        if len(row) == NUM_DATA_COL:
            callback(monitor_id, row)
        else:
            print(f'{filename} has an incomplete row', row)

def insert_sensor(session, monitor_id, row):
    monitor = session.query(Monitor).get(monitor_id)
    try:
        session.add(
            EnvironmentalSensor(
                id=f'{monitor_id}_{row[0]}',
                timestamp=datetime.utcfromtimestamp(int(row[0])),
                time=datetime.strptime(row[1], '%Y/%m/%d %H:%M'),
                temperature=safe_cast(float, row[2]),
                humidity=safe_cast(int, row[3]),
                co2_concentration=safe_cast(int, row[4]),
                sound_pressure=safe_cast(int, row[5]),
                air_pressure=safe_cast(float, row[6]),
                monitor=monitor,
            )
        )
    except IntegrityError:
        session.rollback()


if __name__ == '__main__':
    if len(sys.argv) > 1:
        nilm_folder_path = sys.argv[1]
        DBSession = sessionmaker(bind=engine)
        session = DBSession()

        insert_sensor_with_session = partial(insert_sensor, session)
        loop_csv_files(
            search_files_in_folder(nilm_folder_path, is_sensor_file),
            rpartial(loop_sensors, insert_sensor_with_session)
        )
        session.commit()
        print('done.')
    else:
        print('python insert_enviromental_sensors.py <nilm_folder_path>')
