Skip to content

Instantly share code, notes, and snippets.

@iegrsy
Created September 17, 2018 15:16
Show Gist options
  • Save iegrsy/f5afcff744d86e699f828eaabd6edf5e to your computer and use it in GitHub Desktop.
Save iegrsy/f5afcff744d86e699f828eaabd6edf5e to your computer and use it in GitHub Desktop.
dotnetcore app for grpc
// 1- Boş bir konsol uygulaması oluşturma
~$ dotnet new console -o DemoCameraGrpc
// 2- Uygulama dizinine geçiş
~$ cd DemoCameraGrpc
// 3-
~$ dotnet restore
// 4- Grpc araçlarını kurma işlemi
~$ dotnet add package Grpc.Tools
// 5- proto dizini oluşturma
~$ mkdir pb
// 6- proto dosyaları oluşturma. Protolar dökümanın en altında verildiği gibi düzenlenecek
~$ touch pb/camback.proto
~$ touch pb/lmm.proto
// 7- Çıkan grpc class larının dizini
~$ mkdir Camback
// 8- [user] kendi kullanıcı adınız ile değişecek. Kullanmak üzere classlar oluşturulur.
~$ /home/[user]/.nuget/packages/grpc.tools/1.10.0/tools/linux_x64/protoc -I pb --csharp_out Camback --grpc_out Camback pb/camback.proto --plugin=protoc-gen-grpc=/home/[user]/.nuget/packages/grpc.tools/1.10.0/tools/linux_x64/grpc_csharp_plugin
// 9- [user] kendi kullanıcı adınız ile değişecek. Kullanmak üzere classlar oluşturulur.
~$ /home/[user]/.nuget/packages/grpc.tools/1.10.0/tools/linux_x64/protoc -I pb --csharp_out Camback --grpc_out Camback pb/lmm.proto --plugin=protoc-gen-grpc=/home/[user]/.nuget/packages/grpc.tools/1.10.0/tools/linux_x64/grpc_csharp_plugin
// 10- Paketler eklenir
~$ dotnet add package Grpc
~$ dotnet add package Google.Protobuf
// 11- Program.cs dökümanın altındaki gibi değiştirilir.
// 12- Her hangi bir hata oluşmaz ise camera pan tild zoom değerleri gelir.
~$ dotnet restore
~$ dotnet run
================================tree==============================================
.
├── bin
│   └── Debug
│   └── netcoreapp2.0
│   ├── DemoCameraGrpc.deps.json
│   ├── DemoCameraGrpc.dll
│   ├── DemoCameraGrpc.pdb
│   ├── DemoCameraGrpc.runtimeconfig.dev.json
│   └── DemoCameraGrpc.runtimeconfig.json
├── Camback
│   ├── Camback.cs
│   ├── CambackGrpc.cs
│   ├── Lmm.cs
│   └── LmmGrpc.cs
├── DemoCameraGrpc.csproj
├── obj
│   ├── Debug
│   │   └── netcoreapp2.0
│   │   ├── DemoCameraGrpc.AssemblyInfo.cs
│   │   ├── DemoCameraGrpc.AssemblyInfoInputs.cache
│   │   ├── DemoCameraGrpc.csproj.CoreCompileInputs.cache
│   │   ├── DemoCameraGrpc.csproj.FileListAbsolute.txt
│   │   ├── DemoCameraGrpc.dll
│   │   └── DemoCameraGrpc.pdb
│   ├── DemoCameraGrpc.csproj.nuget.cache
│   ├── DemoCameraGrpc.csproj.nuget.g.props
│   ├── DemoCameraGrpc.csproj.nuget.g.targets
│   └── project.assets.json
├── pb
│   ├── camback.proto
│   └── lmm.proto
└── Program.cs
================================camback.proto=====================================
syntax = "proto3";
package camback;
service PTZService {
rpc GetPTZPosInfo (PTZInfoQ) returns (PTZPosInfo) {}
rpc PanLeft(PtzCmdPar) returns (PtzCommandResult) {}
rpc PanRight(PtzCmdPar) returns (PtzCommandResult) {}
rpc TiltUp(PtzCmdPar) returns (PtzCommandResult) {}
rpc TiltDown(PtzCmdPar) returns (PtzCommandResult) {}
rpc PanStop(PtzCmdPar) returns (PtzCommandResult) {}
rpc PanTiltAbs(PtzCmdPar) returns (PtzCommandResult) {}
rpc ZoomIn(PtzCmdPar) returns (PtzCommandResult) {}
rpc ZoomOut(PtzCmdPar) returns (PtzCommandResult) {}
rpc ZoomStop(PtzCmdPar) returns (PtzCommandResult) {}
}
message PtzCmdPar {
int32 pan_speed = 1;
int32 tilt_speed = 2;
int32 zoom_speed = 3;
float pan_abs = 4;
float tilt_abs = 5;
}
message PtzCommandResult {
int32 err = 1;
}
message PTZInfoQ {
int32 dummy = 1;
}
message PTZPosInfo {
int32 pan_pos = 1;
int32 tilt_pos = 2;
int32 zoom_pos = 3;
}
=============================lmm.proto==============================
syntax = "proto3";
package Lmm;
service PipelineService {
rpc GetInfo (GenericQ) returns (PipelinesInfo) {}
rpc GetQueueInfo(QueueInfoQ) returns (QueueInfo) {}
}
message GenericQ {
int32 dummy = 1;
}
message QueueInfoQ {
int32 pipeline = 1;
int32 element = 2;
int32 outqi = 3;
int32 inqi = 4;
}
message PipelinesInfo {
repeated Pipeline pipelines = 1;
}
message Pipeline {
string name = 1;
repeated Element elements = 2;
}
message Element {
string name = 1;
repeated QueueInfo outq = 2;
repeated QueueInfo inq = 3;
}
message QueueInfo {
int32 bufferCount = 1;
float fps = 2;
int64 elapsedSinceLastAdd = 3;
int32 receivedCount = 4;
int32 sentCount = 5;
int64 totalSize = 6;
int32 bitrate = 7;
enum RateLimit {
NONE = 0;
INTERVAL = 1;
BUFFER_COUNT = 2;
TOTAL_SIZE = 3;
}
RateLimit rateLimit = 8;
}
===========================Program.cs=================================
using System;
using Grpc.Core;
using Google.Protobuf;
using Camback;
using Lmm;
namespace DemoCameraGrpc
{
class Program
{
public static Channel channelPTZ;
public static Channel channelLMM;
static void Main(string[] args)
{
Console.WriteLine("Hello Grpc!");
Console.WriteLine("GetPTZPosInfo client");
channelPTZ = new Channel("10.5.20.94:50051", ChannelCredentials.Insecure);
var client = new Camback.PTZService.PTZServiceClient(channelPTZ);
var rest = client.GetPTZPosInfo(new PTZInfoQ());
Console.WriteLine("Get PTZ Info:"+
"\nPanPos: " + rest.PanPos +
"\nTiltPos: " + rest.TiltPos +
"\nZoomPos: " + rest.ZoomPos);
channelPTZ.ShutdownAsync().Wait();
Console.WriteLine("PTZ close.");
Console.WriteLine("LMM client");
channelLMM = new Channel("10.5.177.54:19101", ChannelCredentials.Insecure);
var clientLmm = new Lmm.PipelineService.PipelineServiceClient(channelLMM);
var restLmm = clientLmm.GetInfo(new GenericQ(), null);
for(int i = 0; i < restLmm.Pipelines[0].Elements.Count; i++){
Console.WriteLine("Inq: " + restLmm.Pipelines[0].Elements[i].Inq[0].BufferCount + "\t" +
"Onq: " + restLmm.Pipelines[0].Elements[i].Outq[0].BufferCount);
}
channelLMM.ShutdownAsync().Wait();
Console.WriteLine("Press any key to exit...");
//Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment