Skip to content

This is a basic program that automatically locks the session if the NFC/RFID card is not present on the USB badge reader connected to the Windows computer.

Notifications You must be signed in to change notification settings

T-wcs/WindowsLockerRFID

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🖥🔐 WindowsLockerRFID

This is a basic program that automatically locks the session if the NFC/RFID card is not present on the USB badge reader connected to the Windows computer.


⚠️ Requirements

It is necessary to have the pyscard library installed : https://github.com/LudovicRousseau/pyscard and the paho-mqtt client library if you want to send your data via mQTT : https://github.com/eclipse/paho.mqtt.python

You can simply run the following command to install the packages:

pip install -r requirements.txt

🔩 Build .exe from .py

To create your executable usable on Windows, I recommend to use PyInstaller with the -F argument to create a single file, as well as the -w option to avoid displaying a command prompt that will be permanent since we are in an infinite While loop.

Build executable :

pyinstaller -F -w --hidden-import=modules.* /path/to/file/main.py -p "C:\Users\%USERNAME\Path_to_repository"

⚙️ Create a installer

To create an installer I usually use inno Setup, which is a simple Pascal script and will allow me to add a lot of options. In our case it is useful to not allow the user to change the installation directory, because it is hard written in the code.


🏠 Home Assistant Integration

You can also integrate the reader status, UID and profile of the read RFID card to send it to Home Assistant, it will be based on the output.json file located in the installation directory.

Structure of output.json :

{
    "WinlockerDetails": {
        "Status": "Connected",
        "ConnectionType": "USB",
        "HostName": "DESKTOP-EXAMPLE",
        "WindowsVersion": "Windows-10-10.0.19044-SP0",
        "ReaderName": "WCR WCR330-ContactLess Reader 0",
        "CardUID": "01020304",
        "CardProfile": "JohnDoe"
    }
}

📑 Parsing output data with Hass Agent on Windows.

To create a sensor with the result data from output.json you need create a PowerShell sensor on Hass Agent. In the script or command section of the Hass agent PowerShell sensor, add the following lines depending on the data you want to obtain :

image

To retrieve the Card UID with a one command :

$json = Get-Content "C:\Users\%USERNAME%\AppData\Roaming\WinLockerRFID\output.json" | ConvertFrom-Json; $json.WinlockerDetails.CardUID

With a PowerShell script :

$jsonPath = "C:\Users\%USERNAME%\AppData\Roaming\WinLockerRFID\output.json"
$json = Get-Content $jsonPath | ConvertFrom-Json
$cardUID = $json.WinlockerDetails.CardUID
Write-Output $cardUID

Then specify the path to the script in the installation directory, named GetCardUID.ps1 into script or command section.


To retrieve the Card profile with a one command :

$json = Get-Content "C:\Users\%USERNAME%\AppData\Roaming\WinLockerRFID\output.json" | ConvertFrom-Json; $json.WinlockerDetails.ProfileCard

With a PowerShell script :

$jsonPath = "C:\Users\%USERNAME%\AppData\Roaming\WinLockerRFID\output.json"
$json = Get-Content $jsonPath | ConvertFrom-Json
$profileCard = $json.WinlockerDetails.ProfileCard
Write-Output $profileCard

To retrieve the Reader status with a one command :

$json = Get-Content "C:\Users\%USERNAME%\AppData\Roaming\WinLockerRFID\output.json" | ConvertFrom-Json; $json.WinlockerDetails.Status

With a PowerShell script :

$jsonPath = "C:\Users\%USERNAME%\AppData\Roaming\WinLockerRFID\output.json"
$json = Get-Content $jsonPath | ConvertFrom-Json
$Status = $json.WinlockerDetails.Status
Write-Output $Status

To retrieve the Windows Version with a one command :

$json = Get-Content "C:\Users\%USERNAME%\AppData\Roaming\WinLockerRFID\output.json" | ConvertFrom-Json; $json.WinlockerDetails.WindowsVersion

With a PowerShell script :

$jsonPath = "C:\Users\%USERNAME%\AppData\Roaming\WinLockerRFID\output.json"
$json = Get-Content $jsonPath | ConvertFrom-Json
$WindowsVersion = $json.WinlockerDetails.WindowsVersion
Write-Output $WindowsVersion

To retrieve the HostName with a one command :

$json = Get-Content "C:\Users\%USERNAME%\AppData\Roaming\WinLockerRFID\output.json" | ConvertFrom-Json; $json.WinlockerDetails.HostName

With a PowerShell script :

$jsonPath = "C:\Users\%USERNAME%\AppData\Roaming\WinLockerRFID\output.json"
$json = Get-Content $jsonPath | ConvertFrom-Json
$HostName = $json.WinlockerDetails.HostName
Write-Output $HostName

To retrieve the Reader Name with a one command :

$json = Get-Content "C:\Users\%USERNAME%\AppData\Roaming\WinLockerRFID\output.json" | ConvertFrom-Json; $json.WinlockerDetails.ReaderName

With a PowerShell script :

$jsonPath = "C:\Users\%USERNAME%\AppData\Roaming\WinLockerRFID\output.json"
$json = Get-Content $jsonPath | ConvertFrom-Json
$ReaderName = $json.WinlockerDetails.ReaderName
Write-Output $ReaderName

Output :

image


📡 MQTT module

The code is now able to send data via the MQTT protocol based on a configuration file mqttConfig.json with the following structure:

{
  "hostname":"homeassistant.lan",
  "Port": 1883,
  "login": "YOU_LOGIN",
  "password": "YOU_PASSWORD",
  "topic": "homeassistant/sensor/WinLockerRFID/config"
}

In your Home Assistant configuration.yaml file it will also be necessary to add the following lines:

mqtt:        
  sensor:
    - name: "Winlocker Status"
      state_topic: "homeassistant/sensor/WinLockerRFID/config"
      value_template: "{{ value_json.WinlockerDetails.Status }}"
      json_attributes_topic: "homeassistant/sensor/WinLockerRFID/attributes"

    - name: "Winlocker Card UID"
      state_topic: "homeassistant/sensor/WinLockerRFID/config"
      value_template: "{{ value_json.WinlockerDetails.CardUID }}"
      json_attributes_topic: "homeassistant/sensor/WinLockerRFID/attributes"
      
    - name: "Winlocker Profile Card"
      state_topic: "homeassistant/sensor/WinLockerRFID/config"
      value_template: "{{ value_json.WinlockerDetails.CardProfile }}"
      json_attributes_topic: "homeassistant/sensor/WinLockerRFID/attributes"     

Now you can search for the entity with the name given to the variable name in the configuration.yaml file

Example with Winlocker Status into Development Tools > State

image


You can also add a command with Hass Agent to control the lockwin.exe process which will allow you to stop or start the process and monitor whether it is active or not.

Create a command from the Hass Agent menu, choose PowerShell as the command type and add this line:

Stop-Process -Name lockwin

image

Click on Save and activate the command.

You can now create a map on your dashboard with the entity

image

Now to be able to start the process lockwin.exe from Home Assistant, we will have to create another command which is the execution of the script RunProcess.ps1 located in the installation directory.

Here are the contents of the file:

$processPath = "C:\Users\%USERNAME%\AppData\Roaming\WinLockerRFID\lockwin.exe"
$processName = "lockwin"
$process = Get-Process -Name $processName -ErrorAction SilentlyContinue

if ($process -eq $null) {
    Start-Process $processPath
}

This script will check if the process is in the list of active processes, if it is not then it starts it.

On the Home Assistant Dashboard you can have this:

image

If the number of processes is equal to 0 then it means that lockwin.exe is not active on the computer.


✅ Reader tested

ACR122U :

image


WCR 330 :

image


Improvement

  1. Create a table to manage the different readers (ACR122U / WCR330 etc)

About

This is a basic program that automatically locks the session if the NFC/RFID card is not present on the USB badge reader connected to the Windows computer.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published