Ejemplo n.º 1
0
 def test_stop(self):
     ptl = periodic_task.PeriodicTaskLoop()
     ptl.start()
     ptl.stop()
     self.assertFalse(ptl.running)
     time.sleep(3)
     self.assertFalse(ptl._thread.is_alive())
Ejemplo n.º 2
0
    def test_callbacks_2(self):
        callback = mock.MagicMock()
        args = ('arg_1', 'arg_2')
        kwargs = {'kwarg_1': 'kwarg_1', 'kwarg_2': 'kwarg_2'}
        finish_cb = mock.MagicMock()
        finish_args = ('finish_arg_1', 'finish_arg_2')
        finish_kwargs = {
            'finish_kwarg_1': 'finish_kwarg_1',
            'finish_kwarg_2': 'finish_kwarg_2'
        }
        count_increase_cb = mock.MagicMock()
        count_increase_cb_args = ('increase_cb_arg_1', 'increase_cb_arg_2')
        count_increase_cb_kwargs = {
            'increase_cb_kwarg_1': 'increase_cb_kwarg_1',
            'increase_cb_kwarg_2': 'increase_cb_kwarg_2'
        }

        ptl = periodic_task.PeriodicTaskLoop()
        ptl.start()
        pt = periodic_task.PeriodicTask(
            ptl,
            datetime.datetime.now() + datetime.timedelta(seconds=2),
            callback,
            args=args,
            kwargs=kwargs,
            finish_cb=finish_cb,
            finish_args=finish_args,
            finish_kwargs=finish_kwargs,
            count_increase_cb=count_increase_cb,
            count_increase_cb_args=count_increase_cb_args,
            count_increase_cb_kwargs=count_increase_cb_kwargs,
            name='test',
            count=4,
            count_done=1,
            period=2,
            end_time=None)
        pt.start()
        time.sleep(8)
        ptl.stop()
        self.assertEquals(callback.mock_calls, [
            mock.call(*args, **kwargs),
            mock.call(*args, **kwargs),
            mock.call(*args, **kwargs)
        ])
        finish_cb.assert_called_once_with(*finish_args, **finish_kwargs)
        self.assertEquals(count_increase_cb.mock_calls, [
            mock.call(*count_increase_cb_args, **count_increase_cb_kwargs),
            mock.call(*count_increase_cb_args, **count_increase_cb_kwargs),
            mock.call(*count_increase_cb_args, **count_increase_cb_kwargs)
        ])
Ejemplo n.º 3
0
def main():
    parse_args(sys.argv)
    logging.setup(CONF, CONF.logger_name)
    log = logging.getLogger(__name__)
    log.info('Starting Smart')

    sql_model.connect_db()
    
    # initialize all devices from handler_dev table
    handler_devs = handler.init_handler_devs()
    
    # initialize all sensors from sensor table
    # 'active' sensors should start listen to incoming values
    # if many sensors share same listen port - singleton sensor should be used
    # this singleton should have mapping name_of_sensor TO sensor_name
    # where name_of_sensor come with value from sensor and sensor.id from DB
    #
    # all device for controll are sensors. for example 
    # control button in web page is sensor too.
    sensors = sensor.init_sensors()
    
    # apply all rules, run periodic_task_loop
    task_loop = periodic_task.PeriodicTaskLoop()
    # Function apply_rules creates many PeriodicTasks using 
    #      - 'when' conditions
    #      - callbacks(callback, increment_tick_cb, finish_cb)
    # Callback must check sql_condition using table sensor_values and
    # if condition match DO action with device from handler_dev table
    # Each rule will produce 3 callbacks.
    # Callback functions will be generated with 
    #     - standart SET function and
    #     - arguments connected to SET func with functool.partial
    #       arguments will have condition str to choose device: device.id=XXX
    #       and VALUE which will be set with conn_set_str string
    # apply_db_rules returns mapping rule_id TO PeriodicTask
    rule_id_2_periodic_task_map = rule.apply_db_rules(
        task_loop, handler_devs, sensors)
    task_loop.start()
    
    # run xml-rpc server thread to accept WEB server request 
    # send map rule_id_2_periodic_task_map to controll existing rules
    # and add new if needed.
    # map rule_id_2_periodic_task_map must be synchronized with locker
    xmlrpc_loop = xmlrpc_server.run_xml_rpc_server(
        rule_id_2_periodic_task_map, handler_devs, sensors)
    
    task_loop.join()
    xmlrpc_loop.join()
Ejemplo n.º 4
0
    def test_shedule_cancel_task_before_start(self):
        def cb1():
            pass

        def cb2():
            pass

        ptl = periodic_task.PeriodicTaskLoop()
        dtime = [
            datetime.datetime(year=2015, month=12, day=1),
            datetime.datetime(year=2015, month=12, day=2)
        ]
        ptl.schedule_task(dtime[1], cb1)
        ptl.schedule_task(dtime[0], cb2)
        self.assertEqual(ptl._action_ordered_list[0][0], dtime[0])
        self.assertEqual(ptl._action_ordered_list[1][0], dtime[1])
        self.assertEqual(len(ptl._action_ordered_list), 2)

        ptl.cancel_task(cb1)
        ptl.cancel_task(cb2)
        self.assertEqual(len(ptl._action_ordered_list), 0)
Ejemplo n.º 5
0
    def test_loop(self):
        call_list = []

        def cb2():
            call_list.append('cb2')

        def cb4():
            call_list.append('cb4')

        def cb3():
            call_list.append('cb3')

        def cb3_after_start():
            call_list.append('cb3_after_start')

        def cb4_after_start():
            call_list.append('cb4_after_start')

        now = datetime.datetime.now()
        dtime2 = now + datetime.timedelta(seconds=2)
        dtime4 = now + datetime.timedelta(seconds=4)
        dtime3 = now + datetime.timedelta(seconds=3)

        ptl = periodic_task.PeriodicTaskLoop()
        ptl.schedule_task(dtime2, cb2)
        ptl.schedule_task(dtime4, cb4)
        ptl.schedule_task(dtime3, cb3)
        ptl.start()
        ptl.schedule_task(dtime3, cb3_after_start)
        ptl.schedule_task(dtime4, cb4_after_start)
        time.sleep(1)
        ptl.cancel_task(cb4_after_start)
        time.sleep(5)
        ptl.stop()
        self.assertTrue(
            call_list == ['cb2', 'cb3', 'cb3_after_start', 'cb4']
            or call_list == ['cb2', 'cb3_after_start', 'cb3', 'cb4'],
            msg='actual call_list == %s' % str(call_list))
Ejemplo n.º 6
0
 def test_start(self):
     ptl = periodic_task.PeriodicTaskLoop()
     ptl.start()
     self.assertTrue(ptl.running)
     self.assertTrue(ptl._thread.is_alive())
     ptl.stop()
Ejemplo n.º 7
0
 def test_init(self):
     ptl = periodic_task.PeriodicTaskLoop()