connect method
Implementation
Future<CameraConnectState> connect(
{bool lanScan = true, int connectCount = 3}) async {
if (_destroyFlag == true) {
print("id:$id isDestroy:$_destroyFlag error");
return CameraConnectState.disconnect;
}
if (isBack || isRemoteClose) {
print(
"id:$id CameraConnectState.disconnect error isBack:$isBack isRemoteClose:$isRemoteClose");
return CameraConnectState.disconnect;
}
if (connectState == CameraConnectState.connecting ||
connectState == CameraConnectState.logging ||
connectState == CameraConnectState.connected) {
return connectState;
}
connectState = CameraConnectState.connecting;
_connectExitFlag = false;
var clientId = await getClientId();
int clientPtr = await getClientPtr();
print('id:$id clientId:$clientId clientPtr:$clientPtr');
if (clientPtr == null || clientPtr == 0) {
connectState = CameraConnectState.offline;
return CameraConnectState.offline;
}
if (verifyOffline) {
var offline = await _checkOffline(lanScan, clientId, clientPtr);
if (offline == true) {
print("id:$id CameraConnectState.offline end");
connectState = CameraConnectState.offline;
return connectState;
}
}
// this.statusResult?.batteryRate = null;
// this.statusResult?.isCharge = null;
this.statusResult = this.statusResult;
late ClientConnectState p2pState;
this.removeListener<P2PConnectStateChanged>(_connectStateListener);
var cType = connectType;
for (int i = 0; i < connectCount && !_connectExitFlag; i++) {
/// 检测连接类型
requestWakeup();
cType = await _checkConnectType(i, clientId, clientPtr);
p2pState = await super
.p2pConnect(lanScan: lanScan, reConnectCount: 1, connectType: cType);
if (p2pState != ClientConnectState.CONNECT_STATUS_ONLINE) {
if (i == 0 && (cType == 0x7B || cType == 95)) {
print("id:$id cType:$cType 转发连接失败后,检查设备离线时间");
var offline = await _checkOffline(lanScan, clientId, clientPtr);
if (offline == true) {
print("id:$id CameraConnectState.offline end");
return CameraConnectState.offline;
}
} else if (i == 1 && cType != 63) {
print("id:$id cType:$cType 转发连接失败后,检查设备离线时间");
var offline = await _checkOffline(lanScan, clientId, clientPtr);
if (offline == true) {
print("id:$id CameraConnectState.offline end");
return CameraConnectState.offline;
}
}
}
if (p2pState == ClientConnectState.CONNECT_STATUS_ONLINE) {
if (connectType == 63 && await super.checkDeviceAp(id)) {
cType = 63;
}
break;
}
}
if (p2pState != ClientConnectState.CONNECT_STATUS_ONLINE) {
connectState = _getConnectState();
print("id:$id $connectState end");
return connectState;
}
this.addListener<P2PConnectStateChanged>(_connectStateListener);
await this.setCommandListener();
connectState = CameraConnectState.logging;
StatusResult? result = await _login(connectCount, password);
if (result == null || result.isSuccess != true) {
connectState = CameraConnectState.timeout;
print("id:$id $connectState end");
return connectState;
}
if (result.result == "-4") {
connectState = CameraConnectState.illegal;
print("id:$id $connectState end");
return connectState;
}
if (result.result == "-1" ||
result.result == "-2" ||
result.result == "-3") {
connectState = CameraConnectState.password;
print("id:$id $connectState end");
return connectState;
}
var realDeviceId = result.realdeviceid;
int currentUsers = int.tryParse(result.current_users ?? "") ?? 0;
int maxSupportUsers = int.tryParse(result.max_support_users ?? "") ?? 0;
if (currentUsers > maxSupportUsers) {
connectState = CameraConnectState.maxUser;
print(
"id:$id currentUsers:$currentUsers maxUsers:$maxSupportUsers $connectState end");
return connectState;
}
if (isVirtualId == true && realDeviceId != null && id != realDeviceId) {
connectState = CameraConnectState.password;
print("id:$id vuid:$realDeviceId $connectState end");
return connectState;
}
String? dual = result.DualAuthentication;
if (dual != null && dual != "0" && verifyListener != null) {
bool verify = await verifyListener!(this);
if (verify == false) {
await disconnect();
connectState = CameraConnectState.disconnect;
print("id:$id dualVerify:$verify $connectState end");
return connectState;
}
}
connectState = CameraConnectState.connected;
print("id:$id $connectState end ");
keepAlive(time: 10);
getStatus(cache: true);
updateDateTime();
return connectState;
}