Commit d1bf93ca authored by Aljaž Srebrnič's avatar Aljaž Srebrnič

Moved to procedural style, using Events to sync

parent 7eae48be
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()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment