I have go routines called in getPosition, getStatus and rerouting for my drone currently I am invoking go GetStatus in my main function which this function has go func() which handles the event for streaming grpc and sse.
currently this is my code and i tried
func GetPositionContext(ctx context.Context, uav pb.UAVControllerClient, uavID *pb.UAVID, projectID string) {
log.Printf("getPosition start")
stream, err := uav.GetPosition(ctx)
if err != nil {
fmt.Printf("ERROR getPosition:%s", err.Error())
}
streamID, eventName := EventsSubscribe(projectID, uavID.Aircraft, "get_position")
position := make(chan models.DronePosition)
// 受信ループ開始
go func() {
fmt.Print("start getPosition loop")
for {
msg, err := stream.Recv() // msg UAVPosition
if err == io.EOF {
// read done.
fmt.Print("start getPosition loop closed")
close(position)
return
}
if err != nil {
log.Fatalf("Failed to receive getPosition : %v", err)
close(position)
return
}
// log.Printf("Position point[%s](%f, %f, %f) H:%f", uavID.Aircraft, msg.Latitude, msg.Longitude, msg.Altitude, msg.Heading)
wayPoint := models.WaypointItem{
Latitude: msg.Latitude,
Longitude: msg.Longitude,
Altitude: msg.Altitude,
Heading: msg.Heading,
}
dronePosition := models.DronePosition{
Name: uavID.Aircraft,
ItemParameter: wayPoint,
}
// publish to eventgo
publishNotif(dronePosition, streamID, eventName)
return
}
}()
startMsg := pb.UAVControllerPositionRequest{
UavID: uavID,
Instruction: true,
Interval: 2,
}
fmt.Print("send getPosition start")
if err := stream.Send(&startMsg); err != nil {
log.Fatalf("Failed to send getPosition: %v", err)
}
<-position
stream.CloseSend()
fmt.Print("end of getPosition")
}
this is the part where i call this function
go utils.GetPosition(ctx, uavService, &uavID, projectID)
I want to get the return values from go func like if everything works fine in the grpc server and no problem i should return 200 success and 500 if something failed.
for {
time.Sleep(time.Duration(60) * time.Second)
}
after the call i have this code which it should return every 10s a success
return c.JSON(500, failed or pass)
I expect something to return to the ui if go routines is success or failed when streaming part is working for the response not to be pending or else other api call will not work.