def run(self):
        """
        Main method. Currently also stores and sum the spectra as well.
        Current way to stop is only using a keyboard interrupt.
        """

        if self.transport == 'any':
            devs = kromek.discover()
        else:
            devs = kromek.discover(self.transport)

        print('Discovered %s' % devs)

        if len(devs) <= 0:
            return

        filtered = []

        for dev in devs:
            if self.device == 'all' or dev[0] in self.device:
                filtered.append(dev)

        devs = filtered
        if len(devs) <= 0:
            return

        done_devices = set()
        try:
            while self.running:
                print("Plot_manager.run: getting data")
                with kromek.Controller(devs, self.interval) as controller:
                    for reading in controller.read():
                        if self.create_structures:
                            self.total = np.array(reading[4])
                            self.lst = np.array([reading[4]])
                            self.create_structures = False
                        else:
                            self.total += np.array(reading[4])
                            self.lst = np.concatenate(
                                (self.lst, [np.array(reading[4])]))
                        serial = reading[0]
                        dev_count = reading[1]
                        if serial not in done_devices:
                            this_start, this_end = self.get_interval(
                                time.time() - self.interval)

                            self.handle_spectra(
                                this_start, this_end, reading[4])
                        if dev_count >= self.count > 0:
                            done_devices.add(serial)
                            controller.stop_collector(serial)
                        if len(done_devices) >= len(devs):
                            break
        except KeyboardInterrupt:
            self.vprint(1, '\nKeyboardInterrupt: stopping Manager run')
            self.takedown()
        except SystemExit:
            self.vprint(1, '\nSystemExit: taking down Manager')
            self.takedown()
    def run(self):
        """
        Main method. Currently also stores and sum the spectra as well.

        Current way to stop is only using a keyboard interrupt.
        """

        if self.transport == 'any':
            devs = kromek.discover()
        else:
            devs = kromek.discover(self.transport)
        print 'Discovered %s' % devs
        if len(devs) <= 0:
            return

        filtered = []

        for dev in devs:
            if self.device == 'all' or dev[0] in self.device:
                filtered.append(dev)

        devs = filtered
        if len(devs) <= 0:
            return

        done_devices = set()
        try:
            while self.running:
                with kromek.Controller(devs, self.interval) as controller:
                    for reading in controller.read():
                        if self.create_structures:
                            self.total = np.array(reading[4])
                            self.lst = np.array([reading[4]])
                            self.create_structures = False
                        else:
                            self.total += np.array(reading[4])
                            self.lst = np.concatenate(
                                (self.lst, [np.array(reading[4])]))
                        serial = reading[0]
                        dev_count = reading[1]
                        if serial not in done_devices:
                            this_start, this_end = self.get_interval(
                                time.time() - self.interval)

                            self.handle_spectra(
                                this_start, this_end, reading[4])
                        if dev_count >= self.count > 0:
                            done_devices.add(serial)
                            controller.stop_collector(serial)
                        if len(done_devices) >= len(devs):
                            break
        except KeyboardInterrupt:
            self.vprint(1, '\nKeyboardInterrupt: stopping Manager run')
            self.takedown()
        except SystemExit:
            self.vprint(1, '\nSystemExit: taking down Manager')
            self.takedown()
Example #3
0
def pre_run():
    kdevs = kromek.discover()
    print('Discovered %s' % kdevs)
    if len(kdevs) <= 0:
        print('No devices?')
        return

    try:
        kconn = kromek.connect(kdevs[0])
    except:
        print('Could not connect to kromek device.')
        return None

    try:
        cconn = Camera.Camera()
    except:
        print('Could not create camera instance.')
        return None

    try:
        ser = kromek.get_value(kconn, param='serial')
    except:
        return None

    server_config = {
        'provisioning_token_path': './provisioning_token.json',
        'url_base': 'https://irc-dev.lbl.gov/demo1',
        'credentials_path': './credentials.json',
        'params_path': './device_params.json',
        'device_name': fileIntoString('device_name.txt'),
        'device_type': 'kromek_d3s+picamera',
        'device_serial': ser['serial'].encode('ascii'),
    }

    sconn = ServerConnection.ServerConnection(server_config)

    cfg = {k: base_config[k] for k in base_config}
    cfg['kconn'] = kconn
    cfg['sconn'] = sconn
    cfg['cconn'] = cconn
    cconn.setParams(cfg['camera'])

    if True:
        synchronizeSystemClock()

    return cfg
Example #4
0
    def run(self):
        """
        Main method to run the sensors continuously, the run
        procedure is determined by the sensor_type of the instance.
        """
        if self.new_setup:
            self.vprint(
                    1, NEW_SENSOR_DISPLAY_TEXT.format(sensor_name=self.sensor_names[self.sensor_type-1]))
        else:
            self.vprint(
                    1, OLD_SENSOR_DISPLAY_TEXT.format(sensor_name=self.sensor_names[self.sensor_type-1]))
        this_start, this_end = self.get_interval(time.time())
        if self.sensor_type != 2:
            self.vprint(
                1, RUNNING_DISPLAY_TEXT.format(
                    start_time=datetime_from_epoch(this_start).strftime(strf),
                    date=str(datetime.date.today()),
                    interval=self.interval))
        self.running = True

        if self.sensor_type == 1:
            try:
                while self.running:
                    self.vprint(3, 'Sleeping at {} until {}'.format(
                        datetime_from_epoch(time.time()),
                        datetime_from_epoch(this_end)))
                    try:
                        self.sleep_until(this_end)
                    except SleepError:
                        self.vprint(1, 'SleepError: system clock skipped ahead!')
                        self.vprint(
                            3, 'former this_start = {}, this_end = {}'.format(
                                datetime_from_epoch(this_start),
                                datetime_from_epoch(this_end)))
                        this_start, this_end = self.get_interval(
                            time.time() - self.interval)

                    self.handle_data(this_start, this_end, None)
                    if self.quit_after_interval:
                        self.vprint(1, 'Reboot: taking down Manager')
                        self.stop()
                        self.takedown()
                        os.system('sudo {0} {1}'.format(
                            REBOOT_SCRIPT, self.branch))
                    this_start, this_end = self.get_interval(this_end)
            except KeyboardInterrupt:
                self.vprint(1, '\nKeyboardInterrupt: stopping Manager run')
                self.stop()
                self.takedown()
            except SystemExit:
                self.vprint(1, '\nSystemExit: taking down Manager')
                self.stop()
                self.takedown()

        if self.sensor_type == 2:
            print("Attempting to connect to D3S now")

            if self.transport == 'any':
                devs = kromek.discover()
            else:
                devs = kromek.discover(self.transport)

            if len(devs) <= 0:
                print("No D3S connected, exiting manager now")
                self.d3s_LED.stop_blink()
                self.d3s_presence = False
                GPIO.cleanup()
                return
            else:
                print ('Discovered %s' % devs)
                print("D3S device found, checking for data now")
                self.d3s_LED.start_blink(interval=self.d3s_LED_blink_period_2)
                self.d3s_presence = True
            filtered = []

            for dev in devs:
                if self.device == 'all' or dev[0] in self.device:
                    filtered.append(dev)

            devs = filtered
            if len(devs) <= 0:
                print("No D3S connected, exiting manager now")
                self.d3s_LED.stop_blink()
                self.d3s_presence = False
                GPIO.cleanup()
                return

            signal.alarm(10)
            # Checks if the RaspberryPi is getting data from the D3S and turns on
            # the red LED if it is. If a D3S is connected but no data is being recieved,
            # it tries a couple times then reboots the RaspberryPi.
            if self.d3s_presence:
                try:
                    test_time_outer = time.time() + self.signal_test_time + 25
                    while time.time() < test_time_outer:
                        test_time_inner = time.time() + self.signal_test_time + 5
                        while time.time() < test_time_inner:
                            try:
                                with kromek.Controller(devs, self.signal_test_time) as controller:
                                    for reading in controller.read():
                                        if sum(reading[4]) != 0:
                                            self.d3s_light_switch = True
                                            signal.alarm(0)
                                            break
                                        else:
                                            break
                            except Exception as e:
                                print(e)
                                print("Data acquisition attempt {} failed".format(self.d3s_data_attempts))
                                if self.d3s_data_attempts != self.d3s_data_lim:
                                    signal.alarm(10)
                                self.d3s_data_attempts += 1
                        if self.d3s_light_switch:
                            print("Data from D3S found on attempt {}".format(self.d3s_data_attempts))
                            break
                        if self.d3s_data_attempts > self.d3s_data_lim:
                            print("Failed to find data from D3S {} times".format(self.d3s_data_attempts-1))
                            print("The D3S is either having data collection issues or is currently off")
                            self.d3s_presence = False
                            break
                except KeyboardInterrupt:
                    self.vprint(1, '\nKeyboardInterrupt: stopping Manager run')
                    self.takedown()
                except SystemExit:
                    self.vprint(1, '\nSystemExit: taking down Manager')
                    self.takedown()

            if self.d3s_light_switch:
                self.d3s_LED.stop_blink()
                print("D3S data connection found, continuing with normal data collection")
                self.d3s_LED.on()
            else:
                self.d3s_LED.stop_blink()
                print("Turning off light and will try to gather data again at reboot")
                self.d3s_LED.off()
                GPIO.cleanup()
                return

            if self.d3s_presence:
                self.vprint(
                    1, RUNNING_DISPLAY_TEXT.format(
                        start_time=datetime_from_epoch(this_start).strftime(strf),
                        date=str(datetime.date.today()),
                        interval=self.interval))

                done_devices = set()
                try:
                    while self.running:
                        with kromek.Controller(devs, self.interval) as controller:
                            for reading in controller.read():
                                if self.create_structures:
                                    self.total = np.array(reading[4])
                                    self.lst = np.array([reading[4]])
                                    self.create_structures = False
                                else:
                                    self.total += np.array(reading[4])
                                    self.lst = np.concatenate(
                                        (self.lst, [np.array(reading[4])]))
                                serial = reading[0]
                                dev_count = reading[1]
                                if serial not in done_devices:
                                    this_start, this_end = self.get_interval(
                                        time.time() - self.interval)

                                    self.handle_data(
                                        this_start, this_end, reading[4])

                                if dev_count >= self.count > 0:
                                    done_devices.add(serial)
                                    controller.stop_collector(serial)
                                if len(done_devices) >= len(devs):
                                    break
                except KeyboardInterrupt:
                    self.vprint(1, '\nKeyboardInterrupt: stopping Manager run')
                    self.takedown()
                except SystemExit:
                    self.vprint(1, '\nSystemExit: taking down Manager')
                    self.takedown()

        if self.sensor_type == 3 or self.sensor_type == 4 or self.sensor_type == 5:
            try:
                while self.running:
                    self.handle_data(this_start, this_end, None)
                    this_start, this_end = self.get_interval(this_end)
            except KeyboardInterrupt:
                self.vprint(1, '\nKeyboardInterrupt: stopping Manager run')
                self.stop()
                self.takedown()
            except SystemExit:
                self.vprint(1, '\nSystemExit: taking down Manager')
                self.stop()
                self.takedown()
    def run(self):
        """
        Main method. Currently also stores and sum the spectra as well.
        Current way to stop is only using a keyboard interrupt.
        """
        if self.transport == 'any':
            devs = kromek.discover()
        else:
            devs = kromek.discover(self.transport)

        if len(devs) <= 0:
            print("No D3S connected, exiting manager now")
            self.d3s_LED.stop_blink()
            GPIO.cleanup()
            return
        else:
            print 'Discovered %s' % devs
            print("D3S device found, checking for data now")
            self.d3s_LED.start_blink(interval=self.d3s_LED_blink_period_2)
        filtered = []

        for dev in devs:
            if self.device == 'all' or dev[0] in self.device:
                filtered.append(dev)

        devs = filtered
        if len(devs) <= 0:
            print("No D3S connected, exiting manager now")
            self.d3s_LED.stop_blink()
            GPIO.cleanup()
            return

        # Checks if the RaspberryPi is getting data from the D3S and turns on
        # the red LED if it is. If a D3S is connected but no data is being recieved,
        # it tries a couple times then reboots the RaspberryPi.
        try:
            while self.signal_test_attempts < 3 and self.signal_test_connection == False:
                test_time = time.time() + self.signal_test_time + 5
                while time.time() < test_time and self.signal_test_loop:
                    with kromek.Controller(
                            devs, self.signal_test_time) as controller:
                        for reading in controller.read():
                            if sum(reading[4]) != 0:
                                self.d3s_light_switch = True
                                self.signal_test_loop = False
                                break
                            else:
                                self.signal_test_loop = False
                                break
                if self.d3s_light_switch:
                    self.signal_test_connection = True
                else:
                    self.signal_test_attempts += 1
                    self.signal_test_loop = True
                    print(
                        "Connection to D3S not found, trying another {} times".
                        format(3 - self.signal_test_attempts))
            if not self.signal_test_connection:
                print("No data from D3S found, restarting now")
                os.system('sudo reboot')
        except KeyboardInterrupt:
            self.vprint(1, '\nKeyboardInterrupt: stopping Manager run')
            self.takedown()
        except SystemExit:
            self.vprint(1, '\nSystemExit: taking down Manager')
            self.takedown()

        if self.d3s_light_switch:
            self.d3s_LED.stop_blink()
            print(
                "D3S data connection found, continuing with normal data collection"
            )
            self.d3s_LED.on()

        done_devices = set()
        try:
            while self.running:
                with kromek.Controller(devs, self.interval) as controller:
                    for reading in controller.read():
                        if self.create_structures:
                            self.total = np.array(reading[4])
                            self.lst = np.array([reading[4]])
                            self.create_structures = False
                        else:
                            self.total += np.array(reading[4])
                            self.lst = np.concatenate(
                                (self.lst, [np.array(reading[4])]))
                        serial = reading[0]
                        dev_count = reading[1]
                        if serial not in done_devices:
                            this_start, this_end = self.get_interval(
                                time.time() - self.interval)

                            self.handle_spectra(this_start, this_end,
                                                reading[4])

                        if dev_count >= self.count > 0:
                            done_devices.add(serial)
                            controller.stop_collector(serial)
                        if len(done_devices) >= len(devs):
                            break
        except KeyboardInterrupt:
            self.vprint(1, '\nKeyboardInterrupt: stopping Manager run')
            self.takedown()
        except SystemExit:
            self.vprint(1, '\nSystemExit: taking down Manager')
            self.takedown()