コード例 #1
0
def create_dispo(dispo_pk):
    dispo = Dispositivo.objects.get(pk=dispo_pk)
    celery = Celery(app='ProxyManagerWeb:tarefa:create_dispo', task='')
    url = get_url(dispo.mqtt.proxy.url)
    url += '/api/dispositivo/'
    head = {'Authorization': 'token {}'.format(dispo.mqtt.proxy.token)}

    data = {}
    data['nome']=dispo.nome
    data['tipo'] = dispo.tipo
    data['is_int']=dispo.is_int
    data['mqtt']=dispo.mqtt.proxy_alt_id
    try:
        response = requests.post(url, json=data, headers=head)
        if response.status_code == 201:
            jsondis = json.loads(response.text)
            dispo.proxy_alt_id=jsondis['id']
            dispo.save()
            celery.desc = 'Respondeu'
            celery.save()

        elif response.status_code == 404:
            celery.desc = 'Não Respondeu 404'
            celery.save()
        else:
            print('Não Respondeu: {}'.format(str(response.status_code)))
            print(str(response.text))
            celery.desc = 'Não Respondeu: {}'.format(str(response.status_code))
            celery.save()

    except Exception as e:
        celery = Celery(app='ProxyManagerWeb:tarefa:create_dispo', desc='erro',
                        exception=e, task='')
        celery.save()
コード例 #2
0
def create_if_else_sensor_string(task_pk, proxy_pk):
    task = If_else_sensor_string.objects.get(pk=task_pk)
    proxy = Proxy.objects.get(pk=proxy_pk)
    celery = Celery(app='ProxyManagerWeb:tarefa:create_if_else_sensor_string', task='')
    url = get_url(proxy.url)
    url += task.tipo.url_create
    print(url)
    head = {'Authorization': 'token {}'.format(proxy.token)}

    data = {}
    data['comando'] = task.comando
    data['condicao']=task.condicao
    data['valor'] = task.valor
    data['tipo'] = task.tipo.task_tipo

    try:
        response = requests.post(url, json=data, headers=head)
        if response.status_code == 201:
            jsondis = json.loads(response.text)
            task.proxy_alt_id = jsondis['id']
            task.save()
            celery.desc = 'Respondeu'
            celery.save()

        elif response.status_code == 404:
            celery.desc = 'Não Respondeu: 404'+response.text
            celery.save()
        else:
            print('Não Respondeu: {}'.format(str(response.status_code)))
            celery.desc = 'Não Respondeu: {}'.format(str(response.status_code))
            celery.save()

    except Exception as e:
        celery = Celery(app='ProxyManagerWeb:tarefa:create_task', desc='erro',
                        exception=e, task='')
        celery.save()
コード例 #3
0
def delete_dispo(dispo_pk):
    dispo = Dispositivo.objects.get(pk=dispo_pk)
    celery = Celery(app='ProxyManagerWeb:tarefa:delete_dispo', task='')
    url = get_url(dispo.mqtt.proxy.url)
    url += '/api/dispositivo/'
    head = {'Authorization': 'token {}'.format(dispo.mqtt.proxy.token)}

    try:
        response = requests.delete(url, headers=head)
        if response.status_code == 204:
            celery.desc = 'Respondeu e delete'
            celery.save()
        else:
            celery.desc = 'delete não foi completo'
            celery.save()

    except Exception as e:
        celery = Celery(app='ProxyManagerWeb:tarefa:delete_dispo', desc='erro',
                        exception=e, task='')
        celery.save()
コード例 #4
0
def create_atuador_troca_estado(task_pk, proxy_pk):
    print(task_pk)
    task = Atuador_troca_estado.objects.get(pk=task_pk)
    proxy = Proxy.objects.get(pk=proxy_pk)
    celery = Celery(app='ProxyManagerWeb:tarefa:create_atuador_troca_estado', task='')
    url = get_url(proxy.url)
    url += task.tipo.url_create
    head = {'Authorization': 'token {}'.format(proxy.token)}

    data = {}
    data['comando'] = task.comando
    data['estado_atual'] = task.estado_atual
    data['estado_anterior'] = task.estado_anterior
    data['atuador'] = task.atuador.proxy_alt_id
    data['tipo'] = task.tipo.task_tipo

    try:
        response = requests.post(url, json=data, headers=head)
        if response.status_code == 201:
            jsondis = json.loads(response.text)
            task.proxy_alt_id = jsondis['id']
            task.save()
            celery.desc = 'Respondeu'
            celery.save()

        elif response.status_code == 404:
            celery.desc = 'Não Respondeu: 404'
            celery.save()
        else:
            print('Não Respondeu: {}'.format(str(response.status_code)))
            print(str(response.text))
            celery.desc = 'Não Respondeu: {}'.format(str(response.status_code))
            celery.save()

    except Exception as e:
        celery = Celery(app='ProxyManagerWeb:tarefa:create_task', desc='erro',
                        exception=e, task='')
        celery.save()
コード例 #5
0
def delete_task(task_pk, proxy_pk):
    proxy = Proxy.objects.get(pk=proxy_pk)
    celery = Celery(app='ProxyManagerWeb:tarefa:delete_task', task='')
    url = get_url(proxy.url)
    url += '/api/{}/taskUpdate/'.format(task_pk)
    head = {'Authorization': 'token {}'.format(proxy.token)}

    try:
        response = requests.delete(url, headers=head)
        if response.status_code == 204:
            celery.desc = 'Respondeu'
            celery.save()

        elif response.status_code == 404:
            celery.desc = 'Não Respondeu: 404'
            celery.save()
        else:
            print('Não Respondeu: {}'.format(str(response.status_code)))
            celery.desc = 'Não Respondeu: {}'.format(str(response.status_code))
            celery.save()

    except Exception as e:
        celery = Celery(app='ProxyManagerWeb:tarefa:delete_task', desc='erro',
                        exception=e, task='')
        celery.save()
コード例 #6
0
def edit_dispo(dispo_pk):
    dispo = Dispositivo.objects.get(pk=dispo_pk)
    celery = Celery(app='ProxyManagerWeb:tarefa:edit_dispo', task='')
    url = get_url(dispo.mqtt.proxy.url)
    url += '/api/dispositivo/'
    head = {'Authorization': 'token {}'.format(dispo.mqtt.proxy.token)}

    data = {}
    data['nome'] = dispo.nome
    data['tipo'] = dispo.tipo
    data['is_int'] = dispo.is_int
    data['mqtt'] = dispo.mqtt.proxy_alt_id
    try:
        response = requests.put(url, json=data, headers=head)
        if response.status_code == 200:
            celery.desc = 'Respondeu e Update'
            celery.save()
        else:
            celery.desc = 'Update não foi completo'
            celery.save()

    except Exception as e:
        celery = Celery(app='ProxyManagerWeb:tarefa:edit_dispo', desc='erro',
                        exception=e, task='')
        celery.save()
コード例 #7
0
def delete_job(proxyId, url, token):
    celery = Celery(app='ProxyManagerWeb:tarefa:edit_job', task='')
    url = get_url(url)
    url += '/api/{}/jobUpdate/'.format(proxyId)
    head = {'Authorization': 'token {}'.format(token)}

    try:
        response = requests.delete(url, headers=head)
        if response.status_code == 204:
            celery.desc = 'Respondeu'
            celery.save()

        elif response.status_code == 404:
            celery.desc = 'Não Respondeu: 404'
            celery.save()
        else:
            print('Não Respondeu: {}'.format(str(response.status_code)))
            print(str(response.text))
            celery.desc = 'Não Respondeu: {}'.format(str(response.status_code))
            celery.save()

    except Exception as e:
        celery = Celery(app='ProxyManagerWeb:tarefa:delete_job', desc='erro',
                        exception=e, task='')
        celery.save()
コード例 #8
0
def get_broker(proxy):
    celery = Celery(app='ProxyManagerWeb:block:conect_proxy',
                    task='get_broker')
    url = get_url(proxy.url)
    url += '/api/broker/'
    head = {'Authorization': 'token {}'.format(proxy.token)}
    try:
        response = requests.get(url, headers=head)
        if response.status_code == 200:
            brokerjson = json.loads(response.text)[0]
            try:
                broker = proxy.broker
            except:
                broker = Broker()
            broker = save_broker(broker, brokerjson)
            proxy.broker = broker
            proxy.save()
            broker.save()

        elif response.status_code == 404:
            celery.desc = 'Proxy não respondeu'
            celery.exception = response.text
            celery.save()
        elif response.status_code == 400:
            celery.desc = 'Proxy não respondeu'
            celery.exception = response.text
            celery.save()
    except Exception as e:
        celery = Celery(app='ProxyManagerWeb:block:conect_proxy',
                        desc='Get Broker',
                        exception=e,
                        task='get_broker')
        celery.save()
コード例 #9
0
def get_dispositivo(proxy):
    celery = Celery(app='ProxyManagerWeb:block:conect_proxy',
                    task='get_dispositivo')
    url = get_url(proxy.url)
    url += '/api/dispositivo/'
    head = {'Authorization': 'token {}'.format(proxy.token)}
    try:
        response = requests.get(url, headers=head)
        if response.status_code == 200:
            jsondispo = json.loads(response.text)
            for jsons in jsondispo:
                mqtt = Mqtt.objects.get(proxy_alt_id=jsons['mqtt'],
                                        proxy=proxy)
                dispositivo = Dispositivo.objects.filter(proxy=proxy,
                                                         mqtt=mqtt).exists()
                if dispositivo is False:
                    dispo = Dispositivo(mqtt=mqtt,
                                        proxy=proxy,
                                        tipo=jsons['tipo'],
                                        is_int=jsons['is_int'],
                                        nome=jsons['nome'],
                                        proxy_alt_id=jsons['id'])
                    dispo.save()
                else:
                    dispositivo = Dispositivo.objects.filter(
                        proxy=proxy, mqtt=mqtt).first()
                    dispositivo.nome = jsons['nome']
                    dispositivo.tipo = jsons['tipo']
                    dispositivo.is_int = jsons['is_int']
                    dispositivo.proxy_alt_id = jsons['id']
                    dispositivo.save()
        elif response.status_code == 404:
            celery.desc = 'Proxy não respondeu'
            celery.exception = response.text
            celery.save()

        elif response.status_code == 400:
            celery.desc = 'Proxy não respondeu'
            celery.exception = response.text
            celery.save()
    except Exception as e:
        celery = Celery(app='ProxyManagerWeb:block:conect_proxy',
                        desc='get_dispositivo',
                        exception=e,
                        task='get_dispositivo')
        celery.save()
コード例 #10
0
def get_mqtt(proxy):
    celery = Celery(app='ProxyManagerWeb:block:conect_proxy', task='get_mqtt')
    url = get_url(proxy.url)
    url += '/api/mqtt/'
    head = {'Authorization': 'token {}'.format(proxy.token)}
    try:
        response = requests.get(url, headers=head)
        if response.status_code == 200:
            mqttjson = json.loads(response.text)
            for jsons in mqttjson:
                mqtts = Mqtt.objects.filter(topico=jsons['topico'],
                                            proxy=proxy).exists()
                if mqtts is False:
                    mqtt = Mqtt(topico=jsons['topico'],
                                broker=proxy.broker,
                                proxy=proxy,
                                QoS=jsons['QoS'],
                                proxy_alt_id=jsons['id'])
                    mqtt.save()
                else:
                    mqtts = Mqtt.objects.filter(topico=jsons['topico'],
                                                proxy=proxy)
                    for mqtt in mqtts:
                        mqtt.topico = jsons['topico']
                        mqtt.QoS = jsons['QoS']
                        mqtt.proxy_alt_id = jsons['id']
                        mqtt.save()
        elif response.status_code == 404:
            celery.desc = 'Proxy não respondeu'
            celery.exception = response.text
            celery.save()

        elif response.status_code == 400:
            celery.desc = 'Proxy não respondeu'
            celery.exception = response.text
            celery.save()

    except Exception as e:
        celery = Celery(app='ProxyManagerWeb:block:conect_proxy',
                        desc='get_mqtt',
                        exception=e,
                        task='get_mqtt')
        celery.save()
コード例 #11
0
def delete_mqtt(mqtt_pk):
    mqtt = Mqtt.objects.get(pk=mqtt_pk)
    celery = Celery(app='ProxyManagerWeb:block:delete_mqtt', task='')
    url = get_url(mqtt.proxy.url)
    url += '/api/{}/mqttUpdate/'.format(mqtt.proxy_alt_id)
    head = {'Authorization': 'token {}'.format(mqtt.proxy.token)}

    try:
        response = requests.delete(url, headers=head)
        if response.status_code == 200:
            celery.desc = 'Respondeu e delete'
            celery.save()
        else:
            celery.desc = 'delete não foi completo'
            celery.save()
    except Exception as e:
        celery = Celery(app='ProxyManagerWeb:block:delete_mqtt',
                        desc='get_mqtt',
                        exception=e,
                        task='get_mqtt')
        celery.save()
コード例 #12
0
def update_mqtt(mqtt_pk):
    mqtt = Mqtt.objects.get(pk=mqtt_pk)
    celery = Celery(app='ProxyManagerWeb:block:update_mqtt', task='')
    url = get_url(mqtt.proxy.url)
    url += '/api/{}/mqttUpdate/'.format(mqtt.proxy_alt_id)
    head = {'Authorization': 'token {}'.format(mqtt.proxy.token)}

    data = {}
    data['topico'] = mqtt.topico
    data['QoS'] = mqtt.QoS
    data['broker'] = '1'

    try:
        response = requests.put(url, json=data, headers=head)
        if response.status_code == 200:
            celery.desc = 'Respondeu e Update'
            celery.save()
        elif response.status_code == 404:
            celery.desc = 'Não Respondeu 404'
            celery.save()

        else:
            print('Não Respondeu: {}'.format(str(response.status_code)))
            print(str(response.text))
            celery.desc = 'Não Respondeu: {}'.format(str(response.status_code))
            celery.save()
    except Exception as e:
        celery = Celery(app='ProxyManagerWeb:block:conect_proxy',
                        desc='get_mqtt',
                        exception=e,
                        task='get_mqtt')
        celery.save()
コード例 #13
0
def conect_proxy(proxy):
    celery = Celery(app='ProxyManagerWeb:block', task='conect_proxy')
    proxy = Proxy.objects.get(pk=proxy)
    data = {}
    data['username'] = proxy.username
    data['password'] = proxy.password
    url = get_url(proxy.url)
    url += '/api-token-auth/'
    try:
        response = requests.post(url, json=data, timeout=10)
        if response.status_code == 200:
            celery.desc = 'Proxy Respondeu'
            celery.save()
            proxy.status = 1  # conectado com token
            proxy.token = json.loads(response.text)['token']
            proxy.valido = True
            proxy.save()
            get_broker(proxy)
            get_mqtt(proxy)
            get_dispositivo(proxy)
            get_dado(proxy)
            coretask.diff_task(proxy)
            tarefatask.get_job(proxy.pk)
        elif response.status_code == 404:
            celery.desc = 'Proxy não respondeu Respondeu'
            celery.save()
            proxy.status = 4  # não existe
            proxy.token = ''
            proxy.save()
        elif response.status_code == 400:
            proxy.status = 2  # erro
            celery.desc = 'Proxy não respondeu Respondeu'
            celery.exception = response.text
            celery.save()
            proxy.token = ''
            proxy.save()

    except requests.exceptions.ConnectTimeout as timeout:
        proxy.status = 2  # erro
        proxy.token = ''
        celery = Celery(app='ProxyManagerWeb:block',
                        desc='Criar conexão Proxy',
                        exception='',
                        task='conect_proxy')
        celery.save()
        proxy.save()
    except Exception as e:
        proxy.status = 2  # erro
        proxy.token = ''
        celery = Celery(app='ProxyManagerWeb:block',
                        desc='Criar conexão Proxy',
                        exception='',
                        task='conect_proxy')
        celery.save()
        proxy.save()
コード例 #14
0
def create_job(job_pk):
    job = Job.objects.get(pk=job_pk)
    celery = Celery(app='ProxyManagerWeb:tarefa:create_job', task='')
    url = get_url(job.dispositivo.mqtt.proxy.url)
    url += '/api/job/'
    head = {'Authorization': 'token {}'.format(job.dispositivo.mqtt.proxy.token)}

    data = {}
    data['dispositivo'] = job.dispositivo.proxy_alt_id
    data['workspace'] = job.workspace
    try:
        data['firs_task'] = job.firs_task.proxy_alt_id
    except:
        pass
    try:
        response = requests.post(url, json=data, headers=head)
        if response.status_code == 201:
            jsondis = json.loads(response.text)
            job.proxy_alt_id = jsondis['id']

            job.save()
            celery.desc = 'Respondeu'
            celery.save()

        elif response.status_code == 404:
            celery.desc = 'Não Respondeu: 404'
            celery.save()
        else:
            print('Não Respondeu: {}'.format(str(response.status_code)))
            print(str(response.text))
            celery.desc = 'Não Respondeu: {}'.format(str(response.status_code))
            celery.save()
            create_job(job_pk)

    except Exception as e:
        celery = Celery(app='ProxyManagerWeb:tarefa:create_job', desc='erro',
                        exception=e, task='')
        celery.save()
コード例 #15
0
def get_job(proxy_pk):
    proxy = Proxy.objects.get(pk=proxy_pk)
    celery = Celery(app='ProxyManagerWeb:tarefa:get_job', task='')
    url = get_url(proxy.url)
    url += '/api/job/'
    head = {'Authorization': 'token {}'.format(proxy.token)}

    try:
        response = requests.get(url, headers=head)
        if response.status_code == 200:
            jsonjo = json.loads(response.text)
            if jsonjo.__len__()>0:
                for job in jsonjo:
                    joob = Job(workspace=job['workspace'],
                               firs_task=Task.objects.filter(proxy_alt_id=job['firs_task'], proxy=proxy).get(),
                               proxy_alt_id=job['id'],
                               dispositivo=Dispositivo.objects.filter(proxy_alt_id=job['dispositivo'], proxy=proxy).get(),
                               proxy=proxy)
                    joob.save()
            celery.desc = 'Respondeu'
            celery.save()

        elif response.status_code == 404:
            celery.desc = 'Não Respondeu: 404'
            celery.save()
        else:
            print('Não Respondeu: {}'.format(str(response.status_code)))
            celery.desc = 'Não Respondeu: {}'.format(str(response.status_code))
            celery.save()

    except Exception as e:
        celery = Celery(app='ProxyManagerWeb:tarefa:get_job', desc='erro',
                        exception=e, task='')
        celery.save()
コード例 #16
0
def edit_if_else_sensor_string(task_pk, proxy_pk):
    task = If_else_sensor_string.objects.get(pk=task_pk)
    proxy = Proxy.objects.get(pk=proxy_pk)
    celery = Celery(app='ProxyManagerWeb:tarefa:edit_if_else_sensor_string', task='')
    url = get_url(proxy.url)
    while task.proxy_alt_id == None:
        task.refresh_from_db()
    url += task.tipo.url_update.format(task.proxy_alt_id)
    head = {'Authorization': 'token {}'.format(proxy.token)}
    print(url)
    data = {}
    data['comando'] = task.comando
    if task.task_anterior != None:
        while task.task_anterior.proxy_alt_id == None:
            task.refresh_from_db()
        data['task_anterior'] = task.task_anterior.proxy_alt_id
    if task.task_sucessor != None:
        while task.task_sucessor.proxy_alt_id == None:
            task.refresh_from_db()
        data['task_sucessor'] = task.task_sucessor.proxy_alt_id
    if task.elsetask != None:
        while task.elsetask.proxy_alt_id == None:
            task.refresh_from_db()
        data['elsetask'] = task.task_sucessor.proxy_alt_id
    data['condicao']=task.condicao
    data['valor'] = task.valor
    data['tipo'] = task.tipo.task_tipo

    try:
        response = requests.put(url, json=data, headers=head)
        if response.status_code == 200:
            celery.desc = 'Respondeu'
            celery.save()

        elif response.status_code == 404:
            celery.desc = 'Não Respondeu: 404' + response.text
            celery.save()
        else:
            print('Não Respondeu: {}'.format(str(response.status_code)))
            celery.desc = 'Não Respondeu: {}'.format(str(response.status_code))
            celery.save()

    except Exception as e:
        celery = Celery(app='ProxyManagerWeb:tarefa:create_task', desc='erro',
                        exception=e, task='')
        celery.save()
コード例 #17
0
def get_dado(proxy):
    celery = Celery(app='ProxyManagerWeb:block:conect_proxy', task='get_dado')
    url = get_url(proxy.url)
    url += '/api/dado/'
    head = {'Authorization': 'token {}'.format(proxy.token)}
    try:
        response = requests.get(url, headers=head)
        if response.status_code == 200:
            jsondado = json.loads(response.text)
            for jsons in jsondado:
                dado = Dado(QoS=jsons['QoS'],
                            valor_char=jsons['valor_char'],
                            valor_int=jsons['valor_int'],
                            date=jsons['date'],
                            sensor=Dispositivo.objects.filter(
                                proxy_alt_id=jsons['sensor'],
                                proxy=proxy).get())
                dado.save()
        elif response.status_code == 404:
            celery.desc = 'Proxy não respondeu'
            celery.exception = response.text
            celery.save()
        elif response.status_code == 400:
            celery.desc = 'Proxy não respondeu'
            celery.exception = response.text
            celery.save()
    except Exception as e:
        celery = Celery(app='ProxyManagerWeb:block:conect_proxy',
                        desc='get_dado',
                        exception=e,
                        task='get_dado')
        celery.save()