Реализация device-control-service: gRPC + MQTT + Device Shadow в Redis
Добавлен gRPC-контракт proto/device_control (TurnOn/TurnOff/SetLevel), общий Go-модуль proto/ для переиспользования сгенерированного кода. device-control-service принимает команды по gRPC, обновляет desired_state в Redis и публикует их в MQTT; слушает devices/+/telemetry и devices/+/ack для отметки живости устройства и обновления reported_state; горутина health-check переводит устройство в offline по таймауту и публикует событие в RabbitMQ (device.status_changed) для будущего notification-service. Проверено сквозным тестом через docker compose: grpcurl TurnOn/SetLevel меняет desired_state и уходит в MQTT, mosquitto_pub с ack обновляет reported_state и статус, health-check автоматически переводит устройство в offline по истечении таймаута.
This commit is contained in:
@@ -0,0 +1,213 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
// versions:
|
||||
// - protoc-gen-go-grpc v1.6.2
|
||||
// - protoc v7.35.1
|
||||
// source: device_control/device_control.proto
|
||||
|
||||
package device_control
|
||||
|
||||
import (
|
||||
context "context"
|
||||
grpc "google.golang.org/grpc"
|
||||
codes "google.golang.org/grpc/codes"
|
||||
status "google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
// Requires gRPC-Go v1.64.0 or later.
|
||||
const _ = grpc.SupportPackageIsVersion9
|
||||
|
||||
const (
|
||||
DeviceControl_TurnOn_FullMethodName = "/device_control.DeviceControl/TurnOn"
|
||||
DeviceControl_TurnOff_FullMethodName = "/device_control.DeviceControl/TurnOff"
|
||||
DeviceControl_SetLevel_FullMethodName = "/device_control.DeviceControl/SetLevel"
|
||||
)
|
||||
|
||||
// DeviceControlClient is the client API for DeviceControl service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||
//
|
||||
// DeviceControl is the synchronous entry point for issuing commands to a
|
||||
// device. It covers the generic actuator capabilities used across the
|
||||
// platform (device_types.capabilities: turn_on, turn_off, set_level) — no
|
||||
// zone- or device-type-specific methods. A call here updates the device's
|
||||
// desired state and dispatches the command over MQTT; it does not wait for
|
||||
// the device to confirm execution (see the Device Shadow pattern in
|
||||
// device-control-service's README).
|
||||
type DeviceControlClient interface {
|
||||
TurnOn(ctx context.Context, in *TurnOnRequest, opts ...grpc.CallOption) (*CommandResult, error)
|
||||
TurnOff(ctx context.Context, in *TurnOffRequest, opts ...grpc.CallOption) (*CommandResult, error)
|
||||
SetLevel(ctx context.Context, in *SetLevelRequest, opts ...grpc.CallOption) (*CommandResult, error)
|
||||
}
|
||||
|
||||
type deviceControlClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewDeviceControlClient(cc grpc.ClientConnInterface) DeviceControlClient {
|
||||
return &deviceControlClient{cc}
|
||||
}
|
||||
|
||||
func (c *deviceControlClient) TurnOn(ctx context.Context, in *TurnOnRequest, opts ...grpc.CallOption) (*CommandResult, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(CommandResult)
|
||||
err := c.cc.Invoke(ctx, DeviceControl_TurnOn_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *deviceControlClient) TurnOff(ctx context.Context, in *TurnOffRequest, opts ...grpc.CallOption) (*CommandResult, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(CommandResult)
|
||||
err := c.cc.Invoke(ctx, DeviceControl_TurnOff_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *deviceControlClient) SetLevel(ctx context.Context, in *SetLevelRequest, opts ...grpc.CallOption) (*CommandResult, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(CommandResult)
|
||||
err := c.cc.Invoke(ctx, DeviceControl_SetLevel_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// DeviceControlServer is the server API for DeviceControl service.
|
||||
// All implementations must embed UnimplementedDeviceControlServer
|
||||
// for forward compatibility.
|
||||
//
|
||||
// DeviceControl is the synchronous entry point for issuing commands to a
|
||||
// device. It covers the generic actuator capabilities used across the
|
||||
// platform (device_types.capabilities: turn_on, turn_off, set_level) — no
|
||||
// zone- or device-type-specific methods. A call here updates the device's
|
||||
// desired state and dispatches the command over MQTT; it does not wait for
|
||||
// the device to confirm execution (see the Device Shadow pattern in
|
||||
// device-control-service's README).
|
||||
type DeviceControlServer interface {
|
||||
TurnOn(context.Context, *TurnOnRequest) (*CommandResult, error)
|
||||
TurnOff(context.Context, *TurnOffRequest) (*CommandResult, error)
|
||||
SetLevel(context.Context, *SetLevelRequest) (*CommandResult, error)
|
||||
mustEmbedUnimplementedDeviceControlServer()
|
||||
}
|
||||
|
||||
// UnimplementedDeviceControlServer must be embedded to have
|
||||
// forward compatible implementations.
|
||||
//
|
||||
// NOTE: this should be embedded by value instead of pointer to avoid a nil
|
||||
// pointer dereference when methods are called.
|
||||
type UnimplementedDeviceControlServer struct{}
|
||||
|
||||
func (UnimplementedDeviceControlServer) TurnOn(context.Context, *TurnOnRequest) (*CommandResult, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method TurnOn not implemented")
|
||||
}
|
||||
func (UnimplementedDeviceControlServer) TurnOff(context.Context, *TurnOffRequest) (*CommandResult, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method TurnOff not implemented")
|
||||
}
|
||||
func (UnimplementedDeviceControlServer) SetLevel(context.Context, *SetLevelRequest) (*CommandResult, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method SetLevel not implemented")
|
||||
}
|
||||
func (UnimplementedDeviceControlServer) mustEmbedUnimplementedDeviceControlServer() {}
|
||||
func (UnimplementedDeviceControlServer) testEmbeddedByValue() {}
|
||||
|
||||
// UnsafeDeviceControlServer may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to DeviceControlServer will
|
||||
// result in compilation errors.
|
||||
type UnsafeDeviceControlServer interface {
|
||||
mustEmbedUnimplementedDeviceControlServer()
|
||||
}
|
||||
|
||||
func RegisterDeviceControlServer(s grpc.ServiceRegistrar, srv DeviceControlServer) {
|
||||
// If the following call panics, it indicates UnimplementedDeviceControlServer was
|
||||
// embedded by pointer and is nil. This will cause panics if an
|
||||
// unimplemented method is ever invoked, so we test this at initialization
|
||||
// time to prevent it from happening at runtime later due to I/O.
|
||||
if t, ok := srv.(interface{ testEmbeddedByValue() }); ok {
|
||||
t.testEmbeddedByValue()
|
||||
}
|
||||
s.RegisterService(&DeviceControl_ServiceDesc, srv)
|
||||
}
|
||||
|
||||
func _DeviceControl_TurnOn_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(TurnOnRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(DeviceControlServer).TurnOn(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: DeviceControl_TurnOn_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(DeviceControlServer).TurnOn(ctx, req.(*TurnOnRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _DeviceControl_TurnOff_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(TurnOffRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(DeviceControlServer).TurnOff(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: DeviceControl_TurnOff_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(DeviceControlServer).TurnOff(ctx, req.(*TurnOffRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _DeviceControl_SetLevel_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(SetLevelRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(DeviceControlServer).SetLevel(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: DeviceControl_SetLevel_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(DeviceControlServer).SetLevel(ctx, req.(*SetLevelRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
// DeviceControl_ServiceDesc is the grpc.ServiceDesc for DeviceControl service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
var DeviceControl_ServiceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "device_control.DeviceControl",
|
||||
HandlerType: (*DeviceControlServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "TurnOn",
|
||||
Handler: _DeviceControl_TurnOn_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "TurnOff",
|
||||
Handler: _DeviceControl_TurnOff_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "SetLevel",
|
||||
Handler: _DeviceControl_SetLevel_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "device_control/device_control.proto",
|
||||
}
|
||||
Reference in New Issue
Block a user