def test_hdmi_image(self): graph = MmalGraph(display=HDMI) graph.open(self.image) time.sleep(1) self.assertEqual(graph.is_open, True) self.assertEqual(graph.uri, self.image) self.assertEqual(graph.display_num, HDMI)
class RaspiMmalGraphHandle(RaspiIOHandle): __graph = None PATH = __name__.split('.')[-1] CATCH_EXCEPTIONS = (TypeError, ValueError, RuntimeError, OSError) def __init__(self): super(RaspiMmalGraphHandle, self).__init__() def __del__(self): try: if self.__graph.is_open: self.__graph.close() except AttributeError: pass @staticmethod def get_nodes(): return list(map(str, [LCD, HDMI])) async def init(self, ws, data): req = GraphInit(**data) self.__graph = MmalGraph(req.display_num) return True async def open(self, ws, data): # Save graph to a temporary file file_path = await self.receive_binary_file(ws, data) # Display graph via mmal self.__graph.open(file_path) # Remove graph os.remove(file_path) return True async def close(self, ws, data): GraphClose(**data) if self.__graph.is_open: self.__graph.close() async def get_property(self, ws, data): req = GraphProperty(**data) if req.property == GraphProperty.IS_OPEN: return self.__graph.is_open elif req.property == GraphProperty.DISPLAY_NUM: return self.__graph.display_num else: raise ValueError("unknown property")
def test_attribute(self): graph = MmalGraph() self.assertEqual(graph.display_num, HDMI) with self.assertRaises(AttributeError): graph.uri = self.image with self.assertRaises(AttributeError): graph.is_open = True with self.assertRaises(AttributeError): graph.display_num = LCD self.assertEqual(graph.uri, "") self.assertEqual(graph.is_open, False) graph.open(self.image) self.assertEqual(graph.is_open, True) self.assertEqual(graph.uri, self.image) graph.close() self.assertEqual(graph.uri, "") self.assertEqual(graph.is_open, False)
async def init(self, ws, data): req = GraphInit(**data) self.__graph = MmalGraph(req.display_num) return True
class RaspiMmalGraphHandle(RaspiIOHandle): __graph = None PATH = __name__.split('.')[-1] CATCH_EXCEPTIONS = (TypeError, ValueError, RuntimeError, OSError) def __init__(self): super(RaspiMmalGraphHandle, self).__init__() def __del__(self): try: if self.__graph.is_open: self.__graph.close() except AttributeError: pass @staticmethod def get_nodes(): return list(map(str, [LCD, HDMI])) async def init(self, ws, data): req = GraphInit(**data) self.__graph = MmalGraph(req.display_num) return True async def open(self, ws, data): graph_data = bytes() header = RaspiBinaryDataHeader(**data) # Receive graph binary data for i in range(header.slices): temp = await ws.recv() graph_data += temp # Check graph size if len(graph_data) != header.size: raise ValueError("data size do not matched") # Check graph md5 if hashlib.md5(graph_data).hexdigest() != header.md5: raise ValueError("data md5 checksum do not matched") # Save graph to a temporary file file_path = os.path.join("/tmp", "{}.{}".format(header.md5, header.format)) with open(file_path, "wb") as fp: fp.write(graph_data) # Display graph via mmal self.__graph.open(file_path) # Remove graph os.remove(file_path) return True async def close(self, ws, data): req = GraphClose(**data) if self.__graph.is_open: self.__graph.close() async def get_property(self, ws, data): req = GraphProperty(**data) if req.property == GraphProperty.IS_OPEN: return self.__graph.is_open elif req.property == GraphProperty.DISPLAY_NUM: return self.__graph.display_num else: raise ValueError("unknown property")
def test_close(self): graph = MmalGraph() time.sleep(1) graph.close() graph.close()
def test_open(self): with self.assertRaises(TypeError): graph = MmalGraph() graph.open() with self.assertRaises(TypeError): graph = MmalGraph() graph = open() with self.assertRaises(IOError): graph = MmalGraph() graph.open("") graph = MmalGraph() graph.open(self.image)