b741a7a9102ad98525c158a0672b0854b613dd96
[odoo/odoo.git] / addons / hw_posbox_upgrade / controllers / main.py
1 # -*- coding: utf-8 -*-
2 import logging
3 import os
4 import time
5
6 import openerp
7 import openerp.addons.hw_proxy.controllers.main as hw_proxy
8 import threading
9 from openerp import http
10 from openerp.http import request
11 from openerp.tools.translate import _
12
13 _logger = logging.getLogger(__name__)
14
15 upgrade_template = """
16 <!DOCTYPE HTML>
17 <html>
18     <head>
19         <title>OpenERP's PosBox - Software Upgrade</title>
20         <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
21         <script>
22         $(function(){
23             var upgrading = false;
24             $('#upgrade').click(function(){
25                 console.log('click');
26                 if(!upgrading){
27                     upgrading = true;
28                     $('#upgrade').text('Upgrading, Please Wait');
29                     $.ajax({
30                         url:'/hw_proxy/perform_upgrade/'
31                     }).then(function(status){
32                         $('#upgrade').html('Upgrade Successful<br \\>Click to Restart the PosBox');
33                         $('#upgrade').off('click');
34                         $('#upgrade').click(function(){
35                             $.ajax({ url:'hw_proxy/perform_restart' })
36                             $('#upgrade').text('Restarting');
37                             $('#upgrade').off('click');
38                             setTimeout(function(){
39                                 window.location = '/'
40                             },30*1000);
41                         });
42
43                     },function(){
44                         $('#upgrade').text('Upgrade Failed');
45                     });
46                 }
47             });
48         });
49         </script>
50         <style>
51         body {
52             width: 480px;
53             margin: 60px auto;
54             font-family: sans-serif;
55             text-align: justify;
56             color: #6B6B6B;
57         }
58         .centering{
59             text-align: center;
60         }
61         #upgrade {
62             padding: 20px;
63             background: rgb(121, 197, 107);
64             color: white;
65             border-radius: 3px;
66             text-align: center;
67             margin: 30px; 
68             text-decoration: none;
69             display: inline-block;
70         }
71         </style>
72     </head>
73     <body>
74         <h1>PosBox Software Upgrade</h1>
75         <p>
76         This tool will help you perform an upgrade of the PosBox's software.
77         However the preferred method to upgrade the posbox is to flash the sd-card with
78         the <a href='http://nightly.openerp.com/trunk/posbox/'>latest image</a>. The upgrade
79         procedure is explained into to the <a href='/hw_proxy/static/doc/manual.pdf'>PosBox manual</a>
80         </p>
81         <p>
82         To upgrade the posbox, click on the upgrade button. The upgrade will take a few minutes. <b>Do not reboot</b> the PosBox during the upgrade.
83         </p>
84         <div class='centering'>
85             <a href='#' id='upgrade'>Upgrade</a>
86         </div>
87     </body>
88 </html>
89
90 """
91
92 class PosboxUpgrader(hw_proxy.Proxy):
93     def __init__(self):
94         super(PosboxUpgrader,self).__init__()
95         self.upgrading = threading.Lock()
96         self.last_upgrade = 0
97
98     @http.route('/hw_proxy/upgrade', type='http', auth='none', )
99     def upgrade(self):
100         return upgrade_template 
101     
102     @http.route('/hw_proxy/perform_upgrade', type='http', auth='none')
103     def perform_upgrade(self):
104         self.upgrading.acquire()
105         if time.time() - self.last_upgrade < 30:
106             self.upgrading.release()
107             return 'UPTODATE'
108         else:
109             os.system('/bin/bash /home/pi/openerp/update.sh')
110             self.last_upgrade = time.time()
111             self.upgrading.release()
112             return 'SUCCESS'
113
114     @http.route('/hw_proxy/perform_restart', type='http', auth='none')
115     def perform_restart(self):
116         self.upgrading.acquire()
117         if time.time() - self.last_upgrade < 30:
118             self.upgrading.release()
119             return 'RESTARTED'
120         else:
121             os.system('/bin/bash /home/pi/openerp/restart.sh')
122             self.last_upgrade = time.time()
123             self.upgrading.release()
124             return 'SUCCESS'
125
126