diff --git a/dto/dc.pb.go b/dto/dc.pb.go new file mode 100644 index 0000000..1b67cc1 --- /dev/null +++ b/dto/dc.pb.go @@ -0,0 +1,570 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.31.0 +// protoc v4.23.1 +// source: proto/src/dc.proto + +package dto + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +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 DataType int32 + +const ( + DataType_CJ DataType = 0 // 采集码表 + DataType_QD DataType = 1 // 驱动码表 +) + +// Enum value maps for DataType. +var ( + DataType_name = map[int32]string{ + 0: "CJ", + 1: "QD", + } + DataType_value = map[string]int32{ + "CJ": 0, + "QD": 1, + } +) + +func (x DataType) Enum() *DataType { + p := new(DataType) + *p = x + return p +} + +func (x DataType) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (DataType) Descriptor() protoreflect.EnumDescriptor { + return file_proto_src_dc_proto_enumTypes[0].Descriptor() +} + +func (DataType) Type() protoreflect.EnumType { + return &file_proto_src_dc_proto_enumTypes[0] +} + +func (x DataType) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use DataType.Descriptor instead. +func (DataType) EnumDescriptor() ([]byte, []int) { + return file_proto_src_dc_proto_rawDescGZIP(), []int{0} +} + +// Modbus功能 +type Modbus_Function int32 + +const ( + Modbus_ReadCoil Modbus_Function = 0 // 读线圈 + Modbus_ReadDiscreteInput Modbus_Function = 1 // 读离散输入 + Modbus_ReadHoldingRegister Modbus_Function = 2 // 读保持寄存器 + Modbus_ReadInputRegister Modbus_Function = 3 // 读输入寄存器 + Modbus_WriteCoil Modbus_Function = 10 // 写单个线圈 + Modbus_WriteCoils Modbus_Function = 11 // 写多个线圈 + Modbus_WriteRegister Modbus_Function = 12 // 写单个寄存器 + Modbus_WriteRegisters Modbus_Function = 13 // 写多个寄存器 + Modbus_RWCoils Modbus_Function = 20 // 读写多个线圈,定时读,当映射数据更新时执行写操作 + Modbus_RWRegisters Modbus_Function = 21 // 读写多个寄存器,定时读,当映射数据更新时执行写操作 +) + +// Enum value maps for Modbus_Function. +var ( + Modbus_Function_name = map[int32]string{ + 0: "ReadCoil", + 1: "ReadDiscreteInput", + 2: "ReadHoldingRegister", + 3: "ReadInputRegister", + 10: "WriteCoil", + 11: "WriteCoils", + 12: "WriteRegister", + 13: "WriteRegisters", + 20: "RWCoils", + 21: "RWRegisters", + } + Modbus_Function_value = map[string]int32{ + "ReadCoil": 0, + "ReadDiscreteInput": 1, + "ReadHoldingRegister": 2, + "ReadInputRegister": 3, + "WriteCoil": 10, + "WriteCoils": 11, + "WriteRegister": 12, + "WriteRegisters": 13, + "RWCoils": 20, + "RWRegisters": 21, + } +) + +func (x Modbus_Function) Enum() *Modbus_Function { + p := new(Modbus_Function) + *p = x + return p +} + +func (x Modbus_Function) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (Modbus_Function) Descriptor() protoreflect.EnumDescriptor { + return file_proto_src_dc_proto_enumTypes[1].Descriptor() +} + +func (Modbus_Function) Type() protoreflect.EnumType { + return &file_proto_src_dc_proto_enumTypes[1] +} + +func (x Modbus_Function) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use Modbus_Function.Descriptor instead. +func (Modbus_Function) EnumDescriptor() ([]byte, []int) { + return file_proto_src_dc_proto_rawDescGZIP(), []int{0, 0} +} + +// // Modbus服务写策略 +// +// enum WriteStrategy { +// OnUpdate = 0; // 数据更新时写 +// OnScheduled = 1; // 定时写 +// } +// +// 寄存器字节序 +type Modbus_Endianness int32 + +const ( + // 大端 + Modbus_BigEndian Modbus_Endianness = 0 + // 小端 + Modbus_LittleEndian Modbus_Endianness = 1 +) + +// Enum value maps for Modbus_Endianness. +var ( + Modbus_Endianness_name = map[int32]string{ + 0: "BigEndian", + 1: "LittleEndian", + } + Modbus_Endianness_value = map[string]int32{ + "BigEndian": 0, + "LittleEndian": 1, + } +) + +func (x Modbus_Endianness) Enum() *Modbus_Endianness { + p := new(Modbus_Endianness) + *p = x + return p +} + +func (x Modbus_Endianness) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (Modbus_Endianness) Descriptor() protoreflect.EnumDescriptor { + return file_proto_src_dc_proto_enumTypes[2].Descriptor() +} + +func (Modbus_Endianness) Type() protoreflect.EnumType { + return &file_proto_src_dc_proto_enumTypes[2] +} + +func (x Modbus_Endianness) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use Modbus_Endianness.Descriptor instead. +func (Modbus_Endianness) EnumDescriptor() ([]byte, []int) { + return file_proto_src_dc_proto_rawDescGZIP(), []int{0, 1} +} + +type Modbus struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *Modbus) Reset() { + *x = Modbus{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_src_dc_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Modbus) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Modbus) ProtoMessage() {} + +func (x *Modbus) ProtoReflect() protoreflect.Message { + mi := &file_proto_src_dc_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Modbus.ProtoReflect.Descriptor instead. +func (*Modbus) Descriptor() ([]byte, []int) { + return file_proto_src_dc_proto_rawDescGZIP(), []int{0} +} + +// modbus任务配置 +type ModbusConfig struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Url string `protobuf:"bytes,2,opt,name=url,proto3" json:"url,omitempty"` // 连接地址 + UnitId uint32 `protobuf:"varint,3,opt,name=unitId,proto3" json:"unitId,omitempty"` // 从机unitId + Endianness Modbus_Endianness `protobuf:"varint,4,opt,name=endianness,proto3,enum=iot_dto.Modbus_Endianness" json:"endianness,omitempty"` // 16位寄存器字节序 + Interval uint32 `protobuf:"varint,5,opt,name=interval,proto3" json:"interval,omitempty"` // 循环请求间隔(毫秒),0表示不主动请求,只当有变化时请求 + Timeout uint32 `protobuf:"varint,6,opt,name=timeout,proto3" json:"timeout,omitempty"` // 超时时间(毫秒) + Qdl uint32 `protobuf:"varint,7,opt,name=qdl,proto3" json:"qdl,omitempty"` // 驱动数据字节数 + Cjl uint32 `protobuf:"varint,8,opt,name=cjl,proto3" json:"cjl,omitempty"` // 采集数据字节数 + Mapping []*ModbusDcMapping `protobuf:"bytes,10,rep,name=mapping,proto3" json:"mapping,omitempty"` // modbus数据与驱动/采集码表映射配置 +} + +func (x *ModbusConfig) Reset() { + *x = ModbusConfig{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_src_dc_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ModbusConfig) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ModbusConfig) ProtoMessage() {} + +func (x *ModbusConfig) ProtoReflect() protoreflect.Message { + mi := &file_proto_src_dc_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ModbusConfig.ProtoReflect.Descriptor instead. +func (*ModbusConfig) Descriptor() ([]byte, []int) { + return file_proto_src_dc_proto_rawDescGZIP(), []int{1} +} + +func (x *ModbusConfig) GetUrl() string { + if x != nil { + return x.Url + } + return "" +} + +func (x *ModbusConfig) GetUnitId() uint32 { + if x != nil { + return x.UnitId + } + return 0 +} + +func (x *ModbusConfig) GetEndianness() Modbus_Endianness { + if x != nil { + return x.Endianness + } + return Modbus_BigEndian +} + +func (x *ModbusConfig) GetInterval() uint32 { + if x != nil { + return x.Interval + } + return 0 +} + +func (x *ModbusConfig) GetTimeout() uint32 { + if x != nil { + return x.Timeout + } + return 0 +} + +func (x *ModbusConfig) GetQdl() uint32 { + if x != nil { + return x.Qdl + } + return 0 +} + +func (x *ModbusConfig) GetCjl() uint32 { + if x != nil { + return x.Cjl + } + return 0 +} + +func (x *ModbusConfig) GetMapping() []*ModbusDcMapping { + if x != nil { + return x.Mapping + } + return nil +} + +// modbus数据与驱动/采集码表映射配置 +type ModbusDcMapping struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Function Modbus_Function `protobuf:"varint,1,opt,name=function,proto3,enum=iot_dto.Modbus_Function" json:"function,omitempty"` // 功能 + Addr uint32 `protobuf:"varint,2,opt,name=addr,proto3" json:"addr,omitempty"` // 起始地址,当功能为位功能时,表示起始位地址,当功能为寄存器功能时,表示起始字(2个字节)地址 + Quantity uint32 `protobuf:"varint,3,opt,name=quantity,proto3" json:"quantity,omitempty"` // 数量,当功能为位功能时,表示位数,当功能为寄存器功能时,表示字(2个字节)数 + // Modbus.WriteStrategy writeStrategy = 4; // 当功能为写入类功能时,写策略 + Type DataType `protobuf:"varint,5,opt,name=type,proto3,enum=iot_dto.DataType" json:"type,omitempty"` // 对应数据类型 + Start uint32 `protobuf:"varint,6,opt,name=start,proto3" json:"start,omitempty"` // 驱动/采集码表中的起始下标,当功能为位功能时,表示起始位,当功能为寄存器功能时,表示起始字节 +} + +func (x *ModbusDcMapping) Reset() { + *x = ModbusDcMapping{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_src_dc_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ModbusDcMapping) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ModbusDcMapping) ProtoMessage() {} + +func (x *ModbusDcMapping) ProtoReflect() protoreflect.Message { + mi := &file_proto_src_dc_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ModbusDcMapping.ProtoReflect.Descriptor instead. +func (*ModbusDcMapping) Descriptor() ([]byte, []int) { + return file_proto_src_dc_proto_rawDescGZIP(), []int{2} +} + +func (x *ModbusDcMapping) GetFunction() Modbus_Function { + if x != nil { + return x.Function + } + return Modbus_ReadCoil +} + +func (x *ModbusDcMapping) GetAddr() uint32 { + if x != nil { + return x.Addr + } + return 0 +} + +func (x *ModbusDcMapping) GetQuantity() uint32 { + if x != nil { + return x.Quantity + } + return 0 +} + +func (x *ModbusDcMapping) GetType() DataType { + if x != nil { + return x.Type + } + return DataType_CJ +} + +func (x *ModbusDcMapping) GetStart() uint32 { + if x != nil { + return x.Start + } + return 0 +} + +var File_proto_src_dc_proto protoreflect.FileDescriptor + +var file_proto_src_dc_proto_rawDesc = []byte{ + 0x0a, 0x12, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x73, 0x72, 0x63, 0x2f, 0x64, 0x63, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x07, 0x69, 0x6f, 0x74, 0x5f, 0x64, 0x74, 0x6f, 0x22, 0xfd, 0x01, + 0x0a, 0x06, 0x4d, 0x6f, 0x64, 0x62, 0x75, 0x73, 0x22, 0xc3, 0x01, 0x0a, 0x08, 0x46, 0x75, 0x6e, + 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x0c, 0x0a, 0x08, 0x52, 0x65, 0x61, 0x64, 0x43, 0x6f, 0x69, + 0x6c, 0x10, 0x00, 0x12, 0x15, 0x0a, 0x11, 0x52, 0x65, 0x61, 0x64, 0x44, 0x69, 0x73, 0x63, 0x72, + 0x65, 0x74, 0x65, 0x49, 0x6e, 0x70, 0x75, 0x74, 0x10, 0x01, 0x12, 0x17, 0x0a, 0x13, 0x52, 0x65, + 0x61, 0x64, 0x48, 0x6f, 0x6c, 0x64, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, + 0x72, 0x10, 0x02, 0x12, 0x15, 0x0a, 0x11, 0x52, 0x65, 0x61, 0x64, 0x49, 0x6e, 0x70, 0x75, 0x74, + 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x10, 0x03, 0x12, 0x0d, 0x0a, 0x09, 0x57, 0x72, + 0x69, 0x74, 0x65, 0x43, 0x6f, 0x69, 0x6c, 0x10, 0x0a, 0x12, 0x0e, 0x0a, 0x0a, 0x57, 0x72, 0x69, + 0x74, 0x65, 0x43, 0x6f, 0x69, 0x6c, 0x73, 0x10, 0x0b, 0x12, 0x11, 0x0a, 0x0d, 0x57, 0x72, 0x69, + 0x74, 0x65, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x10, 0x0c, 0x12, 0x12, 0x0a, 0x0e, + 0x57, 0x72, 0x69, 0x74, 0x65, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x73, 0x10, 0x0d, + 0x12, 0x0b, 0x0a, 0x07, 0x52, 0x57, 0x43, 0x6f, 0x69, 0x6c, 0x73, 0x10, 0x14, 0x12, 0x0f, 0x0a, + 0x0b, 0x52, 0x57, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x73, 0x10, 0x15, 0x22, 0x2d, + 0x0a, 0x0a, 0x45, 0x6e, 0x64, 0x69, 0x61, 0x6e, 0x6e, 0x65, 0x73, 0x73, 0x12, 0x0d, 0x0a, 0x09, + 0x42, 0x69, 0x67, 0x45, 0x6e, 0x64, 0x69, 0x61, 0x6e, 0x10, 0x00, 0x12, 0x10, 0x0a, 0x0c, 0x4c, + 0x69, 0x74, 0x74, 0x6c, 0x65, 0x45, 0x6e, 0x64, 0x69, 0x61, 0x6e, 0x10, 0x01, 0x22, 0x82, 0x02, + 0x0a, 0x0c, 0x4d, 0x6f, 0x64, 0x62, 0x75, 0x73, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x10, + 0x0a, 0x03, 0x75, 0x72, 0x6c, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x72, 0x6c, + 0x12, 0x16, 0x0a, 0x06, 0x75, 0x6e, 0x69, 0x74, 0x49, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, + 0x52, 0x06, 0x75, 0x6e, 0x69, 0x74, 0x49, 0x64, 0x12, 0x3a, 0x0a, 0x0a, 0x65, 0x6e, 0x64, 0x69, + 0x61, 0x6e, 0x6e, 0x65, 0x73, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x1a, 0x2e, 0x69, + 0x6f, 0x74, 0x5f, 0x64, 0x74, 0x6f, 0x2e, 0x4d, 0x6f, 0x64, 0x62, 0x75, 0x73, 0x2e, 0x45, 0x6e, + 0x64, 0x69, 0x61, 0x6e, 0x6e, 0x65, 0x73, 0x73, 0x52, 0x0a, 0x65, 0x6e, 0x64, 0x69, 0x61, 0x6e, + 0x6e, 0x65, 0x73, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x76, 0x61, 0x6c, + 0x18, 0x05, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x08, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x76, 0x61, 0x6c, + 0x12, 0x18, 0x0a, 0x07, 0x74, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x18, 0x06, 0x20, 0x01, 0x28, + 0x0d, 0x52, 0x07, 0x74, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x71, 0x64, + 0x6c, 0x18, 0x07, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x03, 0x71, 0x64, 0x6c, 0x12, 0x10, 0x0a, 0x03, + 0x63, 0x6a, 0x6c, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x03, 0x63, 0x6a, 0x6c, 0x12, 0x32, + 0x0a, 0x07, 0x6d, 0x61, 0x70, 0x70, 0x69, 0x6e, 0x67, 0x18, 0x0a, 0x20, 0x03, 0x28, 0x0b, 0x32, + 0x18, 0x2e, 0x69, 0x6f, 0x74, 0x5f, 0x64, 0x74, 0x6f, 0x2e, 0x4d, 0x6f, 0x64, 0x62, 0x75, 0x73, + 0x44, 0x63, 0x4d, 0x61, 0x70, 0x70, 0x69, 0x6e, 0x67, 0x52, 0x07, 0x6d, 0x61, 0x70, 0x70, 0x69, + 0x6e, 0x67, 0x22, 0xb4, 0x01, 0x0a, 0x0f, 0x4d, 0x6f, 0x64, 0x62, 0x75, 0x73, 0x44, 0x63, 0x4d, + 0x61, 0x70, 0x70, 0x69, 0x6e, 0x67, 0x12, 0x34, 0x0a, 0x08, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, + 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x18, 0x2e, 0x69, 0x6f, 0x74, 0x5f, 0x64, + 0x74, 0x6f, 0x2e, 0x4d, 0x6f, 0x64, 0x62, 0x75, 0x73, 0x2e, 0x46, 0x75, 0x6e, 0x63, 0x74, 0x69, + 0x6f, 0x6e, 0x52, 0x08, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, + 0x61, 0x64, 0x64, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, 0x61, 0x64, 0x64, 0x72, + 0x12, 0x1a, 0x0a, 0x08, 0x71, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x18, 0x03, 0x20, 0x01, + 0x28, 0x0d, 0x52, 0x08, 0x71, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x12, 0x25, 0x0a, 0x04, + 0x74, 0x79, 0x70, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x11, 0x2e, 0x69, 0x6f, 0x74, + 0x5f, 0x64, 0x74, 0x6f, 0x2e, 0x44, 0x61, 0x74, 0x61, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74, + 0x79, 0x70, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x72, 0x74, 0x18, 0x06, 0x20, 0x01, + 0x28, 0x0d, 0x52, 0x05, 0x73, 0x74, 0x61, 0x72, 0x74, 0x2a, 0x1a, 0x0a, 0x08, 0x44, 0x61, 0x74, + 0x61, 0x54, 0x79, 0x70, 0x65, 0x12, 0x06, 0x0a, 0x02, 0x43, 0x4a, 0x10, 0x00, 0x12, 0x06, 0x0a, + 0x02, 0x51, 0x44, 0x10, 0x01, 0x42, 0x07, 0x5a, 0x05, 0x2e, 0x2f, 0x64, 0x74, 0x6f, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_proto_src_dc_proto_rawDescOnce sync.Once + file_proto_src_dc_proto_rawDescData = file_proto_src_dc_proto_rawDesc +) + +func file_proto_src_dc_proto_rawDescGZIP() []byte { + file_proto_src_dc_proto_rawDescOnce.Do(func() { + file_proto_src_dc_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_src_dc_proto_rawDescData) + }) + return file_proto_src_dc_proto_rawDescData +} + +var file_proto_src_dc_proto_enumTypes = make([]protoimpl.EnumInfo, 3) +var file_proto_src_dc_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_proto_src_dc_proto_goTypes = []interface{}{ + (DataType)(0), // 0: iot_dto.DataType + (Modbus_Function)(0), // 1: iot_dto.Modbus.Function + (Modbus_Endianness)(0), // 2: iot_dto.Modbus.Endianness + (*Modbus)(nil), // 3: iot_dto.Modbus + (*ModbusConfig)(nil), // 4: iot_dto.ModbusConfig + (*ModbusDcMapping)(nil), // 5: iot_dto.ModbusDcMapping +} +var file_proto_src_dc_proto_depIdxs = []int32{ + 2, // 0: iot_dto.ModbusConfig.endianness:type_name -> iot_dto.Modbus.Endianness + 5, // 1: iot_dto.ModbusConfig.mapping:type_name -> iot_dto.ModbusDcMapping + 1, // 2: iot_dto.ModbusDcMapping.function:type_name -> iot_dto.Modbus.Function + 0, // 3: iot_dto.ModbusDcMapping.type:type_name -> iot_dto.DataType + 4, // [4:4] is the sub-list for method output_type + 4, // [4:4] is the sub-list for method input_type + 4, // [4:4] is the sub-list for extension type_name + 4, // [4:4] is the sub-list for extension extendee + 0, // [0:4] is the sub-list for field type_name +} + +func init() { file_proto_src_dc_proto_init() } +func file_proto_src_dc_proto_init() { + if File_proto_src_dc_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_proto_src_dc_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Modbus); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_src_dc_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ModbusConfig); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_src_dc_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ModbusDcMapping); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_proto_src_dc_proto_rawDesc, + NumEnums: 3, + NumMessages: 3, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_proto_src_dc_proto_goTypes, + DependencyIndexes: file_proto_src_dc_proto_depIdxs, + EnumInfos: file_proto_src_dc_proto_enumTypes, + MessageInfos: file_proto_src_dc_proto_msgTypes, + }.Build() + File_proto_src_dc_proto = out.File + file_proto_src_dc_proto_rawDesc = nil + file_proto_src_dc_proto_goTypes = nil + file_proto_src_dc_proto_depIdxs = nil +} diff --git a/dto/mqtt.pb.go b/dto/mqtt.pb.go new file mode 100644 index 0000000..db01345 --- /dev/null +++ b/dto/mqtt.pb.go @@ -0,0 +1,663 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.31.0 +// protoc v4.23.1 +// source: proto/src/mqtt.proto + +package dto + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +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 ServiceState int32 + +const ( + ServiceState_Normal ServiceState = 0 // 正常 + ServiceState_Offline ServiceState = 1 // 离线 + ServiceState_Error ServiceState = 2 // 异常 +) + +// Enum value maps for ServiceState. +var ( + ServiceState_name = map[int32]string{ + 0: "Normal", + 1: "Offline", + 2: "Error", + } + ServiceState_value = map[string]int32{ + "Normal": 0, + "Offline": 1, + "Error": 2, + } +) + +func (x ServiceState) Enum() *ServiceState { + p := new(ServiceState) + *p = x + return p +} + +func (x ServiceState) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (ServiceState) Descriptor() protoreflect.EnumDescriptor { + return file_proto_src_mqtt_proto_enumTypes[0].Descriptor() +} + +func (ServiceState) Type() protoreflect.EnumType { + return &file_proto_src_mqtt_proto_enumTypes[0] +} + +func (x ServiceState) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use ServiceState.Descriptor instead. +func (ServiceState) EnumDescriptor() ([]byte, []int) { + return file_proto_src_mqtt_proto_rawDescGZIP(), []int{0} +} + +// IOT服务状态 +type IotServiceState struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // string cliId = 1; // IOT服务mqtt客户端id + State ServiceState `protobuf:"varint,2,opt,name=state,proto3,enum=iot_dto.ServiceState" json:"state,omitempty"` // 服务状态 +} + +func (x *IotServiceState) Reset() { + *x = IotServiceState{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_src_mqtt_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *IotServiceState) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*IotServiceState) ProtoMessage() {} + +func (x *IotServiceState) ProtoReflect() protoreflect.Message { + mi := &file_proto_src_mqtt_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use IotServiceState.ProtoReflect.Descriptor instead. +func (*IotServiceState) Descriptor() ([]byte, []int) { + return file_proto_src_mqtt_proto_rawDescGZIP(), []int{0} +} + +func (x *IotServiceState) GetState() ServiceState { + if x != nil { + return x.State + } + return ServiceState_Normal +} + +type IotQcServiceStartReq struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // string cliId = 1; // IOT服务mqtt客户端id + Config *ModbusConfig `protobuf:"bytes,2,opt,name=config,proto3" json:"config,omitempty"` // 服务配置 + Force bool `protobuf:"varint,5,opt,name=force,proto3" json:"force,omitempty"` // 是否强制启动(若存在旧的服务则停止重新启动) +} + +func (x *IotQcServiceStartReq) Reset() { + *x = IotQcServiceStartReq{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_src_mqtt_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *IotQcServiceStartReq) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*IotQcServiceStartReq) ProtoMessage() {} + +func (x *IotQcServiceStartReq) ProtoReflect() protoreflect.Message { + mi := &file_proto_src_mqtt_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use IotQcServiceStartReq.ProtoReflect.Descriptor instead. +func (*IotQcServiceStartReq) Descriptor() ([]byte, []int) { + return file_proto_src_mqtt_proto_rawDescGZIP(), []int{1} +} + +func (x *IotQcServiceStartReq) GetConfig() *ModbusConfig { + if x != nil { + return x.Config + } + return nil +} + +func (x *IotQcServiceStartReq) GetForce() bool { + if x != nil { + return x.Force + } + return false +} + +type IotQcServiceStopReq struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *IotQcServiceStopReq) Reset() { + *x = IotQcServiceStopReq{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_src_mqtt_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *IotQcServiceStopReq) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*IotQcServiceStopReq) ProtoMessage() {} + +func (x *IotQcServiceStopReq) ProtoReflect() protoreflect.Message { + mi := &file_proto_src_mqtt_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use IotQcServiceStopReq.ProtoReflect.Descriptor instead. +func (*IotQcServiceStopReq) Descriptor() ([]byte, []int) { + return file_proto_src_mqtt_proto_rawDescGZIP(), []int{2} +} + +type IotQcServiceCommonResp struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // string cliId = 1; // IOT服务mqtt客户端id + Code int32 `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"` // 错误码 + Msg string `protobuf:"bytes,3,opt,name=msg,proto3" json:"msg,omitempty"` // 错误信息 +} + +func (x *IotQcServiceCommonResp) Reset() { + *x = IotQcServiceCommonResp{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_src_mqtt_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *IotQcServiceCommonResp) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*IotQcServiceCommonResp) ProtoMessage() {} + +func (x *IotQcServiceCommonResp) ProtoReflect() protoreflect.Message { + mi := &file_proto_src_mqtt_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use IotQcServiceCommonResp.ProtoReflect.Descriptor instead. +func (*IotQcServiceCommonResp) Descriptor() ([]byte, []int) { + return file_proto_src_mqtt_proto_rawDescGZIP(), []int{3} +} + +func (x *IotQcServiceCommonResp) GetCode() int32 { + if x != nil { + return x.Code + } + return 0 +} + +func (x *IotQcServiceCommonResp) GetMsg() string { + if x != nil { + return x.Msg + } + return "" +} + +// IOT服务获取日志请求 +type IotServiceLogReq struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // string cliId = 1; // IOT服务mqtt客户端id + Count int32 `protobuf:"varint,3,opt,name=count,proto3" json:"count,omitempty"` // 日志条数 +} + +func (x *IotServiceLogReq) Reset() { + *x = IotServiceLogReq{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_src_mqtt_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *IotServiceLogReq) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*IotServiceLogReq) ProtoMessage() {} + +func (x *IotServiceLogReq) ProtoReflect() protoreflect.Message { + mi := &file_proto_src_mqtt_proto_msgTypes[4] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use IotServiceLogReq.ProtoReflect.Descriptor instead. +func (*IotServiceLogReq) Descriptor() ([]byte, []int) { + return file_proto_src_mqtt_proto_rawDescGZIP(), []int{4} +} + +func (x *IotServiceLogReq) GetCount() int32 { + if x != nil { + return x.Count + } + return 0 +} + +// IOT服务日志响应 +type IotServiceLogResp struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // string cliId = 1; // IOT服务mqtt客户端id + Logs []string `protobuf:"bytes,4,rep,name=logs,proto3" json:"logs,omitempty"` // 日志 +} + +func (x *IotServiceLogResp) Reset() { + *x = IotServiceLogResp{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_src_mqtt_proto_msgTypes[5] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *IotServiceLogResp) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*IotServiceLogResp) ProtoMessage() {} + +func (x *IotServiceLogResp) ProtoReflect() protoreflect.Message { + mi := &file_proto_src_mqtt_proto_msgTypes[5] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use IotServiceLogResp.ProtoReflect.Descriptor instead. +func (*IotServiceLogResp) Descriptor() ([]byte, []int) { + return file_proto_src_mqtt_proto_rawDescGZIP(), []int{5} +} + +func (x *IotServiceLogResp) GetLogs() []string { + if x != nil { + return x.Logs + } + return nil +} + +// IOT驱动数据 +type IotQd struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // string cliId = 1; // IOT服务mqtt客户端id + Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"` // 驱动数据 +} + +func (x *IotQd) Reset() { + *x = IotQd{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_src_mqtt_proto_msgTypes[6] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *IotQd) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*IotQd) ProtoMessage() {} + +func (x *IotQd) ProtoReflect() protoreflect.Message { + mi := &file_proto_src_mqtt_proto_msgTypes[6] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use IotQd.ProtoReflect.Descriptor instead. +func (*IotQd) Descriptor() ([]byte, []int) { + return file_proto_src_mqtt_proto_rawDescGZIP(), []int{6} +} + +func (x *IotQd) GetData() []byte { + if x != nil { + return x.Data + } + return nil +} + +// IOT采集数据 +type IotCj struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // string cliId = 1; // IOT服务mqtt客户端id + Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"` // 采集数据 +} + +func (x *IotCj) Reset() { + *x = IotCj{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_src_mqtt_proto_msgTypes[7] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *IotCj) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*IotCj) ProtoMessage() {} + +func (x *IotCj) ProtoReflect() protoreflect.Message { + mi := &file_proto_src_mqtt_proto_msgTypes[7] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use IotCj.ProtoReflect.Descriptor instead. +func (*IotCj) Descriptor() ([]byte, []int) { + return file_proto_src_mqtt_proto_rawDescGZIP(), []int{7} +} + +func (x *IotCj) GetData() []byte { + if x != nil { + return x.Data + } + return nil +} + +var File_proto_src_mqtt_proto protoreflect.FileDescriptor + +var file_proto_src_mqtt_proto_rawDesc = []byte{ + 0x0a, 0x14, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x73, 0x72, 0x63, 0x2f, 0x6d, 0x71, 0x74, 0x74, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x07, 0x69, 0x6f, 0x74, 0x5f, 0x64, 0x74, 0x6f, 0x1a, + 0x12, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x73, 0x72, 0x63, 0x2f, 0x64, 0x63, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x22, 0x3e, 0x0a, 0x0f, 0x49, 0x6f, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, + 0x65, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x2b, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x74, 0x65, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x15, 0x2e, 0x69, 0x6f, 0x74, 0x5f, 0x64, 0x74, 0x6f, 0x2e, + 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x05, 0x73, 0x74, + 0x61, 0x74, 0x65, 0x22, 0x5b, 0x0a, 0x14, 0x49, 0x6f, 0x74, 0x51, 0x63, 0x53, 0x65, 0x72, 0x76, + 0x69, 0x63, 0x65, 0x53, 0x74, 0x61, 0x72, 0x74, 0x52, 0x65, 0x71, 0x12, 0x2d, 0x0a, 0x06, 0x63, + 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x15, 0x2e, 0x69, 0x6f, + 0x74, 0x5f, 0x64, 0x74, 0x6f, 0x2e, 0x4d, 0x6f, 0x64, 0x62, 0x75, 0x73, 0x43, 0x6f, 0x6e, 0x66, + 0x69, 0x67, 0x52, 0x06, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x14, 0x0a, 0x05, 0x66, 0x6f, + 0x72, 0x63, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x08, 0x52, 0x05, 0x66, 0x6f, 0x72, 0x63, 0x65, + 0x22, 0x15, 0x0a, 0x13, 0x49, 0x6f, 0x74, 0x51, 0x63, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, + 0x53, 0x74, 0x6f, 0x70, 0x52, 0x65, 0x71, 0x22, 0x3e, 0x0a, 0x16, 0x49, 0x6f, 0x74, 0x51, 0x63, + 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x43, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x52, 0x65, 0x73, + 0x70, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, + 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x6d, 0x73, 0x67, 0x18, 0x03, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x03, 0x6d, 0x73, 0x67, 0x22, 0x28, 0x0a, 0x10, 0x49, 0x6f, 0x74, 0x53, 0x65, + 0x72, 0x76, 0x69, 0x63, 0x65, 0x4c, 0x6f, 0x67, 0x52, 0x65, 0x71, 0x12, 0x14, 0x0a, 0x05, 0x63, + 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x63, 0x6f, 0x75, 0x6e, + 0x74, 0x22, 0x27, 0x0a, 0x11, 0x49, 0x6f, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x4c, + 0x6f, 0x67, 0x52, 0x65, 0x73, 0x70, 0x12, 0x12, 0x0a, 0x04, 0x6c, 0x6f, 0x67, 0x73, 0x18, 0x04, + 0x20, 0x03, 0x28, 0x09, 0x52, 0x04, 0x6c, 0x6f, 0x67, 0x73, 0x22, 0x1b, 0x0a, 0x05, 0x49, 0x6f, + 0x74, 0x51, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x02, 0x20, 0x01, 0x28, + 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x1b, 0x0a, 0x05, 0x49, 0x6f, 0x74, 0x43, 0x6a, + 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, + 0x64, 0x61, 0x74, 0x61, 0x2a, 0x32, 0x0a, 0x0c, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x53, + 0x74, 0x61, 0x74, 0x65, 0x12, 0x0a, 0x0a, 0x06, 0x4e, 0x6f, 0x72, 0x6d, 0x61, 0x6c, 0x10, 0x00, + 0x12, 0x0b, 0x0a, 0x07, 0x4f, 0x66, 0x66, 0x6c, 0x69, 0x6e, 0x65, 0x10, 0x01, 0x12, 0x09, 0x0a, + 0x05, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x10, 0x02, 0x42, 0x07, 0x5a, 0x05, 0x2e, 0x2f, 0x64, 0x74, + 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_proto_src_mqtt_proto_rawDescOnce sync.Once + file_proto_src_mqtt_proto_rawDescData = file_proto_src_mqtt_proto_rawDesc +) + +func file_proto_src_mqtt_proto_rawDescGZIP() []byte { + file_proto_src_mqtt_proto_rawDescOnce.Do(func() { + file_proto_src_mqtt_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_src_mqtt_proto_rawDescData) + }) + return file_proto_src_mqtt_proto_rawDescData +} + +var file_proto_src_mqtt_proto_enumTypes = make([]protoimpl.EnumInfo, 1) +var file_proto_src_mqtt_proto_msgTypes = make([]protoimpl.MessageInfo, 8) +var file_proto_src_mqtt_proto_goTypes = []interface{}{ + (ServiceState)(0), // 0: iot_dto.ServiceState + (*IotServiceState)(nil), // 1: iot_dto.IotServiceState + (*IotQcServiceStartReq)(nil), // 2: iot_dto.IotQcServiceStartReq + (*IotQcServiceStopReq)(nil), // 3: iot_dto.IotQcServiceStopReq + (*IotQcServiceCommonResp)(nil), // 4: iot_dto.IotQcServiceCommonResp + (*IotServiceLogReq)(nil), // 5: iot_dto.IotServiceLogReq + (*IotServiceLogResp)(nil), // 6: iot_dto.IotServiceLogResp + (*IotQd)(nil), // 7: iot_dto.IotQd + (*IotCj)(nil), // 8: iot_dto.IotCj + (*ModbusConfig)(nil), // 9: iot_dto.ModbusConfig +} +var file_proto_src_mqtt_proto_depIdxs = []int32{ + 0, // 0: iot_dto.IotServiceState.state:type_name -> iot_dto.ServiceState + 9, // 1: iot_dto.IotQcServiceStartReq.config:type_name -> iot_dto.ModbusConfig + 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_proto_src_mqtt_proto_init() } +func file_proto_src_mqtt_proto_init() { + if File_proto_src_mqtt_proto != nil { + return + } + file_proto_src_dc_proto_init() + if !protoimpl.UnsafeEnabled { + file_proto_src_mqtt_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*IotServiceState); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_src_mqtt_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*IotQcServiceStartReq); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_src_mqtt_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*IotQcServiceStopReq); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_src_mqtt_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*IotQcServiceCommonResp); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_src_mqtt_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*IotServiceLogReq); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_src_mqtt_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*IotServiceLogResp); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_src_mqtt_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*IotQd); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_src_mqtt_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*IotCj); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_proto_src_mqtt_proto_rawDesc, + NumEnums: 1, + NumMessages: 8, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_proto_src_mqtt_proto_goTypes, + DependencyIndexes: file_proto_src_mqtt_proto_depIdxs, + EnumInfos: file_proto_src_mqtt_proto_enumTypes, + MessageInfos: file_proto_src_mqtt_proto_msgTypes, + }.Build() + File_proto_src_mqtt_proto = out.File + file_proto_src_mqtt_proto_rawDesc = nil + file_proto_src_mqtt_proto_goTypes = nil + file_proto_src_mqtt_proto_depIdxs = nil +} diff --git a/example/module_use/main.go b/example/module_use/main.go new file mode 100644 index 0000000..4fbec98 --- /dev/null +++ b/example/module_use/main.go @@ -0,0 +1,68 @@ +package main + +import ( + "fmt" + "log/slog" + "time" + + "joylink.club/iot/dto" + "joylink.club/iot/service" +) + +// 作为依赖包使用例子 + +func main() { + // slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{ + // Level: slog.LevelDebug, + // AddSource: false, + // }))) + modbusCfg := &dto.ModbusConfig{ + Url: "tcp://127.0.0.1:502", + UnitId: 2, + Timeout: 500, + Interval: 1000, + Qdl: 2, // 驱动数据字节数 + Cjl: 2, // 采集数据字节数 + Mapping: []*dto.ModbusDcMapping{ + { + // Function: proto.Modbus_ReadHoldingRegister, + Function: dto.Modbus_ReadCoil, + Addr: 0, + Quantity: 16, + Type: dto.DataType_CJ, + Start: 0, + }, + { + Function: dto.Modbus_RWCoils, + Addr: 16, + Quantity: 16, + Type: dto.DataType_QD, + Start: 0, + }, + }, + } + mds, err := service.NewModbusQcService(modbusCfg) + if err != nil { + panic(err) + } + + i := 0 + mds.RegisterQcDataHandleScheduleTask(func() { + c := mds.GetCjBytes() + fmt.Printf("获取采集数据: %v\n", c) + i++ + if i%3 == 0 { + idx := i % 8 + err := mds.WriteQdBytes([]byte{byte(1 << idx), byte(3 << idx)}) + if err != nil { + slog.Error("写入驱动数据失败", "error", err) + } else { + fmt.Printf("写入驱动数据成功: %v\n", mds.GetQdBytes()) + } + } + }, time.Second) + + time.Sleep(time.Minute) + + mds.Stop() +} diff --git a/example/subsys_use/main.go b/example/subsys_use/main.go new file mode 100644 index 0000000..92dda10 --- /dev/null +++ b/example/subsys_use/main.go @@ -0,0 +1,93 @@ +package main + +import ( + "context" + "fmt" + "log" + "net/url" + "time" + + "github.com/eclipse/paho.golang/autopaho" + "github.com/eclipse/paho.golang/autopaho/extensions/rpc" + "github.com/eclipse/paho.golang/paho" + "google.golang.org/protobuf/proto" + "joylink.club/iot/dto" + "joylink.club/iot/mqtt" +) + +// 作为子系统使用方式 + +func main() { + clientId := "iotlogreq_test" + logReqTopic := mqtt.GetLogReqTopic() + cliCfg := getCmConfig(clientId, logReqTopic) + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + cm, err := autopaho.NewConnection(ctx, cliCfg) + if err != nil { + panic(err) + } + h, err := rpc.NewHandler(ctx, rpc.HandlerOpts{ + Conn: cm, + Router: cliCfg.Router, + ResponseTopicFmt: "%s/iotlogresp", + ClientID: clientId, + }) + if err != nil { + log.Fatal(err) + } + + req := &dto.IotServiceLogReq{ + Count: 10, + } + b, err := proto.Marshal(req) + if err != nil { + log.Fatal(err) + } + resp, err := h.Request(ctx, &paho.Publish{ + Topic: logReqTopic, + Payload: b, + }) + if err != nil { + log.Fatal(err) + } + fmt.Printf("请求结果: %v\n", resp) +} + +func getCmConfig(clientId, logReqTopic string) autopaho.ClientConfig { + addr, _ := url.Parse("tcp://192.168.3.233:1883") + cc := autopaho.ClientConfig{ + BrokerUrls: []*url.URL{addr}, + KeepAlive: 60, + OnConnectionUp: func(cm *autopaho.ConnectionManager, connAck *paho.Connack) { + fmt.Println("mqtt connection up") + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second)) + defer cancel() + if _, err := cm.Subscribe(ctx, &paho.Subscribe{ + Subscriptions: []paho.SubscribeOptions{ + {Topic: logReqTopic, QoS: 0}, + }, + }); err != nil { + fmt.Printf("failed to subscribe (%s). This is likely to mean no messages will be received.", err) + return + } + fmt.Println("mqtt subscription made") + }, + OnConnectError: func(err error) { fmt.Printf("error whilst attempting connection: %s\n", err) }, + ClientConfig: paho.ClientConfig{ + ClientID: clientId, + Router: paho.NewStandardRouter(), + OnClientError: func(err error) { fmt.Printf("%s requested disconnect: %s\n", clientId, err) }, + OnServerDisconnect: func(d *paho.Disconnect) { + if d.Properties != nil { + fmt.Printf("%s requested disconnect: %s\n", clientId, d.Properties.ReasonString) + } else { + fmt.Printf("%s requested disconnect; reason code: %d\n", clientId, d.ReasonCode) + } + }, + }, + } + cc.SetUsernamePassword("rtsts_service", []byte("joylink@0503")) + return cc +} diff --git a/main.go b/main.go index b23eb57..8cb3966 100644 --- a/main.go +++ b/main.go @@ -1,106 +1,11 @@ package main -import ( - "fmt" - "log/slog" - "os" - "time" - - "joylink.club/iot/config" - "joylink.club/iot/mqtt" - mproto "joylink.club/iot/mqtt/proto" - "joylink.club/iot/service" - "joylink.club/iot/service/proto" -) +import "joylink.club/iot/server" func main() { // slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{ // Level: slog.LevelDebug, // AddSource: false, // }))) - config.LoadConfig() - mqttcfg := config.Cfg.Mqtt - cmc := &mqtt.IotMqttConfig{ - AppId: mqttcfg.Topic.App, - BrokerUrl: mqttcfg.Address, - ClientId: mqttcfg.ClientId, - Username: mqttcfg.Username, - Password: mqttcfg.Password, - KeepAlive: mqttcfg.KeepAlive, - ConnectRetryDelay: mqttcfg.ConnectRetryDelay, - ConnectTimeout: mqttcfg.ConnectTimeout, - } - err := mqtt.Start(cmc) - if err != nil { - slog.Error("启动MQTT客户端失败", "error", err) - os.Exit(1) - } - - go func() { - for { - time.Sleep(time.Second) - mqtt.PubIotServiceState(&mproto.IotServiceState{ - Code: config.Cfg.Mqtt.ClientId, - State: mproto.ServiceState_Normal, - }) - } - }() - - mds, err := service.NewModbusQcService(&proto.ModbusConfig{ - Url: "tcp://127.0.0.1:502", - UnitId: 2, - Timeout: 500, - Interval: 1000, - Mapping: []*proto.ModbusDcMapping{ - { - // Function: proto.Modbus_ReadHoldingRegister, - Function: proto.Modbus_ReadCoil, - Addr: 0, - Quantity: 16, - Type: proto.DataType_CollectTable, - Start: 0, - }, - { - Function: proto.Modbus_WriteCoils, - Addr: 16, - Quantity: 16, - Type: proto.DataType_DriveTable, - WriteStrategy: proto.Modbus_OnUpdate, - Start: 0, - }, - }, - }, make([]byte, 2), make([]byte, 2)) - if err != nil { - panic(err) - } - - mds.RegisterQcDataHandleScheduleTask(func() { - i := 0 - for { - c := mds.GetCjBytes() - fmt.Printf("发布采集数据: %v\n", c) - mqtt.PubIotCjData(&mproto.IotCj{ - Code: config.Cfg.Mqtt.ClientId, - Data: c, - }) - i++ - if i%3 == 0 { - idx := i % 8 - err := mds.WriteQdBytes([]byte{byte(1 << idx), byte(3 << idx)}) - if err != nil { - slog.Error("设置驱动数据失败", "error", err) - } else { - fmt.Printf("设置驱动数据成功: %v\n", mds.GetQdBytes()) - } - } - if i%10 == 0 { - mds.Stop() - } - time.Sleep(time.Second) - } - }, time.Second) - - time.Sleep(time.Minute) - - mds.Stop() + server.StartIotQcServer() } diff --git a/mqtt/client.go b/mqtt/client.go index 7f34c9d..44b02ba 100644 --- a/mqtt/client.go +++ b/mqtt/client.go @@ -9,7 +9,7 @@ import ( "github.com/eclipse/paho.golang/paho" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/reflect/protoreflect" - mproto "joylink.club/iot/mqtt/proto" + "joylink.club/iot/dto" ) var iotcli *IotClient @@ -62,24 +62,24 @@ func checkConfig(cmc *IotMqttConfig) error { } // 发布IOT服务状态 -func PubIotServiceState(s *mproto.IotServiceState) error { +func PubIotServiceState(s *dto.IotServiceState) error { return pub(GetIotServiceStateTopic(), s) } // 发布IOT采集数据 -func PubIotCjData(cj *mproto.IotCj) error { +func PubIotCjData(cj *dto.IotCj) error { return pub(GetCjTopic(), cj) } // 发布IOT驱动数据 -func PubIotQdData(qd *mproto.IotQd) error { +func PubIotQdData(qd *dto.IotQd) error { return pub(GetCjTopic(), qd) } // 注册IOT采集数据处理 -func RegIotCjHandler(h func(cj *mproto.IotCj)) { +func RegIotCjHandler(h func(cj *dto.IotCj)) { iotcli.cc.Router.RegisterHandler(GetLogReqTopic(), func(p *paho.Publish) { - cmd := &mproto.IotCj{} + cmd := &dto.IotCj{} err := proto.Unmarshal(p.Payload, cmd) if err != nil { slog.Error("采集数据proto.Unmarshal异常", "error", err) @@ -90,9 +90,9 @@ func RegIotCjHandler(h func(cj *mproto.IotCj)) { } // 注册IOT驱动数据处理 -func RegIotQdHandler(h func(qd *mproto.IotQd)) { +func RegIotQdHandler(h func(qd *dto.IotQd)) { iotcli.cc.Router.RegisterHandler(GetLogReqTopic(), func(p *paho.Publish) { - cmd := &mproto.IotQd{} + cmd := &dto.IotQd{} err := proto.Unmarshal(p.Payload, cmd) if err != nil { slog.Error("驱动数据proto.Unmarshal异常", "error", err) @@ -102,10 +102,24 @@ func RegIotQdHandler(h func(qd *mproto.IotQd)) { }) } +// 注册IOT服务启动请求处理 +func RegIotQcServiceStartReqHandler(h func(req *dto.IotQcServiceStartReq) *dto.IotQcServiceCommonResp) { + iotcli.cc.Router.RegisterHandler(GetIotServiceStartTopic(), func(p *paho.Publish) { + reqHandle(p, h, &dto.IotQcServiceStartReq{}) + }) +} + +// 注册IOT服务停止请求处理 +func RegIotQcServiceStopReqHandler(h func(req *dto.IotQcServiceStopReq) *dto.IotQcServiceCommonResp) { + iotcli.cc.Router.RegisterHandler(GetIotServiceStopTopic(), func(p *paho.Publish) { + reqHandle(p, h, &dto.IotQcServiceStopReq{}) + }) +} + // 注册IOT日志查询请求处理 -func RegIotLogReqHandler(h func(req *mproto.IotServiceLogReq) *mproto.IotServiceLogResp) { +func RegIotLogReqHandler(h func(req *dto.IotServiceLogReq) *dto.IotServiceLogResp) { iotcli.cc.Router.RegisterHandler(GetLogReqTopic(), func(p *paho.Publish) { - reqHandle(p, h, &mproto.IotServiceLogReq{}) + reqHandle(p, h, &dto.IotServiceLogReq{}) }) } @@ -121,9 +135,11 @@ func UnregAllHandler() { func subIotQc() { slog.Info("订阅Iot驱采") - sub(GetCjTopic()) // 订阅采集 - sub(GetQdTopic()) // 订阅驱动 - sub(GetLogReqTopic()) // 订阅日志查询请求 + sub(GetCjTopic()) // 订阅采集 + sub(GetQdTopic()) // 订阅驱动 + sub(GetIotServiceStartTopic()) // 订阅启动请求 + sub(GetIotServiceStopTopic()) // 订阅停止请求 + sub(GetLogReqTopic()) // 订阅日志查询请求 } // 发起订阅 diff --git a/mqtt/client_test.go b/mqtt/client_request_test.go similarity index 92% rename from mqtt/client_test.go rename to mqtt/client_request_test.go index c5b5b94..69a7df9 100644 --- a/mqtt/client_test.go +++ b/mqtt/client_request_test.go @@ -14,7 +14,7 @@ import ( "github.com/eclipse/paho.golang/paho" "google.golang.org/protobuf/proto" "joylink.club/iot/config" - mproto "joylink.club/iot/mqtt/proto" + "joylink.club/iot/dto" ) func TestRequest(t *testing.T) { @@ -44,7 +44,7 @@ func TestRequest(t *testing.T) { log.Fatal(err) } - req := &mproto.IotServiceLogReq{ + req := &dto.IotServiceLogReq{ Count: 10, } b, err := proto.Marshal(req) @@ -60,7 +60,9 @@ func TestRequest(t *testing.T) { } fmt.Printf("请求结果: %v\n", resp) - log.Printf("Received response: %s", string(resp.Payload)) + lr := &dto.IotServiceLogResp{} + err = proto.Unmarshal(resp.Payload, lr) + log.Printf("Received response: %s\n", lr) time.Sleep(3 * time.Second) } @@ -90,10 +92,9 @@ func listen() { time.Sleep(2 * time.Second) - RegIotLogReqHandler(func(req *mproto.IotServiceLogReq) *mproto.IotServiceLogResp { + RegIotLogReqHandler(func(req *dto.IotServiceLogReq) *dto.IotServiceLogResp { fmt.Printf("收到日志请求: %v\n", req) - resp := &mproto.IotServiceLogResp{ - Code: req.Code, + resp := &dto.IotServiceLogResp{ Logs: []string{"日志1", "日志2"}, } fmt.Printf("返回日志响应: %v\n", resp) diff --git a/mqtt/client_will_test.go b/mqtt/client_will_test.go new file mode 100644 index 0000000..13dccfb --- /dev/null +++ b/mqtt/client_will_test.go @@ -0,0 +1,7 @@ +package mqtt + +import "testing" + +func TestWillMessage(t *testing.T) { + t.Log("test will message") +} diff --git a/mqtt/proto/mqtt.pb.go b/mqtt/proto/mqtt.pb.go deleted file mode 100644 index 869c768..0000000 --- a/mqtt/proto/mqtt.pb.go +++ /dev/null @@ -1,496 +0,0 @@ -// Code generated by protoc-gen-go. DO NOT EDIT. -// versions: -// protoc-gen-go v1.31.0 -// protoc v4.23.1 -// source: mqtt/mqtt.proto - -package proto - -import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" - reflect "reflect" - sync "sync" -) - -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 ServiceState int32 - -const ( - ServiceState_Normal ServiceState = 0 // 正常 - ServiceState_Offline ServiceState = 1 // 离线 -) - -// Enum value maps for ServiceState. -var ( - ServiceState_name = map[int32]string{ - 0: "Normal", - 1: "Offline", - } - ServiceState_value = map[string]int32{ - "Normal": 0, - "Offline": 1, - } -) - -func (x ServiceState) Enum() *ServiceState { - p := new(ServiceState) - *p = x - return p -} - -func (x ServiceState) String() string { - return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) -} - -func (ServiceState) Descriptor() protoreflect.EnumDescriptor { - return file_mqtt_mqtt_proto_enumTypes[0].Descriptor() -} - -func (ServiceState) Type() protoreflect.EnumType { - return &file_mqtt_mqtt_proto_enumTypes[0] -} - -func (x ServiceState) Number() protoreflect.EnumNumber { - return protoreflect.EnumNumber(x) -} - -// Deprecated: Use ServiceState.Descriptor instead. -func (ServiceState) EnumDescriptor() ([]byte, []int) { - return file_mqtt_mqtt_proto_rawDescGZIP(), []int{0} -} - -// IOT服务状态 -type IotServiceState struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Code string `protobuf:"bytes,1,opt,name=code,proto3" json:"code,omitempty"` // 服务编号 - State ServiceState `protobuf:"varint,2,opt,name=state,proto3,enum=mqtt_api.ServiceState" json:"state,omitempty"` // 服务状态 -} - -func (x *IotServiceState) Reset() { - *x = IotServiceState{} - if protoimpl.UnsafeEnabled { - mi := &file_mqtt_mqtt_proto_msgTypes[0] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *IotServiceState) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*IotServiceState) ProtoMessage() {} - -func (x *IotServiceState) ProtoReflect() protoreflect.Message { - mi := &file_mqtt_mqtt_proto_msgTypes[0] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use IotServiceState.ProtoReflect.Descriptor instead. -func (*IotServiceState) Descriptor() ([]byte, []int) { - return file_mqtt_mqtt_proto_rawDescGZIP(), []int{0} -} - -func (x *IotServiceState) GetCode() string { - if x != nil { - return x.Code - } - return "" -} - -func (x *IotServiceState) GetState() ServiceState { - if x != nil { - return x.State - } - return ServiceState_Normal -} - -// IOT服务获取日志请求 -type IotServiceLogReq struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Code string `protobuf:"bytes,1,opt,name=code,proto3" json:"code,omitempty"` // 服务编号 - Count int32 `protobuf:"varint,3,opt,name=count,proto3" json:"count,omitempty"` // 日志条数 -} - -func (x *IotServiceLogReq) Reset() { - *x = IotServiceLogReq{} - if protoimpl.UnsafeEnabled { - mi := &file_mqtt_mqtt_proto_msgTypes[1] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *IotServiceLogReq) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*IotServiceLogReq) ProtoMessage() {} - -func (x *IotServiceLogReq) ProtoReflect() protoreflect.Message { - mi := &file_mqtt_mqtt_proto_msgTypes[1] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use IotServiceLogReq.ProtoReflect.Descriptor instead. -func (*IotServiceLogReq) Descriptor() ([]byte, []int) { - return file_mqtt_mqtt_proto_rawDescGZIP(), []int{1} -} - -func (x *IotServiceLogReq) GetCode() string { - if x != nil { - return x.Code - } - return "" -} - -func (x *IotServiceLogReq) GetCount() int32 { - if x != nil { - return x.Count - } - return 0 -} - -// IOT服务日志响应 -type IotServiceLogResp struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Code string `protobuf:"bytes,1,opt,name=code,proto3" json:"code,omitempty"` // 服务编号 - Logs []string `protobuf:"bytes,4,rep,name=logs,proto3" json:"logs,omitempty"` // 日志 -} - -func (x *IotServiceLogResp) Reset() { - *x = IotServiceLogResp{} - if protoimpl.UnsafeEnabled { - mi := &file_mqtt_mqtt_proto_msgTypes[2] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *IotServiceLogResp) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*IotServiceLogResp) ProtoMessage() {} - -func (x *IotServiceLogResp) ProtoReflect() protoreflect.Message { - mi := &file_mqtt_mqtt_proto_msgTypes[2] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use IotServiceLogResp.ProtoReflect.Descriptor instead. -func (*IotServiceLogResp) Descriptor() ([]byte, []int) { - return file_mqtt_mqtt_proto_rawDescGZIP(), []int{2} -} - -func (x *IotServiceLogResp) GetCode() string { - if x != nil { - return x.Code - } - return "" -} - -func (x *IotServiceLogResp) GetLogs() []string { - if x != nil { - return x.Logs - } - return nil -} - -// IOT驱动数据 -type IotQd struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Code string `protobuf:"bytes,1,opt,name=code,proto3" json:"code,omitempty"` // 服务编号 - Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"` // 驱动数据 -} - -func (x *IotQd) Reset() { - *x = IotQd{} - if protoimpl.UnsafeEnabled { - mi := &file_mqtt_mqtt_proto_msgTypes[3] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *IotQd) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*IotQd) ProtoMessage() {} - -func (x *IotQd) ProtoReflect() protoreflect.Message { - mi := &file_mqtt_mqtt_proto_msgTypes[3] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use IotQd.ProtoReflect.Descriptor instead. -func (*IotQd) Descriptor() ([]byte, []int) { - return file_mqtt_mqtt_proto_rawDescGZIP(), []int{3} -} - -func (x *IotQd) GetCode() string { - if x != nil { - return x.Code - } - return "" -} - -func (x *IotQd) GetData() []byte { - if x != nil { - return x.Data - } - return nil -} - -// IOT采集数据 -type IotCj struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Code string `protobuf:"bytes,1,opt,name=code,proto3" json:"code,omitempty"` // 服务编号 - Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"` // 采集数据 -} - -func (x *IotCj) Reset() { - *x = IotCj{} - if protoimpl.UnsafeEnabled { - mi := &file_mqtt_mqtt_proto_msgTypes[4] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *IotCj) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*IotCj) ProtoMessage() {} - -func (x *IotCj) ProtoReflect() protoreflect.Message { - mi := &file_mqtt_mqtt_proto_msgTypes[4] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use IotCj.ProtoReflect.Descriptor instead. -func (*IotCj) Descriptor() ([]byte, []int) { - return file_mqtt_mqtt_proto_rawDescGZIP(), []int{4} -} - -func (x *IotCj) GetCode() string { - if x != nil { - return x.Code - } - return "" -} - -func (x *IotCj) GetData() []byte { - if x != nil { - return x.Data - } - return nil -} - -var File_mqtt_mqtt_proto protoreflect.FileDescriptor - -var file_mqtt_mqtt_proto_rawDesc = []byte{ - 0x0a, 0x0f, 0x6d, 0x71, 0x74, 0x74, 0x2f, 0x6d, 0x71, 0x74, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x12, 0x08, 0x6d, 0x71, 0x74, 0x74, 0x5f, 0x61, 0x70, 0x69, 0x22, 0x53, 0x0a, 0x0f, 0x49, - 0x6f, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x12, - 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x6f, - 0x64, 0x65, 0x12, 0x2c, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x74, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, - 0x0e, 0x32, 0x16, 0x2e, 0x6d, 0x71, 0x74, 0x74, 0x5f, 0x61, 0x70, 0x69, 0x2e, 0x53, 0x65, 0x72, - 0x76, 0x69, 0x63, 0x65, 0x53, 0x74, 0x61, 0x74, 0x65, 0x52, 0x05, 0x73, 0x74, 0x61, 0x74, 0x65, - 0x22, 0x3c, 0x0a, 0x10, 0x49, 0x6f, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x4c, 0x6f, - 0x67, 0x52, 0x65, 0x71, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x63, 0x6f, 0x75, 0x6e, - 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x3b, - 0x0a, 0x11, 0x49, 0x6f, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x4c, 0x6f, 0x67, 0x52, - 0x65, 0x73, 0x70, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6c, 0x6f, 0x67, 0x73, 0x18, - 0x04, 0x20, 0x03, 0x28, 0x09, 0x52, 0x04, 0x6c, 0x6f, 0x67, 0x73, 0x22, 0x2f, 0x0a, 0x05, 0x49, - 0x6f, 0x74, 0x51, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, - 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x2f, 0x0a, 0x05, - 0x49, 0x6f, 0x74, 0x43, 0x6a, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, - 0x61, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x2a, 0x27, 0x0a, - 0x0c, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x0a, 0x0a, - 0x06, 0x4e, 0x6f, 0x72, 0x6d, 0x61, 0x6c, 0x10, 0x00, 0x12, 0x0b, 0x0a, 0x07, 0x4f, 0x66, 0x66, - 0x6c, 0x69, 0x6e, 0x65, 0x10, 0x01, 0x42, 0x0e, 0x5a, 0x0c, 0x2e, 0x2f, 0x6d, 0x71, 0x74, 0x74, - 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, -} - -var ( - file_mqtt_mqtt_proto_rawDescOnce sync.Once - file_mqtt_mqtt_proto_rawDescData = file_mqtt_mqtt_proto_rawDesc -) - -func file_mqtt_mqtt_proto_rawDescGZIP() []byte { - file_mqtt_mqtt_proto_rawDescOnce.Do(func() { - file_mqtt_mqtt_proto_rawDescData = protoimpl.X.CompressGZIP(file_mqtt_mqtt_proto_rawDescData) - }) - return file_mqtt_mqtt_proto_rawDescData -} - -var file_mqtt_mqtt_proto_enumTypes = make([]protoimpl.EnumInfo, 1) -var file_mqtt_mqtt_proto_msgTypes = make([]protoimpl.MessageInfo, 5) -var file_mqtt_mqtt_proto_goTypes = []interface{}{ - (ServiceState)(0), // 0: mqtt_api.ServiceState - (*IotServiceState)(nil), // 1: mqtt_api.IotServiceState - (*IotServiceLogReq)(nil), // 2: mqtt_api.IotServiceLogReq - (*IotServiceLogResp)(nil), // 3: mqtt_api.IotServiceLogResp - (*IotQd)(nil), // 4: mqtt_api.IotQd - (*IotCj)(nil), // 5: mqtt_api.IotCj -} -var file_mqtt_mqtt_proto_depIdxs = []int32{ - 0, // 0: mqtt_api.IotServiceState.state:type_name -> mqtt_api.ServiceState - 1, // [1:1] is the sub-list for method output_type - 1, // [1:1] is the sub-list for method input_type - 1, // [1:1] is the sub-list for extension type_name - 1, // [1:1] is the sub-list for extension extendee - 0, // [0:1] is the sub-list for field type_name -} - -func init() { file_mqtt_mqtt_proto_init() } -func file_mqtt_mqtt_proto_init() { - if File_mqtt_mqtt_proto != nil { - return - } - if !protoimpl.UnsafeEnabled { - file_mqtt_mqtt_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*IotServiceState); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - file_mqtt_mqtt_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*IotServiceLogReq); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - file_mqtt_mqtt_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*IotServiceLogResp); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - file_mqtt_mqtt_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*IotQd); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - file_mqtt_mqtt_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*IotCj); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - } - type x struct{} - out := protoimpl.TypeBuilder{ - File: protoimpl.DescBuilder{ - GoPackagePath: reflect.TypeOf(x{}).PkgPath(), - RawDescriptor: file_mqtt_mqtt_proto_rawDesc, - NumEnums: 1, - NumMessages: 5, - NumExtensions: 0, - NumServices: 0, - }, - GoTypes: file_mqtt_mqtt_proto_goTypes, - DependencyIndexes: file_mqtt_mqtt_proto_depIdxs, - EnumInfos: file_mqtt_mqtt_proto_enumTypes, - MessageInfos: file_mqtt_mqtt_proto_msgTypes, - }.Build() - File_mqtt_mqtt_proto = out.File - file_mqtt_mqtt_proto_rawDesc = nil - file_mqtt_mqtt_proto_goTypes = nil - file_mqtt_mqtt_proto_depIdxs = nil -} diff --git a/mqtt/topic.go b/mqtt/topic.go index 46a6d8e..358022c 100644 --- a/mqtt/topic.go +++ b/mqtt/topic.go @@ -21,6 +21,8 @@ var topicMap = make(map[string]string, 4) func BuildTopics(sysCode, iotCode string) { topicMap[Topic_IotServiceState] = fmt.Sprintf(Topic_IotServiceState, sysCode, iotCode) + topicMap[Topic_IotServiceStart] = fmt.Sprintf(Topic_IotServiceStart, sysCode, iotCode) + topicMap[Topic_IotServiceStop] = fmt.Sprintf(Topic_IotServiceStop, sysCode, iotCode) topicMap[Topic_IotLog] = fmt.Sprintf(Topic_IotLog, sysCode, iotCode) topicMap[Topic_IotQd] = fmt.Sprintf(Topic_IotQd, sysCode, iotCode) topicMap[Topic_IotCj] = fmt.Sprintf(Topic_IotCj, sysCode, iotCode) @@ -30,6 +32,14 @@ func GetIotServiceStateTopic() string { return topicMap[Topic_IotServiceState] } +func GetIotServiceStartTopic() string { + return topicMap[Topic_IotServiceStart] +} + +func GetIotServiceStopTopic() string { + return topicMap[Topic_IotServiceStop] +} + func GetLogReqTopic() string { return topicMap[Topic_IotLog] } diff --git a/proto/main.go b/proto/main.go index 039c71d..1df1ad6 100644 --- a/proto/main.go +++ b/proto/main.go @@ -57,7 +57,7 @@ func getProtoFiles() []string { // 编译proto文件为Go文件 func compileProto(protoFiles []string) error { for _, fileName := range protoFiles { - cmd := exec.Command(protocPath, "-I="+protoFolder, fmt.Sprintf("--go_out=%s", goOutDir), fileName) + cmd := exec.Command(protocPath, "-I="+basePath, fmt.Sprintf("--go_out=%s", goOutDir), fileName) fmt.Println(cmd.String()) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr diff --git a/proto/src/dc.proto b/proto/src/dc.proto index 2ad2903..4efcc53 100644 --- a/proto/src/dc.proto +++ b/proto/src/dc.proto @@ -1,7 +1,7 @@ syntax = "proto3"; -package iot_service; -option go_package = "./service/proto"; +package iot_dto; +option go_package = "./dto"; message Modbus { // Modbus功能 @@ -17,14 +17,16 @@ message Modbus { RWCoils = 20; // 读写多个线圈,定时读,当映射数据更新时执行写操作 RWRegisters = 21; // 读写多个寄存器,定时读,当映射数据更新时执行写操作 } - // Modbus服务写策略 - enum WriteStrategy { - OnUpdate = 0; // 数据更新时写 - OnScheduled = 1; // 定时写 - } + // // Modbus服务写策略 + // enum WriteStrategy { + // OnUpdate = 0; // 数据更新时写 + // OnScheduled = 1; // 定时写 + // } // 寄存器字节序 enum Endianness { + // 大端 BigEndian = 0; + // 小端 LittleEndian = 1; } } @@ -36,13 +38,15 @@ message ModbusConfig { Modbus.Endianness endianness = 4; // 16位寄存器字节序 uint32 interval = 5; // 循环请求间隔(毫秒),0表示不主动请求,只当有变化时请求 uint32 timeout = 6; // 超时时间(毫秒) - repeated ModbusDcMapping mapping = 7; // modbus数据与驱动/采集码表映射配置 + uint32 qdl = 7; // 驱动数据字节数 + uint32 cjl = 8; // 采集数据字节数 + repeated ModbusDcMapping mapping = 10; // modbus数据与驱动/采集码表映射配置 } // 数据类型 enum DataType { - CollectTable = 0; // 采集码表 - DriveTable = 1; // 驱动码表 + CJ = 0; // 采集码表 + QD = 1; // 驱动码表 } // modbus数据与驱动/采集码表映射配置 @@ -50,7 +54,7 @@ message ModbusDcMapping { Modbus.Function function = 1; // 功能 uint32 addr = 2; // 起始地址,当功能为位功能时,表示起始位地址,当功能为寄存器功能时,表示起始字(2个字节)地址 uint32 quantity = 3; // 数量,当功能为位功能时,表示位数,当功能为寄存器功能时,表示字(2个字节)数 - Modbus.WriteStrategy writeStrategy = 4; // 当功能为写入类功能时,写策略 + // Modbus.WriteStrategy writeStrategy = 4; // 当功能为写入类功能时,写策略 DataType type = 5; // 对应数据类型 uint32 start = 6; // 驱动/采集码表中的起始下标,当功能为位功能时,表示起始位,当功能为寄存器功能时,表示起始字节 } diff --git a/proto/src/mqtt.proto b/proto/src/mqtt.proto new file mode 100644 index 0000000..25e11cb --- /dev/null +++ b/proto/src/mqtt.proto @@ -0,0 +1,58 @@ +syntax = "proto3"; + +package iot_dto; +option go_package = "./dto"; + +import "proto/src/dc.proto"; + +enum ServiceState { + Normal = 0; // 正常 + Offline = 1; // 离线 + Error = 2; // 异常 +} + +// IOT服务状态 +message IotServiceState { + // string cliId = 1; // IOT服务mqtt客户端id + ServiceState state = 2; // 服务状态 +} + +message IotQcServiceStartReq { + // string cliId = 1; // IOT服务mqtt客户端id + ModbusConfig config = 2; // 服务配置 + bool force = 5; // 是否强制启动(若存在旧的服务则停止重新启动) +} + +message IotQcServiceStopReq { + +} + +message IotQcServiceCommonResp { + // string cliId = 1; // IOT服务mqtt客户端id + int32 code = 2; // 错误码 + string msg = 3; // 错误信息 +} + +// IOT服务获取日志请求 +message IotServiceLogReq { + // string cliId = 1; // IOT服务mqtt客户端id + int32 count = 3; // 日志条数 +} + +// IOT服务日志响应 +message IotServiceLogResp { + // string cliId = 1; // IOT服务mqtt客户端id + repeated string logs = 4; // 日志 +} + +// IOT驱动数据 +message IotQd { + // string cliId = 1; // IOT服务mqtt客户端id + bytes data = 2; // 驱动数据 +} + +// IOT采集数据 +message IotCj { + // string cliId = 1; // IOT服务mqtt客户端id + bytes data = 2; // 采集数据 +} diff --git a/proto/src/mqtt/mqtt.proto b/proto/src/mqtt/mqtt.proto deleted file mode 100644 index b95d6de..0000000 --- a/proto/src/mqtt/mqtt.proto +++ /dev/null @@ -1,40 +0,0 @@ -syntax = "proto3"; - -package mqtt_api; -option go_package = "./mqtt/proto"; - - -enum ServiceState { - Normal = 0; // 正常 - Offline = 1; // 离线 -} - -// IOT服务状态 -message IotServiceState { - string code = 1; // 服务编号 - ServiceState state = 2; // 服务状态 -} - -// IOT服务获取日志请求 -message IotServiceLogReq { - string code = 1; // 服务编号 - int32 count = 3; // 日志条数 -} - -// IOT服务日志响应 -message IotServiceLogResp { - string code = 1; // 服务编号 - repeated string logs = 4; // 日志 -} - -// IOT驱动数据 -message IotQd { - string code = 1; // 服务编号 - bytes data = 2; // 驱动数据 -} - -// IOT采集数据 -message IotCj { - string code = 1; // 服务编号 - bytes data = 2; // 采集数据 -} diff --git a/server/iot_log.go b/server/iot_log.go new file mode 100644 index 0000000..d5d2da8 --- /dev/null +++ b/server/iot_log.go @@ -0,0 +1,10 @@ +package server + +import ( + "joylink.club/iot/dto" +) + +// 获取IOT日志请求响应 +func GetIotLog(req *dto.IotServiceLogReq) *dto.IotServiceLogResp { + return &dto.IotServiceLogResp{} +} diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..679047f --- /dev/null +++ b/server/server.go @@ -0,0 +1,86 @@ +package server + +import ( + "log/slog" + "os" + "time" + + "joylink.club/iot/config" + "joylink.club/iot/dto" + "joylink.club/iot/mqtt" + "joylink.club/iot/service" +) + +var iqcs *IotQcServer + +type IotQcServer struct { + qcMappingService service.IotQcMappingService + tasks []service.IScheduledTask +} + +func (s *IotQcServer) start() error { + startMqttClient() + // 注册服务请求处理 + s.registerReqHandlers() + // 启动服务状态发布定时任务 + iqcs.tasks = append(iqcs.tasks, service.NewScheduledTask(pubServerState, 1*time.Second)) + return nil +} + +func (s *IotQcServer) registerReqHandlers() { + mqtt.RegIotQcServiceStartReqHandler(s.startIotQcMappingService) + mqtt.RegIotQcServiceStopReqHandler(s.stopIotQcMappingService) + mqtt.RegIotLogReqHandler(GetIotLog) +} + +func (s *IotQcServer) startIotQcMappingService(req *dto.IotQcServiceStartReq) *dto.IotQcServiceCommonResp { + mqcs, err := service.NewModbusQcService(req.Config) + if err != nil { + slog.Error("创建Modbus驱采映射服务失败", "err", err) + return &dto.IotQcServiceCommonResp{Code: 1, Msg: err.Error()} + } + s.qcMappingService = mqcs + return &dto.IotQcServiceCommonResp{Code: 0, Msg: "成功"} +} + +func (s *IotQcServer) stopIotQcMappingService(req *dto.IotQcServiceStopReq) *dto.IotQcServiceCommonResp { + if err := s.qcMappingService.Stop(); err != nil { + slog.Error("停止Modbus驱采映射服务失败", "err", err) + return &dto.IotQcServiceCommonResp{Code: 1, Msg: err.Error()} + } + return &dto.IotQcServiceCommonResp{Code: 0, Msg: "成功"} +} + +func StartIotQcServer() { + iqcs = &IotQcServer{ + tasks: []service.IScheduledTask{}, + } + iqcs.start() +} + +func pubServerState() { + state := dto.ServiceState_Normal + mqtt.PubIotServiceState(&dto.IotServiceState{ + State: state, + }) +} + +func startMqttClient() { + config.LoadConfig() + mqttcfg := config.Cfg.Mqtt + cmc := &mqtt.IotMqttConfig{ + AppId: mqttcfg.Topic.App, + BrokerUrl: mqttcfg.Address, + ClientId: mqttcfg.ClientId, + Username: mqttcfg.Username, + Password: mqttcfg.Password, + KeepAlive: mqttcfg.KeepAlive, + ConnectRetryDelay: mqttcfg.ConnectRetryDelay, + ConnectTimeout: mqttcfg.ConnectTimeout, + } + err := mqtt.Start(cmc) + if err != nil { + slog.Error("启动MQTT客户端失败", "error", err) + os.Exit(1) + } +} diff --git a/service/api.go b/service/api.go index 927df4d..18647ec 100644 --- a/service/api.go +++ b/service/api.go @@ -24,7 +24,8 @@ type IotQcMappingService interface { WriteCjBytes(bytes []byte) error } -type QcDataHandleScheduleTask interface { +// 定时任务 +type IScheduledTask interface { // 停止 Stop() } diff --git a/service/iot_qc_service.go b/service/iot_qc_service.go new file mode 100644 index 0000000..4c3009b --- /dev/null +++ b/service/iot_qc_service.go @@ -0,0 +1,13 @@ +package service + +type iotQcService struct { + tasks []IScheduledTask +} + +var iqcs *iotQcService + +func StartIotQcService() { + iqcs = &iotQcService{ + tasks: []IScheduledTask{}, + } +} diff --git a/service/modbus_qc_mapping.go b/service/modbus_qc_mapping.go index 808e110..d87d986 100644 --- a/service/modbus_qc_mapping.go +++ b/service/modbus_qc_mapping.go @@ -7,17 +7,17 @@ import ( "strings" "time" + "joylink.club/iot/dto" "joylink.club/iot/protocol/modbus" "joylink.club/iot/service/model" - sproto "joylink.club/iot/service/proto" ) // Modbus驱采服务 type modbusQcService struct { - config *sproto.ModbusConfig + config *dto.ModbusConfig cli modbus.MasterClient qc model.QC - tasks []QcDataHandleScheduleTask + tasks []IScheduledTask stopped bool // cancel context.CancelFunc // done chan struct{} // 服务协程退出信号 @@ -28,7 +28,7 @@ func (s *modbusQcService) RegisterQcDataHandleScheduleTask(task func(), interval if s.stopped { panic(fmt.Errorf("modbus驱采映射服务已停止")) } - s.tasks = append(s.tasks, NewQcHandleTask(task, interval)) + s.tasks = append(s.tasks, NewScheduledTask(task, interval)) } func (m *modbusQcService) Stop() error { @@ -71,7 +71,7 @@ func (s *modbusQcService) WriteCjBytes(bytes []byte) error { if len(bytes) != len(s.qc.GetCjBytes()) { return fmt.Errorf("写入采集字节长度有误,应为%d,实为%d", len(s.qc.GetCjBytes()), len(bytes)) } - err := s.onWrite(sproto.DataType_CollectTable, bytes) + err := s.onWrite(dto.DataType_CJ, bytes) if err == nil { s.qc.SetCjBytes(bytes) } @@ -83,19 +83,21 @@ func (s *modbusQcService) WriteQdBytes(bytes []byte) error { if len(bytes) != len(s.qc.GetQdBytes()) { return fmt.Errorf("写入驱动字节长度有误,应为%d,实为%d", len(s.qc.GetQdBytes()), len(bytes)) } - err := s.onWrite(sproto.DataType_DriveTable, bytes) + err := s.onWrite(dto.DataType_QD, bytes) if err == nil { s.qc.SetQdBytes(bytes) } return err } -func NewModbusQcService(config *sproto.ModbusConfig, qd []byte, cj []byte) (IotQcMappingService, error) { +func NewModbusQcService(config *dto.ModbusConfig) (IotQcMappingService, error) { // 基础配置检查 if err := checkConfig(config); err != nil { return nil, err } // 映射范围配置检查 + qd := make([]byte, config.Qdl) + cj := make([]byte, config.Cjl) if err := checkConfigMappingRange(config, qd, cj); err != nil { return nil, err } @@ -127,7 +129,7 @@ func NewModbusQcService(config *sproto.ModbusConfig, qd []byte, cj []byte) (IotQ return s, nil } -func (m *modbusQcService) onWrite(dt sproto.DataType, bytes []byte) error { +func (m *modbusQcService) onWrite(dt dto.DataType, bytes []byte) error { mapping := m.config.Mapping for _, mdm := range mapping { if isWriteFunction(mdm.Function) { @@ -137,7 +139,7 @@ func (m *modbusQcService) onWrite(dt sproto.DataType, bytes []byte) error { return errors.New("数据写入失败,modbus客户端未连接") } switch mdm.Function { - case sproto.Modbus_WriteCoil, sproto.Modbus_WriteCoils, sproto.Modbus_RWCoils: + case dto.Modbus_WriteCoil, dto.Modbus_WriteCoils, dto.Modbus_RWCoils: data := getQcBits(bytes, mdm) err := m.cli.WriteCoils(uint16(mdm.Addr), data) if err != nil { @@ -146,7 +148,7 @@ func (m *modbusQcService) onWrite(dt sproto.DataType, bytes []byte) error { } else { slog.Debug("Modbus驱动采集服务写入线圈成功", "url", m.config.Url, "unitid", m.config.UnitId, "Function", mdm.Function, "data", model.BitsDebug(data), "mapping", mdm) } - case sproto.Modbus_WriteRegister, sproto.Modbus_WriteRegisters, sproto.Modbus_RWRegisters: + case dto.Modbus_WriteRegister, dto.Modbus_WriteRegisters, dto.Modbus_RWRegisters: data := getQcBytes(bytes, mdm) err := m.cli.WriteRegisterBytes(uint16(mdm.Addr), data) if err != nil { @@ -165,9 +167,9 @@ func (m *modbusQcService) onWrite(dt sproto.DataType, bytes []byte) error { // func (m *modbusQcService) initOnUpdateTask() { // mapping := m.config.Mapping // for _, mdm := range mapping { -// if mdm.WriteStrategy == sproto.Modbus_OnUpdate && isWriteFunction(mdm.Function) { +// if mdm.WriteStrategy == dto.Modbus_OnUpdate && isWriteFunction(mdm.Function) { // et := model.DCE_Drive_Update -// if mdm.Type == sproto.DataType_CollectTable { +// if mdm.Type == dto.DataType_CollectTable { // et = model.DCE_Collect_Update // } // m.qc.On(et, func(d model.QC) { @@ -176,14 +178,14 @@ func (m *modbusQcService) onWrite(dt sproto.DataType, bytes []byte) error { // return // } // switch mdm.Function { -// case sproto.Modbus_WriteCoil, sproto.Modbus_WriteCoils, sproto.Modbus_RWCoils: +// case dto.Modbus_WriteCoil, dto.Modbus_WriteCoils, dto.Modbus_RWCoils: // err := m.cli.WriteCoils(uint16(mdm.Addr), m.GetDcBits(mdm)) // if err != nil { // slog.Error("Modbus驱动采集服务写入线圈失败", "url", m.config.Url, "error", err, "Function", mdm.Function) // } else { // slog.Info("Modbus驱动采集服务写入线圈成功", "url", m.config.Url, "Function", mdm.Function) // } -// case sproto.Modbus_WriteRegister, sproto.Modbus_WriteRegisters, sproto.Modbus_RWRegisters: +// case dto.Modbus_WriteRegister, dto.Modbus_WriteRegisters, dto.Modbus_RWRegisters: // err := m.cli.WriteRegisterBytes(uint16(mdm.Addr), m.GetDcBytes(mdm)) // if err != nil { // slog.Error("Modbus驱动采集服务写入寄存器失败", "url", m.config.Url, "error", err, "Function", mdm.Function) @@ -216,7 +218,7 @@ func (m *modbusQcService) readTaskExecute() { if m.cli.IsConnected() { for _, mdm := range m.config.Mapping { switch mdm.Function { - case sproto.Modbus_ReadCoil, sproto.Modbus_RWCoils: + case dto.Modbus_ReadCoil, dto.Modbus_RWCoils: data, err := m.cli.ReadCoil(uint16(mdm.Addr), uint16(mdm.Quantity)) if err != nil { slog.Error("Modbus驱动采集服务读取线圈失败", "url", m.config.Url, "unitid", m.config.UnitId, "error", err) @@ -227,7 +229,7 @@ func (m *modbusQcService) readTaskExecute() { slog.Error("Modbus驱动采集服务更新驱采数据失败", "url", m.config.Url, "unitid", m.config.UnitId, "error", err) continue } - case sproto.Modbus_ReadDiscreteInput: + case dto.Modbus_ReadDiscreteInput: data, err := m.cli.ReadDiscreteInput(uint16(mdm.Addr), uint16(mdm.Quantity)) if err != nil { slog.Error("Modbus驱动采集服务读取离散输入失败", "url", m.config.Url, "unitid", m.config.UnitId, "error", err) @@ -238,7 +240,7 @@ func (m *modbusQcService) readTaskExecute() { slog.Error("Modbus驱动采集服务更新驱采数据失败", "url", m.config.Url, "unitid", m.config.UnitId, "error", err) continue } - case sproto.Modbus_ReadHoldingRegister, sproto.Modbus_RWRegisters: + case dto.Modbus_ReadHoldingRegister, dto.Modbus_RWRegisters: data, err := m.cli.ReadHoldingRegisterBytes(uint16(mdm.Addr), uint16(mdm.Quantity*2)) if err != nil { slog.Error("Modbus驱动采集服务读取保持寄存器失败", "url", m.config.Url, "unitid", m.config.UnitId, "error", err) @@ -249,7 +251,7 @@ func (m *modbusQcService) readTaskExecute() { slog.Error("Modbus驱动采集服务更新驱采数据失败", "url", m.config.Url, "unitid", m.config.UnitId, "error", err) continue } - case sproto.Modbus_ReadInputRegister: + case dto.Modbus_ReadInputRegister: data, err := m.cli.ReadInputRegisterBytes(uint16(mdm.Addr), uint16(mdm.Quantity*2)) if err != nil { slog.Error("Modbus驱动采集服务读取输入寄存器失败", "url", m.config.Url, "unitid", m.config.UnitId, "error", err) @@ -260,8 +262,8 @@ func (m *modbusQcService) readTaskExecute() { slog.Error("Modbus驱动采集服务更新驱采数据失败", "url", m.config.Url, "unitid", m.config.UnitId, "error", err) continue } - // case sproto.Modbus_WriteCoil: - // if mdm.WriteStrategy == sproto.Modbus_OnScheduled { // 定时写 + // case dto.Modbus_WriteCoil: + // if mdm.WriteStrategy == dto.Modbus_OnScheduled { // 定时写 // bits := m.GetDcBits(mdm) // err := m.cli.WriteCoil(uint16(mdm.Addr), bits[0]) // if err != nil { @@ -269,8 +271,8 @@ func (m *modbusQcService) readTaskExecute() { // continue // } // } - // case sproto.Modbus_WriteCoils: - // if mdm.WriteStrategy == sproto.Modbus_OnScheduled { // 定时写 + // case dto.Modbus_WriteCoils: + // if mdm.WriteStrategy == dto.Modbus_OnScheduled { // 定时写 // bits := m.GetDcBits(mdm) // err := m.cli.WriteCoils(uint16(mdm.Addr), bits) // if err != nil { @@ -278,8 +280,8 @@ func (m *modbusQcService) readTaskExecute() { // continue // } // } - // case sproto.Modbus_WriteRegister: - // if mdm.WriteStrategy == sproto.Modbus_OnScheduled { // 定时写 + // case dto.Modbus_WriteRegister: + // if mdm.WriteStrategy == dto.Modbus_OnScheduled { // 定时写 // data := m.GetDcBytes(mdm) // err := m.cli.WriteRegisterBytes(uint16(mdm.Addr), data) // if err != nil { @@ -287,8 +289,8 @@ func (m *modbusQcService) readTaskExecute() { // continue // } // } - // case sproto.Modbus_WriteRegisters: - // if mdm.WriteStrategy == sproto.Modbus_OnScheduled { // 定时写 + // case dto.Modbus_WriteRegisters: + // if mdm.WriteStrategy == dto.Modbus_OnScheduled { // 定时写 // data := m.GetDcBytes(mdm) // err := m.cli.WriteRegisterBytes(uint16(mdm.Addr), data) // if err != nil { @@ -303,7 +305,7 @@ func (m *modbusQcService) readTaskExecute() { } } -func getQcBits(bytes []byte, mdm *sproto.ModbusDcMapping) []bool { +func getQcBits(bytes []byte, mdm *dto.ModbusDcMapping) []bool { bits := model.DecodeBools(bytes) start := mdm.Start quantity := mdm.Quantity @@ -313,7 +315,7 @@ func getQcBits(bytes []byte, mdm *sproto.ModbusDcMapping) []bool { return bits[start : start+quantity] } -func getQcBytes(bytes []byte, mdm *sproto.ModbusDcMapping) []byte { +func getQcBytes(bytes []byte, mdm *dto.ModbusDcMapping) []byte { start := mdm.Start quantity := mdm.Quantity * 2 if start+quantity > uint32(len(bytes)) { @@ -322,49 +324,49 @@ func getQcBytes(bytes []byte, mdm *sproto.ModbusDcMapping) []byte { return bytes[start : start+quantity] } -// func (m *modbusQcService) GetDcBits(mdm *sproto.ModbusDcMapping) []bool { +// func (m *modbusQcService) GetDcBits(mdm *dto.ModbusDcMapping) []bool { // switch mdm.Type { -// case sproto.DataType_CollectTable: // 采集数据 +// case dto.DataType_CollectTable: // 采集数据 // return m.qc.GetCjBitsOf(mdm.Start, mdm.Quantity) -// case sproto.DataType_DriveTable: // 驱动数据 +// case dto.DataType_DriveTable: // 驱动数据 // return m.qc.GetQdBitsOf(mdm.Start, mdm.Quantity) // default: // panic("未知数据类型") // } // } -// func (m *modbusQcService) GetDcBytes(mdm *sproto.ModbusDcMapping) []byte { +// func (m *modbusQcService) GetDcBytes(mdm *dto.ModbusDcMapping) []byte { // switch mdm.Type { -// case sproto.DataType_CollectTable: // 采集数据 +// case dto.DataType_CollectTable: // 采集数据 // return m.qc.GetCjBytesOf(mdm.Start, mdm.Quantity*2) -// case sproto.DataType_DriveTable: // 驱动数据 +// case dto.DataType_DriveTable: // 驱动数据 // return m.qc.GetQdBytesOf(mdm.Start, mdm.Quantity*2) // default: // panic("未知数据类型") // } // } -func (m *modbusQcService) updateDcByBits(mdm *sproto.ModbusDcMapping, bits []bool) error { +func (m *modbusQcService) updateDcByBits(mdm *dto.ModbusDcMapping, bits []bool) error { switch mdm.Type { - case sproto.DataType_CollectTable: // 采集数据 + case dto.DataType_CJ: // 采集数据 return m.qc.UpdateCjByBits(mdm.Start, bits) - case sproto.DataType_DriveTable: // 驱动数据 + case dto.DataType_QD: // 驱动数据 return m.qc.UpdateQdByBits(mdm.Start, bits) } return nil } -func (m *modbusQcService) updateDcByBytes(mdm *sproto.ModbusDcMapping, bytes []byte) error { +func (m *modbusQcService) updateDcByBytes(mdm *dto.ModbusDcMapping, bytes []byte) error { switch mdm.Type { - case sproto.DataType_CollectTable: // 采集数据 + case dto.DataType_CJ: // 采集数据 return m.qc.UpdateCjByBytes(mdm.Start, bytes) - case sproto.DataType_DriveTable: // 驱动数据 + case dto.DataType_QD: // 驱动数据 return m.qc.UpdateQdByBytes(mdm.Start, bytes) } return nil } -func checkConfig(config *sproto.ModbusConfig) error { +func checkConfig(config *dto.ModbusConfig) error { if config.Url == "" { return fmt.Errorf("Modbus配置未设置url") } @@ -383,14 +385,14 @@ func checkConfig(config *sproto.ModbusConfig) error { return nil } -func checkConfigMappingRange(modbusConfig *sproto.ModbusConfig, qd []byte, cj []byte) error { +func checkConfigMappingRange(modbusConfig *dto.ModbusConfig, qd []byte, cj []byte) error { for _, mdm := range modbusConfig.Mapping { - if mdm.Type == sproto.DataType_CollectTable { + if mdm.Type == dto.DataType_CJ { err := checkMappingOutRange(mdm, cj) if err != nil { return err } - } else if mdm.Type == sproto.DataType_DriveTable { + } else if mdm.Type == dto.DataType_QD { err := checkMappingOutRange(mdm, qd) if err != nil { return err @@ -400,7 +402,7 @@ func checkConfigMappingRange(modbusConfig *sproto.ModbusConfig, qd []byte, cj [] return nil } -func checkMappingOutRange(mdm *sproto.ModbusDcMapping, bytes []byte) error { +func checkMappingOutRange(mdm *dto.ModbusDcMapping, bytes []byte) error { f := mdm.Function start := mdm.Start quantity := mdm.Quantity @@ -418,37 +420,37 @@ func checkMappingOutRange(mdm *sproto.ModbusDcMapping, bytes []byte) error { return nil } -func convertEndianness(endianness sproto.Modbus_Endianness) modbus.Endianness { +func convertEndianness(endianness dto.Modbus_Endianness) modbus.Endianness { switch endianness { - case sproto.Modbus_BigEndian: + case dto.Modbus_BigEndian: return modbus.BigEndian - case sproto.Modbus_LittleEndian: + case dto.Modbus_LittleEndian: return modbus.LittleEndian } return modbus.BigEndian } -func isWriteFunction(modbus_Function sproto.Modbus_Function) bool { - return modbus_Function == sproto.Modbus_WriteCoil || - modbus_Function == sproto.Modbus_WriteCoils || - modbus_Function == sproto.Modbus_WriteRegister || - modbus_Function == sproto.Modbus_WriteRegisters || - modbus_Function == sproto.Modbus_RWCoils || - modbus_Function == sproto.Modbus_RWRegisters +func isWriteFunction(modbus_Function dto.Modbus_Function) bool { + return modbus_Function == dto.Modbus_WriteCoil || + modbus_Function == dto.Modbus_WriteCoils || + modbus_Function == dto.Modbus_WriteRegister || + modbus_Function == dto.Modbus_WriteRegisters || + modbus_Function == dto.Modbus_RWCoils || + modbus_Function == dto.Modbus_RWRegisters } -func isCoilFunction(modbus_Function sproto.Modbus_Function) bool { - return modbus_Function == sproto.Modbus_ReadCoil || - modbus_Function == sproto.Modbus_ReadDiscreteInput || - modbus_Function == sproto.Modbus_RWCoils || - modbus_Function == sproto.Modbus_WriteCoil || - modbus_Function == sproto.Modbus_WriteCoils +func isCoilFunction(modbus_Function dto.Modbus_Function) bool { + return modbus_Function == dto.Modbus_ReadCoil || + modbus_Function == dto.Modbus_ReadDiscreteInput || + modbus_Function == dto.Modbus_RWCoils || + modbus_Function == dto.Modbus_WriteCoil || + modbus_Function == dto.Modbus_WriteCoils } -// func isRegisterFunction(modbus_Function sproto.Modbus_Function) bool { -// return modbus_Function == sproto.Modbus_ReadInputRegister || -// modbus_Function == sproto.Modbus_ReadHoldingRegister || -// modbus_Function == sproto.Modbus_RWRegisters || -// modbus_Function == sproto.Modbus_WriteRegister || -// modbus_Function == sproto.Modbus_WriteRegisters +// func isRegisterFunction(modbus_Function dto.Modbus_Function) bool { +// return modbus_Function == dto.Modbus_ReadInputRegister || +// modbus_Function == dto.Modbus_ReadHoldingRegister || +// modbus_Function == dto.Modbus_RWRegisters || +// modbus_Function == dto.Modbus_WriteRegister || +// modbus_Function == dto.Modbus_WriteRegisters // } diff --git a/service/proto/dc.pb.go b/service/proto/dc.pb.go deleted file mode 100644 index 72274db..0000000 --- a/service/proto/dc.pb.go +++ /dev/null @@ -1,609 +0,0 @@ -// Code generated by protoc-gen-go. DO NOT EDIT. -// versions: -// protoc-gen-go v1.31.0 -// protoc v4.23.1 -// source: dc.proto - -package proto - -import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" - reflect "reflect" - sync "sync" -) - -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 DataType int32 - -const ( - DataType_CollectTable DataType = 0 // 采集码表 - DataType_DriveTable DataType = 1 // 驱动码表 -) - -// Enum value maps for DataType. -var ( - DataType_name = map[int32]string{ - 0: "CollectTable", - 1: "DriveTable", - } - DataType_value = map[string]int32{ - "CollectTable": 0, - "DriveTable": 1, - } -) - -func (x DataType) Enum() *DataType { - p := new(DataType) - *p = x - return p -} - -func (x DataType) String() string { - return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) -} - -func (DataType) Descriptor() protoreflect.EnumDescriptor { - return file_dc_proto_enumTypes[0].Descriptor() -} - -func (DataType) Type() protoreflect.EnumType { - return &file_dc_proto_enumTypes[0] -} - -func (x DataType) Number() protoreflect.EnumNumber { - return protoreflect.EnumNumber(x) -} - -// Deprecated: Use DataType.Descriptor instead. -func (DataType) EnumDescriptor() ([]byte, []int) { - return file_dc_proto_rawDescGZIP(), []int{0} -} - -// Modbus功能 -type Modbus_Function int32 - -const ( - Modbus_ReadCoil Modbus_Function = 0 // 读线圈 - Modbus_ReadDiscreteInput Modbus_Function = 1 // 读离散输入 - Modbus_ReadHoldingRegister Modbus_Function = 2 // 读保持寄存器 - Modbus_ReadInputRegister Modbus_Function = 3 // 读输入寄存器 - Modbus_WriteCoil Modbus_Function = 10 // 写单个线圈 - Modbus_WriteCoils Modbus_Function = 11 // 写多个线圈 - Modbus_WriteRegister Modbus_Function = 12 // 写单个寄存器 - Modbus_WriteRegisters Modbus_Function = 13 // 写多个寄存器 - Modbus_RWCoils Modbus_Function = 20 // 读写多个线圈,定时读,当映射数据更新时执行写操作 - Modbus_RWRegisters Modbus_Function = 21 // 读写多个寄存器,定时读,当映射数据更新时执行写操作 -) - -// Enum value maps for Modbus_Function. -var ( - Modbus_Function_name = map[int32]string{ - 0: "ReadCoil", - 1: "ReadDiscreteInput", - 2: "ReadHoldingRegister", - 3: "ReadInputRegister", - 10: "WriteCoil", - 11: "WriteCoils", - 12: "WriteRegister", - 13: "WriteRegisters", - 20: "RWCoils", - 21: "RWRegisters", - } - Modbus_Function_value = map[string]int32{ - "ReadCoil": 0, - "ReadDiscreteInput": 1, - "ReadHoldingRegister": 2, - "ReadInputRegister": 3, - "WriteCoil": 10, - "WriteCoils": 11, - "WriteRegister": 12, - "WriteRegisters": 13, - "RWCoils": 20, - "RWRegisters": 21, - } -) - -func (x Modbus_Function) Enum() *Modbus_Function { - p := new(Modbus_Function) - *p = x - return p -} - -func (x Modbus_Function) String() string { - return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) -} - -func (Modbus_Function) Descriptor() protoreflect.EnumDescriptor { - return file_dc_proto_enumTypes[1].Descriptor() -} - -func (Modbus_Function) Type() protoreflect.EnumType { - return &file_dc_proto_enumTypes[1] -} - -func (x Modbus_Function) Number() protoreflect.EnumNumber { - return protoreflect.EnumNumber(x) -} - -// Deprecated: Use Modbus_Function.Descriptor instead. -func (Modbus_Function) EnumDescriptor() ([]byte, []int) { - return file_dc_proto_rawDescGZIP(), []int{0, 0} -} - -// Modbus服务写策略 -type Modbus_WriteStrategy int32 - -const ( - Modbus_OnUpdate Modbus_WriteStrategy = 0 // 数据更新时写 - Modbus_OnScheduled Modbus_WriteStrategy = 1 // 定时写 -) - -// Enum value maps for Modbus_WriteStrategy. -var ( - Modbus_WriteStrategy_name = map[int32]string{ - 0: "OnUpdate", - 1: "OnScheduled", - } - Modbus_WriteStrategy_value = map[string]int32{ - "OnUpdate": 0, - "OnScheduled": 1, - } -) - -func (x Modbus_WriteStrategy) Enum() *Modbus_WriteStrategy { - p := new(Modbus_WriteStrategy) - *p = x - return p -} - -func (x Modbus_WriteStrategy) String() string { - return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) -} - -func (Modbus_WriteStrategy) Descriptor() protoreflect.EnumDescriptor { - return file_dc_proto_enumTypes[2].Descriptor() -} - -func (Modbus_WriteStrategy) Type() protoreflect.EnumType { - return &file_dc_proto_enumTypes[2] -} - -func (x Modbus_WriteStrategy) Number() protoreflect.EnumNumber { - return protoreflect.EnumNumber(x) -} - -// Deprecated: Use Modbus_WriteStrategy.Descriptor instead. -func (Modbus_WriteStrategy) EnumDescriptor() ([]byte, []int) { - return file_dc_proto_rawDescGZIP(), []int{0, 1} -} - -// 寄存器字节序 -type Modbus_Endianness int32 - -const ( - Modbus_BigEndian Modbus_Endianness = 0 - Modbus_LittleEndian Modbus_Endianness = 1 -) - -// Enum value maps for Modbus_Endianness. -var ( - Modbus_Endianness_name = map[int32]string{ - 0: "BigEndian", - 1: "LittleEndian", - } - Modbus_Endianness_value = map[string]int32{ - "BigEndian": 0, - "LittleEndian": 1, - } -) - -func (x Modbus_Endianness) Enum() *Modbus_Endianness { - p := new(Modbus_Endianness) - *p = x - return p -} - -func (x Modbus_Endianness) String() string { - return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) -} - -func (Modbus_Endianness) Descriptor() protoreflect.EnumDescriptor { - return file_dc_proto_enumTypes[3].Descriptor() -} - -func (Modbus_Endianness) Type() protoreflect.EnumType { - return &file_dc_proto_enumTypes[3] -} - -func (x Modbus_Endianness) Number() protoreflect.EnumNumber { - return protoreflect.EnumNumber(x) -} - -// Deprecated: Use Modbus_Endianness.Descriptor instead. -func (Modbus_Endianness) EnumDescriptor() ([]byte, []int) { - return file_dc_proto_rawDescGZIP(), []int{0, 2} -} - -type Modbus struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields -} - -func (x *Modbus) Reset() { - *x = Modbus{} - if protoimpl.UnsafeEnabled { - mi := &file_dc_proto_msgTypes[0] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *Modbus) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*Modbus) ProtoMessage() {} - -func (x *Modbus) ProtoReflect() protoreflect.Message { - mi := &file_dc_proto_msgTypes[0] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use Modbus.ProtoReflect.Descriptor instead. -func (*Modbus) Descriptor() ([]byte, []int) { - return file_dc_proto_rawDescGZIP(), []int{0} -} - -// modbus任务配置 -type ModbusConfig struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Url string `protobuf:"bytes,2,opt,name=url,proto3" json:"url,omitempty"` // 连接地址 - UnitId uint32 `protobuf:"varint,3,opt,name=unitId,proto3" json:"unitId,omitempty"` // 从机unitId - Endianness Modbus_Endianness `protobuf:"varint,4,opt,name=endianness,proto3,enum=iot_service.Modbus_Endianness" json:"endianness,omitempty"` // 16位寄存器字节序 - Interval uint32 `protobuf:"varint,5,opt,name=interval,proto3" json:"interval,omitempty"` // 循环请求间隔(毫秒),0表示不主动请求,只当有变化时请求 - Timeout uint32 `protobuf:"varint,6,opt,name=timeout,proto3" json:"timeout,omitempty"` // 超时时间(毫秒) - Mapping []*ModbusDcMapping `protobuf:"bytes,7,rep,name=mapping,proto3" json:"mapping,omitempty"` // modbus数据与驱动/采集码表映射配置 -} - -func (x *ModbusConfig) Reset() { - *x = ModbusConfig{} - if protoimpl.UnsafeEnabled { - mi := &file_dc_proto_msgTypes[1] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *ModbusConfig) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*ModbusConfig) ProtoMessage() {} - -func (x *ModbusConfig) ProtoReflect() protoreflect.Message { - mi := &file_dc_proto_msgTypes[1] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use ModbusConfig.ProtoReflect.Descriptor instead. -func (*ModbusConfig) Descriptor() ([]byte, []int) { - return file_dc_proto_rawDescGZIP(), []int{1} -} - -func (x *ModbusConfig) GetUrl() string { - if x != nil { - return x.Url - } - return "" -} - -func (x *ModbusConfig) GetUnitId() uint32 { - if x != nil { - return x.UnitId - } - return 0 -} - -func (x *ModbusConfig) GetEndianness() Modbus_Endianness { - if x != nil { - return x.Endianness - } - return Modbus_BigEndian -} - -func (x *ModbusConfig) GetInterval() uint32 { - if x != nil { - return x.Interval - } - return 0 -} - -func (x *ModbusConfig) GetTimeout() uint32 { - if x != nil { - return x.Timeout - } - return 0 -} - -func (x *ModbusConfig) GetMapping() []*ModbusDcMapping { - if x != nil { - return x.Mapping - } - return nil -} - -// modbus数据与驱动/采集码表映射配置 -type ModbusDcMapping struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Function Modbus_Function `protobuf:"varint,1,opt,name=function,proto3,enum=iot_service.Modbus_Function" json:"function,omitempty"` // 功能 - Addr uint32 `protobuf:"varint,2,opt,name=addr,proto3" json:"addr,omitempty"` // 起始地址,当功能为位功能时,表示起始位地址,当功能为寄存器功能时,表示起始字(2个字节)地址 - Quantity uint32 `protobuf:"varint,3,opt,name=quantity,proto3" json:"quantity,omitempty"` // 数量,当功能为位功能时,表示位数,当功能为寄存器功能时,表示字(2个字节)数 - WriteStrategy Modbus_WriteStrategy `protobuf:"varint,4,opt,name=writeStrategy,proto3,enum=iot_service.Modbus_WriteStrategy" json:"writeStrategy,omitempty"` // 当功能为写入类功能时,写策略 - Type DataType `protobuf:"varint,5,opt,name=type,proto3,enum=iot_service.DataType" json:"type,omitempty"` // 对应数据类型 - Start uint32 `protobuf:"varint,6,opt,name=start,proto3" json:"start,omitempty"` // 驱动/采集码表中的起始下标,当功能为位功能时,表示起始位,当功能为寄存器功能时,表示起始字节 -} - -func (x *ModbusDcMapping) Reset() { - *x = ModbusDcMapping{} - if protoimpl.UnsafeEnabled { - mi := &file_dc_proto_msgTypes[2] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *ModbusDcMapping) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*ModbusDcMapping) ProtoMessage() {} - -func (x *ModbusDcMapping) ProtoReflect() protoreflect.Message { - mi := &file_dc_proto_msgTypes[2] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use ModbusDcMapping.ProtoReflect.Descriptor instead. -func (*ModbusDcMapping) Descriptor() ([]byte, []int) { - return file_dc_proto_rawDescGZIP(), []int{2} -} - -func (x *ModbusDcMapping) GetFunction() Modbus_Function { - if x != nil { - return x.Function - } - return Modbus_ReadCoil -} - -func (x *ModbusDcMapping) GetAddr() uint32 { - if x != nil { - return x.Addr - } - return 0 -} - -func (x *ModbusDcMapping) GetQuantity() uint32 { - if x != nil { - return x.Quantity - } - return 0 -} - -func (x *ModbusDcMapping) GetWriteStrategy() Modbus_WriteStrategy { - if x != nil { - return x.WriteStrategy - } - return Modbus_OnUpdate -} - -func (x *ModbusDcMapping) GetType() DataType { - if x != nil { - return x.Type - } - return DataType_CollectTable -} - -func (x *ModbusDcMapping) GetStart() uint32 { - if x != nil { - return x.Start - } - return 0 -} - -var File_dc_proto protoreflect.FileDescriptor - -var file_dc_proto_rawDesc = []byte{ - 0x0a, 0x08, 0x64, 0x63, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0b, 0x69, 0x6f, 0x74, 0x5f, - 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x22, 0xad, 0x02, 0x0a, 0x06, 0x4d, 0x6f, 0x64, 0x62, - 0x75, 0x73, 0x22, 0xc3, 0x01, 0x0a, 0x08, 0x46, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x12, - 0x0c, 0x0a, 0x08, 0x52, 0x65, 0x61, 0x64, 0x43, 0x6f, 0x69, 0x6c, 0x10, 0x00, 0x12, 0x15, 0x0a, - 0x11, 0x52, 0x65, 0x61, 0x64, 0x44, 0x69, 0x73, 0x63, 0x72, 0x65, 0x74, 0x65, 0x49, 0x6e, 0x70, - 0x75, 0x74, 0x10, 0x01, 0x12, 0x17, 0x0a, 0x13, 0x52, 0x65, 0x61, 0x64, 0x48, 0x6f, 0x6c, 0x64, - 0x69, 0x6e, 0x67, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x10, 0x02, 0x12, 0x15, 0x0a, - 0x11, 0x52, 0x65, 0x61, 0x64, 0x49, 0x6e, 0x70, 0x75, 0x74, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, - 0x65, 0x72, 0x10, 0x03, 0x12, 0x0d, 0x0a, 0x09, 0x57, 0x72, 0x69, 0x74, 0x65, 0x43, 0x6f, 0x69, - 0x6c, 0x10, 0x0a, 0x12, 0x0e, 0x0a, 0x0a, 0x57, 0x72, 0x69, 0x74, 0x65, 0x43, 0x6f, 0x69, 0x6c, - 0x73, 0x10, 0x0b, 0x12, 0x11, 0x0a, 0x0d, 0x57, 0x72, 0x69, 0x74, 0x65, 0x52, 0x65, 0x67, 0x69, - 0x73, 0x74, 0x65, 0x72, 0x10, 0x0c, 0x12, 0x12, 0x0a, 0x0e, 0x57, 0x72, 0x69, 0x74, 0x65, 0x52, - 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x73, 0x10, 0x0d, 0x12, 0x0b, 0x0a, 0x07, 0x52, 0x57, - 0x43, 0x6f, 0x69, 0x6c, 0x73, 0x10, 0x14, 0x12, 0x0f, 0x0a, 0x0b, 0x52, 0x57, 0x52, 0x65, 0x67, - 0x69, 0x73, 0x74, 0x65, 0x72, 0x73, 0x10, 0x15, 0x22, 0x2e, 0x0a, 0x0d, 0x57, 0x72, 0x69, 0x74, - 0x65, 0x53, 0x74, 0x72, 0x61, 0x74, 0x65, 0x67, 0x79, 0x12, 0x0c, 0x0a, 0x08, 0x4f, 0x6e, 0x55, - 0x70, 0x64, 0x61, 0x74, 0x65, 0x10, 0x00, 0x12, 0x0f, 0x0a, 0x0b, 0x4f, 0x6e, 0x53, 0x63, 0x68, - 0x65, 0x64, 0x75, 0x6c, 0x65, 0x64, 0x10, 0x01, 0x22, 0x2d, 0x0a, 0x0a, 0x45, 0x6e, 0x64, 0x69, - 0x61, 0x6e, 0x6e, 0x65, 0x73, 0x73, 0x12, 0x0d, 0x0a, 0x09, 0x42, 0x69, 0x67, 0x45, 0x6e, 0x64, - 0x69, 0x61, 0x6e, 0x10, 0x00, 0x12, 0x10, 0x0a, 0x0c, 0x4c, 0x69, 0x74, 0x74, 0x6c, 0x65, 0x45, - 0x6e, 0x64, 0x69, 0x61, 0x6e, 0x10, 0x01, 0x22, 0xe6, 0x01, 0x0a, 0x0c, 0x4d, 0x6f, 0x64, 0x62, - 0x75, 0x73, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x72, 0x6c, 0x18, - 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x72, 0x6c, 0x12, 0x16, 0x0a, 0x06, 0x75, 0x6e, - 0x69, 0x74, 0x49, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x75, 0x6e, 0x69, 0x74, - 0x49, 0x64, 0x12, 0x3e, 0x0a, 0x0a, 0x65, 0x6e, 0x64, 0x69, 0x61, 0x6e, 0x6e, 0x65, 0x73, 0x73, - 0x18, 0x04, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x1e, 0x2e, 0x69, 0x6f, 0x74, 0x5f, 0x73, 0x65, 0x72, - 0x76, 0x69, 0x63, 0x65, 0x2e, 0x4d, 0x6f, 0x64, 0x62, 0x75, 0x73, 0x2e, 0x45, 0x6e, 0x64, 0x69, - 0x61, 0x6e, 0x6e, 0x65, 0x73, 0x73, 0x52, 0x0a, 0x65, 0x6e, 0x64, 0x69, 0x61, 0x6e, 0x6e, 0x65, - 0x73, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x76, 0x61, 0x6c, 0x18, 0x05, - 0x20, 0x01, 0x28, 0x0d, 0x52, 0x08, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x76, 0x61, 0x6c, 0x12, 0x18, - 0x0a, 0x07, 0x74, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0d, 0x52, - 0x07, 0x74, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x12, 0x36, 0x0a, 0x07, 0x6d, 0x61, 0x70, 0x70, - 0x69, 0x6e, 0x67, 0x18, 0x07, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1c, 0x2e, 0x69, 0x6f, 0x74, 0x5f, - 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x4d, 0x6f, 0x64, 0x62, 0x75, 0x73, 0x44, 0x63, - 0x4d, 0x61, 0x70, 0x70, 0x69, 0x6e, 0x67, 0x52, 0x07, 0x6d, 0x61, 0x70, 0x70, 0x69, 0x6e, 0x67, - 0x22, 0x85, 0x02, 0x0a, 0x0f, 0x4d, 0x6f, 0x64, 0x62, 0x75, 0x73, 0x44, 0x63, 0x4d, 0x61, 0x70, - 0x70, 0x69, 0x6e, 0x67, 0x12, 0x38, 0x0a, 0x08, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x1c, 0x2e, 0x69, 0x6f, 0x74, 0x5f, 0x73, 0x65, 0x72, - 0x76, 0x69, 0x63, 0x65, 0x2e, 0x4d, 0x6f, 0x64, 0x62, 0x75, 0x73, 0x2e, 0x46, 0x75, 0x6e, 0x63, - 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x08, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x12, - 0x0a, 0x04, 0x61, 0x64, 0x64, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, 0x61, 0x64, - 0x64, 0x72, 0x12, 0x1a, 0x0a, 0x08, 0x71, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x18, 0x03, - 0x20, 0x01, 0x28, 0x0d, 0x52, 0x08, 0x71, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x12, 0x47, - 0x0a, 0x0d, 0x77, 0x72, 0x69, 0x74, 0x65, 0x53, 0x74, 0x72, 0x61, 0x74, 0x65, 0x67, 0x79, 0x18, - 0x04, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x21, 0x2e, 0x69, 0x6f, 0x74, 0x5f, 0x73, 0x65, 0x72, 0x76, - 0x69, 0x63, 0x65, 0x2e, 0x4d, 0x6f, 0x64, 0x62, 0x75, 0x73, 0x2e, 0x57, 0x72, 0x69, 0x74, 0x65, - 0x53, 0x74, 0x72, 0x61, 0x74, 0x65, 0x67, 0x79, 0x52, 0x0d, 0x77, 0x72, 0x69, 0x74, 0x65, 0x53, - 0x74, 0x72, 0x61, 0x74, 0x65, 0x67, 0x79, 0x12, 0x29, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, - 0x05, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x15, 0x2e, 0x69, 0x6f, 0x74, 0x5f, 0x73, 0x65, 0x72, 0x76, - 0x69, 0x63, 0x65, 0x2e, 0x44, 0x61, 0x74, 0x61, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74, 0x79, - 0x70, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x72, 0x74, 0x18, 0x06, 0x20, 0x01, 0x28, - 0x0d, 0x52, 0x05, 0x73, 0x74, 0x61, 0x72, 0x74, 0x2a, 0x2c, 0x0a, 0x08, 0x44, 0x61, 0x74, 0x61, - 0x54, 0x79, 0x70, 0x65, 0x12, 0x10, 0x0a, 0x0c, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x54, - 0x61, 0x62, 0x6c, 0x65, 0x10, 0x00, 0x12, 0x0e, 0x0a, 0x0a, 0x44, 0x72, 0x69, 0x76, 0x65, 0x54, - 0x61, 0x62, 0x6c, 0x65, 0x10, 0x01, 0x42, 0x11, 0x5a, 0x0f, 0x2e, 0x2f, 0x73, 0x65, 0x72, 0x76, - 0x69, 0x63, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x33, -} - -var ( - file_dc_proto_rawDescOnce sync.Once - file_dc_proto_rawDescData = file_dc_proto_rawDesc -) - -func file_dc_proto_rawDescGZIP() []byte { - file_dc_proto_rawDescOnce.Do(func() { - file_dc_proto_rawDescData = protoimpl.X.CompressGZIP(file_dc_proto_rawDescData) - }) - return file_dc_proto_rawDescData -} - -var file_dc_proto_enumTypes = make([]protoimpl.EnumInfo, 4) -var file_dc_proto_msgTypes = make([]protoimpl.MessageInfo, 3) -var file_dc_proto_goTypes = []interface{}{ - (DataType)(0), // 0: iot_service.DataType - (Modbus_Function)(0), // 1: iot_service.Modbus.Function - (Modbus_WriteStrategy)(0), // 2: iot_service.Modbus.WriteStrategy - (Modbus_Endianness)(0), // 3: iot_service.Modbus.Endianness - (*Modbus)(nil), // 4: iot_service.Modbus - (*ModbusConfig)(nil), // 5: iot_service.ModbusConfig - (*ModbusDcMapping)(nil), // 6: iot_service.ModbusDcMapping -} -var file_dc_proto_depIdxs = []int32{ - 3, // 0: iot_service.ModbusConfig.endianness:type_name -> iot_service.Modbus.Endianness - 6, // 1: iot_service.ModbusConfig.mapping:type_name -> iot_service.ModbusDcMapping - 1, // 2: iot_service.ModbusDcMapping.function:type_name -> iot_service.Modbus.Function - 2, // 3: iot_service.ModbusDcMapping.writeStrategy:type_name -> iot_service.Modbus.WriteStrategy - 0, // 4: iot_service.ModbusDcMapping.type:type_name -> iot_service.DataType - 5, // [5:5] is the sub-list for method output_type - 5, // [5:5] is the sub-list for method input_type - 5, // [5:5] is the sub-list for extension type_name - 5, // [5:5] is the sub-list for extension extendee - 0, // [0:5] is the sub-list for field type_name -} - -func init() { file_dc_proto_init() } -func file_dc_proto_init() { - if File_dc_proto != nil { - return - } - if !protoimpl.UnsafeEnabled { - file_dc_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Modbus); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - file_dc_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*ModbusConfig); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - file_dc_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*ModbusDcMapping); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - } - type x struct{} - out := protoimpl.TypeBuilder{ - File: protoimpl.DescBuilder{ - GoPackagePath: reflect.TypeOf(x{}).PkgPath(), - RawDescriptor: file_dc_proto_rawDesc, - NumEnums: 4, - NumMessages: 3, - NumExtensions: 0, - NumServices: 0, - }, - GoTypes: file_dc_proto_goTypes, - DependencyIndexes: file_dc_proto_depIdxs, - EnumInfos: file_dc_proto_enumTypes, - MessageInfos: file_dc_proto_msgTypes, - }.Build() - File_dc_proto = out.File - file_dc_proto_rawDesc = nil - file_dc_proto_goTypes = nil - file_dc_proto_depIdxs = nil -} diff --git a/service/qc_handle_task.go b/service/scheduled_task.go similarity index 57% rename from service/qc_handle_task.go rename to service/scheduled_task.go index 801b1c4..5d70caf 100644 --- a/service/qc_handle_task.go +++ b/service/scheduled_task.go @@ -6,7 +6,8 @@ import ( "time" ) -type qcHandleTask struct { +// 定时任务实现 +type scheduledTask struct { fn func() interval time.Duration cancel context.CancelFunc @@ -14,13 +15,19 @@ type qcHandleTask struct { } // Stop implements QcDataHandleScheduleTask. -func (t *qcHandleTask) Stop() { +func (t *scheduledTask) Stop() { t.cancel() - <-t.done - slog.Info("驱采数据处理任务线程退出") + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + defer cancel() + select { + case <-t.done: + slog.Info("定时任务线程退出") + case <-ctx.Done(): + slog.Warn("定时任务线程超时退出") + } } -func (t *qcHandleTask) run(ctx context.Context) { +func (t *scheduledTask) run(ctx context.Context) { defer close(t.done) mainLoop: for { @@ -34,11 +41,11 @@ mainLoop: } } -func NewQcHandleTask(run func(), interval time.Duration) QcDataHandleScheduleTask { +func NewScheduledTask(run func(), interval time.Duration) IScheduledTask { if interval <= 0 { interval = time.Second } - task := &qcHandleTask{ + task := &scheduledTask{ fn: run, interval: interval, done: make(chan struct{}),