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
10 from bitstring import BitArray
13 import openerp.addons.hw_proxy.controllers.main as hw_proxy
14 from openerp import http
15 from openerp.http import request
16 from openerp.tools.translate import _
18 _logger = logging.getLogger(__name__)
23 _logger.error('OpenERP module hw_scale depends on the pyserial python module')
31 self.scalelock = Lock()
32 self.status = {'status':'connecting', 'messages':[]}
33 self.input_dir = '/dev/serial/by-id/'
35 self.weight_info = 'ok'
38 def lockedstart(self):
40 if not self.isAlive():
44 def set_status(self, status, message = None):
45 if status == self.status['status']:
46 if message != None and message != self.status['messages'][-1]:
47 self.status['messages'].append(message)
49 self.status['status'] = status
51 self.status['messages'] = [message]
53 self.status['messages'] = []
55 if status == 'error' and message:
56 _logger.error('Scale Error: '+message)
57 elif status == 'disconnected' and message:
58 _logger.warning('Disconnected Scale: '+message)
62 devices = [ device for device in listdir(self.input_dir)]
63 scales = [ device for device in devices if ('mettler' in device.lower()) or ('toledo' in device.lower()) ]
65 print join(self.input_dir,scales[0])
66 self.set_status('connected','Connected to '+scales[0])
67 return serial.Serial(join(self.input_dir,scales[0]),
69 bytesize = serial.SEVENBITS,
70 stopbits = serial.STOPBITS_ONE,
71 parity = serial.PARITY_EVEN,
72 #xonxoff = serial.XON,
76 self.set_status('disconnected','Scale Not Found')
78 except Exception as e:
79 self.set_status('error',str(e))
86 def get_weight_info(self):
88 return self.weight_info
94 def read_weight(self):
98 self.device.write('W')
103 char = self.device.read(1)
110 stat = ord(answer[answer.index('?')+1])
112 self.weight_info = 'ok'
114 self.weight_info = []
116 self.weight_info.append('moving')
118 self.weight_info.append('over_capacity')
120 self.weight_info.append('negative')
123 self.weight_info.append('outside_zero_capture_range')
125 self.weight_info.append('center_of_zero')
127 self.weight_info.append('net_weight')
129 answer = answer[1:-1]
131 answer = answer[0:-1]
132 self.weight = float(''.join(answer))
134 except Exception as e:
135 self.set_status('error',str(e))
142 self.device.write('Z')
143 except Exception as e:
144 self.set_status('error',str(e))
151 self.device.write('T')
152 except Exception as e:
153 self.set_status('error',str(e))
156 def clear_tare(self):
160 self.device.write('C')
161 except Exception as e:
162 self.set_status('error',str(e))
174 self.device = self.get_device()
180 hw_proxy.drivers['scale'] = s
182 class ScaleDriver(hw_proxy.Proxy):
183 @http.route('/hw_proxy/scale_read/', type='json', auth='none', cors='*')
184 def scale_read(self):
185 return {'weight':s.get_weight(), 'unit':'kg', 'info':s.get_weight_info()}
187 @http.route('/hw_proxy/scale_zero/', type='json', auth='none', cors='*')
188 def scale_zero(self):
192 @http.route('/hw_proxy/scale_tare/', type='json', auth='none', cors='*')
193 def scale_tare(self):
197 @http.route('/hw_proxy/scale_clear_tare/', type='json', auth='none', cors='*')
198 def scale_clear_tare(self):