First open source version.
This commit is contained in:
51
src/samples/add_user.py
Normal file
51
src/samples/add_user.py
Normal file
@ -0,0 +1,51 @@
|
||||
"""
|
||||
Sample script to add a new user
|
||||
"""
|
||||
|
||||
from net2xs import Net2XS
|
||||
|
||||
# Uncomment to use dotnet DateTime objects
|
||||
# from System import DateTime
|
||||
|
||||
# Uncomment to use python datetime objects
|
||||
# from datetime import datetime
|
||||
|
||||
|
||||
# Operator id 0 is System Engineer
|
||||
OPERATOR_ID = 0
|
||||
# Default Net2 password
|
||||
OPERATOR_PWD = "net2"
|
||||
# When running on the machine where Net2 is installed
|
||||
NET2_SERVER = "localhost"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
with Net2XS(NET2_SERVER) as net2:
|
||||
# Authenticate
|
||||
net2.authenticate(OPERATOR_ID, OPERATOR_PWD)
|
||||
# Add new user
|
||||
res = net2.add_user(
|
||||
access_level_id=1, # Always, all doors
|
||||
department_id=0, # No department
|
||||
anti_passback_ind=False,
|
||||
alarm_user_ind=False,
|
||||
first_name="John",
|
||||
middle_name=None,
|
||||
sur_name="Doe",
|
||||
telephone_no="12345678",
|
||||
telephone_extension=None,
|
||||
pin_code=None,
|
||||
activation_date=None, # Now
|
||||
# Or supply a dotnet DateTime object (also see import)
|
||||
# activation_date=DateTime(2018, 1, 2),
|
||||
# Or supply a python datetime object (also see import)
|
||||
# activation_date=datetime(2018, 1, 2),
|
||||
active=True,
|
||||
fax_no=None,
|
||||
expiry_date=None) # Never expire (also see activation_date)
|
||||
|
||||
if res:
|
||||
print("Success")
|
||||
else:
|
||||
print("Failure")
|
46
src/samples/add_users_with_card.py
Normal file
46
src/samples/add_users_with_card.py
Normal file
@ -0,0 +1,46 @@
|
||||
"""
|
||||
Sample script to add many users with cards
|
||||
"""
|
||||
from net2xs import Net2XS
|
||||
|
||||
|
||||
# Operator id 0 is System Engineer
|
||||
OPERATOR_ID = 0
|
||||
# Default Net2 password
|
||||
OPERATOR_PWD = "net2"
|
||||
# When running on the machine where Net2 is installed
|
||||
NET2_SERVER = "localhost"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
with Net2XS(NET2_SERVER) as net2:
|
||||
# Authenticate
|
||||
net2.authenticate(OPERATOR_ID, OPERATOR_PWD)
|
||||
|
||||
# Add 1000 users
|
||||
for i in range(1, 1001):
|
||||
first_name = "Test"
|
||||
sur_name = "User #%04d" % (i)
|
||||
card_nr = 77770000 + i
|
||||
|
||||
# Add user
|
||||
res = net2.add_user(
|
||||
first_name=first_name,
|
||||
sur_name=sur_name)
|
||||
if not res:
|
||||
print("Failed to add user %s %s: %s" %
|
||||
(first_name, sur_name, net2.last_error_message))
|
||||
continue
|
||||
|
||||
# Get user id
|
||||
user_id = net2.get_user_id_by_name((first_name, sur_name))
|
||||
if user_id < 0:
|
||||
print("Failed to find user %s %s" % (first_name, sur_name))
|
||||
continue
|
||||
|
||||
# Create card
|
||||
res = net2.add_card(card_nr, 1, user_id)
|
||||
if not res:
|
||||
print("Failed to add card to user %s %s: %s" %
|
||||
(first_name, sur_name, net2.last_error_message))
|
35
src/samples/direct_db_query.py
Normal file
35
src/samples/direct_db_query.py
Normal file
@ -0,0 +1,35 @@
|
||||
"""
|
||||
Sample script to query the database directly
|
||||
"""
|
||||
|
||||
from net2dbxs import Net2DBXS
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
with Net2DBXS() as net2db:
|
||||
# Connect
|
||||
net2db.connect()
|
||||
# Get last log entry relating a device
|
||||
dataset = net2db.query_db(
|
||||
"select * from EventsEx "
|
||||
"where EventID = "
|
||||
"(select max(EventID) from sdk.EventsEx "
|
||||
" where SerialNumber is not NULL)")
|
||||
|
||||
# Non result
|
||||
if (not dataset
|
||||
or dataset.Tables.Count < 1
|
||||
or dataset.Tables[0].Rows.Count < 1):
|
||||
|
||||
print("Nothing relevant found")
|
||||
else:
|
||||
# Just to demonstrate how to get values from a dataset
|
||||
|
||||
# Typically Table[0]
|
||||
table = dataset.Tables[0]
|
||||
# In this case only interested in the first row
|
||||
row = dataset.Tables[0].Rows[0]
|
||||
# For each column
|
||||
for col in table.Columns:
|
||||
val = row.get_Item(col.ColumnName)
|
||||
print("%s = %s" % (col.ColumnName, val))
|
17
src/samples/find_modules.py
Normal file
17
src/samples/find_modules.py
Normal file
@ -0,0 +1,17 @@
|
||||
"""
|
||||
Sample script to detect Net2Plus modules
|
||||
"""
|
||||
|
||||
from network.net2plus import Net2PlusFinder
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
inst = Net2PlusFinder()
|
||||
print("Please wait for the modules to respond...")
|
||||
devs = inst.find()
|
||||
if devs:
|
||||
for d in devs:
|
||||
print(d)
|
||||
else:
|
||||
print("No devices detected")
|
17
src/samples/find_sqlserver.py
Normal file
17
src/samples/find_sqlserver.py
Normal file
@ -0,0 +1,17 @@
|
||||
"""
|
||||
Sample script to detect all SQLServer instances
|
||||
"""
|
||||
|
||||
from network.sqlserver import SqlServerFinder
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
inst = SqlServerFinder()
|
||||
print("Please wait for the servers to respond...")
|
||||
srvs = inst.find()
|
||||
if srvs:
|
||||
for s in srvs:
|
||||
print(s)
|
||||
else:
|
||||
print("No servers detected")
|
22
src/samples/get_access_levels.py
Normal file
22
src/samples/get_access_levels.py
Normal file
@ -0,0 +1,22 @@
|
||||
"""
|
||||
Sample script to fetch all access levels
|
||||
"""
|
||||
|
||||
from net2xs import Net2XS
|
||||
|
||||
# Operator id 0 is System Engineer
|
||||
OPERATOR_ID = 0
|
||||
# Default Net2 password
|
||||
OPERATOR_PWD = "net2"
|
||||
# When running on the machine where Net2 is installed
|
||||
NET2_SERVER = "localhost"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
with Net2XS(NET2_SERVER) as net2:
|
||||
# Authenticate
|
||||
net2.authenticate(OPERATOR_ID, OPERATOR_PWD)
|
||||
# Show all access levels and their details
|
||||
for al in net2.get_py_access_levels():
|
||||
print(al)
|
21
src/samples/get_cards.py
Normal file
21
src/samples/get_cards.py
Normal file
@ -0,0 +1,21 @@
|
||||
"""
|
||||
Sample script to fetch all cards
|
||||
"""
|
||||
|
||||
from net2xs import Net2XS
|
||||
|
||||
# Operator id 0 is System Engineer
|
||||
OPERATOR_ID = 0
|
||||
# Default Net2 password
|
||||
OPERATOR_PWD = "net2"
|
||||
# When running on the machine where Net2 is installed
|
||||
NET2_SERVER = "localhost"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
with Net2XS(NET2_SERVER) as net2:
|
||||
# Authenticate
|
||||
net2.authenticate(OPERATOR_ID, OPERATOR_PWD)
|
||||
# Obtain all Net2 cards
|
||||
print(Net2XS.dataset_to_str(net2.get_cards()))
|
22
src/samples/get_time_zones.py
Normal file
22
src/samples/get_time_zones.py
Normal file
@ -0,0 +1,22 @@
|
||||
"""
|
||||
Sample script to fetch all time zones
|
||||
"""
|
||||
|
||||
from net2xs import Net2XS
|
||||
|
||||
# Operator id 0 is System Engineer
|
||||
OPERATOR_ID = 0
|
||||
# Default Net2 password
|
||||
OPERATOR_PWD = "net2"
|
||||
# When running on the machine where Net2 is installed
|
||||
NET2_SERVER = "localhost"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
with Net2XS(NET2_SERVER) as net2:
|
||||
# Authenticate
|
||||
net2.authenticate(OPERATOR_ID, OPERATOR_PWD)
|
||||
# Show all time zones and their details
|
||||
for tz in net2.get_py_time_zones():
|
||||
print(tz)
|
47
src/samples/inheritance.py
Normal file
47
src/samples/inheritance.py
Normal file
@ -0,0 +1,47 @@
|
||||
"""
|
||||
Sample script to demonstrate inheritance
|
||||
"""
|
||||
|
||||
from net2xs import Net2XS
|
||||
|
||||
# Operator id 0 is System Engineer
|
||||
OPERATOR_ID = 0
|
||||
# Default Net2 password
|
||||
OPERATOR_PWD = "net2"
|
||||
# When running on the machine where Net2 is installed
|
||||
NET2_SERVER = "localhost"
|
||||
|
||||
|
||||
class MyNet2XS(Net2XS):
|
||||
"""Inherited class for additional functionality
|
||||
"""
|
||||
global Net2XS
|
||||
|
||||
def get_current_user_id(self):
|
||||
"""Return logged on user id
|
||||
"""
|
||||
# Place a lock, for thread safety
|
||||
# (if you don't have threads you kan skip this)
|
||||
with Net2XS._lock:
|
||||
# Basic check if client connection is valid
|
||||
self._check_client()
|
||||
return self._client.CurrentUserID
|
||||
|
||||
def get_client_members(self):
|
||||
"""Return all Net2 client object members using the introspective
|
||||
Python dir function
|
||||
"""
|
||||
with Net2XS._lock:
|
||||
self._check_client()
|
||||
return dir(self._client)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
with MyNet2XS(NET2_SERVER) as net2:
|
||||
# Authenticate
|
||||
net2.authenticate(OPERATOR_ID, OPERATOR_PWD)
|
||||
# Show current user id
|
||||
print("Current used id:", net2.get_current_user_id())
|
||||
# Show all members
|
||||
print("Net2 client members:", net2.get_client_members())
|
48
src/samples/modify_user.py
Normal file
48
src/samples/modify_user.py
Normal file
@ -0,0 +1,48 @@
|
||||
"""
|
||||
Sample script to modify a user.
|
||||
It assumes that a John Doe user exists (see add_user sample).
|
||||
"""
|
||||
|
||||
from net2xs import Net2XS
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
# Operator id 0 is System Engineer
|
||||
OPERATOR_ID = 0
|
||||
# Default Net2 password
|
||||
OPERATOR_PWD = "net2"
|
||||
# When running on the machine where Net2 is installed
|
||||
NET2_SERVER = "localhost"
|
||||
|
||||
# First name
|
||||
FIRST_NAME = "John"
|
||||
# Surname
|
||||
SUR_NAME = "Doe"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
with Net2XS(NET2_SERVER) as net2:
|
||||
# Authenticate
|
||||
net2.authenticate(OPERATOR_ID, OPERATOR_PWD)
|
||||
|
||||
# Get user id
|
||||
user_id = net2.get_user_id_by_name((FIRST_NAME, SUR_NAME))
|
||||
|
||||
print("Found user id %d" % (user_id))
|
||||
|
||||
# Found a valid user id
|
||||
if user_id >= 0:
|
||||
|
||||
# Modify expiration date
|
||||
res = net2.modify_user(
|
||||
user_id=user_id,
|
||||
expiry_date=datetime(2022, 12, 31))
|
||||
|
||||
if res:
|
||||
print("Success")
|
||||
else:
|
||||
print("Failure")
|
||||
|
||||
else:
|
||||
print("Failed to find user %s %s" % (FIRST_NAME, SUR_NAME))
|
48
src/samples/monitor_acu.py
Normal file
48
src/samples/monitor_acu.py
Normal file
@ -0,0 +1,48 @@
|
||||
"""
|
||||
Sample script to monitor acu
|
||||
"""
|
||||
|
||||
from time import sleep
|
||||
from net2xs import Net2XS
|
||||
|
||||
|
||||
# Operator id 0 is System Engineer
|
||||
OPERATOR_ID = 0
|
||||
# Default Net2 password
|
||||
OPERATOR_PWD = "net2"
|
||||
# When running on the machine where Net2 is installed
|
||||
NET2_SERVER = "localhost"
|
||||
# Acu to monitor
|
||||
ACU_ADDRESS = 1219195
|
||||
|
||||
|
||||
def handle_acu_event(sender, event_view):
|
||||
"""Handler for ACU event
|
||||
"""
|
||||
print("-----------------------")
|
||||
print("address=", event_view.Address)
|
||||
print("card=", event_view.CardNumber)
|
||||
print("department=", event_view.Department)
|
||||
print("userid=", event_view.UserId)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
with Net2XS(NET2_SERVER) as net2:
|
||||
# Authenticate
|
||||
net2.authenticate(OPERATOR_ID, OPERATOR_PWD)
|
||||
|
||||
# Set function to handle ACU event
|
||||
net2.on_acu_event = handle_acu_event
|
||||
|
||||
res = net2.monitor_acu(ACU_ADDRESS)
|
||||
if not res:
|
||||
print("Failed to monitor ACU %d" % (ACU_ADDRESS))
|
||||
else:
|
||||
print("Monitoring ACU %d; press <ctrl>C to quit" % (ACU_ADDRESS))
|
||||
while True:
|
||||
try:
|
||||
sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
break
|
||||
net2.stop_monitoring_acu(ACU_ADDRESS)
|
60
src/samples/open_all_doors.py
Normal file
60
src/samples/open_all_doors.py
Normal file
@ -0,0 +1,60 @@
|
||||
"""
|
||||
Sample script to hold all known doors open for a few seconds.
|
||||
Also shows how to use the logger function.
|
||||
"""
|
||||
import time
|
||||
|
||||
from net2xs import Net2XS
|
||||
from pylog4net import Log4Net
|
||||
|
||||
# Operator id 0 is System Engineer
|
||||
OPERATOR_ID = 0
|
||||
# Default Net2 password
|
||||
OPERATOR_PWD = "net2"
|
||||
# When running on the machine where Net2 is installed
|
||||
NET2_SERVER = "localhost"
|
||||
|
||||
|
||||
def get_doors(net2):
|
||||
"""Obtain a list of all known doors
|
||||
"""
|
||||
res = []
|
||||
dataset = net2.get_doors()
|
||||
if dataset and dataset.Tables.Count > 0:
|
||||
for row in dataset.Tables[0].Rows:
|
||||
res.append(row.Address)
|
||||
return res
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
# Create logger object
|
||||
logger = Log4Net.get_logger('open_all_doors')
|
||||
|
||||
with Net2XS(NET2_SERVER) as net2:
|
||||
# Authenticate
|
||||
net2.authenticate(OPERATOR_ID, OPERATOR_PWD)
|
||||
# Get list off door addresses
|
||||
doors = get_doors(net2)
|
||||
# Open each door
|
||||
for door in doors:
|
||||
if not net2.hold_door_open(door):
|
||||
logger.Error(
|
||||
"Failed to hold door %d open: %s." %
|
||||
(door, net2.last_error_message))
|
||||
else:
|
||||
logger.Info("Set door %d open." % (door))
|
||||
|
||||
logger.Info("Now all doors are open...")
|
||||
time.sleep(3)
|
||||
|
||||
# Close each door
|
||||
for door in doors:
|
||||
if not net2.close_door(door):
|
||||
logger.Error(
|
||||
"Failed to close door %d: %s." %
|
||||
(door, net2.last_error_message))
|
||||
else:
|
||||
logger.Info("Set door %d closed." % (door))
|
||||
|
||||
logger.Info("Now all doors are closed again")
|
21
src/samples/user_script.py
Normal file
21
src/samples/user_script.py
Normal file
@ -0,0 +1,21 @@
|
||||
"""
|
||||
Sample script to fetch a dataset with all active users
|
||||
"""
|
||||
|
||||
from net2xs import Net2XS
|
||||
|
||||
# Operator id 0 is System Engineer
|
||||
OPERATOR_ID = 0
|
||||
# Default Net2 password
|
||||
OPERATOR_PWD = "net2"
|
||||
# When running on the machine where Net2 is installed
|
||||
NET2_SERVER = "localhost"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
with Net2XS(NET2_SERVER) as net2:
|
||||
# Authenticate
|
||||
net2.authenticate(OPERATOR_ID, OPERATOR_PWD)
|
||||
# Obtain all Net2 users
|
||||
print(Net2XS.dataset_to_str(net2.get_users()))
|
Reference in New Issue
Block a user