Package kenozooid :: Package driver

Source Code for Package kenozooid.driver

  1  # 
  2  # Kenozooid - dive planning and analysis toolbox. 
  3  # 
  4  # Copyright (C) 2009-2019 by Artur Wroblewski <wrobell@riseup.net> 
  5  # 
  6  # This program is free software: you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation, either version 3 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 18  # 
 19   
 20  """ 
 21  Support for dive computers, dive data loggers and other measurment devices 
 22  used in diving. 
 23   
 24  The module specifies set of interfaces to be implemented by device drivers. 
 25  """ 
 26   
 27  import logging 
 28   
 29  import kenozooid.component as kc 
 30   
 31  log = logging.getLogger('kenozooid.driver') 
32 33 -class DeviceDriver(object):
34 """ 35 Device driver interface. 36 37 Every device driver implementation has to implement at least this 38 interface. 39 40 Software using this interface shall get driver instance using 41 `DeviceDriver.scan` method. 42 43 There is only one known method for platform independent device scanning 44 and it applies to USB devices only. Library pyUSB can be used to find 45 devices, i.e.:: 46 47 import usb.core 48 dev = usb.core.find(idVendor=0x0403, idProduct=0x6001) 49 dev.write(...) 50 dev.read(...) 51 52 Above code uses not released yet pyUSB 1.0 interfaces. Device's `write` 53 and `read' methods can be used to communicate with a device. It is not yet 54 possible to determine port of a device (i.e. /dev/ttyUSB0, COM1, etc.), 55 so it is not possible to bind this method with pySerial usage when 56 required. 57 """ 58 @staticmethod
59 - def scan(port=None):
60 """ 61 Scan for connected devices and return device driver instances. 62 63 Each connected dive computer should get one device driver instance. 64 65 If port is specified, then a driver instance should be returned, 66 which connects to the port. 67 """
68
69 - def version(self):
70 """ 71 Get model and firmware version information about connected dive 72 computer. 73 """
74
75 76 -class Simulator(object):
77 """ 78 Diving computer dive simulation interface. 79 """ 80 driver = None 81
82 - def start(self):
83 """ 84 Start dive simulation on dive computer. 85 """ 86 pass
87 88
89 - def stop(self):
90 """ 91 Stop dive simulation on dive computer. 92 """ 93 pass
94 95
96 - def depth(self, d):
97 """ 98 Send simulated depth to a dive computer. 99 """ 100 pass
101
102 103 104 -class DataParser(object):
105 """ 106 Diving computer data parser interface. 107 108 Depending on dive computer firmware capabilities, driver implementing 109 the interface shall dump all possible data from dive computer like 110 111 - dive computer settings 112 - all entries from dive logbook 113 - battery information 114 """ 115 driver = None 116
117 - def dump(self):
118 """ 119 Get raw data from dive computer. 120 """
121
122 - def dives(self, data):
123 """ 124 Parse dive data from raw data. 125 126 Iterator of dives is returned. 127 128 :Parameters: 129 data 130 Raw dive computer data. 131 """
132 133
134 - def gases(self, data):
135 """ 136 Get list of gases per dive. 137 138 Iterator of tuples of gases is returned. A tuple of gases is list 139 of gases used during a dive. 140 141 :Parameters: 142 data 143 Raw dive computer data. 144 """
145 146
147 - def version(self, data):
148 """ 149 Extract model and version information from dive computer raw data. 150 151 :Parameters: 152 data 153 Raw dive computer data. 154 """
155
156 157 -class DeviceError(BaseException):
158 """ 159 Device communication error. 160 """
161
162 163 -def find_driver(iface, query, port=None):
164 """ 165 Find device driver implementing an interface. 166 167 Query parameter is used to find driver by its id or device model. 168 Device error exception is raised if driver is not found. 169 170 If device driver does not support functionality specified by an 171 interface, then None is returned. 172 173 :Parameters: 174 iface 175 Interface of functionality. 176 query 177 Device driver id or device model string. 178 port 179 Device port (i.e. /dev/ttyUSB0, COM1). 180 """ 181 found = False 182 for cls in kc.query(DeviceDriver): 183 p = kc.params(cls) 184 id = p['id'] 185 models = p['models'] 186 if id == query or any(query == m for m in models): 187 found = True 188 log.debug('found device driver for query: {0}'.format(query)) 189 break 190 191 if not found: 192 raise DeviceError('Device driver not found, query: {0}'.format(query)) 193 194 # scan for connected devices 195 try: 196 drv = next(cls.scan(port)) 197 except StopIteration as ex: 198 raise DeviceError('Driver "{}" cannot communicate with a device at' 199 ' port "{}"'.format(id, port)) 200 201 # find class implementing specified interface (and functionality) 202 try: 203 cls = next(kc.query(iface, id=id)) 204 obj = cls() 205 obj.driver = drv 206 return obj 207 except StopIteration as ex: 208 return None
209 210 211 # vim: sw=4:et:ai 212