Exemple #1
0
    def __tcp_read(self, isGpsd=False):
        buf = ''
        data = True

        while data and not self._cancel:
            reads, writes, errors = select.select([self._comm], [self._comm],
                                                  [self._comm], 0.1)
            for read in reads:
                data = read.recv(64)
                buf += data
                while buf.find('\n') != -1:
                    line, buf = buf.split('\n', 1)
                    if not isGpsd:
                        pos = line.find('$')
                        if pos != -1 and pos + 1 < len(line):
                            yield line[pos + 1:].rstrip('\r\n')
                    else:
                        yield line
                    if self._raw:
                        line = limit_to_ascii(line)
                        post_event(self._notify,
                                   EventThread(Event.LOC_RAW, 0, line))

            for write in writes:
                if self._send is not None:
                    write.sendall(self._send)
                    self._send = None

            for _error in errors:
                post_event(self._notify,
                           EventThread(Event.LOC_ERR, 0, 'Connection dropped'))
        return
Exemple #2
0
    def __serial_connect(self):
        self._timeout = Timeout(self.__serial_timeout)
        if self._raw:
            text = 'Opening "{}"'.format(self._device.resource)
            post_event(self._notify, EventThread(Event.LOC_RAW, 0, text))
        try:
            self._comm = serial.Serial(self._device.resource,
                                       baudrate=self._device.baud,
                                       bytesize=self._device.bytes,
                                       parity=self._device.parity,
                                       stopbits=self._device.stops,
                                       xonxoff=self._device.soft,
                                       timeout=0)

        except SerialException as error:
            post_event(self._notify,
                       EventThread(Event.LOC_ERR, 0, error.message))
            return False
        except OSError as error:
            post_event(self._notify, EventThread(Event.LOC_ERR, 0, error))
            return False
        except ValueError as error:
            post_event(self._notify, EventThread(Event.LOC_ERR, 0, error))
            return False

        return True
Exemple #3
0
    def __rtl_setup(self):

        if self.sdr is not None:
            return

        tuner = 0

        if self.isDevice:
            try:
                self.sdr = rtlsdr.RtlSdr(self.indexRtl)
                self.sdr.set_sample_rate(SAMPLE_RATE)
                self.sdr.set_manual_gain_enabled(1)
                self.sdr.set_gain(self.gain)
                tuner = self.sdr.get_tuner_type()
            except IOError as error:
                post_event(self.notify,
                           EventThread(Event.ERROR, 0, error.message))
        else:
            try:
                self.sdr = rtltcp.RtlTcp(self.server, self.port, self.notify)
                self.sdr.set_sample_rate(SAMPLE_RATE)
                self.sdr.set_manual_gain_enabled(1)
                self.sdr.set_gain(self.gain)
                tuner = self.sdr.get_tuner_type()
            except IOError as error:
                post_event(self.notify, EventThread(Event.ERROR, 0, error))

        return tuner
def update_spectrum(notify,
                    lock,
                    start,
                    stop,
                    data,
                    offset,
                    spectrum,
                    average,
                    alertLevel=None):
    with lock:
        updated = False
        if average:
            if len(spectrum) > 0:
                timeStamp = min(spectrum)
            else:
                timeStamp = data[0]
        else:
            timeStamp = data[0]

        freqCentre = data[1]
        scan = data[2]

        upperStart = freqCentre + offset
        upperEnd = freqCentre + offset + BANDWIDTH / 2
        lowerStart = freqCentre - offset - BANDWIDTH / 2
        lowerEnd = freqCentre - offset

        if timeStamp not in spectrum:
            spectrum[timeStamp] = OrderedDict()

        for freq in scan:
            if start <= freq < stop:
                power = 10 * math.log10(scan[freq])
                if upperStart <= freq * 1e6 <= upperEnd or \
                   lowerStart <= freq * 1e6 <= lowerEnd:
                    if freq in spectrum[timeStamp]:
                        spectrum[timeStamp][freq] = \
                            (spectrum[timeStamp][freq] + power) / 2
                        if alertLevel is not None and (
                                spectrum[timeStamp][freq] > alertLevel):
                            post_event(notify, EventThread(Event.LEVEL))
                        updated = True
                    else:
                        spectrum[timeStamp][freq] = power
                        updated = True

        if updated:
            spectrum[timeStamp] = OrderedDict(
                sorted(spectrum[timeStamp].items()))
        #todo add model update here

    post_event(notify, EventThread(Event.UPDATED, None, updated))
Exemple #5
0
    def run(self):
        conn = True

        if self._device.type in [DeviceGPS.NMEA_SERIAL, DeviceGPS.NMEA_TCP]:
            if not self.__nmea_open():
                self.__nmea_close()
                conn = False
        else:
            if not self.__gpsd_open():
                conn = False

        if conn:
            if self._device.type in [
                    DeviceGPS.NMEA_SERIAL, DeviceGPS.NMEA_TCP
            ]:
                self.__nmea_read()
            elif self._device.type == DeviceGPS.GPSD:
                self.__gpsd_read()
            elif self._device.type == DeviceGPS.GPSD_OLD:
                self.__gpsd_old_read()

            if self._device.type in [
                    DeviceGPS.NMEA_SERIAL, DeviceGPS.NMEA_TCP
            ]:
                self.__nmea_close()
            else:
                self.__gpsd_close()

        if self._raw:
            post_event(self._notify, EventThread(Event.LOC_RAW, 0, 'Stopped'))
Exemple #6
0
    def __gpsd_sats(self, satData):
        sats = {}
        for sat in satData:
            sats[sat['PRN']] = [sat['ss'], sat['used']]

        post_event(self._notify,
                   EventThread(Event.LOC_SAT, None, sats))
Exemple #7
0
 def __tcp_connect(self, defaultPort):
     self._comm = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     self._comm.settimeout(5)
     self._comm.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
     url = urlparse('//' + self._device.resource)
     if url.hostname is not None:
         host = url.hostname
     else:
         host = 'localhost'
     if url.port is not None:
         port = url.port
     else:
         port = defaultPort
     if self._raw:
         text = 'Opening "{}"'.format(self._device.resource)
         post_event(self._notify, EventThread(Event.LOC_RAW, 0, text))
     try:
         self._comm.connect((host, port))
     except socket.error as error:
         post_event(self._notify, EventThread(Event.LOC_ERR, 0, error))
 def run(self):
     try:
         while not self.cancel:
             if self.readLen > 0:
                 self.__read_stream()
             else:
                 self.__skip_stream()
     except socket.error as error:
         post_event(self.notify, EventThread(Event.ERROR, 0, error))
     finally:
         self.socket.close()
         self.__do_notify()
Exemple #9
0
    def __init__(self, notify, queue, sdr, settings, device, samples, isCal):
        threading.Thread.__init__(self)
        self.name = 'Scan'
        self.notify = notify
        self.queue = queue
        self.sdr = sdr
        self.fstart = settings.start * 1e6
        self.fstop = settings.stop * 1e6
        self.samples = int(samples)
        self.isCal = isCal
        self.indexRtl = settings.indexRtl
        self.isDevice = settings.devicesRtl[device].isDevice
        self.server = settings.devicesRtl[device].server
        self.port = settings.devicesRtl[device].port
        self.gain = settings.devicesRtl[device].gain
        self.lo = settings.devicesRtl[device].lo * 1e6
        self.offset = settings.devicesRtl[device].offset
        self.cancel = False

        post_event(self.notify, EventThread(Event.STARTING))
        steps = int((self.__f_stop() - self.__f_start()) / self.__f_step())
        post_event(self.notify, EventThread(Event.STEPS, steps))
        self.start()
Exemple #10
0
    def __gpsd_open(self):
        self.__tcp_connect(2947)

        try:
            if self._device.type == DeviceGPS.GPSD:
                self._send = '?WATCH={"enable": true,"json": true}'
            else:
                self._send = 'w'

        except IOError as error:
            post_event(self._notify, EventThread(Event.LOC_ERR, 0, error))
            self._comm.close()
            return False

        return True
Exemple #11
0
    def run(self):
        spectrum = {}
        timeStamp = self.scan[0]
        samples = self.scan[1]
        pos = WINFUNC[::2].index(self.winFunc)
        function = WINFUNC[1::2][pos]

        powers, freqs = matplotlib.mlab.psd(samples,
                                            NFFT=self.nfft,
                                            Fs=SAMPLE_RATE / 1e6,
                                            window=function(self.nfft))
        for freqPsd, pwr in itertools.izip(freqs, powers):
            xr = freqPsd + (self.freq / 1e6)
            xr = xr + (xr * self.cal / 1e6)
            spectrum[xr] = pwr * self.levelOff
        post_event(
            self.notify,
            EventThread(Event.PROCESSED, (timeStamp, self.freq, spectrum)))
Exemple #12
0
    def run(self):
        tuner = self.__rtl_setup()
        if self.sdr is None:
            return
        post_event(self.notify, EventThread(Event.INFO, None, tuner))

        freq = self.__f_start()
        timeStamp = math.floor(time.time())
        while freq <= self.__f_stop():
            if self.cancel:
                post_event(self.notify, EventThread(Event.STOPPED))
                self.rtl_close()
                return
            try:
                scan = self.rtl_scan(freq)
                if len(scan):
                    self.queue.put([freq, (timeStamp, scan)])
                    post_event(self.notify, EventThread(Event.DATA))
                else:
                    post_event(
                        self.notify,
                        EventThread(Event.ERROR, 0, 'No samples returned'))
                    return
            except (AttributeError, MemoryError, TypeError) as error:
                post_event(self.notify,
                           EventThread(Event.ERROR, 0, error.message))
                return
            except (IOError, OSError) as error:
                if self.sdr is not None:
                    self.rtl_close()
                post_event(self.notify,
                           EventThread(Event.ERROR, 0, error.message))
                return

            freq += self.__f_step()

        post_event(self.notify, EventThread(Event.FINISHED, 0, None))

        if self.isCal:
            post_event(self.notify, EventThread(Event.CAL))
Exemple #13
0
    def __nmea_read(self):
        if self._device.type == DeviceGPS.NMEA_SERIAL:
            comm = self.__serial_read()
        else:
            comm = self.__tcp_read()

        for resp in comm:
            nmea = resp.split('*')
            if len(nmea) == 2:
                data = nmea[0].split(',')
                if data[0] in ['GPGGA', 'GPGSV']:
                    checksum = self.__nmea_checksum(nmea[0])
                    if checksum == nmea[1]:
                        if data[0] == 'GPGGA':
                            self.__nmea_global_fix(data)
                        elif data[0] == 'GPGSV':
                            self.__nmea_sats(data)
                    else:
                        error = 'Invalid checksum {}, should be {}'.format(
                            resp[1], checksum)
                        post_event(self._notify,
                                   EventThread(Event.LOC_WARN, 0, error))
Exemple #14
0
 def __serial_read(self):
     isSentence = False
     sentence = ''
     while not self._cancel:
         data = self._comm.read(1)
         if data:
             self._timeout.reset()
             if data == '$':
                 isSentence = True
                 continue
             if data == '\r' or data == '\n':
                 isSentence = False
                 if sentence:
                     yield sentence
                     if self._raw:
                         line = limit_to_ascii(sentence)
                         post_event(self._notify,
                                    EventThread(Event.LOC_RAW, 0, line))
                     sentence = ''
             if isSentence:
                 sentence += data
         else:
             time.sleep(0.1)
Exemple #15
0
    def __nmea_sats(self, data):
        message = int(data[1])
        messages = int(data[1])
        viewed = int(data[3])

        if message == 1:
            self._sats.clear()

        blocks = (len(data) - 4) / 4
        for i in range(0, blocks):
            sat = int(data[4 + i * 4])
            level = data[7 + i * 4]
            used = True
            if level == '':
                level = None
                used = False
            else:
                level = int(level)
            self._sats[sat] = [level, used]

        if message == messages and len(self._sats) == viewed:
            post_event(self._notify,
                       EventThread(Event.LOC_SAT, None, self._sats))
Exemple #16
0
 def redraw_plot(self):
     post_event(self.notify, EventThread(Event.DRAW))
Exemple #17
0
 def __serial_timeout(self):
     self.stop()
     post_event(self._notify, EventThread(Event.LOC_ERR, 0,
                                          'GPS timed out'))
Exemple #18
0
 def redraw_plot(self):
     if self.figure is not None:
         post_event(self.notify, EventThread(Event.DRAW))
Exemple #19
0
 def __post_location(self, lat, lon, alt):
     utc = time.time()
     post_event(self._notify, EventThread(Event.LOC, 0,
                                          [lat, lon, alt, utc]))