Wed, 20 Jan 2021 11:37:03 +0100
reimplemented lasercutter changes
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 | ||
46 | 16 | from xmlrpc.server import SimpleXMLRPCServer |
15 | 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 | ||
46 | 26 | class ProntRPC: |
15 | 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') | |
46 | 48 | self.server.register_function(self.set_extruder_temperature,'settemp') |
49 | self.server.register_function(self.set_bed_temperature,'setbedtemp') | |
50 | self.server.register_function(self.load_file,'load_file') | |
51 | self.server.register_function(self.startprint,'startprint') | |
52 | self.server.register_function(self.pauseprint,'pauseprint') | |
53 | self.server.register_function(self.resumeprint,'resumeprint') | |
54 | self.server.register_function(self.sendhome,'sendhome') | |
55 | self.server.register_function(self.connect,'connect') | |
56 | self.server.register_function(self.disconnect, 'disconnect') | |
57 | self.server.register_function(self.send, 'send') | |
15 | 58 | self.thread = Thread(target = self.run_server) |
59 | self.thread.start() | |
60 | ||
61 | def run_server(self): | |
62 | self.server.serve_forever() | |
63 | ||
64 | def shutdown(self): | |
65 | self.server.shutdown() | |
66 | self.thread.join() | |
67 | ||
68 | def get_status(self): | |
69 | if self.pronsole.p.printing: | |
70 | progress = 100 * float(self.pronsole.p.queueindex) / len(self.pronsole.p.mainqueue) | |
71 | elif self.pronsole.sdprinting: | |
72 | progress = self.pronsole.percentdone | |
73 | else: progress = None | |
74 | if self.pronsole.p.printing or self.pronsole.sdprinting: | |
75 | eta = self.pronsole.get_eta() | |
76 | else: | |
77 | eta = None | |
78 | if self.pronsole.tempreadings: | |
79 | temps = parse_temperature_report(self.pronsole.tempreadings) | |
80 | else: | |
81 | temps = None | |
82 | z = self.pronsole.curlayer | |
83 | return {"filename": self.pronsole.filename, | |
84 | "progress": progress, | |
85 | "eta": eta, | |
86 | "temps": temps, | |
87 | "z": z, | |
88 | } | |
46 | 89 | def set_extruder_temperature(self, targettemp): |
90 | if self.pronsole.p.online: | |
91 | self.pronsole.p.send_now("M104 S" + targettemp) | |
92 | ||
93 | def set_bed_temperature(self,targettemp): | |
94 | if self.pronsole.p.online: | |
95 | self.pronsole.p.send_now("M140 S" + targettemp) | |
96 | ||
97 | def load_file(self,filename): | |
98 | self.pronsole.do_load(filename) | |
99 | ||
100 | def startprint(self): | |
101 | self.pronsole.do_print("") | |
102 | ||
103 | def pauseprint(self): | |
104 | self.pronsole.do_pause("") | |
105 | ||
106 | def resumeprint(self): | |
107 | self.pronsole.do_resume("") | |
108 | def sendhome(self): | |
109 | self.pronsole.do_home("") | |
110 | def connect(self): | |
111 | self.pronsole.do_connect("") | |
112 | def disconnect(self): | |
113 | self.pronsole.do_disconnect("") | |
114 | def send(self, command): | |
115 | self.pronsole.p.send_now(command) |