Пример #1
0
def pr_add_device(request, group_id):
    group = PurpleRobotDeviceGroup.objects.filter(group_id=group_id).first()
    
    if group != None:
        if request.method == 'POST':
            device_id = request.POST['device_id']
            device_name = request.POST['device_name']

            if device_id == None or len(device_id.strip()) == 0 or device_name == None or len(device_name.strip()) == 0:
                request.session['pr_messages'] = [ 'Please provide a non-empty device name and identifier.' ]
            elif PurpleRobotDevice.objects.filter(device_id=device_id).count() == 0:
                device = PurpleRobotDevice(device_id=device_id, name=device_name, device_group=group)

                perf_data = json.loads(device.performance_metadata)
                
                perf_data['latest_readings'] = {}
                perf_data['latest_readings']['edu.northwestern.cbits.purple_robot_manager.probes.builtin.RobotHealthProbe'] = -1
                perf_data['latest_readings']['edu.northwestern.cbits.purple_robot_manager.probes.builtin.BatteryProbe'] = -1
                perf_data['latest_readings']['edu.northwestern.cbits.purple_robot_manager.probes.builtin.SoftwareInformationProbe'] = -1
                perf_data['latest_readings']['edu.northwestern.cbits.purple_robot_manager.probes.builtin.HardwareInformationProbe'] = -1
                
                device.performance_metadata = json.dumps(perf_data, indent=2)

                device.save()
            else:
                request.session['pr_messages'] = [ 'Unable to create device. A device already exists with identifier "' + device_id + '".' ]
    else:
        request.session['pr_messages'] = [ 'Unable to locate group with identifier "' + group_id + '" and create new device.' ]
    
    response = redirect(reverse('pr_home'))
    
    return response
Пример #2
0
def pr_add_device(request, group_id):
    group = PurpleRobotDeviceGroup.objects.filter(group_id=group_id).first()

    if group != None:
        if request.method == 'POST':
            device_id = request.POST['device_id']
            device_name = request.POST['device_name']

            if device_id == None or len(device_id.strip()) == 0 or device_name == None or len(device_name.strip()) == 0:
                request.session['pr_messages'] = [ 'Please provide a non-empty device name and identifier.' ]
            elif PurpleRobotDevice.objects.filter(device_id=device_id).count() == 0:
                device = PurpleRobotDevice(device_id=device_id, name=device_name, device_group=group)

                device.save()
            else:
                request.session['pr_messages'] = [ 'Unable to create device. A device already exists with identifier "' + device_id + '".' ]
    else:
        request.session['pr_messages'] = [ 'Unable to locate group with identifier "' + group_id + '" and create new device.' ]
    
    return redirect(reverse('pr_home'))