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