printrun-src/printrun/rpc.py

Sat, 04 Jun 2016 09:22:51 +0200

author
mbayer
date
Sat, 04 Jun 2016 09:22:51 +0200
changeset 20
03b34402d405
parent 15
0bbb006204fc
child 46
cce0af6351f0
permissions
-rw-r--r--

Code cleanup

15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1 # This file is part of the Printrun suite.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
3 # Printrun is free software: you can redistribute it and/or modify
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
4 # it under the terms of the GNU General Public License as published by
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
5 # the Free Software Foundation, either version 3 of the License, or
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
6 # (at your option) any later version.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
7 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
8 # Printrun is distributed in the hope that it will be useful,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
11 # GNU General Public License for more details.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
12 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
13 # You should have received a copy of the GNU General Public License
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
14 # along with Printrun. If not, see <http://www.gnu.org/licenses/>.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
16 from SimpleXMLRPCServer import SimpleXMLRPCServer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
17 from threading import Thread
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
18 import socket
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19 import logging
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21 from .utils import install_locale, parse_temperature_report
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22 install_locale('pronterface')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24 RPC_PORT = 7978
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26 class ProntRPC(object):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28 server = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 def __init__(self, pronsole, port = RPC_PORT):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 self.pronsole = pronsole
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32 used_port = port
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 while True:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35 self.server = SimpleXMLRPCServer(("localhost", used_port),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 allow_none = True,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 logRequests = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 if used_port != port:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 logging.warning(_("RPC server bound on non-default port %d") % used_port)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40 break
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41 except socket.error as e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 if e.errno == 98:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43 used_port += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44 continue
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46 raise
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47 self.server.register_function(self.get_status, 'status')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48 self.thread = Thread(target = self.run_server)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49 self.thread.start()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51 def run_server(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 self.server.serve_forever()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 def shutdown(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 self.server.shutdown()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 self.thread.join()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 def get_status(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 if self.pronsole.p.printing:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60 progress = 100 * float(self.pronsole.p.queueindex) / len(self.pronsole.p.mainqueue)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61 elif self.pronsole.sdprinting:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 progress = self.pronsole.percentdone
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 else: progress = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64 if self.pronsole.p.printing or self.pronsole.sdprinting:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 eta = self.pronsole.get_eta()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67 eta = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 if self.pronsole.tempreadings:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69 temps = parse_temperature_report(self.pronsole.tempreadings)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 temps = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72 z = self.pronsole.curlayer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 return {"filename": self.pronsole.filename,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74 "progress": progress,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75 "eta": eta,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76 "temps": temps,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 "z": z,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 }

mercurial