[IMP] hw_proxy, hw_escpos: it is now possible to add support for new printers from...
authorFrédéric van der Essen <fva@openerp.com>
Fri, 11 Apr 2014 12:05:10 +0000 (14:05 +0200)
committerFrédéric van der Essen <fva@openerp.com>
Fri, 11 Apr 2014 12:05:10 +0000 (14:05 +0200)
bzr revid: fva@openerp.com-20140411120510-t3a8b3axq1q4wuje

addons/hw_escpos/controllers/main.py
addons/hw_proxy/controllers/main.py

index 938fa06..57fba3a 100644 (file)
@@ -3,6 +3,7 @@ import commands
 import logging
 import simplejson
 import os
+import os.path
 import io
 import base64
 import openerp
@@ -11,6 +12,8 @@ import random
 import math
 import md5
 import openerp.addons.hw_proxy.controllers.main as hw_proxy
+import pickle
+import re
 import subprocess
 import traceback
 from threading import Thread, Lock
@@ -45,10 +48,58 @@ class EscposDriver(Thread):
         self.lock  = Lock()
         self.status = {'status':'connecting', 'messages':[]}
 
+    def supported_devices(self):
+        if not os.path.isfile('escpos_devices.pickle'):
+            return supported_devices.device_list
+        else:
+            try:
+                f = open('escpos_devices.pickle','r')
+                return pickle.load(f)
+                f.close()
+            except Exception as e:
+                self.set_status('error',str(e))
+                return supported_devices.device_list
+
+    def add_supported_device(self,device_string):
+        r = re.compile('[0-9A-Fa-f]{4}:[0-9A-Fa-f]{4}');
+        match = r.search(device_string)
+        if match:
+            match = match.group().split(':')
+            vendor = int(match[0],16)
+            product = int(match[1],16)
+            name = device_string.split('ID')
+            if len(name) >= 2:
+                name = name[1]
+            else:
+                name = name[0]
+            _logger.info('ESC/POS: adding support for device: '+match[0]+':'+match[1]+' '+name)
+            
+            device_list = supported_devices.device_list[:]
+            if os.path.isfile('escpos_devices.pickle'):
+                try:
+                    f = open('escpos_devices.pickle','r')
+                    device_list = pickle.load(f)
+                    f.close()
+                except Exception as e:
+                    self.set_status('error',str(e))
+            device_list.append({
+                'vendor': vendor,
+                'product': product,
+                'name': name,
+            })
+
+            try:
+                f = open('escpos_devices.pickle','w+')
+                f.seek(0)
+                pickle.dump(device_list,f)
+                f.close()
+            except Exception as e:
+                self.set_status('error',str(e))
+
     def connected_usb_devices(self):
         connected = []
         
-        for device in supported_devices.device_list:
+        for device in self.supported_devices():
             if usb.core.find(idVendor=device['vendor'], idProduct=device['product']) != None:
                 connected.append(device)
         return connected
@@ -83,8 +134,9 @@ class EscposDriver(Thread):
         printer.cashdraw(5)
 
     def set_status(self, status, message = None):
+        _logger.info(status+' : '+ (message or 'no message'))
         if status == self.status['status']:
-            if message != None and message != self.status['messages'][-1]:
+            if message != None and (len(self.status['messages']) == 0 or message != self.status['messages'][-1]):
                 self.status['messages'].append(message)
         else:
             self.status['status'] = status
@@ -307,7 +359,22 @@ class EscposProxy(hw_proxy.Proxy):
         driver.push_task('receipt',receipt)
 
     @http.route('/hw_proxy/print_xml_receipt', type='json', auth='none', cors='*')
-    def print_receipt(self, receipt):
+    def print_xml_receipt(self, receipt):
         _logger.info('ESC/POS: PRINT XML RECEIPT') 
         driver.push_task('xml_receipt',receipt)
+
+    @http.route('/hw_proxy/escpos/add_supported_device', type='http', auth='none', cors='*')
+    def add_supported_device(self, device_string):
+        _logger.info('ESC/POS: ADDED NEW DEVICE:'+device_string) 
+        driver.add_supported_device(device_string)
+        return "The device:\n"+device_string+"\n has been added to the list of supported devices.<br/><a href='/hw_proxy/status'>Ok</a>"
+
+    @http.route('/hw_proxy/escpos/reset_supported_devices', type='http', auth='none', cors='*')
+    def reset_supported_devices(self):
+        try:
+            os.remove('escpos_devices.pickle')
+        except Exception as e:
+            pass
+        return 'The list of supported devices has been reset to factory defaults.<br/><a href="/hw_proxy/status">Ok</a>'
+
     
index 9e94e48..ea9b45c 100644 (file)
@@ -97,6 +97,22 @@ class Proxy(http.Controller):
             device_name = device[device.find('ID')+2:]
             resp+= "<div class='device' data-device='"+device+"'>"+device_name+"</div>\n"
         resp += "</div>\n"
+        resp += """
+            <h2>Add New Printer</h2>
+            <p>
+            Copy and paste your printer's device description in the form below. You can find
+            your printer's description in the device list above. If you find that your printer works
+            well, please send your printer's description to <a href='mailto:support@openerp.com'>
+            support@openerp.com</a> so that we can add it to the default list of supported devices.
+            </p>
+            <form action='/hw_proxy/escpos/add_supported_device' method='GET'>
+                <input type='text' style='width:400px' name='device_string' placeholder='123a:b456 Sample Device description' />
+                <input type='submit' value='submit' />
+            </form>
+            <h2>Reset To Defaults</h2>
+            <p>If the added devices cause problems, you can <a href='/hw_proxy/escpos/reset_supported_devices'>Reset the
+            device list to factory default.</a> This operation cannot be undone.</p>
+        """
         resp += "</body>\n</html>\n\n"
 
         return request.make_response(resp,{