示例#1
0
def light_control():
    light_ids = json.loads(request.args.get('light_ids', []))
    if not isinstance(light_ids, list):
        light_ids = [light_ids]
    delay = int(request.args.get('delay', 0))
    group_id = request.args.get('group_id', None)
    status = request.args.get('status', 'OFF')
    updated_status = "ON" if status == "OFF" else "OFF"
    payloads = []
    for light_id in light_ids:
        payload = "{\"status\":\"" + updated_status + "\",\"device_id\":\"" + str(light_id) + "\"}"
        publish_with_delay(get_mqtt_client(), "switch/relay", payload, delay / 1000)
        print("updating light status id:", light_id, "to ", updated_status)
        update_device_status(DeviceStatus(light_id, Status(updated_status)))
        payloads.append(payload)
    if group_id:
        group = load_group_by(group_id)
        updated_group = Group(group.group_id, group.name, group.delay_in_ms, group.ids, Status(updated_status))
        update_group(updated_group)

    print(payloads)
    response = make_response(json.dumps(payloads), 200)
    response.headers['Access-Control-Allow-Origin'] = '*'

    return response
示例#2
0
def remove_group():
    try:
        group_id = request.args['group_id']
        group = load_group_by(group_id)
        delete_group(group_id)
        remove_associated_schedules_for(group_id)
        delete_dynamo_devices(group)
        return redirect(url_for('lights.show_lights', _method='GET', page='groups'))
    except TemplateNotFound:
        abort(404)
示例#3
0
def edit_group():
    try:
        group_id = request.args['group_id']
        statusbar = refresh_statusbar()
        group_to_edit: Group = load_group_by(group_id)
        all_devices: [Device] = [device.as_dict() for device in load_all_devices()]

        return render_template('lights/edit_group.html', active='lights', group=group_to_edit,
                               devices=all_devices, statusbar=statusbar,
                               api_base_url=current_app.config['API_BASE_URL'])
    except TemplateNotFound:
        abort(404)
示例#4
0
def save_new_group_schedule():
    try:
        schedule: Schedule = get_schedule_from_form(request, is_group=True)
        is_update = schedule.schedule_id is not None
        group = load_group_by(schedule.group_id)
        actions = create_actions(schedule, group.delay_in_ms)

        save_new_or_update_schedule(actions, is_update, schedule)

        return redirect(url_for('lights.show_lights', _method='GET', page='groups'))
    except TemplateNotFound:
        abort(404)
示例#5
0
def init_task_scheduler(schedules: [Schedule]):
    # TODO create one liner init
    logger.debug('Initializing {} tasks'.format(len(schedules)))

    for schedule in schedules:

        def publisher(client, topic, payload):
            if client is not None:
                client.publish(topic=topic, payload=payload)
            else:
                logger.error('can not publish message, client is not defined')

        group = load_group_by(schedule.group_id)
        delay_in_ms = group.delay_in_ms if group is not None else 0
        scheduler.schedule_task_from_db(
            schedule,
            create_action(schedule.status, str(schedule.device_id),
                          get_mqtt_client(), publisher, delay_in_ms))
示例#6
0
def lights_data():
    light_id = request.args.get('light_id', None)
    group_id = request.args.get('group_id', None)
    payload = {
        'group_status': None,
        'single_device_status': None
    }
    if group_id:
        group = load_group_by(group_id)
        payload['group_status'] = group.status.value
    if light_id:
        device = load_device_with_status_by(int(light_id))
        payload['single_device_status'] = device.status.value

    print(payload)
    response = make_response(payload, 200)
    response.headers['Access-Control-Allow-Origin'] = '*'

    return response