from datetime import datetime
import re
from app import log
from app import register
from app import device
from app.app_config import *

"""
    desc:
        播放指令执行函数
 
    Parameters:
        param1 - 播放相关的数据
 
    Returns:
        执行状态
"""
@register.action(PLAY)
def action_play(data):
    if "data" in data:
        if not device.play(data.get(DATA)):
            log.logger.error("Play uri:%s on id:%s error" % (data.get(DATA).get("url"), data.get(ID)))
            return False

        log.logger.info("Play uri:%s on id:%s successfully" % (data.get(DATA).get("url"), data.get(ID)))
        return True
    else:
        log.logger.info("Data not found")
        return False

"""
    desc:
        暂停指令执行函数
 
    Parameters:
        param1 - 暂停相关的数据
 
    Returns:
        执行状态
"""
@register.action(PAUSE)
def action_pause(data):
    if "value" in data.get(DATA):
        value = data.get(DATA).get("value")
    else:
        log.logger.info("Value not found")
        return False
    if not device.pause(value):
        log.logger.error("Player pause on id:%s error" % (data.get(ID)))
        return False
    else:
        log.logger.info("Player pause on id:%s successfully" % (data.get(ID)))
        return True

"""
    desc:
        恢复指令执行函数
 
    Parameters:
        param1 - 恢复相关的数据
 
    Returns:
        执行状态
"""
@register.action(RESUME)
def action_resume(data):
    if "value" in data.get(DATA):
        value = data.get(DATA).get("value")
    else:
        log.logger.info("Value not found")
        return False
    if not device.resume(value):
        log.logger.error("Player resume on id:%s error" % (data.get(ID)))
        return False
    else:
        log.logger.info("Player resume on id:%s successfully" % (data.get(ID)))
        return True

"""
    desc:
        停止播放指令执行函数
 
    Parameters:
        param1 - 停止播放相关的数据
 
    Returns:
        执行状态
"""
@register.action(STOP)
def action_stop(data):
    if not device.stop():
        log.logger.error("Player stop on id:%s stop" % (data.get(ID)))

    log.logger.info("Player stop on id:%s successfully" % (data.get(ID)))
    return True

"""
    desc:
        设置分机号指令执行函数
 
    Parameters:
        param1 - 设置分机号相关的数据
 
    Returns:
        执行状态
"""
@register.action(SET_EXTEN)
def action_set_exten(data):
    if "data" in data:
        if HOST in data.get(DATA):
            host = data.get(DATA).get(HOST)

        if EXTEN in data.get(DATA):
            exten = data.get(DATA).get(EXTEN)
        
        if PASSWORD in data.get(DATA):
            password = data.get(DATA).get(PASSWORD)
        
        if not (host and exten and password):
            log.logger.warning("data not compelete")
            return False
    else:
        log.logger.info("data not found")
        return False
    if not device.set_exten(host, exten, password):
        log.logger.error("Set exten:%s on id:%s error" % (exten, data.get(ID)))

    log.logger.info("Set exten:%s on id:%s successfully" % (exten, data.get(ID)))
    return True

"""
    desc:
        设置播放器音量指令执行函数
 
    Parameters:
        param1 - 音量相关的数据
 
    Returns:
        执行状态
"""
@register.action(SET_SOLF_VOLUME)
def action_set_soft_volume(data):
    if "volume" in data.get(DATA):
        volume = int(data.get(DATA).get(VOLUME))
    else:
        log.logger.warning("volume not found!")
        return False
    if not device.set_soft_volume(volume):
        log.logger.error("Set volume:%d on id:%s error" % (volume, data.get(ID)))

    log.logger.info("Set soft volume:%d on id:%s successfully" % (volume, data.get(ID)))
    return True

"""
    desc:
        设置硬件音量指令执行函数
 
    Parameters:
        param1 - 硬件音量相关的数据
 
    Returns:
        执行状态
"""
@register.action(SET_HARD_VOLUME)
def action_set_hard_volume(data):
    if "volume" in data.get(DATA):
        volume = int(data.get(DATA).get(VOLUME))
    else:
        log.logger.warning("volume not found!")
        return False
    if not device.set_hard_volume(volume):
        log.logger.error("Set hard volume:%d on id:%s error" % (volume, data.get(ID)))

    log.logger.info("Set hard volume:%d on id:%s successfully" % (volume, data.get(ID)))
    return True

"""
    desc:
        设置强控指令执行函数
 
    Parameters:
        param1 - 设置强控相关的数据
 
    Returns:
        执行状态
"""
@register.action(SET_HARD_VOLUME_CONTROL)
def action_set_hard_volume_control(data):
    if "value" in data.get(DATA):
        value = data.get(DATA).get(VALUE)
    else:
        log.logger.warning("value not found!")
        return False
    if not device.set_hard_volume_control(value):
        log.logger.error("Set hard volume control:%s on id:%s error" % (value, data.get(ID)))

    log.logger.info("Set hard volume control:%s on id:%s successfully" % (value, data.get(ID)))
    return True

"""
    desc:
        设置相关数据通用指令执行函数
 
    Parameters:
        param1 - 相关的数据
 
    Returns:
        执行状态
"""
@register.action(SET_SERVICE_SETTINGS)
def action_set_service_settings(data):
    if "data" in data:
        data_obj = data.get(DATA)
    if not device.set_service_settings(data_obj):
        log.logger.error("Set service settings:%s on id:%s error" % (data_obj, data.get(ID)))
        return False

    log.logger.info("Set service settings:%s on id:%s successfully" % (data_obj, data.get(ID)))
    return True

"""
    desc:
        开始推送rtsp指令执行函数
 
    Parameters:
        param1 - rtsp相关的数据
 
    Returns:
        执行状态
"""
@register.action("start-pull-rtsp")
def action_start_pull_rtsp(data):
    if "src" in data.get(DATA):
        src = data.get(DATA).get("src")
    else:
        log.logger.warning("src not found!")
        return False

    if "dst" in data.get(DATA):
        dst = data.get(DATA).get("dst")
    else:
        log.logger.warning("dst not found!")
        return False

    if not device.start_pull_rtsp(src, dst):
        log.logger.error("Start pull rtsp src:%s on id:%s error" % (src, data.get(ID)))
    else:
        log.logger.info("Start pull rtsp src:%s on id:%s successfully" % (src, data.get(ID)))
    return True

"""
    desc:
        停止推送rtsp指令执行函数
 
    Parameters:
        param1 - rtsp相关的数据
 
    Returns:
        执行状态
"""
@register.action("stop-pull-rtsp")
def action_stop_pull_rtsp(data):
    if not device.stop_pull_rtsp():
        log.logger.error("Stop pull rtsp on id:%s error" % (data.get(ID)))
    else:
        log.logger.info("Stop pull rtsp on id:%s successfully" % (data.get(ID)))
    return True

@register.action("reboot")
def action_reboot(data):
    device.reboot(data.get("id"))
    log.logger.info("Reboot on id:%s successfully" % (data.get(ID)))
    return True

@register.action("reset")
def action_reset(data):
    device.reset(data.get("id"))
    log.logger.info("Reset on id:%s successfully" % (data.get(ID)))
    return True

@register.action("upgrade")
def action_upgrade(data):
    if not "data" in data:
        log.logger.info("Upgrade on id:%s miss argments" % (data.get(ID)))
        return False
    device.upgrade(data.get("id"), data.get(DATA))
    log.logger.info("Upgrade on id:%s executing..." % (data.get(ID)))
    return True

@register.action("get-upgrade-status")
def action_get_upgrade_status(data):
    device.getUpgradeStatus(data.get("id"))
    log.logger.info("Get upgrade status on id:%s successfully" % (data.get(ID)))
    return True

@register.action("import-config")
def action_import_config(data):
    if not "data" in data:
        log.logger.info("Get import config on id:%s miss argments" % (data.get(ID)))
        return False
    device.importConfig(data.get("id"), data.get(DATA))
    log.logger.info("Get import config on id:%s successfully" % (data.get(ID)))
    return True

@register.action("relay-control")
def action_relay_control(data):
    if not "data" in data:
        log.logger.info("Get relay control on id:%s miss argments" % (data.get(ID)))
        return False
    device.relayControl(data.get("id"), data.get(DATA))
    log.logger.info("Get relay control on id:%s successfully" % (data.get(ID)))
    return True