printrun-src/printrun/rpc.py

Wed, 20 Jan 2021 11:37:03 +0100

author
mdd
date
Wed, 20 Jan 2021 11:37:03 +0100
changeset 48
3c27b4ee6fec
parent 46
cce0af6351f0
permissions
-rw-r--r--

reimplemented lasercutter changes

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

mercurial