Beispiel #1
0
def datapoint_crtate(sensor_symbol=None):
    if not sensor_symbol:
        return erro('no sensor_symbol provided. ')

    (_type,instance)=TokenRef.get_ref(request.headers.get('token'))

    if _type is not 'device':
        return erro('only support device token . ')

    sensors = instance.sensors
    args = request.args
    ticks={}

    for arg in args:
        if arg in sensors:
            ticks[arg]=args.get(arg)

    if len(ticks) > 0:
        try:
            DeviceData(device = instance,ticks=ticks).save()
            return  succ('succ.')
        except Exception as err:
            return erro(err)
    else:
        return erro('nothing saved.')
Beispiel #2
0
def register():
    json=request.get_json()
    try:
        user = User(username=json.get('username'),password=json.get('password'),name=json.get('name')).save()
        return  succ({'token':TokenRef.get_token(user)})
    except mongoengine.errors.ValidationError as err:
        return erro('username or password is out of rules.')
    except mongoengine.errors.NotUniqueError as err:
        return erro('username exists.')
Beispiel #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)
Beispiel #4
0
def action_delete(action_symbol):

    if not action_symbol:
        return erro('No symbol 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
    try:
        actions.pop(action_symbol)
        instance.update(actions=actions)
        return succ({})
    except KeyError:
        return erro('(symbol not found.)action not exists.')
    except Exception as err:
        return erro(err)
Beispiel #5
0
def login():
    # username = request.args.get('username')
    # password = request.args.get('password')
    # if username and password:
    #     users = User.objects(username=username,password=password)
    #     if len(users) is 1:
    #         return succ({'token':TokenRef.get_token(users[0])})
    #     else:
    #         return erro('Wrong username or password.')
    # else:
    #     return erro('please provides username and password.')
    args = request.args
    try:
        users = User.objects(username=args.get('username'),password=args.get('password'))
        return succ({'token':TokenRef.get_token(users[0])})
    except IndexError:
        return erro('Wrong username or password.')
    except mongoengine.errors.ValidationError as err:
        return erro('please provides username and password.')
Beispiel #6
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.')
Beispiel #7
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')