|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "image" |
| 8 | + "image/color" |
| 9 | + "image/png" |
| 10 | + "time" |
| 11 | + |
| 12 | + "google.golang.org/grpc" |
| 13 | + "google.golang.org/grpc/credentials/insecure" |
| 14 | + |
| 15 | + device "github.com/clarkzjw/starlink-grpc-golang/pkg/spacex.com/api/device" |
| 16 | + "github.com/pbnjay/pixfont" |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + defaultDishAddress = "192.168.100.1:9200" |
| 21 | + grpcTimeout = 5 * time.Second |
| 22 | + GRPC_ADDR_PORT string |
| 23 | + DURATION string |
| 24 | + DATA_DIR string |
| 25 | + FPS int |
| 26 | +) |
| 27 | + |
| 28 | +type Exporter struct { |
| 29 | + Conn *grpc.ClientConn |
| 30 | + Client device.DeviceClient |
| 31 | + |
| 32 | + DishID string |
| 33 | + CountryCode string |
| 34 | +} |
| 35 | + |
| 36 | +func NewGrpcClient(address string) (*Exporter, error) { |
| 37 | + conn, err := grpc.NewClient(address, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 38 | + if err != nil { |
| 39 | + return nil, fmt.Errorf("connect to Starlink dish gRPC interface failed: %s", err.Error()) |
| 40 | + } |
| 41 | + |
| 42 | + defer func() { |
| 43 | + if err != nil { |
| 44 | + conn.Close() |
| 45 | + } |
| 46 | + }() |
| 47 | + |
| 48 | + ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) |
| 49 | + defer cancel() |
| 50 | + |
| 51 | + client := device.NewDeviceClient(conn) |
| 52 | + resp, err := client.Handle(ctx, &device.Request{ |
| 53 | + Request: &device.Request_GetDeviceInfo{}, |
| 54 | + }) |
| 55 | + if err != nil { |
| 56 | + return nil, fmt.Errorf("gRPC GetDeviceInfo failed: %s", err.Error()) |
| 57 | + } |
| 58 | + |
| 59 | + deviceInfo := resp.GetGetDeviceInfo().GetDeviceInfo() |
| 60 | + if deviceInfo == nil { |
| 61 | + return nil, fmt.Errorf("gRPC GetDeviceInfo failed: deviceInfo is nil") |
| 62 | + } |
| 63 | + |
| 64 | + return &Exporter{ |
| 65 | + Conn: conn, |
| 66 | + Client: client, |
| 67 | + DishID: deviceInfo.GetId(), |
| 68 | + CountryCode: deviceInfo.GetCountryCode(), |
| 69 | + }, nil |
| 70 | +} |
| 71 | + |
| 72 | +func (e *Exporter) CollectDishStatus() *StarlinkGetStatusResponse { |
| 73 | + req := &device.Request{ |
| 74 | + Request: &device.Request_GetStatus{}, |
| 75 | + } |
| 76 | + |
| 77 | + ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) |
| 78 | + defer cancel() |
| 79 | + resp, err := e.Client.Handle(ctx, req) |
| 80 | + if err != nil { |
| 81 | + fmt.Printf("gRPC GetStatus failed: %s", err.Error()) |
| 82 | + return nil |
| 83 | + } |
| 84 | + |
| 85 | + timestamp := time.Now().Format(time.RFC3339) |
| 86 | + |
| 87 | + dishStatus := resp.GetDishGetStatus() |
| 88 | + dishStatusResp := &StarlinkGetStatusResponse{ |
| 89 | + Timestamp: timestamp, |
| 90 | + HardwareVersion: dishStatus.DeviceInfo.GetHardwareVersion(), |
| 91 | + SoftwareVersion: dishStatus.DeviceInfo.GetSoftwareVersion(), |
| 92 | + CountryCode: dishStatus.DeviceInfo.GetCountryCode(), |
| 93 | + BuildID: dishStatus.DeviceInfo.GetBuildId(), |
| 94 | + DeviceUptimeSeconds: dishStatus.DeviceState.GetUptimeS(), |
| 95 | + ObstructionFractionObstructed: dishStatus.ObstructionStats.GetFractionObstructed(), |
| 96 | + ObstructionTimeObstructed: dishStatus.ObstructionStats.GetTimeObstructed(), |
| 97 | + DownlinkThroughputBps: dishStatus.GetDownlinkThroughputBps(), |
| 98 | + UplinkThroughputBps: dishStatus.GetUplinkThroughputBps(), |
| 99 | + PopPingLatencyMs: dishStatus.GetPopPingLatencyMs(), |
| 100 | + PhyRxBeamSnrAvg: dishStatus.GetPhyRxBeamSnrAvg(), |
| 101 | + } |
| 102 | + return dishStatusResp |
| 103 | +} |
| 104 | + |
| 105 | +type StarlinkGetStatusResponse struct { |
| 106 | + Timestamp string |
| 107 | + HardwareVersion string |
| 108 | + SoftwareVersion string |
| 109 | + CountryCode string |
| 110 | + BuildID string |
| 111 | + DeviceUptimeSeconds uint64 |
| 112 | + ObstructionFractionObstructed float32 |
| 113 | + ObstructionTimeObstructed float32 |
| 114 | + DownlinkThroughputBps float32 |
| 115 | + UplinkThroughputBps float32 |
| 116 | + PopPingLatencyMs float32 |
| 117 | + PhyRxBeamSnrAvg float32 |
| 118 | +} |
| 119 | + |
| 120 | +func (e *Exporter) CollectDishObstructionMap() *StarlinkGetObstructionMapResponse { |
| 121 | + req := &device.Request{ |
| 122 | + Request: &device.Request_DishGetObstructionMap{}, |
| 123 | + } |
| 124 | + |
| 125 | + ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout) |
| 126 | + defer cancel() |
| 127 | + resp, err := e.Client.Handle(ctx, req) |
| 128 | + if err != nil { |
| 129 | + fmt.Printf("gRPC GetObstructionMap failed: %s", err.Error()) |
| 130 | + return nil |
| 131 | + } |
| 132 | + |
| 133 | + dishObstructionMap := resp.GetDishGetObstructionMap() |
| 134 | + rows := int(dishObstructionMap.NumRows) |
| 135 | + cols := int(dishObstructionMap.NumCols) |
| 136 | + referenceFrame := dishObstructionMap.GetMapReferenceFrame().String() |
| 137 | + data := dishObstructionMap.Snr |
| 138 | + |
| 139 | + upLeft := image.Point{0, 0} |
| 140 | + lowRight := image.Point{cols * 2, rows * 2} |
| 141 | + |
| 142 | + img := image.NewRGBA(image.Rectangle{upLeft, lowRight}) |
| 143 | + |
| 144 | + for x := 0; x < cols*2; x++ { |
| 145 | + for y := 0; y < rows*2; y++ { |
| 146 | + img.Set(x, y, color.Black) |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + offsetX := cols / 2 |
| 151 | + offsetY := rows / 2 |
| 152 | + |
| 153 | + for x := 0; x < cols; x++ { |
| 154 | + for y := 0; y < rows; y++ { |
| 155 | + snr := data[y*cols+x] |
| 156 | + if snr > 1 { |
| 157 | + snr = 1.0 |
| 158 | + } |
| 159 | + if snr == -1 { |
| 160 | + continue |
| 161 | + } else if snr >= 0 { |
| 162 | + // use the same image color style as in starlink-grpc-tools |
| 163 | + // https://github.com/sparky8512/starlink-grpc-tools/blob/a3860e0a73d0b2280eed92eb8a2a97de0ea5fe43/dish_obstruction_map.py#L59-L87 |
| 164 | + r := 255 |
| 165 | + g := snr * 255 |
| 166 | + b := snr * 255 |
| 167 | + alpha := 255 |
| 168 | + img.Set(x+offsetX, y+offsetY, color.RGBA{uint8(r), uint8(g), uint8(b), uint8(alpha)}) |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + timestamp := time.Now().Format(time.RFC3339) |
| 174 | + pixfont.DrawString(img, 10, 10, timestamp, color.RGBA{255, 255, 255, 255}) |
| 175 | + |
| 176 | + var buf bytes.Buffer |
| 177 | + if err := png.Encode(&buf, img); err != nil { |
| 178 | + fmt.Printf("Failed to encode image: %s", err.Error()) |
| 179 | + return nil |
| 180 | + } |
| 181 | + |
| 182 | + dishObstructionMapResp := &StarlinkGetObstructionMapResponse{ |
| 183 | + Timestamp: timestamp, |
| 184 | + MapReferenceFrame: referenceFrame, |
| 185 | + Rows: rows, |
| 186 | + Cols: cols, |
| 187 | + Data: buf.Bytes(), |
| 188 | + } |
| 189 | + return dishObstructionMapResp |
| 190 | +} |
| 191 | + |
| 192 | +type StarlinkGetObstructionMapResponse struct { |
| 193 | + Timestamp string |
| 194 | + MapReferenceFrame string |
| 195 | + Rows int |
| 196 | + Cols int |
| 197 | + Data []byte |
| 198 | +} |
0 commit comments