Autodownload the mac address database information and make build script standalone
This commit is contained in:
parent
aaa09162a5
commit
dea195045a
@ -1,16 +1,12 @@
|
||||
# MAC address dabase
|
||||
# Building the MAC address database
|
||||
|
||||
The MAC address database has been created using the dowloaded JSON data from [https://macaddress.io](https://macaddress.io).
|
||||
The MAC address database can be created using the `build-database.py` script. The script will download the latest registered MAC address block information from [https://macaddress.io](https://macaddress.io) and creates a SQLITE database file.
|
||||
|
||||
## Build database
|
||||
|
||||
Before you can start the `build-database.py` script, please make sure you downloaded the the online database in JSON format.
|
||||
|
||||
Once the `macaddress.io-db.json` file has been downloaded and placed into this folder, the python tool can be started in order to generate a read performance optimized and minimal database for searching mac address OUIs (Organizationally Unique Identifiers).
|
||||
The generated database is read performance optimized and tried to keep as small as possible for searching MAC address OUIs (Organizationally Unique Identifiers) blocks and returning the registered company name.
|
||||
|
||||
$ python3 build-database.py
|
||||
|
||||
The final database will be named `mac-addresses.db`.
|
||||
|
||||
In nymea the `MacAddressDatabase` will search for this database file in `/usr/share/nymea/mac-addresses.db` and provides an asynch mechanism to provide the manufacturer name for a given mac address.
|
||||
In nymea the `MacAddressDatabase` will search for this database file in `/usr/share/nymea/mac-addresses.db` and provides an asynch mechanism to get the company name for a given MAC address.
|
||||
|
||||
|
||||
@ -34,22 +34,26 @@ import os
|
||||
import sys
|
||||
import json
|
||||
import sqlite3
|
||||
import requests
|
||||
|
||||
downloadUrl='https://macaddress.io/database/macaddress.io-db.json'
|
||||
jsonDataFileName = 'macaddress.io-db.json'
|
||||
print('Loading JSON data from', jsonDataFileName)
|
||||
databaseFileName = 'mac-addresses.db'
|
||||
|
||||
print('Downloading', jsonDataFileName, '...')
|
||||
downloadRequest = requests.get(downloadUrl)
|
||||
open(jsonDataFileName, 'wb').write(downloadRequest.content)
|
||||
|
||||
print('Reading JSON data..')
|
||||
vendorInfoHash = {}
|
||||
jsonDataFile = open(jsonDataFileName, 'r')
|
||||
lines = jsonDataFile.readlines()
|
||||
|
||||
vendorInfoHash = {}
|
||||
for line in lines:
|
||||
vendorMap = json.loads(line)
|
||||
vendorInfoHash[vendorMap['oui']] = vendorMap['companyName']
|
||||
|
||||
jsonDataFile.close()
|
||||
|
||||
# Sort by oui for good binary search in the db
|
||||
sortedOuiHash = sorted(vendorInfoHash.items(), key=lambda x: x[0], reverse=False)
|
||||
databaseFileName = 'mac-addresses.db'
|
||||
if os.path.exists(databaseFileName):
|
||||
print('Delete old db file and create a new one', databaseFileName)
|
||||
os.remove(databaseFileName)
|
||||
@ -61,6 +65,7 @@ cursor.execute('CREATE TABLE oui (oui TEXT PRIMARY KEY, companyNameIndex INTEGER
|
||||
#cursor.execute('CREATE UNIQUE INDEX ouiIndex ON `oui` (`oui`);')
|
||||
|
||||
# Insert all vendor names alphabetically
|
||||
print('Writing company names into database...')
|
||||
sortedVendorHash = sorted(vendorInfoHash.items(), key=lambda x: x[1], reverse=False)
|
||||
vendorCount = 0
|
||||
for vendorInfo in sortedVendorHash:
|
||||
@ -73,6 +78,9 @@ for vendorInfo in sortedVendorHash:
|
||||
connection.commit()
|
||||
|
||||
# Insert all oui with reference to company name
|
||||
print('Writing company names into database...')
|
||||
# Sort by oui for good binary search in the db
|
||||
sortedOuiHash = sorted(vendorInfoHash.items(), key=lambda x: x[0], reverse=False)
|
||||
ouiCount = 0
|
||||
for vendorInfo in sortedOuiHash:
|
||||
insertQuery = 'INSERT OR IGNORE INTO oui (oui, companyNameIndex) VALUES(?, (SELECT rowid FROM companyNames WHERE companyName = ?));'
|
||||
@ -83,6 +91,6 @@ for vendorInfo in sortedOuiHash:
|
||||
|
||||
connection.commit()
|
||||
connection.close()
|
||||
print('Loaded', ouiCount, 'OUI values from', vendorCount, 'manufacturers into', databaseFileName)
|
||||
print('Finished successfully. Loaded', ouiCount, 'OUI values from', vendorCount, 'manufacturers into', databaseFileName)
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user