def refresh_table(request, project=0):
    variables = Variable.objects.filter(project=project)
    project = Project.objects.get(pk=project)
    client = openshowvar(project.IP, KVP_ROBOT_PORT)

    jsondata_unsorted = [ob.as_json() for ob in variables]
    variables = sorted(jsondata_unsorted, key=lambda k: k['description'])
    # print(variables)
    # variables = sorted(jsondata_unsorted, key = lambda k:k['description'].lower())
    if client.can_connect:
        for item in variables:
            value = client.read(item['name'], KVP_DEBUG)
            value = response_openshowvar(value)
            item.update({'value': value})
    if request.is_ajax():
        return HttpResponse(json.dumps(variables), content_type='json')
    else:
        read_form = ReadForm()
        write_form = WriteForm()
        return render(
            request, 'read_write_variable/project/home.html', {
                'state_form': state_form,
                'read_form': read_form,
                'write_form': write_form,
                'variables': variables,
            })
def read_variable(request, project=0):
    if project == 0:
        IP = request.POST.get('IP')
    else:
        project = Project.objects.get(pk=project)
        IP = project.IP
    variable = request.POST.get('variable')
    client = openshowvar(IP, KVP_ROBOT_PORT)
    if client.can_connect:
        result = client.read(variable, KVP_DEBUG)
        result = response_openshowvar(result)
    else:
        result = 'Variable was not read'

    data = {'result': result}
    # print(request.POST)
    return JsonResponse(data)
def state(request):
    IP = request.POST.get('IP')
    f_ip = forms.GenericIPAddressField()
    try:
        f_ip.clean(IP)
        client = openshowvar(IP, KVP_ROBOT_PORT)
        if client.can_connect:
            robname = client.read('$ROBNAME[]', KVP_DEBUG)
            robname = response_openshowvar(robname)
            state = 'Connected to ' + robname
        else:
            state = 'Conection is not posible'
    except:
        state = 'Invalid IP'

    data = {
        'state': state,
    }
    return JsonResponse(data)
def write_variable(request, project=0):
    if project == 0:
        IP = request.POST.get('IP')
    else:
        project = Project.objects.get(pk=project)
        IP = project.IP
    variable = request.POST.get('variable')
    value = request.POST.get('value')
    client = openshowvar(IP, KVP_ROBOT_PORT)
    if client.can_connect:
        result = client.write(variable, value, KVP_DEBUG)
        result = response_openshowvar(result)
    else:
        result = 'Variable was not written'

    data = {
        'result': result,
    }
    return JsonResponse(data)