Beispiel #1
0
 def __init__(self, inj: Injector):
     self.rc = inj.require(RouteController)  # type: RouteController
     self.s = inj.require(ConnectionService)  # type: ConnectionService
     self.dc = inj.require(
         DisConnectionService)  # type: DisConnectionService
     self.auth = inj.require(
         AuthSlaveMiddleware)  # type: AuthSlaveMiddleware
     self.check_boot = inj.require(BootMiddleware)  # type: BootMiddleware
Beispiel #2
0
 def __init__(self, injector: Injector):
     super().__init__(injector)
     self.apply_ctl(injector.require(PingController), master_http_spec.ping)
     self.apply_ctl(injector.require(AdminController), master_http_spec.admin)
     self.apply_ctl(injector.require(ConnectController), master_http_spec.connect)
     self.apply_ctl(injector.require(MetricsController), master_http_spec.metrics)
     self.apply_ctl(injector.require(StatisticsController), master_http_spec.statistics)
     self.apply_ctl(injector.require(SlaveStateControlController), master_http_spec.slave_state_control)
Beispiel #3
0
 def __init__(self, inj: Injector):
     self.user_model = inj.require(UserModel)  # type: UserModel
     self.room_model = inj.require(RoomModel)  # type: RoomModel
     self.user_in_room_model = inj.require(UserInRoomRelationshipModel)  # type: UserInRoomRelationshipModel
     self.event_model = inj.require(EventModel)  # type: EventModel
     self.cfg_provider = inj.require(ConfigurationProvider)  # type: ConfigurationProvider
     self.master_air_cond = inj.require(MasterAirCond)  # type: MasterAirCond
     self.connection_pool = inj.require(ConnectionPool)  # type: ConnectionPool
     self.password_verifier = inj.require(PasswordVerifier)  # type: PasswordVerifier
     self.jwt = inj.require(JWT)  # type: JWT
     self.random_source = inj.require(SystemEntropyProvider)  # type: SystemEntropyProvider
     self.expire_time = datetime.timedelta(hours=1)
Beispiel #4
0
 def create_table(self, inj: Injector = None):
     inj = inj or self.injector
     for model_prototype in [UserModel, RoomModel, UserInRoomRelationshipModel, MetricModel, StatisticModel,
                             EventModel]:
         model_instance = inj.require(model_prototype)
         created = model_instance.create()
         if not created:
             self.logger.fatal('create table failed', args={'model_type': str(model_prototype)})
Beispiel #5
0
 def build_middleware(self, inj: Injector = None):
     inj = inj or self.injector
     # inj.build(ReceiveRequestMiddleware, ReceiveRequestMiddlewareImpl)
     inj.build(AuthAdminMiddleware, AuthAdminMiddlewareImpl)
     inj.build(AuthSlaveMiddleware, AuthSlaveMiddlewareImpl)
     inj.build(BootMiddleware, BootMiddlewareImpl)
     CORS(inj.require(Flask))
     return inj
Beispiel #6
0
    def expose_service(self, inj: Injector = None):
        inj = inj or self.injector
        opt = inj.require(OptionProvider)  # type: OptionProvider

        MasterFlaskRouter(inj)
        # .run(host=opt.find('host'), port=opt.find('port'))

        self.websocket_conn.sio.run(self.websocket_conn.app, host=opt.find('host'), port=int(opt.find('port')),
                                    debug=True)
Beispiel #7
0
 def __init__(self, inj: Injector):
     self.master_air_cond = inj.require(MasterAirCond)  # type: MasterAirCond
     self.uuid_provider = inj.require(UUIDGenerator)  # type: UUIDGenerator
     self.dispatcher = inj.require(Dispatcher)  # type: Dispatcher
     self.event_model = inj.require(EventModel)  # type: EventModel
     self.dispatcher.on_pop(self._pop_request)
     self.dispatcher.on_fallback(self._fallback_request)
     self.connection_pool = inj.require(ConnectionPool)  # type: ConnectionPool
     self.logger = inj.require(Logger)  # type: Logger
     self.statistic_model = inj.require(StatisticModel) # type: StatisticModel
     self.base_time = time.perf_counter()
Beispiel #8
0
def build_and_run_console(inj: Injector):
    inj.provide(MainConsole, MainConsoleImpl())
    inj.build(BootConsole, BootConsoleImpl)
    inj.build(MetricConsole, MetricConsoleImpl)
    inj.build(ReportConsole, ReportConsoleImpl)
    inj.build(SetMetricFrequencyConsole, SetMetricFrequencyConsoleImpl)
    inj.build(SetModeConsole, SetModeConsoleImpl)
    inj.build(SetStatisticFrequencyConsole, SetStatisticFrequencyConsoleImpl)
    inj.build(SetTemperatureConsole, SetTemperatureConsoleImpl)
    inj.build(ShutdownConsole, ShutdownConsoleImpl)
    inj.build(StatusConsole, StatusConsoleImpl)

    console = inj.require(MainConsole)
    console.start()
Beispiel #9
0
    def boot_server(self, inj: Injector = None):
        inj = inj or self.injector
        dispatcher = inj.require(Dispatcher)  # type: Dispatcher
        dispatcher.boot_up()
        self.connection_pool.boot_up()
        self.create_table(inj)
        if self.cfg.use_test_database:
            um = inj.require(UserModel)  # type: UserModel
            uim = inj.require(UserInRoomRelationshipModel)  # type: UserInRoomRelationshipModel
            rm = inj.require(RoomModel)  # type: RoomModel
            mm = inj.require(MetricModel)  # type: MetricModel
            em = inj.require(EventModel)  # type: EventModel
            sm = inj.require(StatisticModel)  # type: StatisticModel
            pw = inj.require(PasswordVerifier)  # type: PasswordVerifier
            rid = rm.insert('A-101', pw.create('1234'))
            rm.insert('A-102', pw.create('1234'))
            uid = um.insert('xxx')
            uim.insert(user_id=uid, room_id=rid)
            # mm.insert('A-101', '1234')
            # mm.insert('A-102', '1234')r = Room(room_id='metric_test')
            r = Room(room_id='metric_test')
            rid = rm.insert(r.room_id, r.app_key)
            r2 = Room(room_id='metric_test2')
            r2id = rid = rm.insert(r2.room_id, r2.app_key)

            em.insert_connect_event(rid)
            em.insert_start_state_control_event(rid, 'high')
            mm.insert(rid, 'high', 24.0)
            sm.insert(rid, 2.0, 10.0)
            em.insert_start_state_control_event(r2id, 'low')
            mm.insert(rid, 'high', 23.0)
            sm.insert(r2id, 1.0, 5.0)
            sm.insert(rid, 2.0, 10.0)
            mm.insert(r2id, 'low', 24.0)
            sm.insert(rid, 0.2, 1.0)
            em.insert_stop_state_control_event(rid)
            mm.insert(r2id, 'low', 23.0)
            em.insert_disconnect_event(rid)
            sm.insert(r2id, 1.0, 5.0)
            mm.insert(r2id, 'low', 22.0)
            sm.insert(r2id, 0.5, 2.5)
            em.insert_stop_state_control_event(r2id)
            em.insert_disconnect_event(r2id)
        return inj
Beispiel #10
0
    def __init__(self, injector: Injector):
        self.option_context = injector.require(OptionContext)  # type: OptionContext
        self.parser = argparse.ArgumentParser(
            description=self.option_context.description)
        for option_argument in self.option_context.arguments:
            option_string = []
            if option_argument.long_opt:
                option_string.append('--' + option_argument.long_opt)
            if option_argument.short_opt:
                option_string.append('-' + option_argument.short_opt)

            self.parser.add_argument(
                *option_string,
                default=option_argument.default_value,
                help=option_argument.help_msg)

        self.namespace = self.parser.parse_args(self.option_context.option_args)
Beispiel #11
0
    def __init__(self, injector: Injector):

        self.opt = injector.require(OptionProvider)  # type: OptionProvider
        self.file_path = self.opt.find('config')
        if self.file_path:
            with open(self.file_path) as f:
                ext = os.path.splitext(self.file_path)[1]
                if ext == '.yaml' or ext == '.yml' or ext == '':
                    yaml = load_yaml_module()
                    self.config = load_configuration_from_dict(
                        yaml.load(f, yaml.SafeLoader))
                else:
                    raise ValueError(
                        f'configuration file with unknown ext: path {self.file_path}, ext {ext}'
                    )
        else:
            self.config = Configuration()
Beispiel #12
0
class MetricModelTest(unittest.TestCase):
    def setUp(self) -> None:
        self.inj = Injector()
        self.inj.provide(SQLDatabase, BaseSQLDatabaseImpl())
        self.db = self.inj.require(SQLDatabase)
        self.db.connect(argv[1], int(argv[2]), argv[3], argv[4], argv[5])

    def insert_data(self):
        r = Room(room_id='metric_test')
        rm = RoomModelImpl(self.inj)
        rid = rm.insert(r.room_id, r.app_key)
        r2 = Room(room_id='metric_test2')
        r2id = rm.insert(r2.room_id, r2.app_key)

        sleep(1)
        em = EventModelImpl(self.inj)
        mm = MetricsModelImpl(self.inj)
        sm = StatisticModelImpl(self.inj)
        em.insert_connect_event(rid)
        sleep(1)
        em.insert_start_state_control_event(rid, 'high')
        sleep(0.5)
        mm.insert(rid, 'high', 24.0)
        sleep(0.5)
        sm.insert(rid, 2.0, 10.0)
        em.insert_start_state_control_event(r2id, 'low')
        sleep(0.5)
        mm.insert(rid, 'high', 23.0)
        sm.insert(r2id, 1.0, 5.0)
        sleep(0.5)
        sm.insert(rid, 2.0, 10.0)
        mm.insert(r2id, 'low', 24.0)
        sm.insert(rid, 0.2, 1.0)
        em.insert_stop_state_control_event(rid)
        sleep(0.5)
        mm.insert(r2id, 'low', 23.0)
        sleep(0.5)
        em.insert_disconnect_event(rid)
        sm.insert(r2id, 1.0, 5.0)
        sleep(0.5)
        mm.insert(r2id, 'low', 22.0)
        sm.insert(r2id, 0.5, 2.5)
        em.insert_stop_state_control_event(r2id)
        sleep(1)
        em.insert_disconnect_event(r2id)
Beispiel #13
0
 def __init__(self, inj: Injector):
     self.metric_model = inj.require(MetricModel)  # type: MetricModel
     self.master_air_cond = inj.require(
         MasterAirCond)  # type: MasterAirCond
     self.connection_pool = inj.require(
         ConnectionPool)  # type: ConnectionPool
Beispiel #14
0
 def __init__(self, injector: Injector):
     super().__init__(injector)
     self.apply_ctl(injector.require(PingController), master_http_spec.ping)
     self.apply_ctl(injector.require(DaemonAdminController), daemon_http_spec.admin)
Beispiel #15
0
 def __init__(self, inj: Injector):
     self.rc = inj.require(RouteController)  # type: RouteController
     self.auth_slave = inj.require(AuthSlaveMiddleware)  # type: AuthSlaveMiddleware
     self.start_state_control_service = inj.require(StartStateControlService)  # type: StartStateControlService
     self.stop_state_control_service = inj.require(StopStateControlService)  # type: StopStateControlService
     self.check_boot = inj.require(BootMiddleware)  # type: BootMiddleware
Beispiel #16
0
 def __init__(self, inj: Injector):
     self.s = inj.require(Serializer)  # type: Serializer
Beispiel #17
0
 def __init__(self, inj: Injector):
     super().__init__(inj)
     self.event_model = inj.require(EventModel)
     self.statistic_model = inj.require(StatisticModel)
     self.metric_model = inj.require(MetricModel)
     self.room_model = inj.require(RoomModel)
Beispiel #18
0
 def __init__(self, inj: Injector):
     self.rc = inj.require(RouteController)  # type: RouteController
     self.auth_slave = inj.require(AuthSlaveMiddleware)  # type: AuthSlaveMiddleware
     self.s = inj.require(GenerateStatisticService)  # type: GenerateStatisticService
     self.check_boot = inj.require(BootMiddleware)  # type: BootMiddleware
Beispiel #19
0
 def __init__(self, inj: Injector):
     self.v = inj.require(APPVersion)  # type: str
     self.rc = inj.require(RouteController)  # type: RouteController
Beispiel #20
0
 def __init__(self, inj: Injector):
     self.report_model = inj.require(ReportModel)  # type: ReportModel
Beispiel #21
0
def inject_middleware(inj: Injector):
    inj.build(AuthAdminMiddleware, AuthAdminMiddlewareImpl)
    CORS(inj.require(Flask))
    return inj
Beispiel #22
0
 def __init__(self, inj: Injector):
     self.event_model = inj.require(EventModel)
     self.statistic_model = inj.require(StatisticModel)
     self.response_factory = GenerateStatisticResponse
Beispiel #23
0
def expose_service(inj: Injector):
    opt = inj.require(OptionProvider)  # type: OptionProvider

    DaemonFlaskRouter(inj).run(opt.find('host'), opt.find('port'))
Beispiel #24
0
 def __init__(self, inj: Injector):
     self.rc = inj.require(RouteController)  # type: RouteController
     self.jwt = inj.require(JWT)  # type: JWT