# react-native-vstarcam

React Native bridge for VStarCam P2P SDK - enables direct communication with VStarCam cameras for WiFi configuration, video streaming, PTZ control, and more.

## Features

- ✅ P2P Connection to VStarCam cameras
- ✅ WiFi configuration
- ✅ WiFi network scanning
- ✅ PTZ control (pan, tilt, zoom)
- ✅ Video streaming
- ✅ Snapshot capture
- ✅ Two-way audio (planned)

## Installation

```bash
npm install react-native-vstarcam
# or
yarn add react-native-vstarcam
```

### Expo Users

This package requires native code and won't work with Expo Go. You need to use a development build:

```bash
npx expo prebuild
npx expo run:android
# or
npx expo run:ios
```

### Android Setup

The package automatically links the VStarCam AAR library. No additional setup required.

### iOS Setup

1. Run `pod install` in your iOS directory
2. The VStarCam iOS framework needs to be placed in `ios/Frameworks/`

## Usage

```typescript
import { createVStarCamClient, ConnectionState } from "react-native-vstarcam";

// Create a client
const client = createVStarCamClient();

// Connect to camera
async function connectToCamera(deviceId: string) {
  // Create client with device ID
  await client.create(deviceId);

  // Connect via P2P
  const result = await client.connect(true);

  if (result.state === ConnectionState.ONLINE) {
    console.log("Connected!");

    // Login
    await client.login("admin", "888888");

    // Get device info
    const info = await client.getDeviceInfo();
    console.log("Device:", info);
  }
}

// Configure WiFi
async function configureWiFi() {
  // Scan for networks
  const networks = await client.scanWiFi();
  console.log("Available networks:", networks);

  // Configure WiFi
  const success = await client.configureWiFi("MyWiFi", "MyPassword", "WPA2", 6);

  if (success) {
    console.log("WiFi configured successfully!");
  }
}

// PTZ Control
async function moveCameraUp() {
  await client.ptzUp(true); // Continuous movement
  await new Promise((resolve) => setTimeout(resolve, 1000));
  await client.ptzStop();
}

// Cleanup
async function disconnect() {
  await client.disconnect();
  await client.destroy();
}
```

## API Reference

### VStarCamClient

#### Methods

| Method                                          | Description                      |
| ----------------------------------------------- | -------------------------------- |
| `create(deviceId)`                              | Create a P2P client for a device |
| `connect(lanScan?, serverParam?, connectType?)` | Connect to camera                |
| `login(username?, password?)`                   | Login to camera                  |
| `disconnect()`                                  | Disconnect from camera           |
| `destroy()`                                     | Cleanup and release resources    |

#### WiFi Methods

| Method                                               | Description                      |
| ---------------------------------------------------- | -------------------------------- |
| `scanWiFi()`                                         | Scan for available WiFi networks |
| `configureWiFi(ssid, password, security?, channel?)` | Configure camera WiFi            |

#### PTZ Methods

| Method                  | Description          |
| ----------------------- | -------------------- |
| `ptzUp(continuous?)`    | Move camera up       |
| `ptzDown(continuous?)`  | Move camera down     |
| `ptzLeft(continuous?)`  | Move camera left     |
| `ptzRight(continuous?)` | Move camera right    |
| `ptzStop()`             | Stop camera movement |

#### Video Methods

| Method                    | Description        |
| ------------------------- | ------------------ |
| `startVideo(streamType?)` | Start video stream |
| `stopVideo()`             | Stop video stream  |
| `takeSnapshot()`          | Capture a snapshot |

### Enums

```typescript
enum ConnectionState {
  INVALID_CLIENT,
  CONNECTING,
  INITIALIZING,
  ONLINE,
  CONNECT_FAILED,
  DISCONNECTED,
  INVALID_ID,
  OFFLINE,
  TIMEOUT,
  MAX_SESSION,
  MAX,
  REMOVE_CLOSE,
}

enum ConnectionMode {
  NONE,
  P2P,
  RELAY,
  SOCKET,
}
```

## Events

```typescript
// Connection state changes
client.addConnectionListener((clientId, state) => {
  console.log("Connection state:", ConnectionState[state]);
});

// Command responses
client.addCommandListener((clientId, command, data) => {
  console.log("Command:", command, "Data:", data);
});

// Video frames
client.addVideoListener((clientId, frame, width, height, timestamp) => {
  // Handle video frame
});
```

## VStarCam Device ID

The Device ID (DID) is typically printed on the camera or shown in the VStarCam app. Format: `VSTA-XXXXXX-XXXXX`

## Troubleshooting

### Connection fails

1. Ensure the camera is powered on and in pairing mode
2. Check that your phone is on the same network
3. Verify the Device ID is correct

### WiFi configuration fails

1. Ensure you're connected to the camera first
2. Check WiFi password is correct
3. Make sure the network is 2.4GHz (most VStarCam cameras don't support 5GHz)

## License

MIT
