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