Beispiel #1
0
 def kill(self) -> None:
     try:
         self._zthread.kill()
         return True
     except Exception as error:
         Logger.log_error(error)
         return False
Beispiel #2
0
def new_POST(self):
    try:
        if (posted("date_input") and posted("time_input")
                and posted("position_input")):
            date_input, time_input, position_input = get_posted_value(
                "date_input", "time_input", "position_input")
            date_time = datetime.strptime(f"{date_input} {time_input}",
                                          "%Y-%m-%d %H:%M")
            position = int(position_input)

            self._header.selected_curtain().open_percentage(
                desired_position=position, time=date_time)
            session[
                "success"] = f"Successfully created event for {date_input} {time_input} at {position}"

        elif (not posted("date_input")):
            raise Exception("Value date_input not found")
        elif (not posted("time_input")):
            raise Exception("Value time_input not found")
        elif (not posted("position_input")):
            raise Exception("Value position_input not found")

    except Exception as error:
        Logger.log_error(error)
        session["error"] = f"Error setting event {str(error)}"

    return redirect("/new")
Beispiel #3
0
    def _loop_process(self):
        sunrise = self.sunrise_time().replace(tzinfo=None)
        if (sunrise < datetime.now()):
            return Warn("Sunrise has already passed for today. Skipping today")

        for curtain in self._System.Curtains_list():
            try:
                curtain_option = curtain.CurtainOption(self._option_id)
                if (not curtain_option.is_on()): continue

                curtain_buffer_time = 0 if (isinstance(
                    curtain.buffer_time(),
                    type(None))) else curtain.buffer_time()
                buffer_td = timedelta(seconds=curtain_buffer_time / 10 / 2)
                # .5: buffer both sides; .10: precision
                if (curtain.CurtainEvents_for_range(
                        earliest=sunrise - buffer_td,
                        latest=sunrise + buffer_td)):
                    Warn("Event already set for sunrise time.")
                    continue
                    # don't duplicate sunrise

                position = curtain_option.data() if curtain_option.data(
                ) else curtain.length()
                curtain.open(desired_position=position,
                             Options_id=curtain_option.Options_id(),
                             time=sunrise)

            except Exception as error:
                Logger.log_error(error)
Beispiel #4
0
	def _thread_loop(self) -> None:
		try:  # make it safe!!!
			self.sleep(self._sleep_time());
			if(self._is_active):
				self._loop_process();
		except Exception as error:
			try: Logger.log_error(error);
			except: pass;
Beispiel #5
0
	def wrapper(self, *args):
		try:
			return function(self, *args);
		except KeyError as error:
			Logger.log_error(error);
			return "{\"error\" : \"{} missing from request\"}".format(RE_search("'([^']*)'", error.message).group(1));
		except Exception as error:
			Logger.log_error(error);
			return "{\"error\" : \"{}\"}".format(str(error));
Beispiel #6
0
    def _loop_process(self):
        option = self._System.Option_by_name("Event Predictor")
        for curtain_id in self._System.Curtain():
            try:
                curtain = self._System.Curtain(curtain_id)
                curtain_option = curtain.CurtainOptions(option.id())
                if (not curtain_option.is_on()): continue

            except Exception as error:
                Logger.log_error(error)
Beispiel #7
0
def events_POST(self):
    try:
        # if(not (event_id := try_convert(*get_posted_value("event_id"), int)) or not isinstance(event_id, int)):
        event_id = try_convert(*get_posted_value("event_id"), int)
        if (not event_id or not isinstance(event_id, int)):
            raise Exception(f"{event_id} is not of correct type int")

        curtain = header.selected_curtain()
        event = curtain.CurtainEvent(event_id)
        event.delete()

    except Exception as error:
        Logger.log_error(error)
        session["error"] = f"Error setting event {str(error)}"
Beispiel #8
0
	def _activate(self, client, feed_id, position_payload):
		print(f"AdafruitFeed: client: {client}, feed_id: {feed_id}, position: {position_payload}");
		try:
			# Get the curtain for feed ID
			curtain = self._curtain_for_feed_id(feed_id);
			if(not curtain): raise Exception(f"Feed ID: {feed_id} not found");

			# Convert Option to position
			curtain_option = curtain.CurtainOption(self._option_id);
			position = try_convert(position_payload, int) or try_convert(curtain_option.data().get(feed_id, 0), int);
			print(f"Curtain: {curtain.name()}, feed: {feed_id}, position: {position}");
			if(isinstance(position, NONETYPE)): raise Exception("Could not get a valid position");

			# For selected curtain add event for position
			curtain.open_percentage(desired_position=position, Options_id=self._option_id);

		except Exception as error:
			Logger.log_error(error);
Beispiel #9
0
def index_POST(self):
    try:
        header = self._header

        if (posted("open_button")):
            header.selected_curtain().open_immediately(
                header.selected_curtain().length())
        elif (posted("close_button")):
            header.selected_curtain().close_immediately()
        elif (posted("set_button")):
            position = int(request.form["desired_position_input"])
            header.selected_curtain().open_percentage(
                desired_position=position)
            session["success"] = "Successfully created event"

    except Exception as error:
        Logger.log_error(error)
        session["error"] = f"Error setting event {str(error)}"

    return redirect("/")
Beispiel #10
0
    def activate(self):
        Curtain = self._Curtain

        post_json = self.json()
        print("Post data:", end="")
        #TESTING
        print(post_json)
        #TESTING
        try:
            if (not Curtain.is_smart() and Curtain.is_safe()
                    and Curtain.current_position() == self._desired_position):
                raise Exception(
                    "Curtain will not move to a state it believes itself to already be in [is_safe=TRUE]"
                )

            response = post(url=f"http://{Curtain.ip_address()}",
                            json=post_json,
                            timeout=3)
            if (response.status_code != 200):
                raise Exception(
                    f"Status code for event: {self._id} is invalid")
            if ("error" in response.json()):
                raise Exception(
                    f"Received error message: {response.json()['error']}")
            print(response.json())
            #TESTING

            if (not self.is_activated(True) or not self._is_activated):
                raise Exception("Failed to set event activated")
            if (not Curtain.is_activated(True)):
                raise Exception("Failed to set curtain activated")
            if (not Curtain.current_position(self._desired_position)):
                raise Exception("Failed to set curtain position")

        except Exception as error:
            Logger.log_error(error)

        self.delete()