def button_get(self):
        """ Displays the selected configuration of the first selected and online sensor. """
        ip_list = self.ip_selection.get_verified_ip_list()
        if len(ip_list) > 0:
            network_commands = app_variables.CreateNetworkGetCommands()
            try:
                if self.combo_dropdown_selection.value == "Installed Sensors":
                    command = sensor_commands.CreateSensorNetworkCommand(
                        ip_list[0], self.current_config.network_timeout_data,
                        network_commands.installed_sensors_file)
                elif self.combo_dropdown_selection.value == "Configuration":
                    command = sensor_commands.CreateSensorNetworkCommand(
                        ip_list[0], self.current_config.network_timeout_data,
                        network_commands.sensor_configuration_file)
                elif self.combo_dropdown_selection.value == "Wifi":
                    command = sensor_commands.CreateSensorNetworkCommand(
                        ip_list[0], self.current_config.network_timeout_data,
                        network_commands.wifi_config_file)
                elif self.combo_dropdown_selection.value == "Trigger Variances":
                    command = sensor_commands.CreateSensorNetworkCommand(
                        ip_list[0], self.current_config.network_timeout_data,
                        network_commands.variance_config)
                else:
                    command = sensor_commands.CreateSensorNetworkCommand(
                        ip_list[0], self.current_config.network_timeout_data,
                        "")
                    command.command = ""

                self.textbox_config.value = str(
                    sensor_commands.get_data(command))

            except Exception as error:
                app_logger.sensor_logger.error(str(error))
        else:
            app_useful_functions.no_ip_selected_message()
    def datetime_update(self):
        """ Sends the Date & Time update command to the Sensor Units IP, along with the computers Date & Time. """
        app_logger.sensor_logger.debug("Updating Sensors DateTime")
        threads = []

        network_timeout = self.current_config.network_timeout_data
        ip_list = self.ip_selection.get_verified_ip_list()
        if len(ip_list) > 0:
            command = network_commands.set_datetime
            for ip in ip_list:
                sensor_command = sensor_commands.CreateSensorNetworkCommand(
                    ip, network_timeout, command)
                sensor_command.command_data = self.current_config.get_str_datetime_now(
                )
                threads.append(
                    Thread(target=sensor_commands.put_command,
                           args=[sensor_command]))

            for thread in threads:
                thread.start()

            guizero.info(
                "Sensors DateTime Set",
                "Sensors Date & Time synchronized with local computer's")
        else:
            app_useful_functions.no_ip_selected_message()
    def hostname_change(self):
        """ Sends the host name change command to the Sensor Units IP, along with the new host name. """
        app_logger.sensor_logger.debug("Change Sensor Hostname")
        threads = []

        network_timeout = self.current_config.network_timeout_data
        ip_list = self.ip_selection.get_verified_ip_list()
        if len(ip_list) > 0:
            for ip in ip_list:
                new_hostname = simpledialog.askstring(ip, "New Hostname: ")
                app_logger.sensor_logger.debug("Sent Hostname: " +
                                               str(new_hostname))
                validated_hostname = sensor_commands.get_validated_hostname(
                    new_hostname)
                app_logger.sensor_logger.debug("Validated Hostname: " +
                                               validated_hostname)

                if validated_hostname is not "Cancelled":
                    command = network_commands.set_host_name
                    sensor_command = sensor_commands.CreateSensorNetworkCommand(
                        ip, network_timeout, command)
                    sensor_command.command_data = validated_hostname
                    threads.append(
                        Thread(target=sensor_commands.put_command,
                               args=[sensor_command]))
                else:
                    guizero.info(ip, "Hostname Cancelled or blank for " + ip)

            for thread in threads:
                thread.start()
        else:
            app_useful_functions.no_ip_selected_message()
    def send_commands(self, command):
        """ Sends provided command to the Sensor Units IP's. """
        threads = []

        ip_list = self.ip_selection.get_verified_ip_list()
        network_timeout = self.current_config.network_timeout_data

        for ip in ip_list:
            sensor_command = sensor_commands.CreateSensorNetworkCommand(
                ip, network_timeout, command)
            threads.append(
                Thread(target=sensor_commands.send_command,
                       args=[sensor_command]))

        for thread in threads:
            thread.daemon = True
            thread.start()

        if len(ip_list) > 0:
            message = command + " sent to " + str(len(ip_list))
            if len(ip_list) is 1:
                message += " sensor"
            else:
                message += " sensors"

            guizero.info("Sensor Command Sent", message)
        else:
            app_useful_functions.no_ip_selected_message()
 def app_sensor_test_readings(self):
     """ Create a HTML sensor Readings Report containing each IP selected and online. """
     sensor_readings_report = reports.CreateHTMLReadingsData(
         self.current_config)
     ip_list = self.ip_selection.get_verified_ip_list()
     if len(ip_list) > 0:
         reports.sensor_html_report(sensor_readings_report, ip_list)
     else:
         app_useful_functions.no_ip_selected_message()
    def _connect_to_sensor(self):
        """ Prompts for Database to open and opens it. """
        ip_list = self.ip_selection.get_verified_ip_list()

        if len(ip_list) > 0:
            self.selected_ip = ip_list[0]
            self.text_connected_to.value = self.text_variables_sensor.text_connected_to[:-2] + self.selected_ip
            command = self.sensor_get_commands.database_notes
            network_timeout = self.current_config.network_timeout_data
            sensor_command = sensor_commands.CreateSensorNetworkCommand(self.selected_ip, network_timeout, command)

            test_database_notes = sensor_commands.get_data(sensor_command).split(",")

            if str(test_database_notes)[2:-2] == self.text_variables_generic.sensor_return_no_notes:
                self._no_sql_notes()
            else:
                self.database_notes = test_database_notes

                sensor_command.command = self.sensor_get_commands.database_note_dates
                self.database_notes_dates = sensor_commands.get_data(sensor_command).split(",")
                sensor_command.command = self.sensor_get_commands.database_user_note_dates
                self.database_user_note_dates = sensor_commands.get_data(sensor_command).split(",")

                count = 0
                datetime_offset = self.current_config.datetime_offset
                for date in self.database_user_note_dates:
                    new_date = self.adjust_datetime(date, datetime_offset)
                    self.database_user_note_dates[count] = new_date
                    count += 1

                if len(self.database_notes) > 0:
                    self.database_notes = self.undue_sterilize_notes(self.database_notes)

                    self.textbox_total_notes.value = str(len(self.database_notes))
                    self.checkbox_use_current_datetime.enable()
                    self.textbox_current_note.enable()
                    self.textbox_on_number_notes.enable()
                    self.textbox_current_note.value = self.database_notes[0]
                    self.textbox_note_date.value = self.database_user_note_dates[0]
                    self.textbox_on_number_notes.value = "1"

                    self.button_next_note.enable()
                    self.button_back_note.enable()
                    self.button_delete_note.enable()
                    self.button_new_note.enable()
                    self.button_update_note.enable()
                else:
                    self.textbox_current_note.enable()
                    self.textbox_current_note.value = self.text_variables_generic.no_notes_found
                    self._disable_notes_window_functions()
        else:
            self._disable_notes_window_functions()
            self.text_connected_to.value = self.text_variables_sensor.text_connected_to

            app_useful_functions.no_ip_selected_message()
    def button_set(self):
        """ Sends the update configuration command to the Sensor Units IP, along with the new configuration. """
        network_commands = app_variables.CreateNetworkSendCommands()
        ip_list = self.ip_selection.get_verified_ip_list()
        threads = []

        if len(ip_list) > 0:
            for ip in ip_list:
                try:
                    if self.combo_dropdown_selection.value == "Installed Sensors":
                        command = sensor_commands.CreateSensorNetworkCommand(
                            ip, self.current_config.network_timeout_data,
                            network_commands.set_installed_sensors)
                    elif self.combo_dropdown_selection.value == "Configuration":
                        command = sensor_commands.CreateSensorNetworkCommand(
                            ip, self.current_config.network_timeout_data,
                            network_commands.set_configuration)
                    elif self.combo_dropdown_selection.value == "Wifi":
                        command = sensor_commands.CreateSensorNetworkCommand(
                            ip, self.current_config.network_timeout_data,
                            network_commands.set_wifi_configuration)
                    elif self.combo_dropdown_selection.value == "Trigger Variances":
                        command = sensor_commands.CreateSensorNetworkCommand(
                            ip, self.current_config.network_timeout_data,
                            network_commands.set_variance_configuration)
                    else:
                        command = sensor_commands.CreateSensorNetworkCommand(
                            ip, self.current_config.network_timeout_data, "")
                        command.command = ""

                    command.command_data = self.textbox_config.value.strip()

                    threads.append(
                        Thread(target=sensor_commands.put_command,
                               args=[command]))
                except Exception as error:
                    app_logger.sensor_logger.error(str(error))

            for thread in threads:
                thread.start()

            guizero.info(
                "Sensors " + self.combo_dropdown_selection.value + " Set",
                self.combo_dropdown_selection.value +
                " set & services restarted on:\n" + str(ip_list)[1:-1])
        else:
            app_useful_functions.no_ip_selected_message()
    def _app_download_sql_db(self):
        """ Downloads the Interval SQLite3 database to the chosen location, from the selected sensors. """
        ip_list = self.ip_selection.get_verified_ip_list()

        if len(ip_list) > 0:
            threads = []

            for ip in ip_list:
                address_and_port = self._sql_download_check_ip_port(ip)
                threads.append(
                    Thread(target=download_sensor_database,
                           args=[address_and_port]))

            for thread in threads:
                thread.start()
        else:
            app_useful_functions.no_ip_selected_message()
    def send_text_message(self, message):
        """ Sends Message to display on the sensors installed display. """
        app_logger.sensor_logger.debug("Sending message to sensor")

        network_timeout = self.current_config.network_timeout_data
        ip_list = self.ip_selection.get_verified_ip_list()
        ip = ip_list[0]
        if len(ip_list) > 0:
            app_logger.sensor_logger.debug("Sent Message: " + str(message))

            command = network_commands.display_message
            sensor_command = sensor_commands.CreateSensorNetworkCommand(
                ip, network_timeout, command)
            sensor_command.command_data = message
            message_thread = Thread(target=sensor_commands.put_command,
                                    args=[sensor_command])
            message_thread.daemon = True
            message_thread.start()
        else:
            app_useful_functions.no_ip_selected_message()
 def _download_logs(self):
     """ Download all selected and online sensors logs. """
     ip_list = self.ip_selection.get_verified_ip_list()
     if len(ip_list) > 0:
         network_command_data = sensor_commands.CreateSensorNetworkCommand(
             "", self.current_config.network_timeout_data,
             self.network_get_commands.download_zipped_logs)
         for ip in ip_list:
             network_command_data.ip = ip
             network_command_data.check_for_port_in_ip()
             download_url = "http://" + \
                            network_command_data.ip + \
                            ":" + \
                            network_command_data.port + \
                            "/" + \
                            network_command_data.command
             print(download_url)
             sensor_commands.download_zipped_logs(download_url)
     else:
         app_useful_functions.no_ip_selected_message()
 def _get_log(self):
     """ Displays the chosen remote sensor log. """
     ip_list = self.ip_selection.get_verified_ip_list()
     if len(ip_list) > 0:
         network_timeout = self.current_config.network_timeout_data
         command_data = sensor_commands.CreateSensorNetworkCommand(
             ip_list[0], network_timeout, "GetNetworkLog")
         if self.radio_log_type.value == "Network Log":
             log = sensor_commands.get_data(command_data)
         elif self.radio_log_type.value == "Primary Log":
             command_data.command = "GetPrimaryLog"
             log = sensor_commands.get_data(command_data)
         elif self.radio_log_type.value == "Sensors Log":
             command_data.command = "GetSensorsLog"
             log = sensor_commands.get_data(command_data)
         else:
             app_logger.app_logger.error("Bad Log Request")
             log = "Bad Log Request"
         self.textbox_log.value = log
     else:
         app_useful_functions.no_ip_selected_message()
    def _remote_sensor_display_selection(self):
        network_timeout = self.current_config.network_timeout_data
        ip_list = self.ip_selection.get_verified_ip_list()

        if len(ip_list) > 0:
            command_data = sensor_commands.CreateSensorNetworkCommand(
                ip_list[0], network_timeout, "")
            display_message = ""

            if self.checkbox_custom_text.value:
                add_text = simpledialog.askstring(
                    "Send Text to Sensor Display", "")
                display_message += add_text
            if self.checkbox_up_time.value:
                command_data.command = get_commands.system_uptime
                add_text = sensor_commands.get_data(command_data)
                display_message += " UpTime: " + add_text
            if self.checkbox_cpu_temp.value:
                command_data.command = get_commands.cpu_temp
                add_text = sensor_commands.get_data(command_data)
                display_message += " CPU Temp: " + add_text
            if self.checkbox_temperature.value:
                command_data.command = get_commands.environmental_temp
                add_text = sensor_commands.get_data(command_data)
                display_message += " Env Temp: " + add_text
            if self.checkbox_pressure.value:
                command_data.command = get_commands.pressure
                add_text = sensor_commands.get_data(command_data)
                display_message += " Pressure: " + add_text
            if self.checkbox_altitude.value:
                command_data.command = get_commands.altitude
                add_text = sensor_commands.get_data(command_data)
                display_message += " Altitude: " + add_text
            if self.checkbox_humidity.value:
                command_data.command = get_commands.humidity
                add_text = sensor_commands.get_data(command_data)
                display_message += " Humidity: " + add_text
            if self.checkbox_distance.value:
                command_data.command = get_commands.distance
                add_text = sensor_commands.get_data(command_data)
                display_message += " Distance: " + add_text
            if self.checkbox_gas.value:
                command_data.command = get_commands.gas_reduced
                add_text = sensor_commands.get_data(command_data)
                display_message += " GAS: " + add_text
            if self.checkbox_particulate_matter.value:
                command_data.command = get_commands.pm_1
                add_text = sensor_commands.get_data(command_data)
                display_message += " PM: " + add_text
            if self.checkbox_lumen.value:
                command_data.command = get_commands.lumen
                add_text = sensor_commands.get_data(command_data)
                display_message += " Lumen: " + add_text
            if self.checkbox_colour.value:
                command_data.command = get_commands.rgb
                add_text = sensor_commands.get_data(command_data)
                display_message += " Colour: " + add_text
            if self.checkbox_ultra_violet.value:
                command_data.command = get_commands.ultra_violet_a
                add_text = sensor_commands.get_data(command_data)
                display_message += " UV: " + add_text
            if self.checkbox_acc.value:
                command_data.command = get_commands.accelerometer_xyz
                add_text = sensor_commands.get_data(command_data)
                display_message += " Acc: " + add_text
            if self.checkbox_mag.value:
                command_data.command = get_commands.magnetometer_xyz
                add_text = sensor_commands.get_data(command_data)
                display_message += " Mag: " + add_text
            if self.checkbox_gyro.value:
                command_data.command = get_commands.gyroscope_xyz
                add_text = sensor_commands.get_data(command_data)
                display_message += " Gyro: " + add_text

            if display_message != "":
                self.send_text_message(display_message)
        else:
            app_useful_functions.no_ip_selected_message()