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