Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
Printrun
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
machinery
Printrun
Commits
7019d781
Commit
7019d781
authored
Apr 19, 2013
by
D1plo1d
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring pronserve for better code reuse
parent
f01e5747
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
65 deletions
+53
-65
pronserve.py
pronserve.py
+53
-65
No files found.
pronserve.py
View file @
7019d781
...
@@ -111,6 +111,20 @@ class InspectHandler(tornado.web.RequestHandler):
...
@@ -111,6 +111,20 @@ class InspectHandler(tornado.web.RequestHandler):
#class EchoWebSocketHandler(tornado.web.RequestHandler):
#class EchoWebSocketHandler(tornado.web.RequestHandler):
class
ConstructSocketHandler
(
tornado
.
websocket
.
WebSocketHandler
):
class
ConstructSocketHandler
(
tornado
.
websocket
.
WebSocketHandler
):
def
on_sensor_changed
(
self
):
print
"sensor change"
for
name
in
[
'bed'
,
'extruder'
]:
self
.
send
(
sensor_changed
=
{
'name'
:
name
,
'value'
:
pronserve
.
sensors
[
name
]},
)
def
on_uncaught_event
(
self
,
event_name
,
data
):
listener
=
"on_
%
s"
%
event_name
if
event_name
[:
4
]
==
'job_'
:
data
=
pronserve
.
jobs
.
sanitize
(
data
)
self
.
send
({
event_name
:
data
})
def
_execute
(
self
,
transforms
,
*
args
,
**
kwargs
):
def
_execute
(
self
,
transforms
,
*
args
,
**
kwargs
):
if
socket_auth
(
self
):
if
socket_auth
(
self
):
super
(
ConstructSocketHandler
,
self
)
.
_execute
(
transforms
,
*
args
,
**
kwargs
)
super
(
ConstructSocketHandler
,
self
)
.
_execute
(
transforms
,
*
args
,
**
kwargs
)
...
@@ -118,54 +132,14 @@ class ConstructSocketHandler(tornado.websocket.WebSocketHandler):
...
@@ -118,54 +132,14 @@ class ConstructSocketHandler(tornado.websocket.WebSocketHandler):
self
.
stream
.
close
();
self
.
stream
.
close
();
def
open
(
self
):
def
open
(
self
):
pronserve
.
client
s
.
add
(
self
)
pronserve
.
listener
s
.
add
(
self
)
self
.
write_message
({
'connected'
:
{
'jobs'
:
pronserve
.
jobs
.
public_list
()}})
self
.
write_message
({
'connected'
:
{
'jobs'
:
pronserve
.
jobs
.
public_list
()}})
print
"WebSocket opened.
%
i sockets currently open."
%
len
(
pronserve
.
client
s
)
print
"WebSocket opened.
%
i sockets currently open."
%
len
(
pronserve
.
listener
s
)
def
on_sensor_changed
(
self
):
def
send
(
self
,
dict_args
=
{},
**
kwargs
):
print
"sensor change"
args
=
dict
(
dict_args
.
items
()
+
kwargs
.
items
())
self
.
write_message
({
args
[
'timestamp'
]
=
time
.
time
()
'sensor_changed'
:
{
'name'
:
'bed'
,
'value'
:
pronserve
.
sensors
[
'bed'
]},
self
.
write_message
(
args
)
'timestamp'
:
time
.
time
()
})
self
.
write_message
({
'sensor_changed'
:
{
'name'
:
'extruder'
,
'value'
:
pronserve
.
sensors
[
'extruder'
]},
'timestamp'
:
time
.
time
()
})
def
on_job_progress_changed
(
self
,
progress
):
self
.
write_message
({
'job_progress_changed'
:
progress
,
'timestamp'
:
time
.
time
()
})
def
on_job_started
(
self
,
job
):
self
.
write_message
({
'job_started'
:
job
,
'timestamp'
:
time
.
time
()
})
def
on_job_finished
(
self
,
job
):
self
.
write_message
({
'job_finished'
:
job
,
'timestamp'
:
time
.
time
()
})
def
on_job_added
(
self
,
job
):
self
.
write_message
({
'job_added'
:
pronserve
.
jobs
.
sanitize
(
job
)})
def
on_job_removed
(
self
,
job
):
self
.
write_message
({
'job_removed'
:
{
"id"
:
job
[
"id"
]
}})
def
on_job_updated
(
self
,
job
):
print
(
"what the hell?"
)
pprint
(
pronserve
.
jobs
.
sanitize
(
job
))
self
.
write_message
({
'job_updated'
:
pronserve
.
jobs
.
sanitize
(
job
)})
def
on_pronsole_log
(
self
,
msg
):
self
.
write_message
({
'log'
:
{
'msg'
:
msg
,
'level'
:
"debug"
}})
def
on_message
(
self
,
msg
):
def
on_message
(
self
,
msg
):
print
"message received:
%
s"
%
(
msg
)
print
"message received:
%
s"
%
(
msg
)
...
@@ -173,8 +147,8 @@ class ConstructSocketHandler(tornado.websocket.WebSocketHandler):
...
@@ -173,8 +147,8 @@ class ConstructSocketHandler(tornado.websocket.WebSocketHandler):
# self.write_message("You said: " + msg)
# self.write_message("You said: " + msg)
def
on_close
(
self
):
def
on_close
(
self
):
pronserve
.
client
s
.
remove
(
self
)
pronserve
.
listener
s
.
remove
(
self
)
print
"WebSocket closed.
%
i sockets currently open."
%
len
(
pronserve
.
client
s
)
print
"WebSocket closed.
%
i sockets currently open."
%
len
(
pronserve
.
listener
s
)
dir
=
os
.
path
.
dirname
(
__file__
)
dir
=
os
.
path
.
dirname
(
__file__
)
settings
=
dict
(
settings
=
dict
(
...
@@ -195,17 +169,39 @@ application = tornado.web.Application([
...
@@ -195,17 +169,39 @@ application = tornado.web.Application([
],
**
settings
)
],
**
settings
)
# Event Emitter
# -------------------------------------------------
class
EventEmitter
(
object
):
def
__init__
(
self
):
self
.
listeners
=
set
()
def
fire
(
self
,
event_name
,
content
=
None
):
for
listener
in
self
.
listeners
:
event_name
=
"on_"
+
event_name
if
hasattr
(
listener
,
event_name
):
callback
=
getattr
(
listener
,
event_name
)
elif
hasattr
(
listener
,
"on_uncaught_event"
):
callback
=
listener
.
on_uncaught_event
else
:
continue
if
content
==
None
:
callback
()
else
:
callback
(
content
)
# Pronserve: Server-specific functionality
# Pronserve: Server-specific functionality
# -------------------------------------------------
# -------------------------------------------------
class
Pronserve
(
pronsole
.
pronsole
):
class
Pronserve
(
pronsole
.
pronsole
,
EventEmitter
):
def
__init__
(
self
):
def
__init__
(
self
):
pronsole
.
pronsole
.
__init__
(
self
)
pronsole
.
pronsole
.
__init__
(
self
)
EventEmitter
.
__init__
(
self
)
self
.
settings
.
sensor_names
=
{
'T'
:
'extruder'
,
'B'
:
'bed'
}
self
.
settings
.
sensor_names
=
{
'T'
:
'extruder'
,
'B'
:
'bed'
}
self
.
stdout
=
sys
.
stdout
self
.
stdout
=
sys
.
stdout
self
.
ioloop
=
tornado
.
ioloop
.
IOLoop
.
instance
()
self
.
ioloop
=
tornado
.
ioloop
.
IOLoop
.
instance
()
self
.
clients
=
set
()
self
.
settings
.
sensor_poll_rate
=
1
# seconds
self
.
settings
.
sensor_poll_rate
=
1
# seconds
self
.
sensors
=
{
'extruder'
:
-
1
,
'bed'
:
-
1
}
self
.
sensors
=
{
'extruder'
:
-
1
,
'bed'
:
-
1
}
self
.
load_default_rc
()
self
.
load_default_rc
()
...
@@ -216,7 +212,7 @@ class Pronserve(pronsole.pronsole):
...
@@ -216,7 +212,7 @@ class Pronserve(pronsole.pronsole):
self
.
previous_job_progress
=
0
self
.
previous_job_progress
=
0
services
=
({
'type'
:
'_construct._tcp'
,
'port'
:
8888
,
'domain'
:
"local."
})
services
=
({
'type'
:
'_construct._tcp'
,
'port'
:
8888
,
'domain'
:
"local."
})
self
.
mdns
=
mdns
.
publisher
()
.
save_group
({
'name'
:
'pronserve'
,
'services'
:
services
})
self
.
mdns
=
mdns
.
publisher
()
.
save_group
({
'name'
:
'pronserve'
,
'services'
:
services
})
self
.
jobs
.
listeners
.
a
ppen
d
(
self
)
self
.
jobs
.
listeners
.
a
d
d
(
self
)
def
do_print
(
self
):
def
do_print
(
self
):
if
self
.
p
.
online
:
if
self
.
p
.
online
:
...
@@ -264,7 +260,7 @@ class Pronserve(pronsole.pronsole):
...
@@ -264,7 +260,7 @@ class Pronserve(pronsole.pronsole):
def
recvcb
(
self
,
l
):
def
recvcb
(
self
,
l
):
""" Parses a line of output from the printer via printcore """
""" Parses a line of output from the printer via printcore """
l
=
l
.
rstrip
()
l
=
l
.
rstrip
()
print
l
#
print l
if
"T:"
in
l
:
if
"T:"
in
l
:
self
.
_receive_sensor_update
(
l
)
self
.
_receive_sensor_update
(
l
)
if
l
!=
"ok"
and
not
l
.
startswith
(
"ok T"
)
and
not
l
.
startswith
(
"T:"
):
if
l
!=
"ok"
and
not
l
.
startswith
(
"ok T"
)
and
not
l
.
startswith
(
"T:"
):
...
@@ -295,31 +291,24 @@ class Pronserve(pronsole.pronsole):
...
@@ -295,31 +291,24 @@ class Pronserve(pronsole.pronsole):
sensor_name
=
self
.
settings
.
sensor_names
[
key
]
sensor_name
=
self
.
settings
.
sensor_names
[
key
]
self
.
sensors
[
sensor_name
]
=
float
(
value
)
self
.
sensors
[
sensor_name
]
=
float
(
value
)
def
receive_event
(
self
,
src
,
event_name
,
content
=
None
):
def
on_uncaught_event
(
self
,
event_name
,
content
=
None
):
self
.
fire
(
event_name
,
content
)
self
.
fire
(
event_name
,
content
)
def
fire
(
self
,
event_name
,
content
=
None
):
for
client
in
self
.
clients
:
if
content
==
None
:
getattr
(
client
,
"on_"
+
event_name
)()
else
:
getattr
(
client
,
"on_"
+
event_name
)(
content
)
def
log
(
self
,
*
msg
):
def
log
(
self
,
*
msg
):
msg
=
''
.
join
(
str
(
i
)
for
i
in
msg
)
msg
=
''
.
join
(
str
(
i
)
for
i
in
msg
)
print
msg
print
msg
self
.
fire
(
"
pronsole_log"
,
msg
)
self
.
fire
(
"
log"
,
{
'msg'
:
msg
,
'level'
:
"debug"
}
)
def
write_prompt
(
self
):
def
write_prompt
(
self
):
None
None
class
PrintJobQueue
():
class
PrintJobQueue
(
EventEmitter
):
def
__init__
(
self
):
def
__init__
(
self
):
super
(
PrintJobQueue
,
self
)
.
__init__
()
self
.
list
=
deque
([])
self
.
list
=
deque
([])
self
.
__last_id
=
0
self
.
__last_id
=
0
self
.
listeners
=
[]
def
public_list
(
self
):
def
public_list
(
self
):
# A sanitized version of list for public consumption via construct
# A sanitized version of list for public consumption via construct
...
@@ -384,8 +373,7 @@ class PrintJobQueue():
...
@@ -384,8 +373,7 @@ class PrintJobQueue():
def
fire
(
self
,
event_name
,
content
):
def
fire
(
self
,
event_name
,
content
):
self
.
display_summary
()
self
.
display_summary
()
for
listener
in
self
.
listeners
:
super
(
PrintJobQueue
,
self
)
.
fire
(
event_name
,
content
)
listener
.
receive_event
(
self
,
event_name
,
content
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment