重构 #4
							
								
								
									
										10
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								Makefile
									
									
									
									
									
								
							| @@ -10,8 +10,9 @@ help: | |||||||
| 	@echo "  build        Build the application" | 	@echo "  build        Build the application" | ||||||
| 	@echo "  clean        Clean generated files" | 	@echo "  clean        Clean generated files" | ||||||
| 	@echo "  test         Run all tests" | 	@echo "  test         Run all tests" | ||||||
| 	@echo "	 swag        Generate swagger docs" | 	@echo "	 swag         Generate swagger docs" | ||||||
| 	@echo "  help         Show this help message" | 	@echo "  help         Show this help message" | ||||||
|  | 	@echo "  proto        Generate protobuf files" | ||||||
|  |  | ||||||
| # 运行应用 | # 运行应用 | ||||||
| .PHONY: run | .PHONY: run | ||||||
| @@ -36,4 +37,9 @@ test: | |||||||
| # 生成swagger文档 | # 生成swagger文档 | ||||||
| .PHONY: swag | .PHONY: swag | ||||||
| swag: | swag: | ||||||
| 	swag init | 	swag init | ||||||
|  |  | ||||||
|  | # 生成protobuf文件 | ||||||
|  | .PHONY: proto | ||||||
|  | proto: | ||||||
|  | 	protoc --go_out=internal/app/service/device/proto --go_opt=paths=source_relative --go-grpc_out=internal/app/service/device/proto --go-grpc_opt=paths=source_relative -Iinternal/app/service/device/proto internal/app/service/device/proto/device.proto | ||||||
|   | |||||||
							
								
								
									
										263
									
								
								internal/app/service/device/proto/device.pb.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										263
									
								
								internal/app/service/device/proto/device.pb.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,263 @@ | |||||||
|  | // Code generated by protoc-gen-go. DO NOT EDIT. | ||||||
|  | // versions: | ||||||
|  | // 	protoc-gen-go v1.36.9 | ||||||
|  | // 	protoc        v6.32.1 | ||||||
|  | // source: device.proto | ||||||
|  |  | ||||||
|  | package proto | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	protoreflect "google.golang.org/protobuf/reflect/protoreflect" | ||||||
|  | 	protoimpl "google.golang.org/protobuf/runtime/protoimpl" | ||||||
|  | 	anypb "google.golang.org/protobuf/types/known/anypb" | ||||||
|  | 	reflect "reflect" | ||||||
|  | 	sync "sync" | ||||||
|  | 	unsafe "unsafe" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | const ( | ||||||
|  | 	// Verify that this generated code is sufficiently up-to-date. | ||||||
|  | 	_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) | ||||||
|  | 	// Verify that runtime/protoimpl is sufficiently up-to-date. | ||||||
|  | 	_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // 指令类型 | ||||||
|  | type MethodType int32 | ||||||
|  |  | ||||||
|  | const ( | ||||||
|  | 	MethodType_SWITCH MethodType = 0 | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // Enum value maps for MethodType. | ||||||
|  | var ( | ||||||
|  | 	MethodType_name = map[int32]string{ | ||||||
|  | 		0: "SWITCH", | ||||||
|  | 	} | ||||||
|  | 	MethodType_value = map[string]int32{ | ||||||
|  | 		"SWITCH": 0, | ||||||
|  | 	} | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func (x MethodType) Enum() *MethodType { | ||||||
|  | 	p := new(MethodType) | ||||||
|  | 	*p = x | ||||||
|  | 	return p | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (x MethodType) String() string { | ||||||
|  | 	return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (MethodType) Descriptor() protoreflect.EnumDescriptor { | ||||||
|  | 	return file_device_proto_enumTypes[0].Descriptor() | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (MethodType) Type() protoreflect.EnumType { | ||||||
|  | 	return &file_device_proto_enumTypes[0] | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (x MethodType) Number() protoreflect.EnumNumber { | ||||||
|  | 	return protoreflect.EnumNumber(x) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // Deprecated: Use MethodType.Descriptor instead. | ||||||
|  | func (MethodType) EnumDescriptor() ([]byte, []int) { | ||||||
|  | 	return file_device_proto_rawDescGZIP(), []int{0} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // 指令 | ||||||
|  | type Instruction struct { | ||||||
|  | 	state         protoimpl.MessageState `protogen:"open.v1"` | ||||||
|  | 	Method        MethodType             `protobuf:"varint,1,opt,name=method,proto3,enum=device.MethodType" json:"method,omitempty"` | ||||||
|  | 	Data          *anypb.Any             `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"` | ||||||
|  | 	unknownFields protoimpl.UnknownFields | ||||||
|  | 	sizeCache     protoimpl.SizeCache | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (x *Instruction) Reset() { | ||||||
|  | 	*x = Instruction{} | ||||||
|  | 	mi := &file_device_proto_msgTypes[0] | ||||||
|  | 	ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) | ||||||
|  | 	ms.StoreMessageInfo(mi) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (x *Instruction) String() string { | ||||||
|  | 	return protoimpl.X.MessageStringOf(x) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (*Instruction) ProtoMessage() {} | ||||||
|  |  | ||||||
|  | func (x *Instruction) ProtoReflect() protoreflect.Message { | ||||||
|  | 	mi := &file_device_proto_msgTypes[0] | ||||||
|  | 	if x != nil { | ||||||
|  | 		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) | ||||||
|  | 		if ms.LoadMessageInfo() == nil { | ||||||
|  | 			ms.StoreMessageInfo(mi) | ||||||
|  | 		} | ||||||
|  | 		return ms | ||||||
|  | 	} | ||||||
|  | 	return mi.MessageOf(x) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // Deprecated: Use Instruction.ProtoReflect.Descriptor instead. | ||||||
|  | func (*Instruction) Descriptor() ([]byte, []int) { | ||||||
|  | 	return file_device_proto_rawDescGZIP(), []int{0} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (x *Instruction) GetMethod() MethodType { | ||||||
|  | 	if x != nil { | ||||||
|  | 		return x.Method | ||||||
|  | 	} | ||||||
|  | 	return MethodType_SWITCH | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (x *Instruction) GetData() *anypb.Any { | ||||||
|  | 	if x != nil { | ||||||
|  | 		return x.Data | ||||||
|  | 	} | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type Switch struct { | ||||||
|  | 	state         protoimpl.MessageState `protogen:"open.v1"` | ||||||
|  | 	DeviceAction  string                 `protobuf:"bytes,1,opt,name=device_action,json=deviceAction,proto3" json:"device_action,omitempty"`  // 指令 | ||||||
|  | 	BusNumber     int32                  `protobuf:"varint,2,opt,name=bus_number,json=busNumber,proto3" json:"bus_number,omitempty"`          // 总线号 | ||||||
|  | 	BusAddress    int32                  `protobuf:"varint,3,opt,name=bus_address,json=busAddress,proto3" json:"bus_address,omitempty"`       // 总线地址 | ||||||
|  | 	RelayChannel  int32                  `protobuf:"varint,4,opt,name=relay_channel,json=relayChannel,proto3" json:"relay_channel,omitempty"` // 继电器通道号 | ||||||
|  | 	unknownFields protoimpl.UnknownFields | ||||||
|  | 	sizeCache     protoimpl.SizeCache | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (x *Switch) Reset() { | ||||||
|  | 	*x = Switch{} | ||||||
|  | 	mi := &file_device_proto_msgTypes[1] | ||||||
|  | 	ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) | ||||||
|  | 	ms.StoreMessageInfo(mi) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (x *Switch) String() string { | ||||||
|  | 	return protoimpl.X.MessageStringOf(x) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (*Switch) ProtoMessage() {} | ||||||
|  |  | ||||||
|  | func (x *Switch) ProtoReflect() protoreflect.Message { | ||||||
|  | 	mi := &file_device_proto_msgTypes[1] | ||||||
|  | 	if x != nil { | ||||||
|  | 		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) | ||||||
|  | 		if ms.LoadMessageInfo() == nil { | ||||||
|  | 			ms.StoreMessageInfo(mi) | ||||||
|  | 		} | ||||||
|  | 		return ms | ||||||
|  | 	} | ||||||
|  | 	return mi.MessageOf(x) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // Deprecated: Use Switch.ProtoReflect.Descriptor instead. | ||||||
|  | func (*Switch) Descriptor() ([]byte, []int) { | ||||||
|  | 	return file_device_proto_rawDescGZIP(), []int{1} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (x *Switch) GetDeviceAction() string { | ||||||
|  | 	if x != nil { | ||||||
|  | 		return x.DeviceAction | ||||||
|  | 	} | ||||||
|  | 	return "" | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (x *Switch) GetBusNumber() int32 { | ||||||
|  | 	if x != nil { | ||||||
|  | 		return x.BusNumber | ||||||
|  | 	} | ||||||
|  | 	return 0 | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (x *Switch) GetBusAddress() int32 { | ||||||
|  | 	if x != nil { | ||||||
|  | 		return x.BusAddress | ||||||
|  | 	} | ||||||
|  | 	return 0 | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (x *Switch) GetRelayChannel() int32 { | ||||||
|  | 	if x != nil { | ||||||
|  | 		return x.RelayChannel | ||||||
|  | 	} | ||||||
|  | 	return 0 | ||||||
|  | } | ||||||
|  |  | ||||||
|  | var File_device_proto protoreflect.FileDescriptor | ||||||
|  |  | ||||||
|  | const file_device_proto_rawDesc = "" + | ||||||
|  | 	"\n" + | ||||||
|  | 	"\fdevice.proto\x12\x06device\x1a\x19google/protobuf/any.proto\"c\n" + | ||||||
|  | 	"\vInstruction\x12*\n" + | ||||||
|  | 	"\x06method\x18\x01 \x01(\x0e2\x12.device.MethodTypeR\x06method\x12(\n" + | ||||||
|  | 	"\x04data\x18\x02 \x01(\v2\x14.google.protobuf.AnyR\x04data\"\x92\x01\n" + | ||||||
|  | 	"\x06Switch\x12#\n" + | ||||||
|  | 	"\rdevice_action\x18\x01 \x01(\tR\fdeviceAction\x12\x1d\n" + | ||||||
|  | 	"\n" + | ||||||
|  | 	"bus_number\x18\x02 \x01(\x05R\tbusNumber\x12\x1f\n" + | ||||||
|  | 	"\vbus_address\x18\x03 \x01(\x05R\n" + | ||||||
|  | 	"busAddress\x12#\n" + | ||||||
|  | 	"\rrelay_channel\x18\x04 \x01(\x05R\frelayChannel*\x18\n" + | ||||||
|  | 	"\n" + | ||||||
|  | 	"MethodType\x12\n" + | ||||||
|  | 	"\n" + | ||||||
|  | 	"\x06SWITCH\x10\x00B#Z!internal/app/service/device/protob\x06proto3" | ||||||
|  |  | ||||||
|  | var ( | ||||||
|  | 	file_device_proto_rawDescOnce sync.Once | ||||||
|  | 	file_device_proto_rawDescData []byte | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func file_device_proto_rawDescGZIP() []byte { | ||||||
|  | 	file_device_proto_rawDescOnce.Do(func() { | ||||||
|  | 		file_device_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_device_proto_rawDesc), len(file_device_proto_rawDesc))) | ||||||
|  | 	}) | ||||||
|  | 	return file_device_proto_rawDescData | ||||||
|  | } | ||||||
|  |  | ||||||
|  | var file_device_proto_enumTypes = make([]protoimpl.EnumInfo, 1) | ||||||
|  | var file_device_proto_msgTypes = make([]protoimpl.MessageInfo, 2) | ||||||
|  | var file_device_proto_goTypes = []any{ | ||||||
|  | 	(MethodType)(0),     // 0: device.MethodType | ||||||
|  | 	(*Instruction)(nil), // 1: device.Instruction | ||||||
|  | 	(*Switch)(nil),      // 2: device.Switch | ||||||
|  | 	(*anypb.Any)(nil),   // 3: google.protobuf.Any | ||||||
|  | } | ||||||
|  | var file_device_proto_depIdxs = []int32{ | ||||||
|  | 	0, // 0: device.Instruction.method:type_name -> device.MethodType | ||||||
|  | 	3, // 1: device.Instruction.data:type_name -> google.protobuf.Any | ||||||
|  | 	2, // [2:2] is the sub-list for method output_type | ||||||
|  | 	2, // [2:2] is the sub-list for method input_type | ||||||
|  | 	2, // [2:2] is the sub-list for extension type_name | ||||||
|  | 	2, // [2:2] is the sub-list for extension extendee | ||||||
|  | 	0, // [0:2] is the sub-list for field type_name | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func init() { file_device_proto_init() } | ||||||
|  | func file_device_proto_init() { | ||||||
|  | 	if File_device_proto != nil { | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 	type x struct{} | ||||||
|  | 	out := protoimpl.TypeBuilder{ | ||||||
|  | 		File: protoimpl.DescBuilder{ | ||||||
|  | 			GoPackagePath: reflect.TypeOf(x{}).PkgPath(), | ||||||
|  | 			RawDescriptor: unsafe.Slice(unsafe.StringData(file_device_proto_rawDesc), len(file_device_proto_rawDesc)), | ||||||
|  | 			NumEnums:      1, | ||||||
|  | 			NumMessages:   2, | ||||||
|  | 			NumExtensions: 0, | ||||||
|  | 			NumServices:   0, | ||||||
|  | 		}, | ||||||
|  | 		GoTypes:           file_device_proto_goTypes, | ||||||
|  | 		DependencyIndexes: file_device_proto_depIdxs, | ||||||
|  | 		EnumInfos:         file_device_proto_enumTypes, | ||||||
|  | 		MessageInfos:      file_device_proto_msgTypes, | ||||||
|  | 	}.Build() | ||||||
|  | 	File_device_proto = out.File | ||||||
|  | 	file_device_proto_goTypes = nil | ||||||
|  | 	file_device_proto_depIdxs = nil | ||||||
|  | } | ||||||
| @@ -6,16 +6,20 @@ import "google/protobuf/any.proto"; | |||||||
|  |  | ||||||
| option go_package = "internal/app/service/device/proto"; | option go_package = "internal/app/service/device/proto"; | ||||||
|  |  | ||||||
| service Device { | // 指令类型 | ||||||
|   rpc Ping(PingRequest) returns (PingResponse); | enum MethodType{ | ||||||
|  |   SWITCH = 0; | ||||||
| } | } | ||||||
|  |  | ||||||
| message PingRequest { | // 指令 | ||||||
|   int32 message_id = 1; | message Instruction{ | ||||||
|   string method = 2; |   MethodType method = 1; | ||||||
|  |   google.protobuf.Any data = 2; | ||||||
| } | } | ||||||
|  |  | ||||||
| message PingResponse { | message Switch{ | ||||||
|   int32 message_id = 1; |   string device_action = 1; // 指令 | ||||||
|   string message = 2; |   int32 bus_number = 2; // 总线号 | ||||||
| } |   int32 bus_address = 3; // 总线地址 | ||||||
|  |   int32 relay_channel = 4; // 继电器通道号 | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user