# Path: wwb_scanner/scanner/main.py
# class ScannerBase(JSONMixin):
#     WINDOW_TYPES = WINDOW_TYPES
#     def __init__(self, **kwargs):
#         self._running = threading.Event()
#         self._stopped = threading.Event()
#         self._current_freq = None
#         self._progress = 0.
#         ckwargs = kwargs.get('config')
#         if not ckwargs:
#             ckwargs = db_store.get_scan_config()
#         if not ckwargs:
#             ckwargs = {}
#         self.config = ScanConfig(ckwargs)
#         self.device_config = self.config.device
#         self.sampling_config = self.config.sampling
#         if 'spectrum' in kwargs:
#             self.spectrum = Spectrum.from_json(kwargs['spectrum'])
#         else:
#             self.spectrum = Spectrum()
#         self.spectrum.scan_config = self.config
#         if not kwargs.get('__from_json__'):
#             self.sample_collection = SampleCollection(scanner=self)
#     @property
#     def current_freq(self):
#         return self._current_freq
#     @current_freq.setter
#     def current_freq(self, value):
#         self._current_freq = value
#         if value is not None:
#             f_min, f_max = self.config.scan_range
#             self.progress = (value - f_min) / (f_max - f_min)
#         self.on_current_freq(value)
#     def on_current_freq(self, value):
#         pass
#     @property
#     def progress(self):
#         return self._progress
#     @progress.setter
#     def progress(self, value):
#         if value == self._progress:
#             return
#         self._progress = value
#         self.on_progress(value)
#     def on_progress(self, value):
#         pass
#     def build_sample_sets(self):
#         freq,  end_freq = self.config.scan_range
#         sample_collection = self.sample_collection
#         while freq <= end_freq:
#             sample_set = sample_collection.build_sample_set(mhz_to_hz(freq))
#             freq += self.step_size
#     def run_scan(self):
#         self.build_sample_sets()
#         running = self._running
#         running.set()
#         self.sample_collection.scan_all_freqs()
#         self.sample_collection.stopped.wait()
#         if running.is_set():
#             self.save_to_dbstore()
#         running.clear()
#         self._stopped.set()
#     def stop_scan(self):
#         self._running.clear()
#         self.sample_collection.cancel()
#         self._stopped.wait()
#     def save_to_dbstore(self):
#         self.spectrum.save_to_dbstore()
#     def _serialize(self):
#         d = dict(
#             config=self.config._serialize(),
#             spectrum=self.spectrum._serialize(),
#             sample_collection=self.sample_collection._serialize(),
#         )
#         return d
#     def _deserialize(self, **kwargs):
#         data = kwargs.get('sample_collection')
#         self.sample_collection = SampleCollection.from_json(data, scanner=self)
# 
# def hz_to_mhz(hz):
#     return hz / 1000000.0
# 
# def mhz_to_hz(mhz):
#     return mhz * 1000000.0


import subprocess
import shlex
import numpy as np
from rtlsdr import RtlSdr
from wwb_scanner.scanner.main import ScannerBase, hz_to_mhz, mhz_to_hz
        return i
    def run_scan(self):
        self.rtl_bin_size_hz = mhz_to_hz(self.sampling_config.rtl_bin_size)
        self.rtl_gain = self.device_config.gain# / 10.

        cmd_str = [
            'rtl_power -i 1 -d {self.device_index}',
            '-f {self.config.scan_range[0]}M:{self.config.scan_range[1]}M:{self.rtl_bin_size_hz}',
            '-g {self.rtl_gain} -w {self.sampling_config.window_type}',# -i {self.integration_interval}',
            '-c {self.sampling_config.rtl_crop}% -F {self.sampling_config.rtl_fir_size}',
            '-1 -',
        ]
        cmd_str = ' '.join(cmd_str).format(self=self)
        print(cmd_str)
        proc = self.proc = subprocess.Popen(
            shlex.split(cmd_str),
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            universal_newlines=True,
        )
        spectrum = self.spectrum
        while True:
            line = proc.stdout.readline().strip()
            if not line and proc.poll() is not None:
                break
            values = line.split(',')
            if len(values) < 6:
                print(line)
                continue
            f_lo = hz_to_mhz(float(values[2]))

# Path: wwb_scanner/scanner/main.py
# class ScannerBase(JSONMixin):
#     WINDOW_TYPES = WINDOW_TYPES
#     def __init__(self, **kwargs):
#         self._running = threading.Event()
#         self._stopped = threading.Event()
#         self._current_freq = None
#         self._progress = 0.
#         ckwargs = kwargs.get('config')
#         if not ckwargs:
#             ckwargs = db_store.get_scan_config()
#         if not ckwargs:
#             ckwargs = {}
#         self.config = ScanConfig(ckwargs)
#         self.device_config = self.config.device
#         self.sampling_config = self.config.sampling
#         if 'spectrum' in kwargs:
#             self.spectrum = Spectrum.from_json(kwargs['spectrum'])
#         else:
#             self.spectrum = Spectrum()
#         self.spectrum.scan_config = self.config
#         if not kwargs.get('__from_json__'):
#             self.sample_collection = SampleCollection(scanner=self)
#     @property
#     def current_freq(self):
#         return self._current_freq
#     @current_freq.setter
#     def current_freq(self, value):
#         self._current_freq = value
#         if value is not None:
#             f_min, f_max = self.config.scan_range
#             self.progress = (value - f_min) / (f_max - f_min)
#         self.on_current_freq(value)
#     def on_current_freq(self, value):
#         pass
#     @property
#     def progress(self):
#         return self._progress
#     @progress.setter
#     def progress(self, value):
#         if value == self._progress:
#             return
#         self._progress = value
#         self.on_progress(value)
#     def on_progress(self, value):
#         pass
#     def build_sample_sets(self):
#         freq,  end_freq = self.config.scan_range
#         sample_collection = self.sample_collection
#         while freq <= end_freq:
#             sample_set = sample_collection.build_sample_set(mhz_to_hz(freq))
#             freq += self.step_size
#     def run_scan(self):
#         self.build_sample_sets()
#         running = self._running
#         running.set()
#         self.sample_collection.scan_all_freqs()
#         self.sample_collection.stopped.wait()
#         if running.is_set():
#             self.save_to_dbstore()
#         running.clear()
#         self._stopped.set()
#     def stop_scan(self):
#         self._running.clear()
#         self.sample_collection.cancel()
#         self._stopped.wait()
#     def save_to_dbstore(self):
#         self.spectrum.save_to_dbstore()
#     def _serialize(self):
#         d = dict(
#             config=self.config._serialize(),
#             spectrum=self.spectrum._serialize(),
#             sample_collection=self.sample_collection._serialize(),
#         )
#         return d
#     def _deserialize(self, **kwargs):
#         data = kwargs.get('sample_collection')
#         self.sample_collection = SampleCollection.from_json(data, scanner=self)
# 
# def hz_to_mhz(hz):
#     return hz / 1000000.0
# 
# def mhz_to_hz(mhz):
#     return mhz * 1000000.0


# Path: wwb_scanner/scanner/rtlpower_scan.py
import subprocess
import shlex
import numpy as np
from rtlsdr import RtlSdr
from wwb_scanner.scanner.main import ScannerBase, hz_to_mhz, mhz_to_hz

        return i
    def run_scan(self):
        self.rtl_bin_size_hz = mhz_to_hz(self.sampling_config.rtl_bin_size)
        self.rtl_gain = self.device_config.gain# / 10.

        cmd_str = [
            'rtl_power -i 1 -d {self.device_index}',
            '-f {self.config.scan_range[0]}M:{self.config.scan_range[1]}M:{self.rtl_bin_size_hz}',
            '-g {self.rtl_gain} -w {self.sampling_config.window_type}',# -i {self.integration_interval}',
            '-c {self.sampling_config.rtl_crop}% -F {self.sampling_config.rtl_fir_size}',
            '-1 -',
        ]
        cmd_str = ' '.join(cmd_str).format(self=self)
        print(cmd_str)
        proc = self.proc = subprocess.Popen(
            shlex.split(cmd_str),
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            universal_newlines=True,
        )
        spectrum = self.spectrum
        while True:
            line = proc.stdout.readline().strip()
            if not line and proc.poll() is not None:
                break
            values = line.split(',')
            if len(values) < 6:
                print(line)
                continue
            f_lo = hz_to_mhz(float(values[2]))

            f_hi = hz_to_mhz(float(values[3]))