Exemplo n.º 1
0
def add_Terrarium(request):
    if request.method == 'GET':
        form = NewTerrariumForm()
    else:
        form = NewTerrariumForm(request.POST)

        config = ConfigHelper().GetConfig()

        if form.is_valid():
            terra_title = form.cleaned_data['title']
            terra_temperature_min = form.cleaned_data['temperature_min']
            terra_temperature_max = form.cleaned_data['temperature_max']
            terra_sunrise = form.cleaned_data['sunrise']
            terra_sunset = form.cleaned_data['sunset']

            if config is None:
                raise Exception('Configuration not found...')
            else:
                terrarium = Terrarium.objects.create(title=terra_title,
                                                     temperature_min=terra_temperature_min,
                                                     temperature_max=terra_temperature_max,
                                                     sunrise=terra_sunrise,
                                                     sunset=terra_sunset)

                if config.terrarium is None:
                    config.terrarium = terrarium
                    config.save()

                    url = reverse('config:index')
                    return HttpResponseRedirect(url)
                else:
                    terrarium.delete()
                    raise Exception('There is already a terrarium configured...')

    return render(request, 'config/add_Terrarium.html', {'form': form})
Exemplo n.º 2
0
def jobs(request):
    ch = ConfigHelper()
    crons = ch.GetCronTabs()
    template = loader.get_template('config/jobs.html')
    context = RequestContext(request, {
        'crons': crons,
    })
    return HttpResponse(template.render(context))
Exemplo n.º 3
0
def add_WeatherURL(request):
    weatherURL = request.POST.get('inputWeatherURL')
    ch = ConfigHelper()
    config = ch.GetConfig()
    config.weatherURL = weatherURL
    config.save()

    url = reverse('config:index')
    return HttpResponseRedirect(url)
Exemplo n.º 4
0
def delete_Terrarium(request):
    config = Config.objects.all().first()
    terrarium = config.terrarium

    config.terrarium = None
    config.save()

    heater = Heater.objects.filter(terrarium_id=terrarium.id)
    fogger = Fogger.objects.filter(terrarium_id=terrarium.id)
    heater.all().delete()
    fogger.all().delete()

    title = terrarium.title

    try:
        Terrarium.objects.get(id=terrarium.id).delete()
        log = KarniPiLog(type='INFO',
                         datetime=utils.timezone.now(),
                         message="Terrarium '{0}' wurde aus der Konfiguration "
                                 + "entfernt".format(title))
        log.save()
    except:
        log = KarniPiLog(type='INFO',
                         datetime=utils.timezone.now(),
                         message="Es ist ein Fehler beim Entfernen des "
                                 + "Terrarium '{0}' aufgetreten".format(title))
        log.save()

    config = ConfigHelper().GetConfig()

    url = reverse('config:index')
    return HttpResponseRedirect(url)
Exemplo n.º 5
0
def sensors(request):
    config = ConfigHelper().GetConfig()
    objects = config.terrarium.get_sensors()
    template = loader.get_template('config/sensors.html')
    context = RequestContext(request, {
        'sensors': objects,
    })
    return HttpResponse(template.render(context))
Exemplo n.º 6
0
    def WriteCurrentTerraDataToLog(self):
        ch = ConfigHelper()
        terrarium = ch.GetTerrarium()
        sht21 = SHT21Sensor.objects.all().filter(terrarium=terrarium).first()

        if sht21 is None:
            raise Exception('No sensor registered to get terrarium data')
        else:
            time = utils.timezone.now()
            temperature = sht21.read_temperature()
            humidity = sht21.read_humidity()

            terrarium_log = TerrariumLog(time=time,
                                         temperature=temperature,
                                         humidity=humidity,
                                         light=True)
            terrarium_log.save()
Exemplo n.º 7
0
def index(request):

    status_overview = MainHelper().get_status_overview()
    config = ConfigHelper().GetConfig()

    file_object = open(settings.BASE_DIR + "/main/logging/karnipi.log", "r")
    log = file_object.read()
    file_object.close()

    if request.method == "POST":
        mode = request.POST.get("maintenance_mode", "")

        if mode == "an":
            config.maintenance = True
        else:
            config.maintenance = False

        config.save()

        template = loader.get_template("maintenance/index.html")
        context = RequestContext(
            request,
            {
                "status_overview": status_overview,
                "current_site": "maintenance",
                "log": log,
                "maintenance_mode": config.maintenance,
            },
        )
        print context
        return HttpResponse(template.render(context))

    else:
        print "doing something"
        template = loader.get_template("maintenance/index.html")
        context = RequestContext(
            request,
            {
                "status_overview": status_overview,
                "current_site": "maintenance",
                "log": log,
                "maintenance_mode": config.maintenance,
            },
        )
        return HttpResponse(template.render(context))
Exemplo n.º 8
0
def actors(request):
    config = ConfigHelper().GetConfig()

    if config.terrarium is None:
        raise Exception("No terrarium is configured. Please define a terrarium in configuration first!")
    else:
        objects = config.terrarium.get_actors()
        template = loader.get_template('config/actors.html')
        context = RequestContext(request, {
            'actors': objects,
        })
        return HttpResponse(template.render(context))
Exemplo n.º 9
0
def maintenance(request):
    config = ConfigHelper().GetConfig()
    status_overview = None

    if config.maintenance:
            status_overview = MainHelper().get_status_overview()

    template = loader.get_template('config/maintenance.html')
    context = RequestContext(request, {
        'status_overview': status_overview,
    })
    return HttpResponse(template.render(context))
Exemplo n.º 10
0
def index(request):
    ch = ConfigHelper()
    crons = ch.GetCronTabs()
    config = ch.GetConfig()

    if config is None:
        config = ch.CreateConfig()

    light = None
    heater = None
    fogger = None
    fan = None
    sprinkler = None
    sht21 = None
    status_overview = None
    maintenance = None
    disk_space = None

    disk_space = ch.disk_usage()
    usage = int(disk_space[0])
    free = int(disk_space[1])

    if config.maintenance:
            status_overview = MainHelper().get_status_overview()

    terrarium = config.terrarium

    if terrarium is not None:
        light = Light.objects.filter(terrarium=terrarium)
        heater = Heater.objects.filter(terrarium=terrarium)
        fogger = Fogger.objects.filter(terrarium=terrarium)
        sprinkler = Sprinkler.objects.filter(terrarium=terrarium)
        fan = Fan.objects.filter(terrarium=terrarium)
        sht21 = SHT21.objects.filter(terrarium=terrarium)

    template = loader.get_template('config/index.html')
    context = RequestContext(request, {
        'crons': crons,
        'config': config,
        'terrarium': terrarium,
        'light': light,
        'heater': heater,
        'fogger': fogger,
        'sprinkler': sprinkler,
        'fan': fan,
        'sht21': sht21,
        'status_overview': status_overview,
        'maintenance': config.maintenance,
        'disk_space_usage': usage,
	'disk_space_free': free,
        'current_site': 'config',
    })
    return HttpResponse(template.render(context))
Exemplo n.º 11
0
def add_Job(request):

    if request.method == 'GET':
        form = NewJobForm()
    else:
        form = NewJobForm(request.POST)

        from karnipi import settings

        settings.BASE_DIR

        python_path = "{0}/bin/python".format(settings.BASE_DIR)
        django_manage_path = "{0}/manage.py".format(settings.BASE_DIR)

        if form.is_valid():
            job_title = form.cleaned_data['title']
            int_command = form.cleaned_data['command']

            choices = dict(form.fields['command'].choices)
            for c in choices:
                if int(c) == int(int_command):
                    str_command = str(choices[c])
                    break

            job_command = "{0} {1} {2}".format(python_path,
                                               django_manage_path,
                                               str_command)

            KarniLogger().log_debug("Adding job as crontab entry " + str(job_command))

            job_minute = form.cleaned_data['minute']
            job_hour = form.cleaned_data['hour']
            job_day = form.cleaned_data['day']
            job_month = form.cleaned_data['month']

            ConfigHelper().CreateCronJob(job_command,
                                 job_title,
                                 job_hour,
                                 job_minute,
                                 job_day, job_month)

            url = reverse('config:index')
            return HttpResponseRedirect(url)

    return render(request, 'config/add_Job.html', {'form': form})
Exemplo n.º 12
0
def delete_job(request):
    if request.method == 'GET':
        form = DeleteJobForm()
    else:
        form = DeleteJobForm(request.POST)

        url = None

        if form.is_valid():
            str_title = None
            job_title_id = form.cleaned_data['title']
            choices = form.fields['title'].choices

            for c in choices:
                if int(job_title_id) == int(c[0]):
                    str_title = str(c[1])
                    break

            ConfigHelper().DeleteCronJob(str_title)

            url = reverse('config:index')

        return HttpResponseRedirect(url)
    return render(request, 'config/delete_Job.html', {'form': form})
Exemplo n.º 13
0
def index(request):

    status_overview = MainHelper().get_status_overview()
    config = ConfigHelper().GetConfig()

    file_object = open(settings.BASE_DIR + '/main/logging/karnipi.log', 'r')
    log = file_object.read()
    file_object.close()

    if request.method == "POST":
        mode = request.POST.get('maintenance_mode', '')

        if mode == "an":
            config.maintenance = True
        else:
            config.maintenance = False

        config.save()

        template = loader.get_template('maintenance/index.html')
        context = RequestContext(
            request, {
                'status_overview': status_overview,
                'current_site': 'maintenance',
                'log': log,
                'maintenance_mode': config.maintenance,
            })
        print context
        return HttpResponse(template.render(context))

    else:
        print "doing something"
        template = loader.get_template('maintenance/index.html')
        context = RequestContext(
            request, {
                'status_overview': status_overview,
                'current_site': 'maintenance',
                'log': log,
                'maintenance_mode': config.maintenance,
            })
        return HttpResponse(template.render(context))