refactor: replace GetRandom with IoT sensing data update scenario
This commit is contained in:
@@ -50,11 +50,11 @@ func getString(len int64) string {
|
||||
return temp
|
||||
}
|
||||
|
||||
type RandomServer struct {
|
||||
protoapi.UnimplementedRandomServer
|
||||
type IoTServer struct {
|
||||
protoapi.UnimplementedIoTServiceServer
|
||||
}
|
||||
|
||||
func (RandomServer) GetDate(ctx context.Context, r *protoapi.RequestDateTime) (*protoapi.DateTime, error) {
|
||||
func (IoTServer) GetDate(ctx context.Context, r *protoapi.RequestDateTime) (*protoapi.DateTime, error) {
|
||||
currentTime := time.Now()
|
||||
response := &protoapi.DateTime{
|
||||
Value: currentTime.String(),
|
||||
@@ -63,26 +63,18 @@ func (RandomServer) GetDate(ctx context.Context, r *protoapi.RequestDateTime) (*
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (RandomServer) GetRandom(ctx context.Context, r *protoapi.RandomParams) (*protoapi.RandomInt, error) {
|
||||
src := rand.NewSource(r.GetSeed())
|
||||
place := r.GetPlace()
|
||||
temp := random(min, max, src)
|
||||
for {
|
||||
place--
|
||||
if place <= 0 {
|
||||
break
|
||||
}
|
||||
temp = random(min, max, src)
|
||||
}
|
||||
func (IoTServer) UpdateSensingData(ctx context.Context, r *protoapi.SensingData) (*protoapi.SensingResponse, error) {
|
||||
fmt.Printf("Received sensing data - Device: %s, Temp: %.2f°C, Humid: %.2f%%\n", r.GetDeviceId(), r.GetTemperature(), r.GetHumidity())
|
||||
|
||||
response := &protoapi.RandomInt{
|
||||
Value: int64(temp),
|
||||
response := &protoapi.SensingResponse{
|
||||
Success: true,
|
||||
Message: fmt.Sprintf("Sensing data updated successfully for device %s", r.GetDeviceId()),
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (RandomServer) GetRandomPass(ctx context.Context, r *protoapi.RequestPass) (*protoapi.RandomPass, error) {
|
||||
func (IoTServer) GetRandomPass(ctx context.Context, r *protoapi.RequestPass) (*protoapi.RandomPass, error) {
|
||||
rand.Seed(r.GetSeed())
|
||||
temp := getString(r.GetLength())
|
||||
|
||||
@@ -95,8 +87,8 @@ func (RandomServer) GetRandomPass(ctx context.Context, r *protoapi.RequestPass)
|
||||
|
||||
func ServerRun(addr string) {
|
||||
server := grpc.NewServer()
|
||||
var randomServer RandomServer
|
||||
protoapi.RegisterRandomServer(server, randomServer)
|
||||
var iotServer IoTServer
|
||||
protoapi.RegisterIoTServiceServer(server, iotServer)
|
||||
|
||||
reflection.Register(server)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user