grpc added create session and set session location

This commit is contained in:
bharnden 2019-02-24 07:44:41 -08:00
parent e819b706bc
commit fcff9e4551
3 changed files with 98 additions and 6 deletions

View file

@ -8,11 +8,22 @@ import core_pb2
import core_pb2_grpc
def update_proto(obj, **kwargs):
for key in kwargs:
value = kwargs[key]
if value is not None:
logging.info("setting proto key(%s) value(%s)", key, value)
setattr(obj, key, value)
class CoreApiClient(object):
def __init__(self, address="localhost:50051"):
self.address = address
self.stub = None
def create_session(self):
return self.stub.CreateSession(core_pb2.CreateSessionRequest())
def get_sessions(self):
return self.stub.GetSessions(core_pb2.SessionsRequest())
@ -27,10 +38,17 @@ class CoreApiClient(object):
return self.stub.GetSessionOptions(request)
def get_session_location(self, _id):
request = core_pb2.SessionLocationRequest()
request = core_pb2.GetSessionLocationRequest()
request.id = _id
return self.stub.GetSessionLocation(request)
def set_session_location(self, _id, x=None, y=None , z=None, lat=None, lon=None, alt=None, scale=None):
request = core_pb2.SetSessionLocationRequest()
request.id = _id
update_proto(request.position, x=x, y=y, z=z, lat=lat, lon=lon, alt=alt)
update_proto(request, scale=scale)
return self.stub.SetSessionLocation(request)
@contextmanager
def connect(self):
channel = grpc.insecure_channel(self.address)
@ -44,15 +62,33 @@ class CoreApiClient(object):
def main():
client = CoreApiClient()
with client.connect():
# create session
response = client.create_session()
print("created session: %s" % response)
response = client.get_sessions()
print("core client received: %s" % response)
if len(response.sessions) > 0:
session_data = response.sessions[0]
# set session location
response = client.set_session_location(
session_data.id,
x=0, y=0, z=None,
lat=47.57917, lon=-122.13232, alt=2.0,
scale=150000.0
)
print("set location response: %s" % response)
# get session
session = client.get_session(session_data.id)
print(session)
# get options
print(client.get_session_options(session_data.id))
# get location
print(client.get_session_location(session_data.id))

View file

@ -1,4 +1,4 @@
from core.enumerations import NodeTypes
from core.enumerations import NodeTypes, EventTypes
from concurrent import futures
import time
@ -81,6 +81,27 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
super(CoreApiServer, self).__init__()
self.coreemu = coreemu
def CreateSession(self, request, context):
session = self.coreemu.create_session()
session.set_state(EventTypes.DEFINITION_STATE)
# default set session location
session.location.setrefgeo(47.57917, -122.13232, 2.0)
session.location.refscale = 150000.0
# grpc stream handlers
# session.event_handlers.append(websocket_routes.broadcast_event)
# session.node_handlers.append(websocket_routes.broadcast_node)
# session.config_handlers.append(websocket_routes.broadcast_config)
# session.link_handlers.append(websocket_routes.broadcast_link)
# session.exception_handlers.append(websocket_routes.broadcast_exception)
# session.file_handlers.append(websocket_routes.broadcast_file)
response = core_pb2.CreateSessionResponse()
response.id = session.session_id
response.state = session.state
return response
def GetSessions(self, request, context):
response = core_pb2.SessionsResponse()
for session_id in self.coreemu.sessions:
@ -95,7 +116,7 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
session = self.coreemu.sessions.get(request.id)
x, y, z = session.location.refxyz
lat, lon, alt = session.location.refgeo
response = core_pb2.SessionLocationResponse()
response = core_pb2.GetSessionLocationResponse()
update_proto(
response.position,
x=x,
@ -108,6 +129,15 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
update_proto(response, scale=session.location.refscale)
return response
def SetSessionLocation(self, request, context):
session = self.coreemu.sessions.get(request.id)
session.location.refxyz = (request.position.x, request.position.y, request.position.z)
session.location.setrefgeo(request.position.lat, request.position.lon, request.position.alt)
session.location.refscale = request.scale
return core_pb2.SetSessionLocationResponse()
def GetSessionOptions(self, request, context):
session = self.coreemu.sessions.get(request.id)
config = session.options.get_configs()

View file

@ -3,14 +3,29 @@ syntax = "proto3";
package core;
service CoreApi {
rpc CreateSession (CreateSessionRequest) returns (CreateSessionResponse) {
}
rpc GetSessions (SessionsRequest) returns (SessionsResponse) {
}
rpc GetSession (SessionRequest) returns (SessionResponse) {
}
rpc GetSessionOptions (SessionOptionsRequest) returns (SessionOptionsResponse) {
}
rpc GetSessionLocation (SessionLocationRequest) returns (SessionLocationResponse) {
rpc GetSessionLocation (GetSessionLocationRequest) returns (GetSessionLocationResponse) {
}
rpc SetSessionLocation (SetSessionLocationRequest) returns (SetSessionLocationResponse) {
}
}
// rpc request/response messages
message CreateSessionRequest {
}
message CreateSessionResponse {
int32 id = 1;
int32 state = 2;
}
message SessionsRequest {
@ -38,15 +53,26 @@ message SessionOptionsResponse {
repeated ConfigGroup groups = 1;
}
message SessionLocationRequest {
message GetSessionLocationRequest {
int32 id = 1;
}
message SessionLocationResponse {
message GetSessionLocationResponse {
Position position = 1;
float scale = 2;
}
message SetSessionLocationRequest {
int32 id = 1;
Position position = 2;
float scale = 3;
}
message SetSessionLocationResponse {
}
// data structures for messages below
message ConfigGroup {
string name = 1;
repeated ConfigOption options = 2;