Added watchdog
This commit is contained in:
parent
319da9746f
commit
29ee0546c3
288
async-bot.py
288
async-bot.py
|
@ -37,6 +37,7 @@ class AsyncBotManager:
|
||||||
self._last_printout = time_now()
|
self._last_printout = time_now()
|
||||||
|
|
||||||
self._active: set[int] = set()
|
self._active: set[int] = set()
|
||||||
|
self.reader_event = asyncio.Event()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_text_image(text: str, font: ImageFont.ImageFont | ImageFont.FreeTypeFont) -> Image.Image:
|
def get_text_image(text: str, font: ImageFont.ImageFont | ImageFont.FreeTypeFont) -> Image.Image:
|
||||||
|
@ -160,6 +161,7 @@ class AsyncBotManager:
|
||||||
print("Reconnecting")
|
print("Reconnecting")
|
||||||
await sio.connect(f"{self.base}/socket.io")
|
await sio.connect(f"{self.base}/socket.io")
|
||||||
continue
|
continue
|
||||||
|
self.reader_event.set()
|
||||||
if event == "full_state":
|
if event == "full_state":
|
||||||
buffer = b64decode(data["full_state"].encode() + b"=")
|
buffer = b64decode(data["full_state"].encode() + b"=")
|
||||||
image = Image.frombytes("1", (1000, 1000), buffer)
|
image = Image.frombytes("1", (1000, 1000), buffer)
|
||||||
|
@ -260,156 +262,172 @@ async def amain() -> None:
|
||||||
with open("settings.json", "r") as fp:
|
with open("settings.json", "r") as fp:
|
||||||
settings = load(fp)
|
settings = load(fp)
|
||||||
|
|
||||||
async with AsyncBotManager() as mgr:
|
mgr = AsyncBotManager()
|
||||||
for avoid in settings["avoid"]:
|
|
||||||
if avoid["type"] == "rect":
|
|
||||||
mgr.add_avoid_rect(
|
|
||||||
avoid["x"], avoid["y"], avoid["w"], avoid["h"]
|
|
||||||
)
|
|
||||||
print("AVOID rectangle {w}x{h}+{x}+{y}".format(**avoid))
|
|
||||||
elif avoid["type"] == "range":
|
|
||||||
mgr.add_avoid_range(
|
|
||||||
avoid["start"], avoid["stop"], avoid.get("step", 1)
|
|
||||||
)
|
|
||||||
print("AVOID range {start}-{stop}".format(**avoid))
|
|
||||||
elif avoid["type"] == "image":
|
|
||||||
with Image.open(avoid["path"]).convert("LA") as im:
|
|
||||||
assert im.width == 1000 and im.height == 1000
|
|
||||||
for y in range(im.height):
|
|
||||||
for x in range(im.width):
|
|
||||||
l, a = im.getpixel((x, y)) # type: ignore
|
|
||||||
if a > 128:
|
|
||||||
mgr.add_avoid_index(x + y * 1000)
|
|
||||||
print("AVOID image", avoid["path"])
|
|
||||||
|
|
||||||
for elem in settings["elements"]:
|
|
||||||
if elem["type"] == "text":
|
async def watchdog():
|
||||||
mgr.put_text(elem["x"], elem["y"], elem["text"], elem.get("font", "default"), elem.get("size", 8))
|
listener_task = asyncio.create_task(mgr.listener(), name="Listener")
|
||||||
print("ADD text", elem)
|
while True:
|
||||||
elif elem["type"] == "text_anim":
|
try:
|
||||||
|
async with asyncio.timeout(30):
|
||||||
|
await mgr.reader_event.wait()
|
||||||
|
except TimeoutError:
|
||||||
|
print("WATCHDOG TIMER PASSED")
|
||||||
|
listener_task.cancel()
|
||||||
|
listener_task = asyncio.create_task(mgr.listener())
|
||||||
|
|
||||||
|
watchdog_task = asyncio.create_task(watchdog(), name="Watchdog")
|
||||||
|
|
||||||
|
for avoid in settings["avoid"]:
|
||||||
|
if avoid["type"] == "rect":
|
||||||
|
mgr.add_avoid_rect(
|
||||||
|
avoid["x"], avoid["y"], avoid["w"], avoid["h"]
|
||||||
|
)
|
||||||
|
print("AVOID rectangle {w}x{h}+{x}+{y}".format(**avoid))
|
||||||
|
elif avoid["type"] == "range":
|
||||||
|
mgr.add_avoid_range(
|
||||||
|
avoid["start"], avoid["stop"], avoid.get("step", 1)
|
||||||
|
)
|
||||||
|
print("AVOID range {start}-{stop}".format(**avoid))
|
||||||
|
elif avoid["type"] == "image":
|
||||||
|
with Image.open(avoid["path"]).convert("LA") as im:
|
||||||
|
assert im.width == 1000 and im.height == 1000
|
||||||
|
for y in range(im.height):
|
||||||
|
for x in range(im.width):
|
||||||
|
l, a = im.getpixel((x, y)) # type: ignore
|
||||||
|
if a > 128:
|
||||||
|
mgr.add_avoid_index(x + y * 1000)
|
||||||
|
print("AVOID image", avoid["path"])
|
||||||
|
|
||||||
|
for elem in settings["elements"]:
|
||||||
|
if elem["type"] == "text":
|
||||||
|
mgr.put_text(elem["x"], elem["y"], elem["text"], elem.get("font", "default"), elem.get("size", 8))
|
||||||
|
print("ADD text", elem)
|
||||||
|
elif elem["type"] == "text_anim":
|
||||||
|
frames: list[Image.Image] = []
|
||||||
|
for text in elem["lines"]:
|
||||||
|
frames.append(mgr.get_text_image(text, mgr.get_font(elem.get("font", "default"), elem.get("size", 8))))
|
||||||
|
mgr.add_animation(elem["x"], elem["y"], frames, elem["spf"])
|
||||||
|
for frame in frames:
|
||||||
|
frame.close()
|
||||||
|
print("ADD text animation", elem)
|
||||||
|
elif elem["type"] == "image":
|
||||||
|
with Image.open(elem["path"]).convert("LA") as im:
|
||||||
|
mgr.put_image(elem["x"], elem["y"], im)
|
||||||
|
print("ADD image", elem)
|
||||||
|
|
||||||
|
elif elem["type"] == "time":
|
||||||
|
time_format = elem["format"]
|
||||||
|
pos_x, pos_y = elem["x"], elem["y"]
|
||||||
|
font = mgr.get_font(elem.get("font", "default"), elem.get("size", 8))
|
||||||
|
def update() -> PixelMap:
|
||||||
|
now = datetime.datetime.now(datetime.timezone.utc)
|
||||||
|
txt = now.strftime(time_format)
|
||||||
|
img = mgr.get_text_image(txt, font)
|
||||||
|
pixmap = mgr.get_image_diff(pos_x, pos_y, img)
|
||||||
|
img.close()
|
||||||
|
return pixmap
|
||||||
|
mgr.animation_functions.append(update)
|
||||||
|
elif elem["type"] == "tile":
|
||||||
|
with Image.open(elem["path"]).convert("LA") as im:
|
||||||
|
for oy in range(elem.get("h", im.height)):
|
||||||
|
for ox in range(elem.get("w", im.width)):
|
||||||
|
l, a = im.getpixel((ox % im.width, oy % im.height)) # type: ignore
|
||||||
|
if a:
|
||||||
|
x, y = elem["x"] + ox, elem["y"] + oy
|
||||||
|
index = x + y * 1000
|
||||||
|
mgr.put_bit(index, l > 0)
|
||||||
|
print("ADD tile", elem)
|
||||||
|
elif elem["type"] == "animation":
|
||||||
|
with Image.open(elem["path"]) as anim:
|
||||||
frames: list[Image.Image] = []
|
frames: list[Image.Image] = []
|
||||||
for text in elem["lines"]:
|
for frame in ImageSequence.Iterator(anim):
|
||||||
frames.append(mgr.get_text_image(text, mgr.get_font(elem.get("font", "default"), elem.get("size", 8))))
|
frames.append(frame.copy())
|
||||||
mgr.add_animation(elem["x"], elem["y"], frames, elem["spf"])
|
mgr.add_animation(elem["x"], elem["y"], frames, elem["spf"])
|
||||||
for frame in frames:
|
for frame in frames:
|
||||||
frame.close()
|
frame.close()
|
||||||
print("ADD text animation", elem)
|
print("ADD animation", elem)
|
||||||
elif elem["type"] == "image":
|
elif elem["type"] == "rgb111":
|
||||||
with Image.open(elem["path"]).convert("LA") as im:
|
ox, oy = elem["x"], elem["y"]
|
||||||
mgr.put_image(elem["x"], elem["y"], im)
|
with Image.open(elem["path"]).convert("RGBA") as im:
|
||||||
print("ADD image", elem)
|
for y in range(im.height):
|
||||||
|
for x in range(im.width):
|
||||||
|
r, g, b, a = im.getpixel((x, y)) # type: ignore
|
||||||
|
if a < 128:
|
||||||
|
continue
|
||||||
|
|
||||||
elif elem["type"] == "time":
|
start_ndx = (x + ox + (y + oy) * 577) * 3
|
||||||
time_format = elem["format"]
|
mgr.put_bit(start_ndx, r > 128)
|
||||||
pos_x, pos_y = elem["x"], elem["y"]
|
mgr.put_bit(start_ndx + 1, g > 128)
|
||||||
font = mgr.get_font(elem.get("font", "default"), elem.get("size", 8))
|
mgr.put_bit(start_ndx + 2, b > 128)
|
||||||
def update() -> PixelMap:
|
elif elem["type"] == "rgb565":
|
||||||
now = datetime.datetime.now(datetime.timezone.utc)
|
ox, oy = elem["x"], elem["y"]
|
||||||
txt = now.strftime(time_format)
|
with Image.open(elem["path"]).convert("RGBA") as im:
|
||||||
img = mgr.get_text_image(txt, font)
|
for y in range(im.height):
|
||||||
pixmap = mgr.get_image_diff(pos_x, pos_y, img)
|
for x in range(im.width):
|
||||||
img.close()
|
r, g, b, a = im.getpixel((x, y)) # type: ignore
|
||||||
return pixmap
|
if a < 128:
|
||||||
mgr.animation_functions.append(update)
|
continue
|
||||||
elif elem["type"] == "tile":
|
|
||||||
with Image.open(elem["path"]).convert("LA") as im:
|
|
||||||
for oy in range(elem.get("h", im.height)):
|
|
||||||
for ox in range(elem.get("w", im.width)):
|
|
||||||
l, a = im.getpixel((ox % im.width, oy % im.height)) # type: ignore
|
|
||||||
if a:
|
|
||||||
x, y = elem["x"] + ox, elem["y"] + oy
|
|
||||||
index = x + y * 1000
|
|
||||||
mgr.put_bit(index, l > 0)
|
|
||||||
print("ADD tile", elem)
|
|
||||||
elif elem["type"] == "animation":
|
|
||||||
with Image.open(elem["path"]) as anim:
|
|
||||||
frames: list[Image.Image] = []
|
|
||||||
for frame in ImageSequence.Iterator(anim):
|
|
||||||
frames.append(frame.copy())
|
|
||||||
mgr.add_animation(elem["x"], elem["y"], frames, elem["spf"])
|
|
||||||
for frame in frames:
|
|
||||||
frame.close()
|
|
||||||
print("ADD animation", elem)
|
|
||||||
elif elem["type"] == "rgb111":
|
|
||||||
ox, oy = elem["x"], elem["y"]
|
|
||||||
with Image.open(elem["path"]).convert("RGBA") as im:
|
|
||||||
for y in range(im.height):
|
|
||||||
for x in range(im.width):
|
|
||||||
r, g, b, a = im.getpixel((x, y)) # type: ignore
|
|
||||||
if a < 128:
|
|
||||||
continue
|
|
||||||
|
|
||||||
start_ndx = (x + ox + (y + oy) * 577) * 3
|
offset: int = (x + ox + (y + oy) * 250) * 16
|
||||||
mgr.put_bit(start_ndx, r > 128)
|
|
||||||
mgr.put_bit(start_ndx + 1, g > 128)
|
|
||||||
mgr.put_bit(start_ndx + 2, b > 128)
|
|
||||||
elif elem["type"] == "rgb565":
|
|
||||||
ox, oy = elem["x"], elem["y"]
|
|
||||||
with Image.open(elem["path"]).convert("RGBA") as im:
|
|
||||||
for y in range(im.height):
|
|
||||||
for x in range(im.width):
|
|
||||||
r, g, b, a = im.getpixel((x, y)) # type: ignore
|
|
||||||
if a < 128:
|
|
||||||
continue
|
|
||||||
|
|
||||||
offset: int = (x + ox + (y + oy) * 250) * 16
|
# pack rgb888 to rgb565
|
||||||
|
rgb: int = (r >> 3) << 11
|
||||||
|
rgb |= (g >> 2) << 5
|
||||||
|
rgb |= (b >> 3)
|
||||||
|
|
||||||
# pack rgb888 to rgb565
|
# write to a bitstream
|
||||||
rgb: int = (r >> 3) << 11
|
for i in range(16):
|
||||||
rgb |= (g >> 2) << 5
|
mgr.put_bit(
|
||||||
rgb |= (b >> 3)
|
offset + i, ((rgb << i) & 0x8000) > 0
|
||||||
|
)
|
||||||
# write to a bitstream
|
elif elem["type"] == "rect":
|
||||||
for i in range(16):
|
for y in range(elem["y"], elem["y"] + elem["h"]):
|
||||||
mgr.put_bit(
|
for x in range(elem["x"], elem["x"] + elem["w"]):
|
||||||
offset + i, ((rgb << i) & 0x8000) > 0
|
mgr.put_pixel(x, y, elem["fill"])
|
||||||
)
|
elif elem["type"] == "blob":
|
||||||
elif elem["type"] == "rect":
|
with open(elem["path"], "rb") as fp:
|
||||||
for y in range(elem["y"], elem["y"] + elem["h"]):
|
offset = elem["offset"]
|
||||||
for x in range(elem["x"], elem["x"] + elem["w"]):
|
length = elem.get("length", 1000000)
|
||||||
mgr.put_pixel(x, y, elem["fill"])
|
written = 0
|
||||||
elif elem["type"] == "blob":
|
while (char := fp.read(1)):
|
||||||
with open(elem["path"], "rb") as fp:
|
byte = char[0]
|
||||||
offset = elem["offset"]
|
if written > length:
|
||||||
length = elem.get("length", 1000000)
|
break
|
||||||
written = 0
|
for i in range(8):
|
||||||
while (char := fp.read(1)):
|
|
||||||
byte = char[0]
|
|
||||||
if written > length:
|
if written > length:
|
||||||
break
|
break
|
||||||
for i in range(8):
|
mgr.put_bit(offset, bool((byte >> (7 - i)) & 1))
|
||||||
if written > length:
|
written += 1
|
||||||
break
|
offset += 1
|
||||||
mgr.put_bit(offset, bool((byte >> (7 - i)) & 1))
|
|
||||||
written += 1
|
|
||||||
offset += 1
|
|
||||||
|
|
||||||
mgr.get_difference_image().save("result.png")
|
mgr.get_difference_image().save("result.png")
|
||||||
mgr.get_avoid_image().save("avoid.png")
|
mgr.get_avoid_image().save("avoid.png")
|
||||||
|
|
||||||
print("Starting writers...")
|
print("Starting writers...")
|
||||||
if n_proxies := len(settings["proxies"]):
|
if n_proxies := len(settings["proxies"]):
|
||||||
res = await asyncio.gather(
|
res = await asyncio.gather(
|
||||||
*[
|
*[
|
||||||
mgr.writer(
|
mgr.writer(
|
||||||
i,
|
i,
|
||||||
settings["proxies"][i % n_proxies],
|
settings["proxies"][i % n_proxies],
|
||||||
settings["delay"],
|
settings["delay"],
|
||||||
)
|
)
|
||||||
for i in range(settings["n_bots"])
|
for i in range(settings["n_bots"])
|
||||||
],
|
],
|
||||||
return_exceptions=True,
|
return_exceptions=True,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
res = await asyncio.gather(
|
res = await asyncio.gather(
|
||||||
*[mgr.writer(i) for i in range(settings["n_bots"])],
|
*[mgr.writer(i) for i in range(settings["n_bots"])],
|
||||||
return_exceptions=True,
|
return_exceptions=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
for ret in res:
|
for ret in res:
|
||||||
print("RETURN", repr(ret))
|
print("RETURN", repr(ret))
|
||||||
|
|
||||||
mgr._shutdown = True
|
mgr._shutdown = True
|
||||||
|
watchdog_task.cancel()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue