Skip to content

Commit

Permalink
Fix: Appearance(s) now use Decimal value(s) and categories
Browse files Browse the repository at this point in the history
This matches how company_ids work as well. They're decimals, even though Bluetooth SIG defines them as hex.
  • Loading branch information
dinesharjani committed Jan 16, 2024
1 parent 621f406 commit 0688c6e
Show file tree
Hide file tree
Showing 4 changed files with 1,320 additions and 59 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ Appearance is a new addition to this database prompted by user [eriklins](https:

| Field | Type | Description | Required |
| ------|------|----------| --- |
| category | `Int` | Decimal value identifying the Category, corresponding to reversed bits 15 to 6 of Appearance Data | **Yes** |
| category | `Integer` | Decimal value identifying the Category, corresponding to reversed bits 15 to 6 of Appearance Data | **Yes** |
| name | `String` | Name of the Category | **Yes** |
| subcategory | `[Sub-Category]` | Array of Sub-Categories within an Appearance Category | **No** |

#### Sub-Category Schema

| Field | Type | Description | Required |
| ------|------|----------| --- |
| value | `Int` | Decimal value identifying the sub-category within a parent category, corresponding to the reversed bits 5 to 0 from Appearance Data | **Yes** |
| value | `Integer` | Decimal value identifying the sub-category within a parent category, corresponding to the reversed bits 5 to 0 from Appearance Data | **Yes** |
| name | `String` | Name of the Sub-Category | **Yes** |

## Rules and Contributions
Expand Down
36 changes: 35 additions & 1 deletion decimal_ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
const version = require('./package.json').version;
const pathForJson = filename => `${__dirname}/v${version.split('.')[0]}/${filename}.json`;

// Task 1: Fix Company IDs

const companyIdsFile = pathForJson('company_ids')
console.debug(companyIdsFile)

Expand Down Expand Up @@ -68,4 +70,36 @@ readlineInterface.on('close', () => {
}
});
console.log(`Hex Company IDs Fix-up completed. ${modificationsCount} IDs modified.`);
});
});

// Task 2: Fix Appearances values / categories

const appearancesFile = pathForJson('gap_appearance')
const rawData = fs.readFileSync(appearancesFile);
let jsonData = JSON.parse(rawData);

// Helper function to convert hex to decimal for the subcategories
function convertHexToDecimal(subcategories) {
subcategories.forEach(subcategory => {
if (subcategory.hasOwnProperty('value')) {
subcategory.value = parseInt(subcategory.value, 16);
}
});
}

// Iterate through the main categories
jsonData.forEach(category => {
// Convert hex to decimal for the main category
if (category.hasOwnProperty('category')) {
category.category = parseInt(category.category, 16);
}

// Check if subcategories exist and convert hex to decimal for them
if (category.hasOwnProperty('subcategory')) {
convertHexToDecimal(category.subcategory);
}
});

// Write the updated data back to the file
const updatedData = JSON.stringify(jsonData, null, 2);
fs.writeFileSync(appearancesFile, updatedData);
5 changes: 2 additions & 3 deletions v1/appearance_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"type": "object",
"properties": {
"category": {
"type": "string",
"pattern": "^[0-9A-Fa-f]+$"
"type": "integer"
},
"name": {
"type": "string"
Expand All @@ -15,7 +14,7 @@
"items": {
"type": "object",
"properties": {
"value": { "type": "string" },
"value": { "type": "integer" },
"name": { "type": "string" }
},
"required": ["value", "name"]
Expand Down
Loading

0 comments on commit 0688c6e

Please sign in to comment.