3ea2de4424b7b008ebc4b11d8b9cc3c16cfd31d9
[odoo/odoo.git] / addons / hw_proxy / controllers / main.py
1 # -*- coding: utf-8 -*-
2 import logging
3 import simplejson
4 import os
5 import os.path
6 import openerp
7 import time
8 import random
9 import subprocess
10 import simplejson
11 import werkzeug
12 import werkzeug.wrappers
13 _logger = logging.getLogger(__name__)
14
15
16 from openerp import http
17 from openerp.http import request
18 from openerp.addons.web.controllers.main import manifest_list, module_boot, html_template
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     def __init__(self):
27         self.scale = 'closed'
28         self.scale_weight = 0.0
29
30     def get_status(self):
31         statuses = {}
32         for driver in drivers:
33             statuses[driver] = drivers[driver].get_status()
34         return statuses
35
36     @http.route('/hw_proxy/hello', type='http', auth='admin')
37     def hello(self):
38         return request.make_response('ping', {
39             'Cache-Control': 'no-cache', 
40             'Content-Type': 'text/html; charset=utf-8',
41             'Access-Control-Allow-Origin':  '*',
42             'Access-Control-Allow-Methods': 'GET',
43             })
44
45     @http.route('/hw_proxy/handshake', type='json', auth='admin')
46     def handshake(self):
47         return True
48
49     @http.route('/hw_proxy/status', type='http', auth='admin')
50     def status_http(self):
51         resp = '<html>\n<body>\n<h1>Hardware Proxy Status</h1>\n'
52         statuses = self.get_status()
53         for driver in statuses:
54
55             status = statuses[driver]
56
57             if status['status'] == 'connecting':
58                 color = 'black'
59             elif status['status'] == 'connected':
60                 color = 'green'
61             else:
62                 color = 'red'
63
64             resp += "<h2 style='color:"+color+";'>"+driver+' : '+status['status']+"</h2>\n"
65             resp += "<ul>\n"
66             for msg in status['messages']:
67                 resp += '<li>'+msg+'</li>\n'
68             resp += "</ul>\n"
69         resp += "<script>\n\tsetTimeout(function(){window.location.reload();},30000);\n</script>\n</body>\n</html>\n\n"
70
71         return request.make_response(resp,{
72             'Cache-Control': 'no-cache', 
73             'Content-Type': 'text/html; charset=utf-8',
74             'Access-Control-Allow-Origin':  '*',
75             'Access-Control-Allow-Methods': 'GET',
76             })
77
78     @http.route('/hw_proxy/status_json', type='json', auth='admin')
79     def status_json(self):
80         return self.get_status()
81
82     @http.route('/hw_proxy/scan_item_success', type='json', auth='admin')
83     def scan_item_success(self, ean):
84         """
85         A product has been scanned with success
86         """
87         print 'scan_item_success: ' + str(ean)
88
89     @http.route('/hw_proxy/scan_item_error_unrecognized', type='json', auth='admin')
90     def scan_item_error_unrecognized(self, ean):
91         """
92         A product has been scanned without success
93         """
94         print 'scan_item_error_unrecognized: ' + str(ean)
95
96     @http.route('/hw_proxy/help_needed', type='json', auth='admin')
97     def help_needed(self):
98         """
99         The user wants an help (ex: light is on)
100         """
101         print "help_needed"
102
103     @http.route('/hw_proxy/help_canceled', type='json', auth='admin')
104     def help_canceled(self):
105         """
106         The user stops the help request
107         """
108         print "help_canceled"
109
110     @http.route('/hw_proxy/weighting_start', type='json', auth='admin')
111     def weighting_start(self):
112         if self.scale == 'closed':
113             print "Opening (Fake) Connection to Scale..."
114             self.scale = 'open'
115             self.scale_weight = 0.0
116             time.sleep(0.1)
117             print "... Scale Open."
118         else:
119             print "WARNING: Scale already Connected !!!"
120
121     @http.route('/hw_proxy/weighting_read_kg', type='json', auth='admin')
122     def weighting_read_kg(self):
123         if self.scale == 'open':
124             print "Reading Scale..."
125             time.sleep(0.025)
126             self.scale_weight += 0.01
127             print "... Done."
128             return self.scale_weight
129         else:
130             print "WARNING: Reading closed scale !!!"
131             return 0.0
132
133     @http.route('/hw_proxy/weighting_end', type='json', auth='admin')
134     def weighting_end(self):
135         if self.scale == 'open':
136             print "Closing Connection to Scale ..."
137             self.scale = 'closed'
138             self.scale_weight = 0.0
139             time.sleep(0.1)
140             print "... Scale Closed."
141         else:
142             print "WARNING: Scale already Closed !!!"
143
144     @http.route('/hw_proxy/payment_request', type='json', auth='admin')
145     def payment_request(self, price):
146         """
147         The PoS will activate the method payment 
148         """
149         print "payment_request: price:"+str(price)
150         return 'ok'
151
152     @http.route('/hw_proxy/payment_status', type='json', auth='admin')
153     def payment_status(self):
154         print "payment_status"
155         return { 'status':'waiting' } 
156
157     @http.route('/hw_proxy/payment_cancel', type='json', auth='admin')
158     def payment_cancel(self):
159         print "payment_cancel"
160
161     @http.route('/hw_proxy/transaction_start', type='json', auth='admin')
162     def transaction_start(self):
163         print 'transaction_start'
164
165     @http.route('/hw_proxy/transaction_end', type='json', auth='admin')
166     def transaction_end(self):
167         print 'transaction_end'
168
169     @http.route('/hw_proxy/cashier_mode_activated', type='json', auth='admin')
170     def cashier_mode_activated(self):
171         print 'cashier_mode_activated'
172
173     @http.route('/hw_proxy/cashier_mode_deactivated', type='json', auth='admin')
174     def cashier_mode_deactivated(self):
175         print 'cashier_mode_deactivated'
176
177     @http.route('/hw_proxy/open_cashbox', type='json', auth='admin')
178     def open_cashbox(self):
179         print 'open_cashbox'
180
181     @http.route('/hw_proxy/print_receipt', type='json', auth='admin')
182     def print_receipt(self, receipt):
183         print 'print_receipt' + str(receipt)
184
185     @http.route('/hw_proxy/is_scanner_connected', type='json', auth='admin')
186     def print_receipt(self, receipt):
187         print 'is_scanner_connected?' 
188         return False
189
190     @http.route('/hw_proxy/scanner', type='json', auth='admin')
191     def print_receipt(self, receipt):
192         print 'scanner' 
193         time.sleep(10)
194         return ''
195
196     @http.route('/hw_proxy/log', type='json', auth='admin')
197     def log(self, arguments):
198         _logger.info(' '.join(str(v) for v in arguments))
199
200     @http.route('/hw_proxy/print_pdf_invoice', type='json', auth='admin')
201     def print_pdf_invoice(self, pdfinvoice):
202         print 'print_pdf_invoice' + str(pdfinvoice)
203
204