コード例 #1
0
ファイル: sensor.py プロジェクト: heymind/simple-iot-server
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)
コード例 #2
0
ファイル: sensor.py プロジェクト: heymind/simple-iot-server
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.')
コード例 #3
0
ファイル: user.py プロジェクト: heymind/simple-iot-server
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.')
コード例 #4
0
ファイル: device.py プロジェクト: heymind/simple-iot-server
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.')
コード例 #5
0
ファイル: action.py プロジェクト: heymind/simple-iot-server
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)
コード例 #6
0
ファイル: user.py プロジェクト: heymind/simple-iot-server
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.')
コード例 #7
0
ファイル: device.py プロジェクト: heymind/simple-iot-server
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.')
コード例 #8
0
ファイル: action.py プロジェクト: heymind/simple-iot-server
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)
コード例 #9
0
ファイル: action.py プロジェクト: heymind/simple-iot-server
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)
コード例 #10
0
ファイル: device.py プロジェクト: heymind/simple-iot-server
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')