Initial test
This commit is contained in:
166
setUser.py
Normal file
166
setUser.py
Normal file
@@ -0,0 +1,166 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import os
|
||||
import sys
|
||||
import csv
|
||||
|
||||
CWD = os.path.dirname(os.path.realpath(__file__))
|
||||
ROOT_DIR = os.path.dirname(CWD)
|
||||
sys.path.append(ROOT_DIR)
|
||||
|
||||
from zk import ZK, const
|
||||
import zk
|
||||
print (zk.__file__)
|
||||
|
||||
mesin_absen = [
|
||||
{ "lokasi" : "Painting In", "ip": "10.35.14.14" },
|
||||
{ "lokasi" : "Painting Out" , "ip": "10.35.14.13" },
|
||||
{ "lokasi" : "Injection In", "ip": "10.35.7.10"},
|
||||
{ "lokasi" : "Injection Out", "ip": "10.35.7.11"},
|
||||
{ "lokasi" : "Office", "ip": "10.35.14.10"},
|
||||
{ "lokasi" : "Hotpress in", "ip":"10.35.14.15"},
|
||||
{ "lokasi" : "Hotpress out", "ip":"10.35.14.16"},
|
||||
{ "lokasi" : "KIIC", "ip": "10.45.7.2"},
|
||||
{ "lokasi" : "Injection 2", "ip":"10.35.7.12"},
|
||||
]
|
||||
|
||||
#print(mesin_absen[0]['lokasi'])
|
||||
|
||||
def pilih_mesin():
|
||||
print("Daftar mesin attendance : ")
|
||||
for i, mesin in enumerate(mesin_absen, start=1):
|
||||
print(f"{i}: {mesin['lokasi']} (IP: {mesin['ip']})")
|
||||
|
||||
while True:
|
||||
try:
|
||||
choice = int(input("Pilih nomor mesin (1-9): "))
|
||||
if 1 <= choice <= 9:
|
||||
mesin_dipilih = mesin_absen[choice - 1]
|
||||
ipaddress = mesin_dipilih["ip"]
|
||||
lokasi = mesin_dipilih["lokasi"]
|
||||
print(f"Dipilih {lokasi}, IP Address mesin absen: {ipaddress}")
|
||||
return ipaddress
|
||||
else:
|
||||
print("Masukan angka 1-9")
|
||||
except ValueError:
|
||||
print("Invalid input. Masukan angka.")
|
||||
|
||||
def koneksi_mesin(ipaddress):
|
||||
conn = None
|
||||
zk = ZK(ipaddress, port=4370, verbose=True)
|
||||
# connect to device
|
||||
conn = zk.connect()
|
||||
# disable device, this method ensures no activity on the device while the process is run
|
||||
conn.disable_device()
|
||||
print("Koneksi tersambung")
|
||||
return conn
|
||||
|
||||
def menu():
|
||||
operasi = [
|
||||
"Input karyawan baru",
|
||||
"Import data karyawan",
|
||||
"Tampilkan data karyawan",
|
||||
]
|
||||
|
||||
print("Operasi yang tersedia : ")
|
||||
for i, ops in enumerate(operasi, start=1):
|
||||
print(f"{i}: {ops}")
|
||||
|
||||
while True:
|
||||
try:
|
||||
choice = int(input("Pilih operasi: "))
|
||||
if 1 <= choice <= 3:
|
||||
operasi_dipilih = operasi[choice - 1]
|
||||
print(f"{operasi_dipilih} dipilih")
|
||||
return operasi_dipilih
|
||||
else:
|
||||
print("Masukan angka")
|
||||
except ValueError:
|
||||
print("Invalid input. Masukan angka.")
|
||||
|
||||
def input_karyawan():
|
||||
try:
|
||||
name = input("Masukkan nama karyawan: ")
|
||||
user_id = input("Masukkan user ID karyawan: ")
|
||||
privilege = const.USER_DEFAULT
|
||||
password = input("Masukkan password (kosongkan jika tidak ada): ")
|
||||
|
||||
conn.set_user(uid = last_uid + 1, name=name, privilege=privilege, password=password, user_id=user_id)
|
||||
print("Karyawan baru berhasil ditambahkan!")
|
||||
except Exception as e:
|
||||
print(f"Terjadi kesalahan: {e}")
|
||||
finally:
|
||||
if conn:
|
||||
conn.disconnect()
|
||||
|
||||
|
||||
def importCSV():
|
||||
#Import user from csv
|
||||
with open('user_dump1.csv', mode='r') as file:
|
||||
reader = csv.reader(file, delimiter=',')
|
||||
|
||||
# Skip the header row
|
||||
next(reader)
|
||||
|
||||
# Specify column indices to read
|
||||
column_indices = [1, 4] # Example: reads the first and third columns
|
||||
|
||||
for row in reader:
|
||||
selected_columns = [row[i] for i in column_indices]
|
||||
print(selected_columns)
|
||||
#uid = row[0]
|
||||
name = row[1]
|
||||
user_id = row[4]
|
||||
|
||||
#for row in reader:
|
||||
# Check if the row has enough columns
|
||||
# if all(i < len(row) for i in column_indices):
|
||||
# selected_columns = [row[i] for i in column_indices]
|
||||
# print(selected_columns)
|
||||
# else:
|
||||
# print(f"Skipping row with missing columns: {row}")
|
||||
|
||||
try:
|
||||
conn = zk.connect()
|
||||
conn.set_user(name=name, privilege=const.USER_DEFAULT, password='', user_id=user_id)#(uid=uid, name=name, privilege=const.USER_DEFAULT, password='', user_id=user_id)
|
||||
#conn.test_voice(index=0)
|
||||
except Exception as e:
|
||||
print ("Process terminate : {}".format(e))
|
||||
finally:
|
||||
if conn:
|
||||
conn.disconnect()
|
||||
|
||||
def get_latest_uid():
|
||||
users = conn.get_users()
|
||||
if users:
|
||||
return max(user.uid for user in users)
|
||||
return 0
|
||||
|
||||
def tampilkan_data_karyawan():
|
||||
users = conn.get_users()
|
||||
for user in users:
|
||||
privilege = 'User'
|
||||
if user.privilege == const.USER_ADMIN:
|
||||
privilege = 'Admin'
|
||||
print ('+ UID #{}'.format(user.uid))
|
||||
print (' Name : {}'.format(user.name))
|
||||
print (' Privilege : {}'.format(privilege))
|
||||
print (' Password : {}'.format(user.password))
|
||||
print (' Group ID : {}'.format(user.group_id))
|
||||
print (' User ID : {}'.format(user.user_id))
|
||||
|
||||
print("UID Terakhir: ", last_uid)
|
||||
|
||||
def operasi(dipilih):
|
||||
if dipilih == "Input karyawan baru":
|
||||
input_karyawan()
|
||||
elif dipilih == "Import data karyawan":
|
||||
importCSV()
|
||||
elif dipilih == "Tampilkan data karyawan":
|
||||
tampilkan_data_karyawan()
|
||||
|
||||
|
||||
ipaddress = pilih_mesin()
|
||||
conn = koneksi_mesin(ipaddress)
|
||||
dipilih = menu()
|
||||
last_uid = get_latest_uid()
|
||||
operasi(dipilih)
|
||||
Reference in New Issue
Block a user