Example #1
0
class SpectrogramWindow(QWidget):
    """
    This is the class for the spectrogram window
    """
    def __init__(self, station_list):
        super(QWidget, self).__init__()
        self.layout = QGridLayout(self)
        self.station_list = station_list
        self.hidden = False

        self.spectrogram_1 = PlotWidget(self)
        self.spectrogram_1.setFixedWidth(400)
        self.spectrogram_1.setFixedHeight(550)
        self.spectrogram_2 = PlotWidget(self)
        self.spectrogram_2.setFixedWidth(400)

        self.station_box_1 = QComboBox(self)
        self.station_box_2 = QComboBox(self)

        self.waiting_traces = [None, None]
        self.waiting_p_picks = [None, None]
        self.filter_stats = FilterStats(True)

        self.spectrogram_threads = [None, None]
        self.spectrogram_threads[0] = SpectrogramCalculatorThread(
            self.filter_stats, 0)
        self.spectrogram_threads[0].signal.connect(self.plotSpectrogram)
        self.spectrogram_threads[1] = SpectrogramCalculatorThread(
            self.filter_stats, 1)
        self.spectrogram_threads[1].signal.connect(self.plotSpectrogram)

        self.filter_widget = FilterWidget(self, self.filter_stats)

        self.layout.addWidget(self.spectrogram_1, 0, 0, 1, 2)
        self.layout.addWidget(self.spectrogram_2, 0, 2, 1, 2)
        self.layout.addWidget(self.station_box_1, 1, 0)
        self.layout.addWidget(self.station_box_2, 1, 2)
        self.layout.addWidget(self.filter_widget, 2, 0)

        self.station_box_1.activated.connect(self.replotSpectrogram1)
        self.station_box_2.activated.connect(self.replotSpectrogram2)
        self.setStationsFromNewEvent()

    def closeEvent(self, event):
        """
        This function will be called when the spectrogramWindow Closes
        """
        self.hidden = True

    def replotSpectrogram1(self):
        """
        This function is to be used only by station_box_1
        """
        self.getTraceForSpectrogramThread(0)

    def replotSpectrogram2(self):
        """
        This function is to be used only by station_box_2
        """
        self.getTraceForSpectrogramThread(1)

    def getTraceForSpectrogramThread(self, spectrogram_id):
        """
        Fetch correct trace for the spectrogram_thread. Choose this from the value from station_box_1 or station_box_2
        """
        station_name = None
        if spectrogram_id == 0:
            station_name = self.station_box_1.currentText()
        elif spectrogram_id == 1:
            station_name = self.station_box_2.currentText()

        trace, p_pick = self.station_list.getCurrentTraceAndPPickForStation(
            station_name)

        if trace is None:
            return

        self.calculateSpectrogram(trace, p_pick, spectrogram_id)

    def setStationsFromNewEvent(self):
        """
        Set the focused event to spectrogram window
        """
        stations = [x[0] for x in self.station_list.getOrderedStationList()]

        if len(stations) < 2:
            return

        self.station_box_1.clear()
        self.station_box_2.clear()

        for stat in stations:
            self.station_box_1.addItem(stat)
            self.station_box_2.addItem(stat)

        self.station_box_1.setCurrentIndex(0)
        self.station_box_2.setCurrentIndex(1)

        self.getTraceForSpectrogramThread(0)
        self.getTraceForSpectrogramThread(1)

    def filterChange(self):
        """
        Function that is called when the filters have been changed
        """
        self.getTraceForSpectrogramThread(0)
        self.getTraceForSpectrogramThread(1)

    def calculateSpectrogram(self, waveform_trace, p_pick, spectrogram_id):
        """
        Function for calculating a spectrogram
        """
        if self.spectrogram_threads[spectrogram_id].running:
            self.waiting_traces[spectrogram_id] = waveform_trace
            self.waiting_p_picks[spectrogram_id] = p_pick
            self.spectrogram_threads[spectrogram_id].interrupt = True
        else:
            self.spectrogram_threads[
                spectrogram_id].waveform_trace = waveform_trace
            self.spectrogram_threads[spectrogram_id].p_pick = p_pick
            self.spectrogram_threads[spectrogram_id].running = True
            self.spectrogram_threads[spectrogram_id].start()

    def plotSpectrogram(self, return_values):
        """
        Function for plotting a spectrogram
        """
        spectrogram_id, spectrogram, sample_frequencies, sample_times = return_values
        print("Window {0} done".format(spectrogram_id))

        self.spectrogram_threads[spectrogram_id].running = False
        self.spectrogram_threads[spectrogram_id].interrupt = False

        if self.waiting_traces[spectrogram_id] is not None:
            self.calculateSpectrogram(self.waiting_traces[spectrogram_id],
                                      self.waiting_p_picks[spectrogram_id],
                                      spectrogram_id)
            self.waiting_traces[spectrogram_id] = None
            self.waiting_p_picks[spectrogram_id] = None

        if spectrogram is None:
            return

        if spectrogram_id == 0:
            self.spectrogram_1.clear()
            self.spectrogram_1.addItem(spectrogram)
        elif spectrogram_id == 1:
            self.spectrogram_2.clear()
            self.spectrogram_2.addItem(spectrogram)
Example #2
0
class ControllerGui(Ui_ControllerGUI, QMainWindow):

    def __init__(self):
        super(ControllerGui, self).__init__()
        self.setupUi(self)
        self.m_clear.clicked.connect(self.clear_plot)
        self.m_send.clicked.connect(self.send_start)
        self.m_stop.clicked.connect(self.send_stop)
        self.interface = SerialInterface()
        self.interface.received.connect(self.show_received)
        self.m_interval.setValidator(QIntValidator(0, 10000))
        self.m_interval.returnPressed.connect(self.send_start)

        brush = QBrush(QColor(255, 255, 255))

        box = QVBoxLayout(self.m_list)

        self.temperature_plot = PlotWidget(self)
        self.temperature_plot.setBackgroundBrush(brush)
        self.temperature_plot.setFixedHeight(300)
        box.addWidget(self.temperature_plot)
        self.temperature_curves = {}

        self.humidity_plot = PlotWidget(self)
        self.humidity_plot.setBackgroundBrush(brush)
        self.humidity_plot.setFixedHeight(300)
        box.addWidget(self.humidity_plot)
        self.humidity_curves = {}

        self.illumination_plot = PlotWidget(self)
        self.illumination_plot.setBackgroundBrush(brush)
        self.illumination_plot.setFixedHeight(300)
        box.addWidget(self.illumination_plot)
        self.illumination_curves = {}
        box.addItem(QSpacerItem(40, 20, QSizePolicy.Maximum, QSizePolicy.Expanding))
        self.m_list.setLayout(box)

        box = QVBoxLayout(self.m_devices)
        box.addItem(QSpacerItem(40, 20, QSizePolicy.Maximum, QSizePolicy.Expanding))
        self.m_devices.setLayout(box)
	self.file = open('result.txt', 'w')

    def closeEvent(self, event):
	self.file.close()
        self.interface.mif.finishAll()

    def show_received(self, node_id, sequence_num, timestamp, temperature, humidity, illumination):
        result = "ID:" + str(node_id) + " SeqNo:" + str(sequence_num) + " temperature:" + str(temperature) + \
              " humidity:" + str(humidity) + " illumination:" + str(illumination) + " time:" + str(timestamp) + "\n"
        self.file.write(result)
        print("ID:" + str(node_id) + " SeqNo:" + str(sequence_num))
        
	try:
            temperature_curve = self.temperature_curves[str(node_id)]
            humidity_curve = self.humidity_curves[str(node_id)]
            illumination_curve = self.illumination_curves[str(node_id)]
        except KeyError:
            temperature_curve = self.temperature_plot.plot([], [])
            humidity_curve = self.humidity_plot.plot([], [])
            illumination_curve = self.illumination_plot.plot([], [])
            self.temperature_curves[str(node_id)] = temperature_curve
            self.humidity_curves[str(node_id)] = humidity_curve
            self.illumination_curves[str(node_id)] = illumination_curve

            box = self.m_devices.layout()

            device_item = DeviceItem(self.m_devices, node_id)
            device_item.colorChanged.connect(self.set_device_color)
            self.set_device_color(node_id, device_item.color)
            device_item.showChanged.connect(self.change_device_show)
            device_item.temperatureChanged.connect(self.change_device_temperature)
            device_item.humidityChanged.connect(self.change_device_humidity)
            device_item.illuminationChanged.connect(self.change_device_illumination)

            box.insertWidget(box.count() - 1, device_item)
            self.m_aside.setFixedWidth(device_item.width())

        data = temperature_curve.getData()
        data_x, temperature_y = list(data[0]), list(data[1])
        data_x.append(timestamp / 1000.0)
        temperature_y.append(temperature)
        temperature_curve.setData(data_x, temperature_y)

        data = humidity_curve.getData()
        data_x, humidity_y = list(data[0]), list(data[1])
        data_x.append(timestamp / 1000.0)
        humidity_y.append(humidity)
        humidity_curve.setData(data_x, humidity_y)

        data = illumination_curve.getData()
        data_x, illumination_y = list(data[0]), list(data[1])
        data_x.append(timestamp / 1000.0)
        illumination_y.append(illumination)
        illumination_curve.setData(data_x, illumination_y)

    def send_start(self):
        self.interface.send(CONTROL_START, int(self.m_interval.text()))

    def send_stop(self):
        self.interface.send(CONTROL_STOP)

    def clear_plot(self):
        for key in self.temperature_curves:
            self.temperature_curves[key].setData([], [])
            self.humidity_curves[key].setData([], [])
            self.illumination_curves[key].setData([], [])

    def set_device_color(self, node_id, color):
        self.temperature_curves[str(node_id)].setPen(color)
        self.humidity_curves[str(node_id)].setPen(color)
        self.illumination_curves[str(node_id)].setPen(color)

    def change_device_show(self, node_id, check, t_check, h_check, i_check):
        if check:
            self.change_device_temperature(node_id, t_check)
            self.change_device_humidity(node_id, h_check)
            self.change_device_illumination(node_id, i_check)
        else:
            self.temperature_curves[str(node_id)].hide()
            self.humidity_curves[str(node_id)].hide()
            self.illumination_curves[str(node_id)].hide()

    def change_device_temperature(self, node_id, check):
        if check:
            self.temperature_curves[str(node_id)].show()
        else:
            self.temperature_curves[str(node_id)].hide()

    def change_device_humidity(self, node_id, check):
        if check:
            self.humidity_curves[str(node_id)].show()
        else:
            self.humidity_curves[str(node_id)].hide()

    def change_device_illumination(self, node_id, check):
        if check:
            self.illumination_curves[str(node_id)].show()
        else:
            self.illumination_curves[str(node_id)].hide()