Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
T
test-project-please-ignore
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
0
Merge Requests
0
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Incidents
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Progetti
test-project-please-ignore
Commits
d1bf93ca
Commit
d1bf93ca
authored
Oct 21, 2018
by
Aljaž Srebrnič
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved to procedural style, using Events to sync
parent
7eae48be
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
32 deletions
+51
-32
ksp_client.py
ksp_client.py
+51
-32
No files found.
ksp_client.py
View file @
d1bf93ca
import
krpc
import
zmq
import
argparse
from
threading
import
Lock
,
Event
class
ModelRocket
:
socket
=
None
realRocket
=
None
def
main
():
parser
=
argparse
.
ArgumentParser
(
description
=
''
)
parser
.
add_argument
(
'addr'
,
type
=
str
,
default
=
"localhost:5556"
,
nargs
=
"?"
)
args
=
parser
.
parse_args
()
host
=
"*"
port
=
"5556"
topic
=
"events"
states
=
{
-
1
:
"abort"
,
0
:
"pre_launch"
,
1
:
"liftoff"
,
2
:
"stage_1_separation"
,
3
:
"stage_2_separation"
}
stage_map
=
{
0
:
0
,
1
:
1
,
2
:
2
,
3
:
3
}
def
send
(
self
,
stage
):
self
.
socket
.
send
(
"%d %d"
%
(
self
.
topic
,
self
.
stage_map
[
stage
]))
def
__init__
(
self
,
addr
=
"localhost:5556"
):
context
=
zmq
.
Context
()
self
.
socket
=
context
.
socket
(
zmq
.
PUB
)
self
.
socket
.
bind
(
"tcp://%s"
%
addr
)
states
=
{
-
1
:
"abort"
,
0
:
"pre_launch"
,
1
:
"liftoff"
,
2
:
"stage_1_separation"
,
3
:
"stage_2_separation"
}
stage_map
=
dict
([(
i
,
i
)
for
i
in
range
(
10
)])
context
=
zmq
.
Context
()
socket
=
context
.
socket
(
zmq
.
PUB
)
socket
.
bind
(
f
"tcp://
{
host
}
:
{
port
}
"
)
conn
=
krpc
.
connect
()
running
=
Event
()
stopping
=
Event
()
if
__name__
==
"__main__"
:
parser
=
argparse
.
ArgumentParser
(
description
=
''
)
parser
.
add_argument
(
'addr'
,
type
=
str
,
default
=
"localhost:5556"
,
nargs
=
"?"
)
args
=
parser
.
parse_args
()
def
unlock
(
scene
):
print
(
f
"In GameScene
{
scene
.
name
}
"
)
if
(
scene
==
conn
.
krpc
.
GameScene
.
flight
):
running
.
set
()
stopping
.
clear
()
else
:
running
.
clear
()
stopping
.
set
()
conn
=
krpc
.
connect
()
vessel
=
conn
.
space_center
.
active_vessel
control
=
vessel
.
control
rocket
=
ModelRocket
(
args
.
addr
)
def
stage_changed
(
new_stage
):
try
:
socket
.
send_string
(
f
"
{
topic
}
{
stage_map
[
new_stage
]
}
"
)
print
(
f
"Sending event
{
topic
}
{
new_stage
}
"
)
except
Exception
as
e
:
pass
scene
=
conn
.
add_stream
(
getattr
,
conn
.
krpc
,
'current_game_scene'
)
scene
.
add_callback
(
unlock
)
scene
.
start
()
while
True
:
running
.
wait
()
running
.
clear
()
vessel
=
conn
.
space_center
.
active_vessel
control
=
vessel
.
control
stage
=
conn
.
add_stream
(
getattr
,
control
,
'current_stage'
,
scene
)
stage
=
conn
.
add_stream
(
getattr
,
control
,
'current_stage'
)
stage
.
add_callback
(
rocket
.
send
)
stage
.
add_callback
(
stage_changed
)
stage
.
start
(
)
def
activate_callback
(
scene
):
if
(
scene
==
conn
.
krpc
.
GameScene
.
flight
):
stage
.
start
(
)
else
:
stage
.
wait
()
stopping
.
wait
()
stopping
.
clear
()
print
(
"Removing callback..."
)
stage
.
remove_callback
(
stage_changed
)
stage
.
remove
()
scene
=
conn
.
add_stream
(
getattr
,
conn
.
krpc
,
'current_game_scene'
)
scene
.
add_callback
(
activate_callback
)
while
42
:
pass
if
__name__
==
"__main__"
:
main
()
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