Skip to content

Commit

Permalink
Improve code in camera entity
Browse files Browse the repository at this point in the history
  • Loading branch information
ufozone committed Feb 24, 2024
1 parent d4e5db6 commit f433b03
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions custom_components/zcsmower/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def __init__(
self._generate_image()

def _generate_image(self) -> None:
"""Generate image."""
if self.config_entry.options.get(CONF_MAP_ENABLE, False):
map_image_path = self.config_entry.options.get(CONF_MAP_IMAGE_PATH, None)
if map_image_path and os.path.isfile(map_image_path):
Expand Down Expand Up @@ -264,6 +265,7 @@ def _find_points_on_line(
point_1: ImgPoint,
point_2: ImgPoint,
) -> list[ImgPoint]:
"""Find points on line between two points."""
dash_length = 10
line_length = math.sqrt(
(point_2[0] - point_1[0]) ** 2 + (point_2[1] - point_1[1]) ** 2
Expand All @@ -285,6 +287,7 @@ def _get_point_on_vector(
terminal_pt: ImgPoint,
distance: int,
) -> ImgPoint:
"""Get point on vector."""
v = np.array(initial_pt, dtype=float)
u = np.array(terminal_pt, dtype=float)
n = v - u
Expand All @@ -295,17 +298,17 @@ def _get_point_on_vector(

def _scale_to_image(
self,
lat_lon: GpsPoint,
location: GpsPoint,
size: ImgDimensions
) -> ImgPoint:
"""Convert from latitude and longitude to the image pixels."""
old = (self.gps_bottom_right[0], self.gps_top_left[0])
new = (0, size[1])
y = ((lat_lon[0] - old[0]) * (new[1] - new[0]) / (old[1] - old[0])) + new[0]
y_gps = (self.gps_bottom_right[0], self.gps_top_left[0])
y_img = (0, size[1])
y = ((location[0] - y_gps[0]) * (y_img[1] - y_img[0]) / (y_gps[1] - y_gps[0])) + y_img[0]

old = (self.gps_top_left[1], self.gps_bottom_right[1])
new = (0, size[0])
x = ((lat_lon[1] - old[0]) * (new[1] - new[0]) / (old[1] - old[0])) + new[0]
x_gps = (self.gps_top_left[1], self.gps_bottom_right[1])
x_img = (0, size[0])
x = ((location[1] - x_gps[0]) * (x_img[1] - x_img[0]) / (x_gps[1] - x_gps[0])) + x_img[0]

return (int(x), size[1] - int(y))

Expand Down Expand Up @@ -333,6 +336,7 @@ def _create_empty_map_image(self, text: str = "No map") -> Image:
return map_image

def _image_to_bytes(self) -> None:
"""Saves generated image in variable."""
img_byte_arr = io.BytesIO()
self._image.save(
img_byte_arr,
Expand Down

0 comments on commit f433b03

Please sign in to comment.