Esempio n. 1
0
 def __init__(self):
     QMainWindow.__init__(self)
     self.setupUi(self)
     self.stateLabel = QLabel()
     self.statusBar().addPermanentWidget(self.stateLabel)
     self.__model = WeatherModel()
     self.__model.dataChanged.connect(self._handleModelChanges)
     self.__model.modelReset.connect(self._handleModelChanges)
     self.__model.modelReset.connect(self.weatherTable.resizeColumnsToContents, Qt.QueuedConnection)
     self.weatherTable.setModel(self.__model)
     self.weatherTable.setItemDelegateForColumn(2, SpinBoxDelegate(self))
     self.buttonBox.clicked.connect(self._handleButtonsClick)
     self.weatherView.sectorClicked.connect(self.weatherTable.selectRow)
     handlers = {('init', 'race_state'): self._setRaceState,
                 ('update', 'weather'): self._weatherChanged}
     SubscriberApplication.registerMsgHandlers(handlers)
     SubscriberApplication.instance().subscribed.connect(self.statusBar().clearMessage)
     SubscriberApplication.instance().subscriptionError.connect(self._subscriptionError)
     SubscriberApplication.instance().subscribe()
Esempio n. 2
0
 def update(self):
     has_changed = False
     now = datetime.now()
     if self.first_update:
         print("First update, fetching weather...")
         has_changed = True
         self.first_update = False
     #print("Last update: {0}, {1} ago".format(self.last_update, now - self.last_update))
     #print("Minimum interval: {0}".format(self.interval))
     if now - self.last_update > self.interval:
         print("Last update ({0}) too long ago: {1} > {2}".format(
             self.last_update, now - self.last_update, self.interval))
         has_changed = True
     if has_changed:
         self.last_update = now
         self.response = self.fetch_weather()
         # Update view
         if not self.response is None:
             self.weather = WeatherModel(self.response)
             self.view.set_weather(self.weather)
     return has_changed
Esempio n. 3
0
class WeatherStationWindow(QMainWindow, Ui_WeatherStationWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.stateLabel = QLabel()
        self.statusBar().addPermanentWidget(self.stateLabel)
        self.__model = WeatherModel()
        self.__model.dataChanged.connect(self._handleModelChanges)
        self.__model.modelReset.connect(self._handleModelChanges)
        self.__model.modelReset.connect(self.weatherTable.resizeColumnsToContents, Qt.QueuedConnection)
        self.weatherTable.setModel(self.__model)
        self.weatherTable.setItemDelegateForColumn(2, SpinBoxDelegate(self))
        self.buttonBox.clicked.connect(self._handleButtonsClick)
        self.weatherView.sectorClicked.connect(self.weatherTable.selectRow)
        handlers = {('init', 'race_state'): self._setRaceState,
                    ('update', 'weather'): self._weatherChanged}
        SubscriberApplication.registerMsgHandlers(handlers)
        SubscriberApplication.instance().subscribed.connect(self.statusBar().clearMessage)
        SubscriberApplication.instance().subscriptionError.connect(self._subscriptionError)
        SubscriberApplication.instance().subscribe()

    def _checkReply(self, reply):
        if reply != "ok":
            self.statusBar().showMessage(str(reply), 5000)

    def _handleButtonsClick(self, button):
        role = self.buttonBox.buttonRole(button)
        if role == QDialogButtonBox.ApplyRole:
            Weather.scheduleChange(self._checkReply, 0, self.__model.changes())
            self.__model.discardChanges()
        elif role == QDialogButtonBox.ResetRole:
            self.__model.discardChanges()

    def _handleModelChanges(self, _topLeft=None, _bottomRight=None):
        self.buttonBox.setEnabled(len(self.__model.changes()) > 0)

    def _setRaceState(self, state):
        self.stateLabel.setText(" Simulation %s   " % state.text)

    def _subscriptionError(self):
        self.statusBar().showMessage("Subscription failed, retrying ...", 1500)

    def _weatherChanged(self, weather):
        msg = "sector" + ("s " if len(weather) > 1 else " ")
        msg += ', '.join([str(s) for s, _ in weather])
        self.statusBar().showMessage("Weather changed in " + msg, 10000)
    def data_organizerForecast(self, raw_api_dict):

        city_dict = raw_api_dict["city"]
        coordinates = city_dict["coord"]

        location_of_the_forecast = LocationModel(coordinates["lat"],
                                                 coordinates["lon"])

        total_elements = raw_api_dict["cnt"]

        element_list = raw_api_dict["list"]

        data = []
        for element in element_list:

            date_string = element["dt_txt"]

            # '2018-07-01 09:00:00'
            #datetime.strptime('Jun 1 2005  1:33PM', '%b %d %Y %I:%M%p')
            date = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S')

            wind_dict = element["wind"]

            wind_speed = wind_dict["speed"]
            wind_degree = wind_dict["deg"]

            rain_dict = element["rain"]

            rain_chance = rain_dict.get("3h", 0)

            weather_dict = element["weather"][0]

            weather_description = weather_dict["description"]
            # I believe we don't need much from here...

            main_dict = element["main"]

            pressure = main_dict['pressure']
            ground_level = main_dict['grnd_level']
            sea_level = main_dict['sea_level']

            temp_kf = main_dict['temp_kf']
            temp_max = main_dict["temp_max"]
            temp_min = main_dict['temp_min']
            temp = main_dict['temp']

            humidity = main_dict['humidity']

            cloud_dict = element["clouds"]

            cloud_coverage = cloud_dict["all"]

            # temp = raw_api_dict['list'][elements]['main'].get('temp'),
            # temp_max = raw_api_dict['list'][elements]['main'].get('temp_max'),
            # temp_min = raw_api_dict['list'][elements]['main'].get('temp_min'),
            # humidity = raw_api_dict['list'][elements]['main'].get('humidity'),
            # pressure = raw_api_dict['list'][elements]['main'].get('pressure'),
            # sky = raw_api_dict['list'][elements]['weather'][0]['main'],
            # wind = raw_api_dict['list'][elements]['wind'].get('speed'),
            # wind_deg = raw_api_dict['list'][elements]['wind'].get('deg'),
            # dt_txt = raw_api_dict['list'][elements]['dt_txt'],
            # cloudiness=raw_api_dict['list'][elements]['clouds'].get('all'),
            # rain=raw_api_dict['list'][elements]['rain'],
            # typeid=raw_api_dict['list'][elements]['weather'][0]['id'],
            # wtype=raw_api_dict['list'][elements]['weather'][0]['description']

            sat_count = 0
            visibility = 0
            #TODO fill date time as well
            #TODO integrate sat count as well
            new_weather_model = WeatherModel(location_of_the_forecast,
                                             date,
                                             rain_chance,
                                             temp,
                                             temp_max,
                                             temp_min,
                                             humidity,
                                             wind_speed,
                                             cloud_coverage,
                                             sat_count,
                                             visibility,
                                             pressure,
                                             wind_degree=wind_degree)
            data.append(new_weather_model)
        # These can be placed inside the weather model, but as a coordinate, we are mostly interested in weather
        # conditions at spesific coords, (wont fly drones over a city wide area)
        # data['city'] = raw_api_dict.get('city').get('name')
        # data['country'] = raw_api_dict.get('city').get('country'),

        return data