Exemplo n.º 1
0
    def main(self, datalog, average_data, this_start, this_end):
        """
        Determines how to handle the average air quality data
        """
        start_text = datetime_from_epoch(this_start).strftime(strf)
        end_text = datetime_from_epoch(this_end).strftime(strf)

        self.vprint(
            1, TIME_DISPLAY_TEXT.format(
                start_time=start_text,
                end_time=end_text))
        for i in range(3):
        	self.vprint(
                1, AQ_PM_DISPLAY_TEXT.format(
                    variable=self.variables[i],
                    avg_data=average_data[i]))
        for i in range(3, 9):
        	self.vprint(
                1, AQ_P_DISPLAY_TEXT.format(
                    variable=self.variables[i],
                    avg_data=average_data[i]))
        self.vprint(
            1, BREAK_LINE)

        self.manager.data_log(datalog, average_data=average_data)

        if self.manager.test:
            self.send_to_memory(average_data)
        elif not self.manager.config:
            self.no_config_send(average_data)
        elif not self.manager.publickey:
            self.no_publickey_send(average_data)
        else:
            try:
                self.regular_send(this_end, average_data)
            except (socket.gaierror, socket.error, socket.timeout) as e:
                if e == socket.gaierror:
                    if e[0] == socket.EAI_AGAIN:
                        # TCP and UDP
                        # network is down, but NetworkStatus didn't notice yet
                        # (resolving DNS like dosenet.dhcp.lbl.gov)
                        self.vprint(
                            1,
                            'Failed to send packet! Address resolution error')
                    else:
                        self.vprint(
                            1, 'Failed to send packet! Address error: ' +
                            '{}: {}'.format(*e))
                elif e == socket.error:
                    if e[0] == errno.ECONNREFUSED:
                        # TCP
                        # server is not accepting connections
                        self.vprint(
                            1, 'Failed to send packet! Connection refused')
                    elif e[0] == errno.ENETUNREACH:
                        # TCP and UDP
                        # network is down, but NetworkStatus didn't notice yet
                        # (IP like 131.243.51.241)
                        self.vprint(
                            1, 'Failed to send packet! Network is unreachable')
                    else:
                        # consider handling errno.ECONNABORTED errno.ECONNRESET
                        self.vprint(
                            1, 'Failed to send packet! Socket error: ' +
                            '{}: {}'.format(*e))
                elif e == socket.timeout:
                    # TCP
                    self.vprint(1, 'Failed to send packet! Socket timeout')
                self.send_to_memory(average_data)
Exemplo n.º 2
0
    def main(self, datalog, this_start, this_end, **kwargs):
        """
        Determines how to handle the sensor data.
        """
        start_text = datetime_from_epoch(this_start).strftime(strf)
        end_text = datetime_from_epoch(this_end).strftime(strf)
        date = str(datetime.date.today())
        if self.manager.sensor_type == 1:
            cpm, cpm_err = kwargs.get('cpm'), kwargs.get('cpm_err')
            counts = kwargs.get('counts')
            self.vprint(1, SINGLE_BREAK_LINE)
            self.vprint(
                1,
                TIME_DISPLAY_TEXT.format(start_time=start_text,
                                         end_time=end_text,
                                         date=date))
            self.vprint(
                1,
                CPM_DISPLAY_TEXT.format(counts=counts,
                                        cpm=cpm,
                                        cpm_err=cpm_err))
            self.vprint(1, SINGLE_BREAK_LINE)
            self.manager.data_log(datalog, cpm=cpm, cpm_err=cpm_err)
            if self.manager.test:
                self.send_to_memory(cpm=cpm, cpm_err=cpm_err)
            elif not self.manager.config:
                self.no_config_send(cpm=cpm, cpm_err=cpm_err)
            elif not self.manager.publickey:
                self.no_publickey_send(cpm=cpm, cpm_err=cpm_err)

        if self.manager.sensor_type == 2:
            spectra = kwargs.get('spectra')
            calibrationlog = kwargs.get('calibrationlog')
            self.vprint(1, SINGLE_BREAK_LINE)
            self.vprint(
                1,
                TIME_DISPLAY_TEXT.format(start_time=start_text,
                                         end_time=end_text,
                                         date=date))
            self.vprint(1,
                        SPECTRA_DISPLAY_TEXT.format(total_counts=sum(spectra)))
            self.vprint(1, SINGLE_BREAK_LINE)

            self.manager.data_log(datalog, spectra=spectra)
            self.manager.calibration_log(calibrationlog, spectra)
            if self.manager.test:
                self.send_to_memory(spectra=spectra)
            elif not self.manager.config:
                self.no_config_send(spectra=spectra)
            elif not self.manager.publickey:
                self.no_publickey_send(spectra=spectra)

        if self.manager.sensor_type == 3:
            average_data = kwargs.get('average_data')
            self.vprint(1, SINGLE_BREAK_LINE)
            self.vprint(
                1,
                TIME_DISPLAY_TEXT.format(start_time=start_text,
                                         end_time=end_text,
                                         date=date))
            for i in range(3):
                self.vprint(
                    1,
                    AQ_PM_DISPLAY_TEXT.format(variable=self.variables[i],
                                              avg_data=average_data[i]))
            for i in range(3, 9):
                self.vprint(
                    1,
                    AQ_P_DISPLAY_TEXT.format(variable=self.variables[i],
                                             avg_data=average_data[i]))
            self.vprint(1, SINGLE_BREAK_LINE)

            self.manager.data_log(datalog, average_data=average_data)

            if self.manager.test:
                self.send_to_memory(average_data=average_data)
            elif not self.manager.config:
                self.no_config_send(average_data=average_data)
            elif not self.manager.publickey:
                self.no_publickey_send(average_data=average_data)

        if self.manager.sensor_type == 4:
            average_data = kwargs.get('average_data')
            self.vprint(1, SINGLE_BREAK_LINE)
            self.vprint(
                1,
                TIME_DISPLAY_TEXT.format(start_time=start_text,
                                         end_time=end_text,
                                         date=date))
            for i in range(len(self.variables)):
                self.vprint(
                    1,
                    CO2_DISPLAY_TEXT.format(variable=self.variables[i],
                                            data=average_data[i]))
            self.vprint(1, SINGLE_BREAK_LINE)

            self.manager.data_log(datalog, average_data=average_data)

            if self.manager.test:
                self.send_to_memory(average_data=average_data)
            elif not self.manager.config:
                self.no_config_send(average_data=average_data)
            elif not self.manager.publickey:
                self.no_publickey_send(average_data=average_data)

        if self.manager.sensor_type == 5:
            average_data = kwargs.get('average_data')
            self.vprint(1, SINGLE_BREAK_LINE)
            self.vprint(
                1,
                TIME_DISPLAY_TEXT.format(start_time=start_text,
                                         end_time=end_text,
                                         date=date))
            for i in range(len(self.variables)):
                self.vprint(
                    1,
                    WEATHER_DISPLAY_TEXT.format(variable=self.variables[i],
                                                unit=self.variables_units[i],
                                                data=average_data[i]))
            self.vprint(1, SINGLE_BREAK_LINE)

            self.manager.data_log(datalog, average_data=average_data)

            if self.manager.test:
                self.send_to_memory(average_data=average_data)
            elif not self.manager.config:
                self.no_config_send(average_data=average_data)
            elif not self.manager.publickey:
                self.no_publickey_send(average_data=average_data)

        if not self.manager.test:
            try:
                if self.manager.sensor_type == 1:
                    self.regular_send(this_end, cpm=cpm, cpm_err=cpm_err)
                if self.manager.sensor_type == 2:
                    self.regular_send(this_end, spectra=spectra)
                if self.manager.sensor_type == 3 or \
                    self.manager.sensor_type == 4 or self.manager.sensor_type == 5:
                    self.regular_send(this_end, average_data=average_data)
            except (socket.gaierror, socket.error, socket.timeout) as e:
                if e == socket.gaierror:
                    if e[0] == socket.EAI_AGAIN:
                        # TCP and UDP
                        # network is down, but NetworkStatus didn't notice yet
                        # (resolving DNS like dosenet.dhcp.lbl.gov)
                        self.vprint(
                            1,
                            'Failed to send packet! Address resolution error')
                    else:
                        self.vprint(
                            1, 'Failed to send packet! Address error: ' +
                            '{}: {}'.format(*e))
                elif e == socket.error:
                    if e[0] == errno.ECONNREFUSED:
                        # TCP
                        # server is not accepting connections
                        self.vprint(
                            1, 'Failed to send packet! Connection refused')
                    elif e[0] == errno.ENETUNREACH:
                        # TCP and UDP
                        # network is down, but NetworkStatus didn't notice yet
                        # (IP like 131.243.51.241)
                        self.vprint(
                            1, 'Failed to send packet! Network is unreachable')
                    else:
                        # consider handling errno.ECONNABORTED errno.ECONNRESET
                        self.vprint(
                            1, 'Failed to send packet! Socket error: ' +
                            '{}: {}'.format(*e))
                elif e == socket.timeout:
                    # TCP
                    self.vprint(1, 'Failed to send packet! Socket timeout')
                if self.manager.sensor_type == 1:
                    if self.send_fail:
                        self.led.stop_blink()
                        self.led.off()
                    if not self.send_fail:
                        self.send_fail = True
                        self.led.start_blink(
                            interval=self.blink_period_lost_connection)
                    self.send_to_memory(cpm=cpm, cpm_err=cpm_err)
                if self.manager.sensor_type == 2:
                    self.send_to_memory(spectra=spectra)
                if self.manager.sensor_type == 3 or \
                    self.manager.sensor_type == 4 or self.manager.sensor_type == 5:
                    self.send_to_memory(average_data=average_data)
         +
         '{red}port,\n then only one process can access that port. So shut down any other {reset}'
         +
         '{red}process that is running the Air Quality sensor and try again.{reset}'
     ).format(red=ANSI_RED, reset=ANSI_RESET))
 if any(data != 0 for data in average_data) and not ind_err:
     AQ_data = True
     print('{green}Found data from the {sensor}!{reset}'.format(
         green=ANSI_GR, reset=ANSI_RESET, sensor=names[1]))
     for i in range(3):
         print(
             AQ_PM_DISPLAY_TEXT.format(variable=AQ_VARIABLES[i],
                                       avg_data=average_data[i]))
     for i in range(3, 9):
         print(
             AQ_P_DISPLAY_TEXT.format(variable=AQ_VARIABLES[i],
                                      avg_data=average_data[i]))
     testing = False
 else:
     if not ind_err:
         print('{red}No data found from the {sensor}!{reset}'.format(
             red=ANSI_RED, reset=ANSI_RESET, sensor=names[1]))
         print((
             '{red}Either the interval was too short, the sensor is not connected or no data\n{reset}'
             +
             '{red}was found. Make sure the sensor is connected and try again to determine\n{reset}'
             + '{red}whether it is the circuit or not.{reset}').format(
                 red=ANSI_RED, reset=ANSI_RESET))
     print('\n')
     retry = ques_conv(
         '{yellow}Would you like to try again?  {reset}'.format(
             yellow=ANSI_YEL, reset=ANSI_RESET))