refactor: 删除未使用的代码文件
- 移除 Core 目录下的 CallBack.go、core.go 和 Type.go 文件 - 这些文件包含了未使用的类型定义和函数实现 - 删除冗余代码有助于简化项目结构,提高代码可读性和维护性
This commit is contained in:
		
							
								
								
									
										201
									
								
								BallCamera.go
									
									
									
									
									
								
							
							
						
						
									
										201
									
								
								BallCamera.go
									
									
									
									
									
								
							| @@ -1,19 +1,9 @@ | ||||
| package HikSDK | ||||
|  | ||||
| /* | ||||
| #cgo LDFLAGS: -Wl,--allow-multiple-definition | ||||
| #include <stdio.h> | ||||
| #include <string.h> | ||||
| */ | ||||
| import "C" | ||||
| import ( | ||||
| 	"encoding/binary" | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"gitea.com/kunmeng/HikNetSDKPkg/Core" | ||||
| 	"sync" | ||||
| 	"time" | ||||
| 	"unsafe" | ||||
| ) | ||||
|  | ||||
| const ( | ||||
| @@ -161,15 +151,15 @@ var HikPTZEnum = struct { | ||||
| } | ||||
|  | ||||
| type BallCamera struct { | ||||
| 	userId       Core.LONG | ||||
| 	userId       LONG | ||||
| 	_type        uint8 | ||||
| 	deviceInfo   Core.NET_DVR_DEVICEINFO_V30 | ||||
| 	deviceInfo   NET_DVR_DEVICEINFO | ||||
| 	mu           sync.Mutex | ||||
| 	expectedType byte | ||||
| } | ||||
|  | ||||
| func NewBallCamera(Ip string, Port int, Username, Password string, Type uint8) (*BallCamera, error) { | ||||
| 	UserId, DeviceInfo, err := Core.Login(Ip, Port, Username, Password) | ||||
| 	UserId, DeviceInfo, err := login(Ip, Port, Username, Password) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| @@ -181,9 +171,10 @@ func NewBallCamera(Ip string, Port int, Username, Password string, Type uint8) ( | ||||
| } | ||||
|  | ||||
| type PTZ struct { | ||||
| 	P float32 | ||||
| 	T float32 | ||||
| 	Z float32 | ||||
| 	Action int | ||||
| 	P      float64 | ||||
| 	T      float64 | ||||
| 	Z      float64 | ||||
| } | ||||
|  | ||||
| func (this *BallCamera) GetPTZ() (PTZ, error) { | ||||
| @@ -191,81 +182,26 @@ func (this *BallCamera) GetPTZ() (PTZ, error) { | ||||
| 		return PTZ{}, errors.New("BallCamera is nil") | ||||
| 	} | ||||
| 	if this._type == BuKongQiu { | ||||
| 		var data PTZ | ||||
|  | ||||
| 		ch := make(chan bool) | ||||
| 		SerialStartHandle, err := Core.SerialStart(this.userId, func(lSerialHandle Core.LONG, lChannel Core.LONG, pRecvDataBuffer []byte, dwBufSize Core.DWORD, pUser unsafe.Pointer) { | ||||
| 			if dwBufSize != 7 { | ||||
| 				ch <- false | ||||
| 				return | ||||
| 			} | ||||
| 			Type := pRecvDataBuffer[3] | ||||
| 			this.mu.Lock() | ||||
| 			expected := this.expectedType | ||||
| 			this.mu.Unlock() | ||||
| 			if Type != expected { | ||||
| 				ch <- false | ||||
| 				return | ||||
| 			} | ||||
| 			switch Type { | ||||
| 			case 0x59: | ||||
| 				data.P = float32(binary.BigEndian.Uint16(pRecvDataBuffer[4:6])) / 100. | ||||
| 			case 0x5B: | ||||
| 				data.T = float32(binary.BigEndian.Uint16(pRecvDataBuffer[4:6])) / 100. | ||||
| 			case 0x5D: | ||||
| 				data.Z = float32(binary.BigEndian.Uint16(pRecvDataBuffer[4:6])) / 100. | ||||
| 			default: | ||||
| 				ch <- false | ||||
| 			} | ||||
| 			ch <- true | ||||
| 		}) | ||||
| 		ptzpos, err := GetPTZPOS(this.userId) | ||||
| 		if err != nil { | ||||
| 			return data, err | ||||
| 			return PTZ{}, err | ||||
| 		} | ||||
| 		defer func() { | ||||
| 			err = Core.SerialStop(SerialStartHandle) | ||||
| 			if err != nil { | ||||
| 				println(err.Error()) | ||||
| 			} | ||||
| 			time.Sleep(1 * time.Second) | ||||
| 		}() | ||||
|  | ||||
| 		// 获取P值 | ||||
| 		this.mu.Lock() | ||||
| 		this.expectedType = 0x59 | ||||
| 		this.mu.Unlock() | ||||
| 		if err := this.retrySend(SerialStartHandle, []byte{0xff, 0x01, 0x00, 0x51, 0x00, 0x00, 0x52}, 5, ch); err != nil { | ||||
| 			return data, fmt.Errorf("获取P值失败: %w", err) | ||||
| 		} | ||||
|  | ||||
| 		this.mu.Lock() | ||||
| 		this.expectedType = 0x5B | ||||
| 		this.mu.Unlock() | ||||
| 		if err := this.retrySend(SerialStartHandle, []byte{0xff, 0x01, 0x00, 0x53, 0x00, 0x00, 0x54}, 5, ch); err != nil { | ||||
| 			return data, fmt.Errorf("获取T值失败: %w", err) | ||||
| 		} | ||||
|  | ||||
| 		this.mu.Lock() | ||||
| 		this.expectedType = 0x5D | ||||
| 		this.mu.Unlock() | ||||
| 		if err := this.retrySend(SerialStartHandle, []byte{0xff, 0x01, 0x00, 0x55, 0x00, 0x00, 0x56}, 5, ch); err != nil { | ||||
| 			return data, fmt.Errorf("获取Z值失败: %w", err) | ||||
| 		} | ||||
| 		return data, nil | ||||
| 		return PTZ{ | ||||
| 			Action: ptzpos.Action, | ||||
| 			P:      float64(ptzpos.P), | ||||
| 			T:      float64(ptzpos.T), | ||||
| 			Z:      float64(ptzpos.Z), | ||||
| 		}, nil | ||||
| 	} | ||||
| 	var data Core.CDVR_PTZPOS | ||||
| 	var dataPtr = unsafe.Pointer(&data) | ||||
| 	err := Core.GetDVRConfig(this.userId, 293, 1, dataPtr, Core.DWORD(unsafe.Sizeof(data))) | ||||
| 	ptz, err := getPTZBase(this.userId) | ||||
| 	if err != nil { | ||||
| 		return PTZ{}, err | ||||
| 	} | ||||
| 	res := data.Go() | ||||
| 	return PTZ{ | ||||
| 		P: float32(res.WPanPos), | ||||
| 		T: float32(res.WTiltPos), | ||||
| 		Z: float32(res.WZoomPos), | ||||
| 		P: ptz.P, | ||||
| 		T: ptz.T, | ||||
| 		Z: ptz.Z, | ||||
| 	}, nil | ||||
|  | ||||
| } | ||||
|  | ||||
| func padding(n int) ([]byte, error) { | ||||
| @@ -274,25 +210,11 @@ func padding(n int) ([]byte, error) { | ||||
| 	} | ||||
| 	return []byte{byte(n >> 8), byte(n & 0xFF)}, nil | ||||
| } | ||||
|  | ||||
| func (this *BallCamera) PtzGotoPut(Action int, P, T, Z float64) error { | ||||
| 	if this == nil { | ||||
| 		return errors.New("BallCamera is nil") | ||||
| 	} | ||||
| 	if this._type == BuKongQiu { | ||||
| 		SerialStartHandle, err := Core.SerialStart(this.userId, func(lSerialHandle Core.LONG, lChannel Core.LONG, pRecvDataBuffer []byte, dwBufSize Core.DWORD, pUser unsafe.Pointer) { | ||||
| 		}) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		defer func() { | ||||
| 			err = Core.SerialStop(SerialStartHandle) | ||||
| 			if err != nil { | ||||
| 				println(err.Error()) | ||||
| 			} | ||||
| 			time.Sleep(1 * time.Second) | ||||
| 		}() | ||||
|  | ||||
| 		PByte, err := padding(int(P * 100)) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| @@ -324,92 +246,25 @@ func (this *BallCamera) PtzGotoPut(Action int, P, T, Z float64) error { | ||||
| 		pBuf = append(pBuf, pBufv) | ||||
| 		tBuf = append(tBuf, tBufv) | ||||
| 		zBuf = append(zBuf, zBufv) | ||||
| 		switch Action { | ||||
| 		case 1: | ||||
| 			err = Core.SerialSend(SerialStartHandle, pBuf) | ||||
| 			if err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 			err = Core.SerialSend(SerialStartHandle, tBuf) | ||||
| 			if err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 			err = Core.SerialSend(SerialStartHandle, zBuf) | ||||
| 			if err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 			break | ||||
| 		case 2: | ||||
| 			err = Core.SerialSend(SerialStartHandle, pBuf) | ||||
| 			if err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 			break | ||||
| 		case 3: | ||||
| 			err = Core.SerialSend(SerialStartHandle, tBuf) | ||||
| 			if err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 			break | ||||
| 		case 4: | ||||
| 			err = Core.SerialSend(SerialStartHandle, zBuf) | ||||
| 			if err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 			break | ||||
| 		case 5: | ||||
| 			err = Core.SerialSend(SerialStartHandle, pBuf) | ||||
| 			if err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 			err = Core.SerialSend(SerialStartHandle, tBuf) | ||||
| 			if err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 			break | ||||
| 		default: | ||||
| 			return errors.New("action error") | ||||
| 		err = SetPTZPos(this.userId, pBuf, tBuf, zBuf) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		return nil | ||||
| 	} | ||||
| 	var data Core.CDVR_PTZPOS | ||||
| 	data.Set(float64(Action), P, T, Z) | ||||
| 	var dataPtr = unsafe.Pointer(&data) | ||||
| 	err := Core.SetDVRConfig(this.userId, 292, 1, dataPtr, Core.DWORD(unsafe.Sizeof(data))) | ||||
|  | ||||
| 	err := setPTZBase(this.userId, PTZ{Action, P, T, Z}) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (this *BallCamera) retrySend(handle Core.LONG, cmd []byte, maxRetries int, ch <-chan bool) error { | ||||
| 	for retry := 0; retry < maxRetries; retry++ { | ||||
| 		if err := Core.SerialSend(handle, cmd); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 		select { | ||||
| 		case success := <-ch: | ||||
| 			if success { | ||||
| 				return nil | ||||
| 			} | ||||
| 			if retry == maxRetries-1 { | ||||
| 				return fmt.Errorf("达到最大重试次数 %d", maxRetries) | ||||
| 			} | ||||
| 		case <-time.After(2 * time.Second): // 添加超时机制 | ||||
| 			if retry == maxRetries-1 { | ||||
| 				return fmt.Errorf("响应超时,重试 %d 次后失败", maxRetries) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (receiver *BallCamera) StartBus(direction int, speed int) error { | ||||
| 	if receiver == nil { | ||||
| 		return errors.New("BallCamera is nil") | ||||
| 	} | ||||
| 	err := Core.PTZControlWithSpeed_Other(receiver.userId, Core.LONG(receiver.deviceInfo.ByStartChan), Core.DWORD(PTZEnum.toHikPTZEnum(direction)), Core.DWORD(0), Core.DWORD(speed)) | ||||
| 	err := pTZControlWithSpeed_Other(receiver.userId, DWORD(PTZEnum.toHikPTZEnum(direction)), DWORD(0), DWORD(speed)) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| @@ -419,7 +274,7 @@ func (receiver *BallCamera) StopBus(direction int, speed int) error { | ||||
| 	if receiver == nil { | ||||
| 		return errors.New("BallCamera is nil") | ||||
| 	} | ||||
| 	err := Core.PTZControlWithSpeed_Other(receiver.userId, Core.LONG(receiver.deviceInfo.ByStartChan), Core.DWORD(PTZEnum.toHikPTZEnum(direction)), Core.DWORD(1), Core.DWORD(speed)) | ||||
| 	err := pTZControlWithSpeed_Other(receiver.userId, DWORD(PTZEnum.toHikPTZEnum(direction)), DWORD(1), DWORD(speed)) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| @@ -443,12 +298,12 @@ func (this *BallCamera) Logout() error { | ||||
| 	if this == nil { | ||||
| 		return errors.New("BallCamera is nil") | ||||
| 	} | ||||
| 	return Core.Logout(this.userId) | ||||
| 	return logout(this.userId) | ||||
| } | ||||
|  | ||||
| func (this *BallCamera) Status() bool { | ||||
| 	if this == nil { | ||||
| 		return false | ||||
| 	} | ||||
| 	return Core.NET_DVR_RemoteControl(this.userId) | ||||
| 	return deviceOnline(this.userId) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 kunmeng
					kunmeng