Example #1
0
    def configure(self, config_name, action, contents):
        """
        The main configuration callback.

        """
        _log.info('Received configuration {} signal: {}'.format(
            action, config_name))
        self.current_config = self.default_config.copy()
        self.current_config.update(contents)

        self.analysis_name = self.current_config.get("analysis_name")
        self.schedule_time = self.current_config.get("schedule_time")
        self.device = self.current_config.get("device")
        self.mht = self.current_config.get("mht")
        self.excess_operation = self.current_config.get("excess_operation")
        self.interval = self.current_config.get("interval")
        self.timezone = self.current_config.get("timezone")
        self.condition_list = self.current_config.get("condition_list", {})
        self.device_true_time = 0

        campus = self.device["campus"]
        building = self.device["building"]
        device_config = self.device["unit"]
        self.publish_topics = "/".join([self.analysis_name, campus, building])
        multiple_devices = isinstance(device_config, dict)
        self.command_devices = device_config.keys()

        try:
            for device_name in device_config:
                device_topic = topics.DEVICES_VALUE(campus=campus, building=building, \
                                                    unit=device_name, path="", \
                                                    point="all")

                self.device_topic_list.update({device_topic: device_name})
                self.device_name.append(device_name)

        except Exception as e:
            _log.error('Error configuring signal: {}'.format(e))

        date_today = datetime.utcnow().astimezone(
            dateutil.tz.gettz(self.timezone))
        print(date_today)
        if date_today in holidays.US(
                years=2020) or date_today.weekday() == 5 and 6:
            schedule_time = "* * * * *"
            self.core.schedule(cron(schedule_time), self.run_schedule)
        else:
            self.core.schedule(cron(self.schedule_time), self.run_schedule)
Example #2
0
    def configure(self, config_name, action, contents):
        """
        The main configuration callback.

        """
        _log.info('Received configuration {} signal: {}'.format(
            action, config_name))
        self.current_config = self.default_config.copy()
        self.current_config.update(contents)

        self.analysis_name = self.current_config.get("analysis_name")
        self.schedule_time = self.current_config.get("schedule_time")
        self.device = self.current_config.get("device")
        self.maximum_hour_threshold = self.current_config.get("mht")
        self.excess_operation = self.current_config.get("excess_operation")
        self.timezone = self.current_config.get("timezone", "PDT")
        self.condition_list = self.current_config.get("condition_list", {})
        self.device_true_time = 0
        _log.info("current date time {}".format(datetime.utcnow()))
        self.on_subscribe()
        # self.core.periodic(self.interval, self.run_schedule)
        self.device_true_time = 0  #at mid night zero the total minute
        date_today = datetime.utcnow().astimezone(
            dateutil.tz.gettz(self.timezone))
        if date_today in holidays.US(
                years=2020) or date_today.weekday() == 5 and 6:
            schedule_time = "* * * * *"
        else:
            schedule_time = self.schedule_time
        self.core.schedule(cron(schedule_time), self.run_schedule)
Example #3
0
    def setup(self, sender, **kwargs):
        """
        Set up subscriptions for demand limiting case.
        :param sender:
        :param kwargs:
        :return:
        """
        if self.price_file is None:
            _log.debug(
                "Electric supplier has no price information from file: {}".
                format(self.price_file))
            sys.exit()
        try:
            self.power_prices = pd.read_csv(self.price_file)
            self.power_prices = self.power_prices.set_index(
                self.power_prices.columns[0])
            self.power_prices.index = pd.to_datetime(self.power_prices.index)

            self.power_prices['month'] = self.power_prices.index.month.astype(
                int)
            self.power_prices['day'] = self.power_prices.index.day.astype(int)
            self.power_prices['hour'] = self.power_prices.index.hour.astype(
                int)
        except:
            _log.debug("ERROR reading price file!")
            sys.exit()
        if self.building_sim_topic is not None:
            self.vip.pubsub.subscribe(peer='pubsub',
                                      prefix=self.building_sim_topic,
                                      callback=self.simulation_time_handler)
        self.core.schedule(cron(self.cron_schedule), self.run_process)
Example #4
0
    def configure(self, config_name, action, contents):
        """
        The main configuration callback.

        """
        _log.info('Received configuration {} signal: {}'.format(
            action, config_name))
        self.current_config = self.default_config.copy()
        self.current_config.update(contents)
        campus = self.current_config.get("campus", "")
        building = self.current_config.get("building", "")
        self.station_code = self.current_config.get("station_code", "")
        self.run_schedule = self.current_config.get("run_schedule", "")
        self.timezone = self.current_config.get("timezone", "US/Pacific")
        self.tz = pytz.timezone(self.timezone)
        sensor_conditions = self.current_config.get("sensors_condition", {})
        self.device_dict = {}
        for device, conditions in sensor_conditions.items():
            self.device_dict[device] = Sensor(campus, building, device,
                                              conditions)
        self.core.schedule(cron(self.run_schedule),
                           self.run_diagnostics_realtime)
        self.core.schedule(cron("59 23 * * *"), self.report)
Example #5
0
    def configure(self, config_name, action, contents):
        """
        The main configuration callback.

        """
        _log.info('Received configuration {} signal: {}'.format(
            action, config_name))
        self.current_config = self.default_config.copy()
        self.current_config.update(contents)

        campus = self.current_config.get("campus", "")
        building = self.current_config.get("building", "")
        self.device_list = self.current_config.get("device", [])
        self.station_code = self.current_config.get("station_code", "")
        self.run_schedule = self.current_config.get("run_schedule", "")
        self.timezone = self.current_config.get("timezone", "")
        self.device_point_name = self.current_config.get(
            "device_point_name", [])
        self.weather_point_name = self.current_config.get(
            "weather_point_name", [])
        self.sensors_condition = self.current_config.get(
            "sensors_condition", {})
        self.publish_topics = "/".join(
            ["Diagnosis", self.campus, self.building])
        self.headers = {
            "Accept": "application/json",
            "Accept-Language": "en-US"
        }
        try:
            for device_name in self.device_list:
                device_topic = topics.DEVICES_VALUE(campus=campus, building=building, \
                                                    unit=device_name, path="", \
                                                    point="all")
                self.device_topic_list.update({device_topic: device_name})
                self.device_name.append(device_name)
        except Exception as e:
            _log.error('Error configuring signal: {}'.format(e))

        for device in self.device_topic_list:
            _log.info("Subscribing to " + device)
            try:
                self.vip.pubsub.subscribe(peer="pubsub",
                                          prefix=device,
                                          callback=self.on_data)
            except Exception as e:
                _log.error('Error configuring signal: {}'.format(e))

        self.core.schedule(cron(self.run_schedule),
                           self.run_diagnostics_realtime)
    def starting_base(self, **kwargs):
        """Instantiate each diagnostic in the diagnostic list for the
        ProactiveDiagnostic.  Setup device data subscriptions.

        :param: kwargs: empty
        :return: None
        """
        # For each diagnostic instantiate a Diagnostic
        # and pass it a configuration (diagnostic) and a reference to the
        # ProactiveDiagnostic.
        for diagnostic in self.diagnostics:
            LOG.debug("Configure %s", diagnostic.get("name"))
            self.diagnostics_container.append(Diagnostic(diagnostic, self))
        for device in self.device_topics_list:
            LOG.debug("Subscribing to %s", device)
            self.vip.pubsub.subscribe(peer="pubsub",
                                      prefix=device,
                                      callback=self.new_data,
                                      all_platforms=True)
        # Using cron string in configuration schedule the diagnostics to run.
        self.core.schedule(cron(self.run_schedule), self.run_process)
Example #7
0
    def onstart(self, sender, **kwargs):
        """
        onstart method handles scheduling regression execution.
        Either cron schedule for periodic updating of model parameters
        or one_shot to run once.
        :param sender: str;
        :param kwargs: None
        :return: None
        """
        # TODO: note in function.  reschedule do not exit.
        #if not self.validate_historian_reachable():
        #    _log.debug("Cannot verify historian is running!")
        #    sys.exit()

        if not self.one_shot:
            if not self.simulation:
                self.core.schedule(cron(self.run_schedule),
                                   self.scheduled_run_process)
            else:
                self.simulation_setup()
            if self.run_onstart:
                self.scheduled_run_process()
        else:
            try:
                self.start = parser.parse(self.start)
                self.start = self.local_tz.localize(self.start)
                self.start = self.start.astimezone(UTC_TZ)
                self.end = parser.parse(self.end)
                self.end = self.local_tz.localize(self.end)
                self.end = self.end.astimezone(UTC_TZ)
            except (NameError, ValueError) as ex:
                _log.debug(
                    'One shot regression:  start_time or end_time '
                    'not specified correctly!: *%s*', ex)
                self.end = dt.now(self.local_tz).replace(hour=0,
                                                         minute=0,
                                                         second=0,
                                                         microsecond=0)
                self.start = self.end - td(days=self.training_interval)
            self.main_run_process()
Example #8
0
    def configure(self, config_name, action, contents):
        """Set agent parameters, subscribe to message bus, and schedule
        recurring events

        Called after the Agent has connected to the message bus. If a
        configuration exists at startup this will be called before onstart.

        Is called every time the configuration in the store changes.

        :param config_name: unused
        :param action: unused
        :param contents: contents of stored configuration
        """
        config = self.default_config.copy()
        config.update(contents)

        LOG.debug("Configuring Agent")

        # interactions with platform
        self.make_reservations = config.get("make_reservations", False)
        self.historian_vip_id = config.get("historian_vip_id",
                                           "platform.historian")

        # debug files
        optimizer_debug = config.get("optimizer_debug")
        if optimizer_debug is not None:
            optimizer_debug = os.path.expanduser(optimizer_debug)
        command_debug = config.get("command_debug")
        if command_debug is not None:
            command_debug = os.path.expanduser(command_debug)

        # required sections
        try:
            optimizer_config = config["optimizer"]
            weather_config = config["weather"]
            forecast_configs = config["forecast_models"]
            component_configs = config["component_models"]
        except KeyError as e:
            raise ValueError("Required section missing from config: {}"
                             "".format(repr(e)))

        # for co-simulation with EnergyPlus
        self.simulation_mode = bool(config.get("simulation_mode", False))

        # for fully independent simulation
        # allow offline_mode to be boolean or dict of params
        offline_mode = config.get("offline_mode", False)
        try:
            if isinstance(offline_mode, (bool, str)):
                self.offline_mode = bool(offline_mode)
                offline_config = {}
            elif isinstance(offline_mode, dict):
                offline_config = offline_mode
                self.offline_mode = bool(
                    offline_config.pop("offline_mode", True))
            else:
                raise ValueError
        except ValueError:
            LOG.error("problem reading offline_mode section of configuration")
            return

        if self.offline_mode and self.simulation_mode:
            LOG.error("simulation mode and offline mode are incompatible.")
            return

        # process offline_mode config
        if self.offline_mode:
            input_data = offline_config.get("input_data", None)

        # start and end should be UTC, timezone-naive (for `periodic`)
        start = config.get("schedule_start")
        if start is None:
            start = utils.get_aware_utc_now()
        else:
            start = utils.parse_timestamp_string(start)
            try:
                start = pytz.UTC.localize(start)
            except ValueError:
                pass
        # enforce timezone naive, UTC
        start = (start - start.utcoffset()).replace(tzinfo=None)
        self.schedule_start = start

        end = config.get("schedule_end")
        if end is not None:
            end = utils.parse_timestamp_string(end)
            try:
                end = pytz.UTC.localize(end)
            except ValueError:
                pass
            # enforce timezone naive, UTC (not if end is None)
            end = (end - end.utcoffset()).replace(tzinfo=None)
        self.schedule_end = end

        if end is None:
            LOG.debug("Running from {start}".format(start=start))
        else:
            LOG.debug("Running from {start} to {end}".format(start=start,
                                                             end=end))

        # schedules can be int (period in hours) or cron schedule
        training_schedule = config.get("training_schedule", 0)
        if training_schedule:
            self.historian_training = True
            try:
                # assume schedule is int, try `periodic`
                self.training_schedule = periodic(
                    timedelta(minutes=int(training_schedule)),
                    start=start,
                    stop=end)
            except ValueError:
                # assume `training_schedule` is valid cron string

                # cron does not always return start if start matches
                # warning: may return the minute before "start"
                if start is not None:
                    _start = start - timedelta(minutes=1)
                else:
                    _start = None
                self.training_schedule = cron(training_schedule,
                                              start=_start,
                                              end=end)
        else:
            # never train
            self.historian_training = False  # redundant, False by default
            self.training_schedule = iter(())
        self.training_schedule = localize_datetime_iter(self.training_schedule)

        optimization_schedule = config.get("optimization_schedule", 60)
        try:
            # assume `optimization_schedule` is int, try `periodic`
            self.optimization_schedule = periodic(
                timedelta(minutes=int(optimization_schedule)),
                start=start,
                stop=end)
        except ValueError:
            # assume `optimization_schedule` is valid cron string

            # cron does not always return start if start matches
            # warning: may return the minute before "start"
            if start is not None:
                _start = start - timedelta(minutes=1)
            else:
                _start = None
            self.optimization_schedule = cron(optimization_schedule,
                                              start=_start,
                                              stop=end)
        self.optimization_schedule = localize_datetime_iter(
            self.optimization_schedule)

        self.model = build_model_from_config(weather_config, optimizer_config,
                                             component_configs,
                                             forecast_configs, optimizer_debug,
                                             command_debug)

        if self.offline_mode:
            self._setup_timed_events()
            # launch self.run_offline as greenlet
            self.core.schedule(utils.get_aware_utc_now(),
                               self.run_offline,
                               input_data=input_data)
        else:
            # greenlets already spawned by _setup_timed_events
            self.input_topics = self.model.get_component_input_topics()
            self.all_topics = self._create_all_topics(self.input_topics)
            self._create_subscriptions(self.all_topics)
            self.remaining_simulation_inputs = self.all_topics.copy()
            self._setup_timed_events()