-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy path18_forceip.py
More file actions
133 lines (109 loc) · 4.44 KB
/
Copy path18_forceip.py
File metadata and controls
133 lines (109 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# ******************************************************************************
# pyorbbecsdk Advanced Example 18 — Force Static IP Assignment
#
# What you will learn:
# 1. Discover network-attached cameras connected over Ethernet
# 2. Construct an OBDeviceIpAddrConfig with a desired static IP address
# 3. Apply the static IP configuration to the device via the SDK
# 4. Reconnect and verify the device is reachable at the new address
#
# Device requirement: Femto Mega, Gemini 2 XL
#
# Run:
# python examples/advanced/18_forceip.py
# ******************************************************************************
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from pyorbbecsdk import Context, OBDeviceIpAddrConfig # type: ignore
def get_ip_config():
"""Get the new IP configuration from user input"""
cfg = OBDeviceIpAddrConfig()
cfg.dhcp = 0 # Static IP configuration
print("Please enter the network configuration information:")
# Get and validate IP address
while True:
val = input("Enter IP address: ")
parts = val.split(".")
if len(parts) == 4 and all(p.isdigit() and 0 <= int(p) <= 255 for p in parts):
cfg.address = val
break
print("Invalid format.")
# Get and validate Subnet Mask
while True:
val = input("Enter Subnet Mask: ")
parts = val.split(".")
if len(parts) == 4 and all(p.isdigit() and 0 <= int(p) <= 255 for p in parts):
cfg.netmask = val
break
print("Invalid format.")
# Get and validate Gateway address
while True:
val = input("Enter Gateway address: ")
parts = val.split(".")
if len(parts) == 4 and all(p.isdigit() and 0 <= int(p) <= 255 for p in parts):
cfg.gateway = val
break
print("Invalid format.")
return cfg
def select_device(device_list):
"""Select a device to operate, specifically filtering for Ethernet devices"""
device_count = device_list.get_count()
if device_count == 0:
print("No devices found.")
return -1
index_list = []
ethernet_dev_num = 0
print("Ethernet device list:")
for i in range(device_count):
conn_type = device_list.get_device_connection_type_by_index(i)
# Only show and allow selection of Ethernet-connected devices
if conn_type != "Ethernet":
continue
print(
f"{ethernet_dev_num}. Name: {device_list.get_device_name_by_index(i)}, "
f"Mac: 0x{device_list.get_device_uid_by_index(i)}, "
f"Serial Number: {device_list.get_device_serial_number_by_index(i)}, "
f"IP: {device_list.get_device_ip_address_by_index(i)}, "
f"Subnet Mask: {device_list.get_device_subnet_mask_by_index(i)}, "
f"Gateway: {device_list.get_device_gateway_by_index(i)}"
)
index_list.append(i)
ethernet_dev_num += 1
if not index_list:
print("No network devices found.")
return -1
# User input loop for device selection
while True:
try:
choice = int(input("Enter your choice: "))
if 0 <= choice < len(index_list):
return index_list[choice]
else:
print("Invalid input, please enter a valid index number.")
except ValueError:
print("Invalid input, please enter a number.")
return -1
def main():
try:
# Create a Context object to interact with Orbbec devices
context = Context()
# Query the list of connected devices
device_list = context.query_devices()
# Select a device to operate
device_number = select_device(device_list)
if device_number != -1:
# Get the new IP configuration from user input
config = get_ip_config()
# Change device IP configuration (Force IP)
# This is typically used when the device is on a different subnet
device_uid = device_list.get_device_uid_by_index(device_number)
device_status = context.ob_force_ip_config(device_uid, config)
if device_status is not True:
print("Failed to apply the new IP configuration.")
else:
print("The new IP configuration has been successfully applied to the device.")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
main()