示例#1
0
class PowerSupply(Concrete):
    identifier = 'power_supply'
    default_dimension = (1, 1, 1)
    default_orientation = (0, 1, 0)

    default_supply_power_ratings = [ElectricPower(110)]

    def __init__(self, uuid, dimension_x=(1, 1, 1), selected_functions=(Function.POWERED, Function.TASKED)):
        super().__init__(uuid=uuid, dimension_x=dimension_x, matter_type=MatterType.METAL,
                         selected_functions=selected_functions)
        self.active = False

    def get_default_shape(self):
        i = self.matter_type.value
        shape = np.array([[[i]]])

        return shape

    def validate_functions(self, selected_functions):
        if Function.POWERED in selected_functions and Function.TASKED in selected_functions:
            return True
        else:
            return False

    def define_functions(self, selected_functions):
        if Function.POWERED in selected_functions:
            powered = Powered(self)
            powered.power_outputs.append(PowerOutput(self))
            powered.output_power_ratings.extend(PowerSupply.default_supply_power_ratings)
            self.functions[Function.POWERED] = powered

        if Function.TASKED in selected_functions:
            self.functions[Function.TASKED] = PowerSupplyTasked(self)
示例#2
0
    def test_input_power(self):
        def check_active(l_light, expected):
            if expected:
                self.assertTrue(l_light.active)
            else:
                self.assertFalse(l_light.active)

        light = Light(uuid=uuid.uuid4(),
                      selected_functions=(Function.POWERED, ))
        check_active(light, False)

        light.get_function(Function.POWERED).get_power_input().set_power(
            ElectricPower(110))
        check_active(light, True)

        light.get_function(Function.POWERED).get_power_input().set_power(
            ElectricPower(220))
        check_active(light, False)
示例#3
0
    def test_input_power_autodeactivate(self):
        def check_active(l_light, expected):
            if expected:
                self.assertTrue(l_light.active)
                l_light.send_task(SystemTask(TaskName.GET_SYSTEM_INFO))
                self.assertTrue(
                    light.recv_task_resp().value['system_info']['active'])
            else:
                self.assertFalse(l_light.active)
                l_light.send_task(SystemTask(TaskName.GET_SYSTEM_INFO))
                self.assertFalse(
                    light.recv_task_resp().value['system_info']['active'])

        light = Light(uuid=uuid.uuid4(),
                      selected_functions=(Function.POWERED, Function.TASKED))
        check_active(light, False)

        light.send_task(SystemTask(TaskName.ACTIVE))
        light.recv_task_resp()
        check_active(light, False)

        light.send_task(
            SystemTask(TaskName.SET_POWER, value={'power':
                                                  ElectricPower(110)}))
        light.send_task(SystemTask(TaskName.ACTIVE))
        light.recv_task_resp()
        check_active(light, True)

        light.send_task(
            SystemTask(TaskName.SET_POWER, value={'power': ElectricPower(0)}))
        check_active(light, False)
        light.send_task(SystemTask(TaskName.ACTIVE))
        light.recv_task_resp()
        check_active(light, False)

        light.send_task(
            SystemTask(TaskName.SET_POWER, value={'power':
                                                  ElectricPower(220)}))
        check_active(light, False)
        light.send_task(SystemTask(TaskName.ACTIVE))
        light.recv_task_resp()
        check_active(light, False)
示例#4
0
    def test_recv_task_resp(self):
        self.assertRaises(
            FunctionUnsupportedError, lambda: self.light.send_task(
                SystemTask(name=TaskName.SET_POWER,
                           value={'power': ElectricPower(110)})))
        self.assertRaises(FunctionUnsupportedError,
                          lambda: self.light.recv_task_resp())

        # SystemTask
        self.assertRaises(
            FunctionUnsupportedError,
            lambda: self.light.send_task(SystemTask(name=TaskName.ACTIVE)))
        self.assertRaises(FunctionUnsupportedError,
                          lambda: self.light.recv_task_resp())

        # EntityTask
        self.assertRaises(
            FunctionUnsupportedError,
            lambda: self.light.send_task(Task(name=TaskName.GET_INFO)))
        self.assertRaises(FunctionUnsupportedError,
                          lambda: self.light.recv_task_resp())
示例#5
0
    def test_recv_task_resp(self):
        self.light.send_task(
            SystemTask(name=TaskName.SET_POWER,
                       value={'power': ElectricPower(110)}))
        self.assertEqual(TaskResponse(Status.OK), self.light.recv_task_resp())

        self.light.send_task(SystemTask(name=TaskName.ACTIVE))
        self.assertEqual(TaskResponse(Status.OK), self.light.recv_task_resp())

        self.light.send_task(Task(name=TaskName.GET_INFO))
        self.assertEqual(
            TaskResponse(status=Status.OK,
                         value={
                             'info': {
                                 'uuid': str(self.light.uuid),
                                 'identifier': type(self.light).identifier,
                                 'type': {
                                     'actuator': ['POWER'],
                                     'sensor': [],
                                     'accessor': []
                                 },
                             }
                         }), self.light.recv_task_resp())
示例#6
0
    def test_active_deactivate(self):
        self.light.send_task(
            SystemTask(name=TaskName.SET_POWER,
                       value={'power': ElectricPower(110)}))
        self.assertEqual(self.light.recv_task_resp(),
                         TaskResponse(status=Status.OK, value=None))

        self.light.send_task(SystemTask(name=TaskName.DEACTIVATE))
        self.assertEqual(self.light.recv_task_resp(),
                         TaskResponse(status=Status.OK, value=None))

        self.light.send_task(SystemTask(name=TaskName.GET_SYSTEM_INFO))
        self.assertFalse(
            self.light.recv_task_resp().value['system_info']['active'])

        self.light.send_task(SystemTask(name=TaskName.ACTIVE))
        self.assertEqual(self.light.recv_task_resp(),
                         TaskResponse(status=Status.OK, value=None))

        self.light.send_task(SystemTask(name=TaskName.GET_SYSTEM_INFO))
        self.assertTrue(
            self.light.recv_task_resp().value['system_info']['active'])

        self.light.send_task(SystemTask(name=TaskName.DEACTIVATE))
        self.assertEqual(self.light.recv_task_resp(),
                         TaskResponse(status=Status.OK, value=None))
        self.light.send_task(SystemTask(name=TaskName.GET_SYSTEM_INFO))
        self.assertFalse(
            self.light.recv_task_resp().value['system_info']['active'])

        self.light.send_task(Task(name=TaskName.GET_INFO))
        self.assertEqual(
            self.light.recv_task_resp(),
            TaskResponse(status=Status.OK,
                         value={
                             'info': {
                                 'uuid': str(self.light.uuid),
                                 'identifier': type(self.light).identifier,
                                 'type': {
                                     'actuator': ['POWER'],
                                     'sensor': [],
                                     'accessor': []
                                 },
                             }
                         }))

        self.light.send_task(SystemTask(name=TaskName.GET_SYSTEM_INFO))
        self.assertFalse(
            self.light.recv_task_resp().value['system_info']['active'])

        self.light.send_task(SystemTask(name=TaskName.DEACTIVATE))
        self.assertEqual(self.light.recv_task_resp(),
                         TaskResponse(status=Status.OK, value=None))

        self.light.send_task(SystemTask(name=TaskName.GET_SYSTEM_INFO))
        self.assertFalse(
            self.light.recv_task_resp().value['system_info']['active'])

        self.light.send_task(SystemTask(name=TaskName.ACTIVE))
        self.assertEqual(self.light.recv_task_resp(),
                         TaskResponse(status=Status.OK, value=None))

        self.light.send_task(SystemTask(name=TaskName.GET_SYSTEM_INFO))
        self.assertTrue(
            self.light.recv_task_resp().value['system_info']['active'])

        self.light.send_task(Task(name=TaskName.GET_INFO))
        self.assertEqual(
            self.light.recv_task_resp(),
            TaskResponse(status=Status.OK,
                         value={
                             'info': {
                                 'uuid': str(self.light.uuid),
                                 'identifier': type(self.light).identifier,
                                 'type': {
                                     'actuator': ['POWER'],
                                     'sensor': [],
                                     'accessor': []
                                 },
                             }
                         }))
示例#7
0
文件: Light.py 项目: ys-warble/Mesh
class Light(Concrete):
    identifier = 'light'
    default_dimension = (3, 3, 3)
    default_orientation = (0, 1, 0)

    default_consume_power_ratings = [ElectricPower(110)]

    default_hue = 208
    default_saturation = 100
    default_brightness = 90

    default_wattage = 15

    default_temperature_raise = 5  # Kelvin

    def __init__(self, uuid, dimension_x=(1, 1, 1),
                 selected_functions=(Function.POWERED, Function.TASKED, Function.COMPUTE, Function.ACTUATE),
                 hue=default_hue, saturation=default_saturation, brightness=default_brightness,
                 wattage=default_wattage,
                 temperature_raise=default_temperature_raise):
        self.active = False
        self.hue = hue
        self.saturation = saturation
        self.brightness = brightness
        self.wattage = wattage
        self.temperature_raise = temperature_raise
        super().__init__(uuid=uuid, dimension_x=dimension_x, matter_type=MatterType.GLASS,
                         selected_functions=selected_functions)

    def get_default_shape(self):
        i = self.matter_type.value
        shape = np.array([
            [[0, 0, 0],
             [0, i, 0],
             [0, 0, 0]],
            [[0, i, 0],
             [i, i, i],
             [0, i, 0]],
            [[0, 0, 0],
             [0, i, 0],
             [0, 0, 0]],
        ])

        return shape

    def validate_functions(self, selected_functions):
        if (Function.COMPUTE in selected_functions or Function.TASKED in selected_functions) and \
                Function.POWERED not in selected_functions:
            return False
        else:
            return True

    def define_functions(self, selected_functions):
        if Function.POWERED in selected_functions:
            powered = Powered(self)
            powered.power_inputs.append(PowerInput(self))
            powered.input_power_ratings.extend(Light.default_consume_power_ratings)
            self.functions[Function.POWERED] = powered

        if Function.TASKED in selected_functions:
            self.functions[Function.TASKED] = LightTasked(self)

        if Function.COMPUTE in selected_functions:
            self.functions[Function.COMPUTE] = Compute(self)

        if Function.ACTUATE in selected_functions:
            self.functions[Function.ACTUATE] = LightActuate(self)