[IMP] hw_proxy, hw_escpos: it is now possible to add support for new printers from...
[odoo/odoo.git] / addons / hw_proxy / controllers / main.py
1 # -*- coding: utf-8 -*-
2 import logging
3 import commands
4 import simplejson
5 import os
6 import os.path
7 import openerp
8 import time
9 import random
10 import subprocess
11 import simplejson
12 import werkzeug
13 import werkzeug.wrappers
14 _logger = logging.getLogger(__name__)
15
16
17 from openerp import http
18 from openerp.http import request
19 from openerp.addons.web.controllers.main import manifest_list, module_boot, html_template
20
21
22 # drivers modules must add to drivers an object with a get_status() method 
23 # so that 'status' can return the status of all active drivers
24 drivers = {}
25
26 class Proxy(http.Controller):
27     def __init__(self):
28         self.scale = 'closed'
29         self.scale_weight = 0.0
30
31     def get_status(self):
32         statuses = {}
33         for driver in drivers:
34             statuses[driver] = drivers[driver].get_status()
35         return statuses
36
37     @http.route('/hw_proxy/hello', type='http', auth='none', cors='*')
38     def hello(self):
39         return "ping"
40
41     @http.route('/hw_proxy/handshake', type='json', auth='none', cors='*')
42     def handshake(self):
43         return True
44
45     @http.route('/hw_proxy/status', type='http', auth='none', cors='*')
46     def status_http(self):
47         resp = """
48 <!DOCTYPE HTML>
49 <html>
50     <head>
51         <title>OpenERP's PosBox</title>
52         <style>
53         body {
54             width: 480px;
55             margin: 60px auto;
56             font-family: sans-serif;
57             text-align: justify;
58             color: #6B6B6B;
59         }
60         .device {
61             border-bottom: solid 1px rgb(216,216,216);
62             padding: 9px;
63         }
64         .device:nth-child(2n) {
65             background:rgb(240,240,240);
66         }
67         </style>
68     </head>
69     <body>
70         <h1>Hardware Status</h1>
71         <p>The list of enabled drivers and their status</p>
72 """
73         statuses = self.get_status()
74         for driver in statuses:
75
76             status = statuses[driver]
77
78             if status['status'] == 'connecting':
79                 color = 'black'
80             elif status['status'] == 'connected':
81                 color = 'green'
82             else:
83                 color = 'red'
84
85             resp += "<h3 style='color:"+color+";'>"+driver+' : '+status['status']+"</h3>\n"
86             resp += "<ul>\n"
87             for msg in status['messages']:
88                 resp += '<li>'+msg+'</li>\n'
89             resp += "</ul>\n"
90         resp += """
91             <h2>Connected Devices</h2>
92             <p>The list of connected USB devices as seen by the posbox</p>
93         """
94         devices = commands.getoutput("lsusb").split('\n')
95         resp += "<div class='devices'>\n"
96         for device in devices:
97             device_name = device[device.find('ID')+2:]
98             resp+= "<div class='device' data-device='"+device+"'>"+device_name+"</div>\n"
99         resp += "</div>\n"
100         resp += """
101             <h2>Add New Printer</h2>
102             <p>
103             Copy and paste your printer's device description in the form below. You can find
104             your printer's description in the device list above. If you find that your printer works
105             well, please send your printer's description to <a href='mailto:support@openerp.com'>
106             support@openerp.com</a> so that we can add it to the default list of supported devices.
107             </p>
108             <form action='/hw_proxy/escpos/add_supported_device' method='GET'>
109                 <input type='text' style='width:400px' name='device_string' placeholder='123a:b456 Sample Device description' />
110                 <input type='submit' value='submit' />
111             </form>
112             <h2>Reset To Defaults</h2>
113             <p>If the added devices cause problems, you can <a href='/hw_proxy/escpos/reset_supported_devices'>Reset the
114             device list to factory default.</a> This operation cannot be undone.</p>
115         """
116         resp += "</body>\n</html>\n\n"
117
118         return request.make_response(resp,{
119             'Cache-Control': 'no-cache', 
120             'Content-Type': 'text/html; charset=utf-8',
121             'Access-Control-Allow-Origin':  '*',
122             'Access-Control-Allow-Methods': 'GET',
123             })
124
125     @http.route('/hw_proxy/status_json', type='json', auth='none', cors='*')
126     def status_json(self):
127         return self.get_status()
128
129     @http.route('/hw_proxy/scan_item_success', type='json', auth='none', cors='*')
130     def scan_item_success(self, ean):
131         """
132         A product has been scanned with success
133         """
134         print 'scan_item_success: ' + str(ean)
135
136     @http.route('/hw_proxy/scan_item_error_unrecognized', type='json', auth='none', cors='*')
137     def scan_item_error_unrecognized(self, ean):
138         """
139         A product has been scanned without success
140         """
141         print 'scan_item_error_unrecognized: ' + str(ean)
142
143     @http.route('/hw_proxy/help_needed', type='json', auth='none', cors='*')
144     def help_needed(self):
145         """
146         The user wants an help (ex: light is on)
147         """
148         print "help_needed"
149
150     @http.route('/hw_proxy/help_canceled', type='json', auth='none', cors='*')
151     def help_canceled(self):
152         """
153         The user stops the help request
154         """
155         print "help_canceled"
156
157     @http.route('/hw_proxy/weighting_start', type='json', auth='none', cors='*')
158     def weighting_start(self):
159         if self.scale == 'closed':
160             print "Opening (Fake) Connection to Scale..."
161             self.scale = 'open'
162             self.scale_weight = 0.0
163             time.sleep(0.1)
164             print "... Scale Open."
165         else:
166             print "WARNING: Scale already Connected !!!"
167
168     @http.route('/hw_proxy/weighting_read_kg', type='json', auth='none', cors='*')
169     def weighting_read_kg(self):
170         if self.scale == 'open':
171             print "Reading Scale..."
172             time.sleep(0.025)
173             self.scale_weight += 0.01
174             print "... Done."
175             return self.scale_weight
176         else:
177             print "WARNING: Reading closed scale !!!"
178             return 0.0
179
180     @http.route('/hw_proxy/weighting_end', type='json', auth='none', cors='*')
181     def weighting_end(self):
182         if self.scale == 'open':
183             print "Closing Connection to Scale ..."
184             self.scale = 'closed'
185             self.scale_weight = 0.0
186             time.sleep(0.1)
187             print "... Scale Closed."
188         else:
189             print "WARNING: Scale already Closed !!!"
190
191     @http.route('/hw_proxy/payment_request', type='json', auth='none', cors='*')
192     def payment_request(self, price):
193         """
194         The PoS will activate the method payment 
195         """
196         print "payment_request: price:"+str(price)
197         return 'ok'
198
199     @http.route('/hw_proxy/payment_status', type='json', auth='none', cors='*')
200     def payment_status(self):
201         print "payment_status"
202         return { 'status':'waiting' } 
203
204     @http.route('/hw_proxy/payment_cancel', type='json', auth='none', cors='*')
205     def payment_cancel(self):
206         print "payment_cancel"
207
208     @http.route('/hw_proxy/transaction_start', type='json', auth='none', cors='*')
209     def transaction_start(self):
210         print 'transaction_start'
211
212     @http.route('/hw_proxy/transaction_end', type='json', auth='none', cors='*')
213     def transaction_end(self):
214         print 'transaction_end'
215
216     @http.route('/hw_proxy/cashier_mode_activated', type='json', auth='none', cors='*')
217     def cashier_mode_activated(self):
218         print 'cashier_mode_activated'
219
220     @http.route('/hw_proxy/cashier_mode_deactivated', type='json', auth='none', cors='*')
221     def cashier_mode_deactivated(self):
222         print 'cashier_mode_deactivated'
223
224     @http.route('/hw_proxy/open_cashbox', type='json', auth='none', cors='*')
225     def open_cashbox(self):
226         print 'open_cashbox'
227
228     @http.route('/hw_proxy/print_receipt', type='json', auth='none', cors='*')
229     def print_receipt(self, receipt):
230         print 'print_receipt' + str(receipt)
231
232     @http.route('/hw_proxy/is_scanner_connected', type='json', auth='none', cors='*')
233     def print_receipt(self, receipt):
234         print 'is_scanner_connected?' 
235         return False
236
237     @http.route('/hw_proxy/scanner', type='json', auth='none', cors='*')
238     def print_receipt(self, receipt):
239         print 'scanner' 
240         time.sleep(10)
241         return ''
242
243     @http.route('/hw_proxy/log', type='json', auth='none', cors='*')
244     def log(self, arguments):
245         _logger.info(' '.join(str(v) for v in arguments))
246
247     @http.route('/hw_proxy/print_pdf_invoice', type='json', auth='none', cors='*')
248     def print_pdf_invoice(self, pdfinvoice):
249         print 'print_pdf_invoice' + str(pdfinvoice)
250
251