# 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




class RtlPowerScanner(ScannerBase):
    @property
    def device_index(self):
        i = getattr(self, '_device_index', None)
        if i is None:
            serial_number = self.device_config.serial_number
            if serial_number is None:
                i = 0
            else:

# 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





class RtlPowerScanner(ScannerBase):
    @property
    def device_index(self):
        i = getattr(self, '_device_index', None)
        if i is None:
            serial_number = self.device_config.serial_number
            if serial_number is None:
                i = 0
            else:

                i = RtlSdr.get_device_index_by_serial(serial_number)