# Path: Devices/Device.py
# class Device(threading.Thread):
# 
#     def __init__(self, sinterval):
#         """ Initializes device with the sampling interval. """
#         threading.Thread.__init__(self)
#         self.queues = []
#         self.sinterval = float(sinterval)
# 
#     def deviceFactory(device_type, name, ip, port,
#                       channels, sinterval, username = None, password = None):
#         """ Device factory that instantiates the following types of devices: 
#             1. Eaton
#             2. Veris
#             3. Zwave (SmartSwitch, TemperatureSensor, LightSensor),
#             4. Raritan
#             Note: Only Raritan requires that the username and password be specified
#             """
#         import LabSenseModbus.Eaton.EatonDevice as EatonDevice
#         import LabSenseModbus.Veris.VerisDevice as VerisDevice
#         import LabSenseZwave.ZwaveDevice as ZwaveDevice
#         import LabSenseRaritan.RaritanDevice as RaritanDevice
# 
#         if device_type == "Eaton":
#             return EatonDevice.EatonDevice(name, 
#                                            ip,
#                                            port,
#                                            channels,
#                                            sinterval)
#         elif device_type == "Veris":
#             return VerisDevice.VerisDevice(name,
#                                            ip,
#                                            port,
#                                            channels,
#                                            sinterval)
#         elif device_type == "Raritan":
#             return RaritanDevice.RaritanDevice(name,
#                                            ip,
#                                            port,
#                                            channels,
#                                            sinterval,
#                                            username,
#                                            password)
#         elif device_type in ["SmartSwitch",
#                              "LightSensor",
#                              "TemperatureSensor"]:
#             return ZwaveDevice.ZwaveDevice(device_type,
#                                            name,
#                                            ip,
#                                            port,
#                                            channels,
#                                            sinterval)
#         else:
#             raise KeyError("Unrecognized Device Name " +
#                            device_type)
#     # Make deviceFactory static so Device does not have to be instantiated
#     # to run
#     deviceFactory = staticmethod(deviceFactory)
# 
#     def attach(self, queue):
#         if not queue in self.queues:
#             self.queues.append(queue)
# 
#     def detach(self, queue):
#         try:
#             self.queues.remove(queue)
#         except ValueError:
#             pass
# 
#     def notify(self, data):
#         if data:
#             for queue in self.queues:
#                 queue.put(data)
# 
#     def getData(self):
#         data = self.client.getData()
#         if data:
#             self.notify(data)
#             return data
# 
#     def run(self):
#         print "Starting Device"
#         while True:
#             self.getData()
#             time.sleep(self.sinterval)


import argparse                             # For parsing command line arguments
import sys, os                              # for importing from project directory
import time                                 # For sleeping between uploads
import Queue                                # For communicating between datasinks and devices
    import LabSenseHandler.configReader as configReader
from SmartSwitchClient import SmartSwitchClient
from LightSensorClient import LightSensorClient
from TemperatureSensorClient import TemperatureSensorClient
from Devices.Device import Device 
    from DataSinks.DataSink import DataSink

    # Import sinks and configReader

    # Parse command line arguments
    parser = argparse.ArgumentParser()
    parser.add_argument("config", help="Configuration path.")
    parser.add_argument("Devicename", help="Choose between the following Zwave\
                        Devices: SmartSwitch, LightSensor, TemperatureSensor")
    parser.add_argument("name", help="Name of device (name field in config.json file)")
    args = parser.parse_args()

    # Read configuration
    config = configReader.readConfiguration(args.config)

    # Create communication threads
    threads = []

    # Get the device config
    device_name = args.Devicename
    for device, dev_config in config.iteritems():
        if device == device_name:
            if dev_config["name"] == args.name:
                device_config = dev_config

    # If the device is present, run it
    if device_config:
        print "Found %s device" % device_name

        # Initialize the Zwave Device thread
        device = ZwaveDevice(args.Devicename,

# Path: Devices/Device.py
# class Device(threading.Thread):
# 
#     def __init__(self, sinterval):
#         """ Initializes device with the sampling interval. """
#         threading.Thread.__init__(self)
#         self.queues = []
#         self.sinterval = float(sinterval)
# 
#     def deviceFactory(device_type, name, ip, port,
#                       channels, sinterval, username = None, password = None):
#         """ Device factory that instantiates the following types of devices: 
#             1. Eaton
#             2. Veris
#             3. Zwave (SmartSwitch, TemperatureSensor, LightSensor),
#             4. Raritan
#             Note: Only Raritan requires that the username and password be specified
#             """
#         import LabSenseModbus.Eaton.EatonDevice as EatonDevice
#         import LabSenseModbus.Veris.VerisDevice as VerisDevice
#         import LabSenseZwave.ZwaveDevice as ZwaveDevice
#         import LabSenseRaritan.RaritanDevice as RaritanDevice
# 
#         if device_type == "Eaton":
#             return EatonDevice.EatonDevice(name, 
#                                            ip,
#                                            port,
#                                            channels,
#                                            sinterval)
#         elif device_type == "Veris":
#             return VerisDevice.VerisDevice(name,
#                                            ip,
#                                            port,
#                                            channels,
#                                            sinterval)
#         elif device_type == "Raritan":
#             return RaritanDevice.RaritanDevice(name,
#                                            ip,
#                                            port,
#                                            channels,
#                                            sinterval,
#                                            username,
#                                            password)
#         elif device_type in ["SmartSwitch",
#                              "LightSensor",
#                              "TemperatureSensor"]:
#             return ZwaveDevice.ZwaveDevice(device_type,
#                                            name,
#                                            ip,
#                                            port,
#                                            channels,
#                                            sinterval)
#         else:
#             raise KeyError("Unrecognized Device Name " +
#                            device_type)
#     # Make deviceFactory static so Device does not have to be instantiated
#     # to run
#     deviceFactory = staticmethod(deviceFactory)
# 
#     def attach(self, queue):
#         if not queue in self.queues:
#             self.queues.append(queue)
# 
#     def detach(self, queue):
#         try:
#             self.queues.remove(queue)
#         except ValueError:
#             pass
# 
#     def notify(self, data):
#         if data:
#             for queue in self.queues:
#                 queue.put(data)
# 
#     def getData(self):
#         data = self.client.getData()
#         if data:
#             self.notify(data)
#             return data
# 
#     def run(self):
#         print "Starting Device"
#         while True:
#             self.getData()
#             time.sleep(self.sinterval)


# Path: Devices/LabSenseZwave/ZwaveDevice.py
import argparse                             # For parsing command line arguments
import sys, os                              # for importing from project directory
import time                                 # For sleeping between uploads
import Queue                                # For communicating between datasinks and devices
    import LabSenseHandler.configReader as configReader
from SmartSwitchClient import SmartSwitchClient
from LightSensorClient import LightSensorClient
from TemperatureSensorClient import TemperatureSensorClient
from Devices.Device import Device 
    from DataSinks.DataSink import DataSink


    # Import sinks and configReader

    # Parse command line arguments
    parser = argparse.ArgumentParser()
    parser.add_argument("config", help="Configuration path.")
    parser.add_argument("Devicename", help="Choose between the following Zwave\
                        Devices: SmartSwitch, LightSensor, TemperatureSensor")
    parser.add_argument("name", help="Name of device (name field in config.json file)")
    args = parser.parse_args()

    # Read configuration
    config = configReader.readConfiguration(args.config)

    # Create communication threads
    threads = []

    # Get the device config
    device_name = args.Devicename
    for device, dev_config in config.iteritems():
        if device == device_name:
            if dev_config["name"] == args.name:
                device_config = dev_config

    # If the device is present, run it
    if device_config:
        print "Found %s device" % device_name

        # Initialize the Zwave Device thread
        device = ZwaveDevice(args.Devicename,

                             device_config["name"],