1 # -*- coding: utf-8 -*-
6 from os.path import join
7 from threading import Thread, Lock
8 from select import select
9 from Queue import Queue, Empty
12 import openerp.addons.hw_proxy.controllers.main as hw_proxy
13 from openerp import http
14 from openerp.http import request
15 from openerp.tools.translate import _
17 _logger = logging.getLogger(__name__)
22 _logger.error('OpenERP module hw_scale depends on the pyserial python module')
30 self.scalelock = Lock()
31 self.status = {'status':'connecting', 'messages':[]}
32 self.input_dir = '/dev/serial/by-id/'
34 self.weight_info = 'ok'
37 def lockedstart(self):
39 if not self.isAlive():
43 def set_status(self, status, message = None):
44 if status == self.status['status']:
45 if message != None and message != self.status['messages'][-1]:
46 self.status['messages'].append(message)
48 self.status['status'] = status
50 self.status['messages'] = [message]
52 self.status['messages'] = []
54 if status == 'error' and message:
55 _logger.error('Scale Error: '+message)
56 elif status == 'disconnected' and message:
57 _logger.warning('Disconnected Scale: '+message)
61 devices = [ device for device in listdir(self.input_dir)]
62 scales = [ device for device in devices if ('mettler' in device.lower()) or ('toledo' in device.lower()) ]
64 print join(self.input_dir,scales[0])
65 self.set_status('connected','Connected to '+scales[0])
66 return serial.Serial(join(self.input_dir,scales[0]),
68 bytesize = serial.SEVENBITS,
69 stopbits = serial.STOPBITS_ONE,
70 parity = serial.PARITY_EVEN,
71 #xonxoff = serial.XON,
75 self.set_status('disconnected','Scale Not Found')
77 except Exception as e:
78 self.set_status('error',str(e))
85 def get_weight_info(self):
87 return self.weight_info
93 def read_weight(self):
97 self.device.write('W')
102 char = self.device.read(1)
109 stat = ord(answer[answer.index('?')+1])
111 self.weight_info = 'ok'
113 self.weight_info = []
115 self.weight_info.append('moving')
117 self.weight_info.append('over_capacity')
119 self.weight_info.append('negative')
122 self.weight_info.append('outside_zero_capture_range')
124 self.weight_info.append('center_of_zero')
126 self.weight_info.append('net_weight')
128 answer = answer[1:-1]
130 answer = answer[0:-1]
132 self.weight = float(''.join(answer))
133 except ValueError as v:
134 self.set_status('error','No data Received, please power-cycle the scale');
137 except Exception as e:
138 self.set_status('error',str(e))
145 self.device.write('Z')
146 except Exception as e:
147 self.set_status('error',str(e))
154 self.device.write('T')
155 except Exception as e:
156 self.set_status('error',str(e))
159 def clear_tare(self):
163 self.device.write('C')
164 except Exception as e:
165 self.set_status('error',str(e))
177 self.device = self.get_device()
183 hw_proxy.drivers['scale'] = s
185 class ScaleDriver(hw_proxy.Proxy):
186 @http.route('/hw_proxy/scale_read/', type='json', auth='none', cors='*')
187 def scale_read(self):
188 return {'weight':s.get_weight(), 'unit':'kg', 'info':s.get_weight_info()}
190 @http.route('/hw_proxy/scale_zero/', type='json', auth='none', cors='*')
191 def scale_zero(self):
195 @http.route('/hw_proxy/scale_tare/', type='json', auth='none', cors='*')
196 def scale_tare(self):
200 @http.route('/hw_proxy/scale_clear_tare/', type='json', auth='none', cors='*')
201 def scale_clear_tare(self):