Ejemplo n.º 1
0
def main():

    control = KarniPiControl()

    # run as far as you can
    while True:
        exit_code = control.get_exit_code()
        KarniLogger().log_info("Checking for exit code..." + str(exit_code))

        if int(exit_code) == 0:
            KarniLogger().log_info("Staring next cycle...")

            try:
                try:
                    terrarium = TerrariumHelper().get_terrarium()
                except:
                    KarniLogger().log_info("Couldn't get terrarium...")

                if terrarium.sleep_mode is False:
                    control.handle_control(terrarium)
                else:
                    KarniLogger().log_info("Sleeping so well...")

            except (KeyboardInterrupt, SystemExit):
                KarniLogger().log_info("Exiting...")
                sys.exit()
            finally:
                KarniLogger().log_info("Idling for 10 minutes")
                time.sleep(10 * 60)
        else:
            KarniLogger().log_info("Exiting due to exit code...")
            sys.exit()
Ejemplo n.º 2
0
    def get_state(self):

        try:
            if self.data_address == 14:
                retVal = self.bus.read_byte_data(0x20, 0x14)
                KarniLogger().log_info("bus read byte data = " + str(retVal))
                return retVal
            elif self.data_address == 15:
                retVal = self.bus.read_byte_data(0x20, 0x15)
                KarniLogger().log_info("bus read byte data = " + str(retVal))
                return retVal
            else:
                raise Exception
        except IOError as e:
            errno, strerror = e.args
            KarniLogger().log_info("I/O error({0}): {1}".format(
                errno, strerror))
            pass
Ejemplo n.º 3
0
    def __init__(self, chip_address, data_address):

        self.bus = smbus.SMBus(1)  # Rev 2 Pi uses 1

        print "Prepared SMBus"

        self.chip_address = 20

        if data_address == "14":
            self.data_address = 14
        elif data_address == "15":
            self.data_address = 15
        else:
            raise Exception('Invalid data address ' + data_address)

        KarniLogger().log_info(
            "Initialized bus with chip address {0} and data address {1}".
            format(self.chip_address, self.data_address))
        KarniLogger().log_info("Checking current bus state...")
        self.get_state()
Ejemplo n.º 4
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})
Ejemplo n.º 5
0
    def handle_control(self, terrarium):

        if self.action is None:
            self.action = "heating"

        KarniLogger().log_info("Getting terrarium data...")
        sht21 = TerrariumHelper().get_sht21_sensor(terrarium)
        print sht21
        temperature = int(sht21.read_temperature())
        print temperature
        # terra_data = TerrariumHelper().GetCurrentTerrariumData()

        KarniLogger().log_info("Accessing heater...")
        heater = TerrariumHelper().get_heater(terrarium)

        # if heater is available
        if heater is not None:
            KarniLogger().log_info("Terrarium: {0} C".format(int(temperature)))
            KarniLogger().log_info("Ziel-Temperatur: {0} C".format(
                int(terrarium.temperature_max)))
            KarniLogger().log_info("Action is '{0}'".format(self.action))

            if int(temperature) < int(terrarium.temperature_min):
                self.action = "heating"
                heater.on()
                KarniLogger().log_info("Heating...")
            elif (int(temperature) <= int(
                    terrarium.temperature_max)) & (self.action == "heating"):
                heater.on()
            elif (int(temperature) <= int(
                    terrarium.temperature_max)) & (self.action == "cool down"):
                heater.off()
            elif int(temperature) > int(terrarium.temperature_max):
                self.action = "cool down"
                heater.off()
                KarniLogger().log_info("Cooling down...")
            else:
                KarniLogger().log_info("Don't know what to do...")
Ejemplo n.º 6
0
    def set_state(self, value_address, state):

        KarniLogger().log_info(
            "Setting state for value address {0} and state {1}".format(
                value_address, state))

        current_int = self.get_state()

        # get binary string of current bus state
        bus_binarystring = list(self.hex_to_binarystring(hex(current_int)))
        KarniLogger().log_info("Currently on bus = " + str(bus_binarystring))

        value_int = int(value_address)
        value_binarystring = list(self.hex_to_binarystring(hex(value_int)))
        KarniLogger().log_info("Value address    = " + str(value_binarystring))

        new_int = None

        # if device is switched on
        if str(state) == "1":
            new_int = int(value_address) | current_int
            KarniLogger().log_info("{0} XOR {1} = {2}".format(
                str(int(value_address)), str(current_int), new_int))
        # if device is switched off
        elif str(state) == "0":

            for i in range(len(bus_binarystring)):
                bus_bit = str(bus_binarystring[i])
                value_bit = str(value_binarystring[i])

                print "index = {0}, value = {1}, bus = {2}".format(
                    i, value_bit, bus_bit)

                # if there are matching bits with value 1, set bus bit to 0
                if (bus_bit == "1") & (value_bit == "1"):
                    KarniLogger().log_debug("found mismatch at index " +
                                            str(i))
                    bus_binarystring[i] = "0"

            new_bus_binarystring = "".join(bus_binarystring)
            KarniLogger().log_info("New bus binary " + new_bus_binarystring)
            new_int = int(new_bus_binarystring, 2)
            KarniLogger().log_info("New bus int " + str(new_int))

        if str(self.data_address) == "14":
            self.bus.write_byte_data(0x20, self.IODIRA, 0x00)
        elif str(self.data_address) == "15":
            self.bus.write_byte_data(0x20, self.IODIRB, 0x00)
        else:
            KarniLogger().log_debug("PORT {0} unknown".format(
                str(self.data_address)))

        KarniLogger().log_info("Now working with {0} {1} {2}".format(
            self.chip_address, self.data_address, new_int))

        print type(self.data_address)
        if self.data_address == 14:
            self.bus.write_byte_data(0x20, 0x14, new_int)
        elif self.data_address == 15:
            self.bus.write_byte_data(0x20, 0x15, new_int)
        else:
            raise AttributeError("WTF")

        current_int = self.get_state()
Ejemplo n.º 7
0
 def __init__(self):
     KarniLogger().log_info("Init control...")