Esempio n. 1
0
def device_get(device_id=None):
    (_type,instance)=TokenRef.get_ref(request.headers.get('token'))
    if _type is 'user':
        device = Device.get_instance(device_id)
        if device:
            if device in instance.devices:
                return succ_doc(device)
            else:
                return erro('permission not allow.')
        return succ_doc(instance.devices)
    elif _type is 'device':
        return  succ_doc(instance)
    return erro('wrong token.')
Esempio n. 2
0
def sensor_crtate(sensor_symbol=None):
    if not sensor_symbol:
        return erro('no sensor_symbol provided. ')

    arg=request.args
    sensor_name=arg.get('name')
    sensor_type=arg.get('type')
    if not (sensor_type and sensor_type):
        return erro('no sensor_type sensor_name provided. ')

    sensor_type = sensor_type.upper()

    (_type,instance)=TokenRef.get_ref(request.headers.get('token'))
    if _type is not 'device':
        return erro('only support device token . ')

    sensors = instance.sensors or {}
    sensor = Sensor(name=sensor_name,type=sensor_type)
    sensors[sensor_symbol]=sensor

    try:
        instance.update(sensors=sensors)
        return succ_doc(sensor)
    except Exception as err:
        return erro(err)
Esempio n. 3
0
def action_get(action_symbol=None):

    if not action_symbol:
        return erro('No symbol provided.')

    arg = request.args
    (arg_device,arg_state) = (arg.get('device'),arg.get('state'))


    (_type,instance)=TokenRef.get_ref(request.headers.get('token'))
    if not _type:
        return erro('wrong token.')

    device = None
    updated= None

    if _type is 'user':
        _device = Device.get_instance(arg_device)
        if not _device:
            return erro('wrong device id.')
        if instance.check_device(_device):
            device = _device
            updated='should_state'
        else:
            return erro('permission not allow.')
    elif _type is 'device':
        device = instance
        updated='state'

    actions = device.actions
    action = None
    try:
        action = actions[action_symbol]
    except KeyError:
        return erro('(symbol not found.)action not exists.')

    if arg_state:
        #update
        try:
            action[updated] = arg_state
            actions[action_symbol] = action
            device.update(actions=actions)
            return succ({})
        finally:
            return erro('unknown error.')
    else:
        #get
        return succ_doc(action)
Esempio n. 4
0
def device_delete(device_id):
    (_type,instance)=TokenRef.get_ref(request.headers.get('token'))
    if _type is 'device':
        try:
            instance.delete()
            return succ({})
        finally:
            return erro('unknown mistakes.')
    elif _type is 'user':
        device = Device.get_instance(device_id)
        if device:
            try:
                instance.update(pull__devices=device)
                return succ_doc(instance)
            finally:
                return erro('unknown mistakes.')
        else:
            erro('wrong device_id.')
    else:
        return erro('wrong token.')
Esempio n. 5
0
def action_create(action_symbol):

    if not action_symbol:
        return erro('No symbol provided.')

    json=request.get_json()
    if not json:
        return erro('No action info provided.')

    (_type,instance)=TokenRef.get_ref(request.headers.get('token'))
    if not _type is 'device':
        return erro('Wrong token.(only support device token.)')

    actions = instance.actions or {}
    action = Action(name=json.get('name'),type=json.get('type'),multi_switches=json.get('multi_switches'))
    actions[action_symbol] = action

    try:
        instance.update(actions=actions)
        return succ_doc(action)
    except Exception as err:
        return erro(err)
Esempio n. 6
0
def device_post():
    def bind_device(user,device):
        user.update(push__devices=device)

    (_type,instance)=TokenRef.get_ref(request.headers.get('token'))
    json=request.get_json()
    if _type is 'user':
        user = instance
        if not json:
            json = {}
        arg_device = json.get('device')
        arg_bindcode =  json.get('bind_code')
        arg_name=json.get('name')
        if arg_device and arg_bindcode:
            #bind a device
            try:
                d= Device.get_instance(arg_device)
                if not d:
                    return erro('wrong device id.')
                if arg_bindcode is d.bind_code:
                    bind_device(user,d)
                    return succ_doc(d)
                else:
                    return erro('wring bind code.')
            finally:
                #if program here,some mistake must happend.
                return  erro('unknown misktake.')
        elif arg_device:
            #clone a device
            try:
                be_cloned= Device.get_instance(arg_device)
                if not be_cloned:
                    return erro('wrong device id.')
                clone_device = Device.clone(be_cloned)
                clone_device.name = arg_name
                clone_device.save()
                bind_device(user,clone_device)
                return succ({
                        'device':clone_device.id,
                        'token':TokenRef.get_token(clone_device),
                        'bind_code':clone_device.bind_code
                })
            finally:#if program here,some mistake must happend.
                return erro('unknown misktake.')
        else:
            #create a new Device
            try:
                device = Device(name = arg_name).save()
                bind_device(user,device)
                return succ({
                        'device':str(device.id),
                        'token':TokenRef.get_token(device),
                        'bind_code':device.bind_code
                })
            except Exception as err:
                raise(err);
                return erro(err)
            # finally:
            #     return erro('unknown misktake.')
    elif _type is 'device':
        #update a device
        try:
            instance.update(**json)
            return succ_doc(instance)
        finally:
            return erro('something wrong when update the device.')
    else:
        return erro('wrong token')