Skip to content
Snippets Groups Projects
Hardware.py 4.54 KiB
Newer Older
  • Learn to ignore specific revisions
  • import logging as log
    from sqlalchemy.orm import Session
    from pilab.crud.Util import from_json, update_attrs
    
    from pilab.crud.Host import Host
    from pilab.schemas.Cube import CubesType
    from pilab.schemas.Pi import PiType
    from pilab.schemas.Hardware import SwitchType, ControllerType
    from pilab.events import meta
    
    log.getLogger('sqlalchemy.engine').setLevel(log.WARNING)
    
    class Switch(object):
        @staticmethod
        def create(db: Session, switch: meta.Switch):#DONE
            host = Host.create(db, switch)
            db_switch = from_json(SwitchType, switch.dict(), host_id=host.id)
            db.add(db_switch)
            db.flush()
            switch = meta.Switch(**vars(db_switch), **host.dict())
            return switch
    
        @staticmethod
        def get(db: Session, host_id: int = None, cube_id: int = None, pi_id: int = None):
            if host_id:
                db_switch = db.query(SwitchType).filter(
                    SwitchType.host_id == host_id
                ).first()
            elif cube_id:
                db_cube = db.query(CubesType).filter(
                    CubesType.switch_id == host_id
                ).first()
                db_switch = db.query(SwitchType).filter(
                    SwitchType.host_id == db_cube.switch_id
                ).first()
            elif pi_id:
                db_pi= db.query(PiType).filter(
                    PiType.host_id == pi_id
                ).first()
                db_cube = db.query(CubesType).filter(
                    CubesType.id == db_pi.cube_id
                ).first()
                db_switch = db.query(SwitchType).filter(
                    SwitchType.host_id == db_cube.switch_id
                ).first()
            
            host = Host.get(db, db_switch.host_id)
            return meta.Switch(**vars(db_switch), **host.dict())
    
        @staticmethod#DONE
        def delete(db: Session, host_id: int):
            db_switch = db.query(SwitchType).filter(
                SwitchType.host_id == host_id
            ).first()
            db.delete(db_switch)
            db.flush()
            Host.delete(db, host_id)
    
        @staticmethod
        def update(db: Session, switch: meta.HostUpdate, _id: int):
            host = Host.update(db, switch, _id)
            return host
    
        @staticmethod
        def getAll(db: Session):
            switches = []
            db_switches = db.query(SwitchType).all()
            for db_switch in db_switches:
                host = Host.get(db, db_switch.host_id)
                switches.append(meta.Switch(**vars(db_switch), **host.dict()))
            return switches
    
    
    class Controller(object):
        @staticmethod
        def create(db: Session, controller: meta.Controller):
            host = Host.create(db, controller)
            db_controller = from_json(ControllerType, controller.dict(), host_id=host.id)
            db.add(db_controller)
            db.flush()
            controller = meta.Controller(**vars(db_controller), **host.dict())
            return controller
    
        @staticmethod#DONE
        def delete(db: Session, host_id: int):
            db_controller = db.query(ControllerType).filter(
                ControllerType.host_id == host_id
            ).first()
            db.delete(db_controller)
            db.flush()
            Host.delete(db, host_id)
    
        @staticmethod
        def get(db: Session, host_id: int = None, cube_id: int = None, pi_id: int = None):
            if host_id:
                db_controller = db.query(ControllerType).filter(
                    ControllerType.host_id == host_id
                ).first()
            elif cube_id:
                db_cube = db.query(CubesType).filter(
                    CubesType.controller_id == host_id
                ).first()
                db_controller = db.query(ControllerType).filter(
                    ControllerType.host_id == db_cube.controller_id
                ).first()
            elif pi_id:
                db_pi= db.query(PiType).filter(
                    PiType.host_id == pi_id
                ).first()
                db_cube = db.query(CubesType).filter(
                    CubesType.id == db_pi.cube_id
                ).first()
                db_controller= db.query(ControllerType).filter(
                    ControllerType.host_id == db_cube.controller_id
                ).first()
            
            host = Host.get(db, db_controller.host_id)
            return meta.Controller(**vars(db_controller), **host.dict())
    
        @staticmethod
        def update(db: Session, controller: meta.HostUpdate, _id: int):
            host = Host.update(db, controller, _id)
            return host
    
        @staticmethod
        def getAll(db: Session):
            controllers = []
            db_controllers = db.query(ControllerType).all()
            for db_controller in db_controllers:
                host = Host.get(db, db_controller.host_id)
                controllers.append(meta.Controller(**vars(db_controller), **host.dict()))
            return controllers