[4] | 1 | ############################################################################ |
---|
| 2 | # |
---|
| 3 | # DIM - A Direct Interaction Manager for SAGE |
---|
| 4 | # Copyright (C) 2007 Electronic Visualization Laboratory, |
---|
| 5 | # University of Illinois at Chicago |
---|
| 6 | # |
---|
| 7 | # All rights reserved. |
---|
| 8 | # |
---|
| 9 | # Redistribution and use in source and binary forms, with or without |
---|
| 10 | # modification, are permitted provided that the following conditions are met: |
---|
| 11 | # |
---|
| 12 | # * Redistributions of source code must retain the above copyright |
---|
| 13 | # notice, this list of conditions and the following disclaimer. |
---|
| 14 | # * Redistributions in binary form must reproduce the above |
---|
| 15 | # copyright notice, this list of conditions and the following disclaimer |
---|
| 16 | # in the documentation and/or other materials provided with the distribution. |
---|
| 17 | # * Neither the name of the University of Illinois at Chicago nor |
---|
| 18 | # the names of its contributors may be used to endorse or promote |
---|
| 19 | # products derived from this software without specific prior written permission. |
---|
| 20 | # |
---|
| 21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 22 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 23 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 24 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
| 25 | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
| 26 | # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
| 27 | # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
| 28 | # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
| 29 | # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
| 30 | # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
| 31 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 32 | # |
---|
| 33 | # Direct questions, comments etc about SAGE UI to www.evl.uic.edu/cavern/forum |
---|
| 34 | # |
---|
| 35 | # Author: Ratko Jagodic |
---|
| 36 | # |
---|
| 37 | ############################################################################ |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | import os, os.path |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | class DeviceManager: |
---|
| 46 | |
---|
| 47 | def __init__(self): |
---|
| 48 | self.devices = {} # key=deviceId, value=deviceObj |
---|
| 49 | |
---|
| 50 | # load all the plugins now |
---|
| 51 | self.__devicePlugins = {} # key=deviceType, value=devicePlugin |
---|
| 52 | self.__loadAllPlugins() |
---|
| 53 | |
---|
| 54 | # for special devices and assigning unique ids |
---|
| 55 | self.__specialDevices = [] # a list of special devices |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | def onHWMessage(self, deviceId, deviceType, data): |
---|
| 60 | """ this gets called when a message arrives from any hw device """ |
---|
| 61 | |
---|
| 62 | # check whether the device already exists, if so pass the message to it |
---|
| 63 | if deviceId in self.devices: |
---|
| 64 | self.devices[ deviceId ].onMessage(data, False) |
---|
| 65 | |
---|
| 66 | else: |
---|
| 67 | if deviceType not in self.__devicePlugins: # devicePlugin isn't loaded yet |
---|
| 68 | if not self.__loadDevicePlugin( deviceType ): |
---|
| 69 | return # couldn't load the plugin |
---|
| 70 | # at this point we have a plugin loaded so create a device object to do the conversion |
---|
| 71 | |
---|
| 72 | newDeviceObj = self.__devicePlugins[ deviceType ].makeNew(deviceId) |
---|
| 73 | self.devices[ deviceId ] = newDeviceObj |
---|
| 74 | |
---|
| 75 | # set a special id if the device is special |
---|
| 76 | if newDeviceObj.specialDevice: |
---|
| 77 | specId = len(self.__specialDevices) |
---|
| 78 | newDeviceObj.setSpecialId(specId) |
---|
| 79 | self.__specialDevices.append(deviceId) |
---|
| 80 | |
---|
| 81 | newDeviceObj.onMessage(data, firstMsg=True) |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | def removeDevice(self, deviceId): |
---|
| 85 | # remove from the list of devices |
---|
| 86 | if deviceId in self.devices: |
---|
| 87 | self.devices[ deviceId ].destroy() |
---|
| 88 | del self.devices[ deviceId ] |
---|
| 89 | |
---|
| 90 | # remove from the list of special devices if necessary |
---|
| 91 | if deviceId in self.__specialDevices: |
---|
| 92 | self.__specialDevices.remove(deviceId) |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | def __loadAllPlugins(self): |
---|
| 96 | """ goes into the 'devices' directory and loads all the plugins from there """ |
---|
| 97 | |
---|
| 98 | for entry in os.listdir("devices"): |
---|
| 99 | if os.path.splitext(entry)[1] == ".py" and entry != "__init__.py": |
---|
| 100 | self.__loadDevicePlugin( os.path.splitext(entry)[0] ) |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | def __loadDevicePlugin(self, deviceType): |
---|
| 104 | """ try to load the devicePlugin for the requested deviceType """ |
---|
| 105 | try: |
---|
| 106 | #(f, path, desc) = imp.find_module(deviceType, "devices") |
---|
| 107 | #devicePlugin = imp.load_module(deviceType, f, path, desc) |
---|
| 108 | devicePlugin = __import__("devices."+deviceType, globals(), locals(), [deviceType]) |
---|
| 109 | self.__devicePlugins[ deviceType ] = devicePlugin |
---|
| 110 | print "Device plugin loaded: ", deviceType |
---|
| 111 | return True |
---|
| 112 | |
---|
| 113 | except ImportError: |
---|
| 114 | return False # failed to load the module |
---|
| 115 | |
---|
| 116 | |
---|