/DeviceServer

An WPF app with an integrated HTTP server that enables the web browser to access local smartcard devices that would otherwise not be accessible via JavaScript.

Primary LanguageC#MIT LicenseMIT

DeviceServer

A WPF app with an integrated HTTP server that enables the web browser to access local hardware devices that would otherwise not be accessible via JavaScript.

Table of Contents

Motivation

The client part of a Web application often needs to access local devices. One example is access to NFC hardware. Specifications like Web NFC for direct support by the browser are not yet generally available, so I came up with the idea of a local program that allows access to local resources via an HTTP interface.

System sketch

NFC tags are very well suited for identifying things. They are cheap and robust.

What is a thing?

A thing is a physical asset modeled by a digital twin. In the DeviceServer a thing is identified by a type and a unique ID. A GUID is used as ID, see class definition Thing.cs. After the production of a thing in a factory, the type and ID are written to an NFC tag by a Web application and the tag is attached to the product. The tag can be read again afterwards, using again the DeviceServer's functionality. The ID of a thing serves as a pointer to a complex data set, which is stored e.g. in the digital twin in the cloud or in a storage built on distributed ledger technology.

Thing ID and serial number

The thing ID is not related to the serial number of a physical asset. Unlike the ID, serial numbers are not necessarily unique; different manufacturers of products may use the same serial numbers. They are therefore stored as a normal attribute in the digital twin.

HTTP client examples

Following are a few simple examples that show how to use the HTTP server from within the PowerShell.

Reading thing data from NFC tag

PS C:\> $response = Invoke-WebRequest -Uri http://SANTACLARA.muc.smarttrust.de:9090/readthing -UseBasicParsing
PS C:\> $response.StatusCode
200

PS C:\> $response.Content | ConvertFrom-Json | ConvertTo-Json
{
    "Type":  9000,
    "TypeAsString":  "Digger",
    "Id":  "fbc0ceff-ed5b-4e7e-8160-2862dfe5bf57",
    "CreatedWhen":  "0001-01-01T00:00:00"
}

Writing thing data to NFC tag

PS C:\> $thing = @{ Type = "80"; 
                    Id = [System.Guid]::NewGuid().toString() }

PS C:\> $json = $thing | ConvertTo-Json 
PS C:\> $json
{
    "Id":  "7b3b9920-225f-4f4e-b1e3-62b08413fdd7",
    "Type":  "80"
}

PS C:\> $response = Invoke-WebRequest -Uri http://SANTACLARA.muc.smarttrust.de:9090/writething -Method POST -Body $json
PS C:\> $response.StatusCode
204

See definition of ThingType in Thing.cs for known thing types.

Softwarestack

DeviceServer is written in C# Version 8.0. It uses the following technologies and software modules:

You do not have to obtain any packages manually, the project file DeviceServer.csproj does this automatically.

Why not building an UWP app?

When a UWP app provides a network service, only its own code or an app on another machine can access the service. Since the app and the web browser should run on the same machine, UWP is not an alternative.

Status

The Software is WIP, development started on June 11, 2020.

What already runs (Thursday, 20/07/16 - 13:10 CEST):

  • Identification of Smartcard readers
  • Identification of NFC tags
  • Writing Thing data to / Reading Thing data from MIFARE Ultralight NFC tag
  • Services of the HTTP Server:
    GET /settings, GET /reader, GET /nfctag, GET /readthing and POST /writething
  • Simple file based emulation mode
  • Setting CORS response headers

Because development takes place in my free time (and I love my family), progress is slow.

Platform and Tools

How to run?

Building the app

Clone the repository, open DeviceServer.sln in VS16 and build the solution.

DeviceServer UI

Click on Start Browser and your standard browser navigates to index.html.

Emulation mode

If a file Thing.json exist in the app's root directory, thing data will be read from / written to it. This simple emulation mode facilitates integration tests without NFC hardware, it can be activated on the app's UI.

DeviceServer UI, Emulation

The following is a file Thing.json resulting from a POST /writething in emulation mode:

PS Microsoft.PowerShell.Core\FileSystem::\\sandboxes.muc.smarttrust.de\Sandboxes\markus\Git-Repositories\DeviceServer\DeviceServer\bin\Debug\netcoreapp3.1> dir

    Verzeichnis: \\sandboxes.muc.smarttrust.de\Sandboxes\markus\Git-Repositories\DeviceServer\DeviceServer\bin\Debug\netcoreapp3.1

Mode                LastWriteTime         Length Name                                                                                                                              
----                -------------         ------ ----                                                                                                                              
d-----       20.06.2020     11:27                logs                                                                                                                              
d-----       12.06.2020     16:21                media                                                                                                                             
-a----       15.06.2020     12:26           4463 DeviceServer.deps.json                                                                                                            
-a----       20.06.2020     11:27         116736 DeviceServer.dll                                                                                                                  
-a----       20.06.2020     11:27         174592 DeviceServer.exe                                                                                                                  
-a----       20.06.2020     11:27          35480 DeviceServer.pdb                                                                                                                  
-a----       15.06.2020     12:26            236 DeviceServer.runtimeconfig.dev.json                                                                                               
-a----       15.06.2020     12:26            161 DeviceServer.runtimeconfig.json                                                                                                   
-a----       09.11.2019     00:56         693680 Newtonsoft.Json.dll                                                                                                               
-a----       26.05.2020     00:17         127488 Serilog.dll                                                                                                                       
-a----       15.05.2020     21:31          29696 Serilog.Sinks.File.dll                                                                                                            
-a----       20.06.2020     12:13            143 Thing.json                                                                                                                        
-a----       20.06.2020     12:11              2 Thing.json.backup                                                                                                                 

PS Microsoft.PowerShell.Core\FileSystem::\\sandboxes.muc.smarttrust.de\Sandboxes\markus\Git-Repositories\DeviceServer\DeviceServer\bin\Debug\netcoreapp3.1> Get-Content -Path .\Thing.json
{
  "Type": 3,
  "TypeAsString": "ExhaustSystem",
  "Id": "00e5acbe-50d3-4563-ac93-94c6af6da61b",
  "CreatedWhen": "0001-01-01T00:00:00"
}

Running the server without Visual Studio

To run the DeviceServer on a computer with Windows 10, a number of six files are required, which are located in the folder bin/Debug/netcoreapp3.1. Additionally version 3.1 of the .NET Core Desktop Runtime is required. The installation requires administration rights.

You also need a PC/SC device driver for the NFC reader to be used.

Hardware

The app was tested with the following hardware:

Smardcard readers

NFC tags