import os
import rospy

from geometry_msgs.msg import Twist
from evdev import InputDevice, list_devices, ecodes, categorize
from lg_common.helpers import rewrite_message_to_dict, find_device
from interactivespaces_msgs.msg import GenericMessage


class LgActivityException(Exception):
    pass


class DeviceReplay:
    """
    Initialized with device name
    Needs a publisher to publish messages
    """
    def __init__(self, publisher, device_name, event_ecode='EV_KEY', device=None):
        """
        Needs to be initialized with publisher and device name which is an identifier that DeviceReplay
        will attach to. Optional parameter is a device_path mainly for testing purposes.
        """
        self.event_ecode = event_ecode
        self.publisher = publisher
        self.device_name = device_name
        self.device = device
        # TODO (wz): set device permissions using udev rules because otherwise this node needs sudo
        if self.device:
            self.device = device
            rospy.loginfo("Initializing device replay with devic: %s" % self.device)
        else:
            try:
                device_path = find_device(self.device_name)
                self.device = InputDevice(device_path)
                rospy.loginfo("Initialize device reader for %s" % self.device)
            except IndexError, e:
                rospy.logerr("No device with name: '%s'" % self.device_name)

    def run(self):
        for event in self.device.read_loop():
            rospy.loginfo("Catched event: %s" % event)
            if self.event_ecode:
                if event.type == getattr(ecodes, self.event_ecode):
                    event = categorize(event)
                    publishable_event = rewrite_message_to_dict(event)
                    self.publisher.publish_event(publishable_event)
            else:
                event = categorize(event)
                publishable_event = rewrite_message_to_dict(event)
                self.publisher.publish_event(publishable_event)


class DevicePublisher:
    """
    Initialized with topic name and message type
    """
    def __init__(self, publisher):
        self.publisher = publisher
        rospy.loginfo("Initialized device publisher for %s" % self.publisher)

    def publish_event(self, event):
        rospy.loginfo("Publishing event %s" % event)
        msg = GenericMessage()
        msg.type = 'json'
        msg.message = str(event)
        self.publisher.publish(msg)

@@ -834,32 +834,96 @@
 device = device%0A
+        self.event_code_num = getattr(ecodes, self.event_ecode)%0A
         # TODO (
@@ -1006,16 +1006,16 @@
 ds sudo%0A
-
         
@@ -1554,100 +1554,8 @@
 ():%0A
-            rospy.loginfo(%22Catched event: %25s%22 %25 event)%0A            if self.event_ecode:%0A    
     
@@ -1578,32 +1578,16 @@
 .type ==
- getattr(ecodes,
  self.ev
@@ -1594,260 +1594,17 @@
 ent_
-e
 code
-):%0A                    event = categorize(event)%0A                    publishable_event = rewrite_message_to_dict(event)%0A                    self.publisher.publish_event(publishable_event)%0A            else:%0A                event = categorize(event)
+_num:
 %0A   
@@ -1970,16 +1970,16 @@
 isher)%0A%0A
+
     def 
@@ -2010,61 +2010,8 @@
 t):%0A
-        rospy.loginfo(%22Publishing event %25s%22 %25 event)%0A
     

optimize message publishing