示例#1
0
class Notifications(object):
    instance = None

    @classmethod
    def get_instance(cls):
        if not cls.instance:
            cls.instance = Notifications()
        return cls.instance

    def __init__(self):
        print("Creating Notifications instance")
        self.notification_subscriber = rospy.Subscriber(
            NOTIFICATION_TOPIC, String, self.notification_callback)
        self.notifications = []
        self.notification_cleanup = 0

    def cleanup_notifications(self):
        if self.notification_cleanup < time.time() - 5:
            bak = self.notifications
            self.notifications = []
            for t, nt in bak:
                if t > time.time() - 15:
                    self.notifications.append((t, nt))

    def get_notifications(self, since):
        self.cleanup_notifications()
        return [y for x, y in self.notifications if x >= since]

    def notification_callback(self, msg):
        self.log("Notification received: %s" % (repr(msg)))
        try:
            t = time.time()
            now = datetime.datetime.now()
            notification = json.loads(msg.data)
            notification['received'] = t
            self.notifications.append((t, notification))
        except ValueError, e:
            self.notifications.append((time.time(), {
                "message":
                "Incorrectly formed notification received: \"%s\"" % msg.data,
                "level":
                "HIGH",
                "timeout":
                4000,
                "received":
                time.time()
            }))

        self.log("Generating video")
        ih = ImageHandler.get_instance()
        ih.generate_video()