[IMP] point_of_sale, hw_scale: first commit for the hw_scale module, which handles...
[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
28     def get_status(self):
29         statuses = {}
30         for driver in drivers:
31             statuses[driver] = drivers[driver].get_status()
32         return statuses
33
34     @http.route('/hw_proxy/hello', type='http', auth='none', cors='*')
35     def hello(self):
36         return "ping"
37
38     @http.route('/hw_proxy/handshake', type='json', auth='none', cors='*')
39     def handshake(self):
40         return True
41
42     @http.route('/hw_proxy/status', type='http', auth='none', cors='*')
43     def status_http(self):
44         resp = """
45 <!DOCTYPE HTML>
46 <html>
47     <head>
48         <title>OpenERP's PosBox</title>
49         <style>
50         body {
51             width: 480px;
52             margin: 60px auto;
53             font-family: sans-serif;
54             text-align: justify;
55             color: #6B6B6B;
56         }
57         .device {
58             border-bottom: solid 1px rgb(216,216,216);
59             padding: 9px;
60         }
61         .device:nth-child(2n) {
62             background:rgb(240,240,240);
63         }
64         </style>
65     </head>
66     <body>
67         <h1>Hardware Status</h1>
68         <p>The list of enabled drivers and their status</p>
69 """
70         statuses = self.get_status()
71         for driver in statuses:
72
73             status = statuses[driver]
74
75             if status['status'] == 'connecting':
76                 color = 'black'
77             elif status['status'] == 'connected':
78                 color = 'green'
79             else:
80                 color = 'red'
81
82             resp += "<h3 style='color:"+color+";'>"+driver+' : '+status['status']+"</h3>\n"
83             resp += "<ul>\n"
84             for msg in status['messages']:
85                 resp += '<li>'+msg+'</li>\n'
86             resp += "</ul>\n"
87         resp += """
88             <h2>Connected Devices</h2>
89             <p>The list of connected USB devices as seen by the posbox</p>
90         """
91         devices = commands.getoutput("lsusb").split('\n')
92         resp += "<div class='devices'>\n"
93         for device in devices:
94             device_name = device[device.find('ID')+2:]
95             resp+= "<div class='device' data-device='"+device+"'>"+device_name+"</div>\n"
96         resp += "</div>\n"
97         resp += """
98             <h2>Add New Printer</h2>
99             <p>
100             Copy and paste your printer's device description in the form below. You can find
101             your printer's description in the device list above. If you find that your printer works
102             well, please send your printer's description to <a href='mailto:support@openerp.com'>
103             support@openerp.com</a> so that we can add it to the default list of supported devices.
104             </p>
105             <form action='/hw_proxy/escpos/add_supported_device' method='GET'>
106                 <input type='text' style='width:400px' name='device_string' placeholder='123a:b456 Sample Device description' />
107                 <input type='submit' value='submit' />
108             </form>
109             <h2>Reset To Defaults</h2>
110             <p>If the added devices cause problems, you can <a href='/hw_proxy/escpos/reset_supported_devices'>Reset the
111             device list to factory default.</a> This operation cannot be undone.</p>
112         """
113         resp += "</body>\n</html>\n\n"
114
115         return request.make_response(resp,{
116             'Cache-Control': 'no-cache', 
117             'Content-Type': 'text/html; charset=utf-8',
118             'Access-Control-Allow-Origin':  '*',
119             'Access-Control-Allow-Methods': 'GET',
120             })
121
122     @http.route('/hw_proxy/status_json', type='json', auth='none', cors='*')
123     def status_json(self):
124         return self.get_status()
125
126     @http.route('/hw_proxy/scan_item_success', type='json', auth='none', cors='*')
127     def scan_item_success(self, ean):
128         """
129         A product has been scanned with success
130         """
131         print 'scan_item_success: ' + str(ean)
132
133     @http.route('/hw_proxy/scan_item_error_unrecognized', type='json', auth='none', cors='*')
134     def scan_item_error_unrecognized(self, ean):
135         """
136         A product has been scanned without success
137         """
138         print 'scan_item_error_unrecognized: ' + str(ean)
139
140     @http.route('/hw_proxy/help_needed', type='json', auth='none', cors='*')
141     def help_needed(self):
142         """
143         The user wants an help (ex: light is on)
144         """
145         print "help_needed"
146
147     @http.route('/hw_proxy/help_canceled', type='json', auth='none', cors='*')
148     def help_canceled(self):
149         """
150         The user stops the help request
151         """
152         print "help_canceled"
153
154     @http.route('/hw_proxy/payment_request', type='json', auth='none', cors='*')
155     def payment_request(self, price):
156         """
157         The PoS will activate the method payment 
158         """
159         print "payment_request: price:"+str(price)
160         return 'ok'
161
162     @http.route('/hw_proxy/payment_status', type='json', auth='none', cors='*')
163     def payment_status(self):
164         print "payment_status"
165         return { 'status':'waiting' } 
166
167     @http.route('/hw_proxy/payment_cancel', type='json', auth='none', cors='*')
168     def payment_cancel(self):
169         print "payment_cancel"
170
171     @http.route('/hw_proxy/transaction_start', type='json', auth='none', cors='*')
172     def transaction_start(self):
173         print 'transaction_start'
174
175     @http.route('/hw_proxy/transaction_end', type='json', auth='none', cors='*')
176     def transaction_end(self):
177         print 'transaction_end'
178
179     @http.route('/hw_proxy/cashier_mode_activated', type='json', auth='none', cors='*')
180     def cashier_mode_activated(self):
181         print 'cashier_mode_activated'
182
183     @http.route('/hw_proxy/cashier_mode_deactivated', type='json', auth='none', cors='*')
184     def cashier_mode_deactivated(self):
185         print 'cashier_mode_deactivated'
186
187     @http.route('/hw_proxy/open_cashbox', type='json', auth='none', cors='*')
188     def open_cashbox(self):
189         print 'open_cashbox'
190
191     @http.route('/hw_proxy/print_receipt', type='json', auth='none', cors='*')
192     def print_receipt(self, receipt):
193         print 'print_receipt' + str(receipt)
194
195     @http.route('/hw_proxy/is_scanner_connected', type='json', auth='none', cors='*')
196     def print_receipt(self, receipt):
197         print 'is_scanner_connected?' 
198         return False
199
200     @http.route('/hw_proxy/scanner', type='json', auth='none', cors='*')
201     def print_receipt(self, receipt):
202         print 'scanner' 
203         time.sleep(10)
204         return ''
205
206     @http.route('/hw_proxy/log', type='json', auth='none', cors='*')
207     def log(self, arguments):
208         _logger.info(' '.join(str(v) for v in arguments))
209
210     @http.route('/hw_proxy/print_pdf_invoice', type='json', auth='none', cors='*')
211     def print_pdf_invoice(self, pdfinvoice):
212         print 'print_pdf_invoice' + str(pdfinvoice)
213
214