From 15e79e67dfd80f514a06dd8be39bc086aeb43644 Mon Sep 17 00:00:00 2001 From: jimmy Date: Sat, 23 Jul 2022 05:27:29 +1200 Subject: [PATCH] Remove mqtt --- slave/main.py | 48 +++++++----------------------------------------- slave/video.py | 12 +++++++----- 2 files changed, 14 insertions(+), 46 deletions(-) diff --git a/slave/main.py b/slave/main.py index 888f2ad..fd24b12 100644 --- a/slave/main.py +++ b/slave/main.py @@ -13,12 +13,9 @@ from image import Image import multiprocessing class App: - def __init__(self, client, x, y): + def __init__(self, x, y): self.x = x self.y = y - - - self.client = client self.video = Video(x, y) self.image = Image(x, y) self.videos = [join("slave/videos", f) for f in listdir("slave/videos") if isfile(join("slave/videos", f))] @@ -27,41 +24,17 @@ class App: self.lastimage = -1 print(self.videos) print(self.videos[randint(0, len(self.videos)-1)]) - - def on_connect(self, client, userdata, flags, rc): - print("Connected with result code "+str(rc)) - self.client.subscribe("slave") - - - def on_message(self, client, userdata, msg): - print(msg.topic+" "+str(msg.payload)) - if msg.payload == b'idle': - print("Idle") - self.x = multiprocessing.Process(target = self.idle) - self.x.start() - - elif msg.payload == b'stop': - print("Stop") - try: - self.x.terminate() - except: - pass - + def idle(self): last = -1 - while True: - try: - self.x.terminate() - except: - pass - + while True: # if i == last: # continue if randint(0,5) == 0: self.showimage() else: - self.playvideo() + self.playvideo() def showimage(self): totalimages = len(self.images)-1 @@ -72,7 +45,7 @@ class App: i = 0 self.lastimage = i self.image.show(self.images[i]) - cv2.waitKey(1000) + cv2.waitKey(10000) def playvideo(self): totalvideos = len(self.videos)-1 @@ -90,15 +63,8 @@ class App: def main(): - client = mqtt.Client() - app = App(client, int(sys.argv[1]), int(sys.argv[2])) - client.on_connect = app.on_connect - client.on_message = app.on_message - - client.connect("10.1.1.162", 1883, 60) - - client.loop_forever() - + app = App(int(sys.argv[1]), int(sys.argv[2])) + app.idle() if __name__ == "__main__": main() \ No newline at end of file diff --git a/slave/video.py b/slave/video.py index 6b41037..29693eb 100644 --- a/slave/video.py +++ b/slave/video.py @@ -2,6 +2,7 @@ from random import randint import cv2 import multiprocessing import time +import sys class Video: def __init__(self, x, y): @@ -27,7 +28,7 @@ class Video: delay = 1/fps * 1000 print(fps, delay) i = 0 - playframes = randint(50, 200) + playframes = randint(300, 500) while(self.cap.isOpened() ): i += 1 if i > playframes: @@ -45,14 +46,15 @@ class Video: cv2.imshow("window", frame) - if cv2.waitKey(int(delay)) == ord('q'): - break + if cv2.waitKey(int(delay)) & 0xFF == ord('q'): + self.cap.release() + cv2.destroyAllWindows() + sys.exit(0) # Break the loop else: break - #self.cap.release() - + self.cap.release() # Closes all the frames #cv2.destroyAllWindows()