Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dots basic translators #1

Open
wants to merge 99 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
99 commits
Select commit Hold shift + click to select a range
b13880a
Chirpstack and TTN basic translator templates for dots products.
dparyani Mar 14, 2024
4dbe2b3
basic translators generator commonJS file.
dparyani Mar 14, 2024
4462833
Added export knownSchemas statement to the template and generated dot…
dparyani Mar 16, 2024
e71c240
Chirpstack and TTN template file changes.
dparyani Mar 16, 2024
04b5202
Basic translators shell script updated.
dparyani Mar 16, 2024
6d3fada
working node js script to generate R11+ dots products' basic translat…
dparyani Mar 16, 2024
456eeec
Chirpstack LNS generated R11+ dots products' basic translators ES5 Ja…
dparyani Mar 16, 2024
f0a6307
TheThingsNetwork LNS generated R11+ dots products' basic translators …
dparyani Mar 16, 2024
89d7a36
git ignore vscode related project file
dparyani Mar 16, 2024
2ace038
Added support for chirpstack v4, renamed respective folders & file(s)…
dparyani Mar 20, 2024
93923c6
Filter out schemas where mapData do not have any output field.
dparyani Apr 18, 2024
09901d6
export knownSchemas updated based on feedback from Lars Mats.
dparyani Apr 19, 2024
924e609
Fixed some bugs and modularized code.
dparyani Apr 20, 2024
323db52
cosmetic changes to be similar to ttn template.
dparyani Apr 20, 2024
b44500a
Updated Chirpstack V3, V4 and TTN basic translators uptill R14.
dparyani Apr 20, 2024
3d8324a
Removed unwanted commented code.
dparyani Apr 21, 2024
cfc8e73
Removed unwanted commented code.
dparyani Apr 21, 2024
723191f
Updated README with basic translator information
James-Holmquist May 8, 2024
0fe90bf
Updated Translators for release R15
dparyani May 14, 2024
2d055b7
Added references in the README to the basic translator branch.
James-Holmquist May 15, 2024
152aa53
Updated README with basic translator information
James-Holmquist May 8, 2024
7d34334
Updated script file to be able to run from ZShell.
dparyani Jul 2, 2024
d63fe4a
Added documentation related to each function.
dparyani Jul 2, 2024
2f3d67e
Added comment generator to show similar schema for CRCs above schema …
dparyani Jul 2, 2024
6bf7afb
R16 related updates.
dparyani Jul 2, 2024
bd63f74
Updated respective LNS' templates as per the changes in dots-translat…
dparyani Jul 17, 2024
7484ab7
Improvization with naming generated basic translator filenames.
dparyani Jul 17, 2024
2e538ed
Updated basic translators for R18 release.
dparyani Jul 17, 2024
cd06f90
Added references in the README to the basic translator branch.
James-Holmquist May 15, 2024
83872f6
Original README.md for this branch.
dparyani Jul 19, 2024
5b632cc
Updated basic translators to reflect R19 release.
dparyani Jul 19, 2024
e0d2c28
Fixed ES6 related issue. Chirpstack V3 follows ES5.
dparyani Jul 29, 2024
6f4cc41
Chirpstack V3 R19 related basic translators regenerated.
dparyani Jul 29, 2024
1ec4c26
Re-generated basic translators uptill R21.
dparyani Aug 9, 2024
c597272
Error in app crc calculation.
dparyani Aug 15, 2024
94ae27b
Error in app crc calculation - basic translators re-generated.
dparyani Aug 15, 2024
aaa14a4
Added README to Dots Basic Translators branch
bjornakesson4 Oct 22, 2024
7b09cc9
Plural fix
bjornakesson4 Oct 22, 2024
ec9a0a5
Added Applications table to README
bjornakesson4 Oct 22, 2024
2d1e875
Typo
bjornakesson4 Oct 22, 2024
15e9837
Updated for release R23.
dparyani Oct 25, 2024
89ec2c1
Sort mapData based on second item column and then based on third iste…
dparyani Oct 26, 2024
cbf63e7
Refactored code/files....new requirement superset mapData implemented…
dparyani Oct 28, 2024
d386612
Updated script file for executing the basic translators generation code.
dparyani Oct 28, 2024
732a6b5
Regenerated basic translators based on the new requirement.
dparyani Oct 28, 2024
a2ada80
Updated basic translators with R24 release
dparyani Nov 15, 2024
358696e
Bug fix: toISOString not needed in addSemtechWifiObject for TTN.
dparyani Nov 25, 2024
3525f06
regenerated ttn basic translators.
dparyani Nov 25, 2024
5ad051a
Updated LNS templates.
dparyani Nov 29, 2024
b9de293
Script to perform git activities for the basic translator branch.
dparyani Nov 29, 2024
f84b4c0
Updated Basic translators to R25
dparyani Nov 29, 2024
959445f
Script updated to generate basic translators for valid products.
dparyani Dec 2, 2024
a41304a
Regenrated R25 for valid products' apps.
dparyani Dec 2, 2024
9fb8d14
Cosmetic syntax error fix.
dparyani Dec 6, 2024
c7df445
ES5 error fixes.
dparyani Jan 24, 2025
de5be89
Regenerated basic translators after bug fix.
dparyani Jan 24, 2025
9e1e0ff
Fix for fport 23 not working in Chirpstack.
dparyani Jan 27, 2025
e9cad33
[Re-generated device translators] Fix for fport 23 not working in Chi…
dparyani Jan 27, 2025
e2c1cb7
Text formatting adjustment
Sensative-Lars-Mats Dec 9, 2024
ab1fcbf
New lifefinder apps
Sensative-Lars-Mats Dec 10, 2024
da9e9fa
Correction based on Dhiraj's review
Sensative-Lars-Mats Dec 12, 2024
09577a9
WIP
Sensative-Lars-Mats Dec 18, 2024
b4ba4c6
Corrected error
Sensative-Lars-Mats Dec 18, 2024
d21c51d
Working mesh translation through yggio
Sensative-Lars-Mats Dec 18, 2024
27b6cf8
Updated with one more mesh app (test app)
Sensative-Lars-Mats Dec 19, 2024
930ae8c
Update of mesh representation
Sensative-Lars-Mats Dec 19, 2024
5d933b2
Updated motion app
Sensative-Lars-Mats Dec 19, 2024
a2b39e5
Upped version
Sensative-Lars-Mats Dec 19, 2024
0fe905b
new version for mesh comfort timecount app
Sensative-Lars-Mats Dec 20, 2024
1dd0c02
New version
Sensative-Lars-Mats Dec 20, 2024
29b4d1e
Added motion measure unconfirmed variant
Sensative-Lars-Mats Dec 20, 2024
4c1df03
WIP: Integrating updated mesh applications
Sensative-Lars-Mats Jan 9, 2025
c2cc8e8
New lifefinder apps
Sensative-Lars-Mats Jan 15, 2025
3b0efd6
Adding new version 106, mostly lifefinder apps update
Sensative-Lars-Mats Jan 15, 2025
3a68cdc
Corrected time ordering of 0-time diff reports
Sensative-Lars-Mats Jan 20, 2025
bed69fa
Remake ordering of 0-time-difference outputs (older follows newer, no…
Sensative-Lars-Mats Jan 20, 2025
892fa19
Remade ordering in time for outputs with zero-time-difference
Sensative-Lars-Mats Jan 20, 2025
c20589c
New Motion Spectrum analysis app.
Sensative-Lars-Mats Jan 24, 2025
54c3fb8
New motion spectrum app version
Sensative-Lars-Mats Jan 25, 2025
e78b335
New version
Sensative-Lars-Mats Jan 25, 2025
b44ed6e
Added decode of mesh stats
Sensative-Lars-Mats Jan 27, 2025
404ab14
Add timestamp to mesh stats
Sensative-Lars-Mats Jan 27, 2025
6cec408
Updated motion spectrum and mesh apps
Sensative-Lars-Mats Jan 27, 2025
84b9139
Correction
Sensative-Lars-Mats Jan 27, 2025
3105f16
1) Added generation of separate release-only translator (only release…
Sensative-Lars-Mats Jan 27, 2025
200e11c
New version of temp modules updates most apps
Sensative-Lars-Mats Jan 28, 2025
46f0eb7
Version 0.2.115
Sensative-Lars-Mats Jan 28, 2025
7c4cd98
Added port 300+ for mesh uplinks, ensuring that they cannot be confus…
Sensative-Lars-Mats Jan 28, 2025
7e1a229
Added mesh comfort extender application
Sensative-Lars-Mats Jan 28, 2025
be80298
New app - comfort mesh extender
Sensative-Lars-Mats Jan 28, 2025
5ff5082
New lifefinder versions
Sensative-Lars-Mats Jan 30, 2025
43e383c
R26 included
Sensative-Lars-Mats Jan 30, 2025
2010222
Updated templates based on parent and in ES5 format.
dparyani Jan 30, 2025
4fff111
Added motion-spectrum sensor + some refactoring.
dparyani Jan 30, 2025
9c424a0
Missing export fix.
dparyani Jan 30, 2025
9c58ee6
Re-generated basic translators based on Release 26
dparyani Jan 30, 2025
7555c98
Updated applications and respective data in the README file.
dparyani Jan 30, 2025
17236e6
Cosmetic fix.
dparyani Jan 30, 2025
f63dd59
Couple of Bug fixes.
dparyani Jan 31, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
working node js script to generate R11+ dots products' basic translat…
…ors from respective LNS' known templates.
  • Loading branch information
dparyani committed Nov 29, 2024
commit 6d3fadab151724a3ae698a5fce99c19c951a43cd
250 changes: 192 additions & 58 deletions basic-translators/dots-basic-translators-generator.cjs
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
/*
Copryright (C) 2024, Sensative AB. All rights reserved.
Author: Dhiraj Paryani

Note:

This is a script which will combine the couple of files
TBD
*/
/**
* Copryright (C) 2024, Sensative AB. All rights reserved.
* Author: Dhiraj Paryani
*
* This script generates JavaScript files based on template files specified in the 'template' array.
* It reads 'knownSchemas' from the 'dots-translator-generated.cjs' file and then iterates over each schema
* to generate a new file for each template.
*
* The script first purges existing files in the output directories of each template.
*
* The program then reads and manipulates data from the knownSchemas, namely replacing newline characters in
* the 'mapData' properties with backslashes. It substitutes schema-specific values into each read-in template,
* including a CRC value, the schema data itself, and a translator version read from 'package.json'. The newly
* generated content replaces the original template content. The output filename is constructed based on the name
* and version of the schema.
*
* Each new JavaScript file is then written to the appropriate output directory corresponding to its template.
* This script automates the generation of Basic Translators' JavaScript files by leveraging templates defined in an
* array. It begins by extracting schema information from a predefined file called 'dots-translator-generated.cjs'. The
* script then iterates over each schema to produce a new file for each template.
*
* Furthermore, the script also generates a CSV file for each template. The CSV files contains the filename,
* schema name, schema versions, CRC value, and template name for each generated JavaScript file. These CSV files
* are also saved in each template's respective output directory.
*
* This script is ES6 compatible and utilizes the Node.js File System (fs) module for file handling.
* Before generating new files, the script clears existing files in the output directories for each template to ensure a
* clean workspace.
*
* Next, it processes the schema data, specifically manipulating the 'mapData' property by replacing newline characters
* with ' + ' delimiters. It inserts schema-specific details such as CRC values, schema data itself, and a translator
* version obtained from 'package.json' into each template. The modified content is then used to replace the original
* template content. The output filenames are constructed based on the schema's name and version.
*
* The generated JavaScript files are saved in their respective output directories.
*
* Additionally, the script generates a CSV file for each template, containing details such as filename, schema name,
* schema versions, CRC value, and template name for every generated JavaScript file. These CSV files are also stored in
* the corresponding output directories.
*
* This script is compatible with ES6 and utilizes the Node.js File System (fs) module for file handling. It provides an
* automated solution for generating JavaScript files tailored to specific schema data.
*/

import * as fs from 'fs';
import * as path from 'path';
import { translate } from '../dots-translator-generated.cjs';
const fs = require('fs');
const path = require('path');
const { knownSchemas } = require('../dots-translator-generated.cjs');

// Check if knownSchemas is defined
if (!knownSchemas) {
console.log("No known schemas found. Exiting.");
process.exit(1);
}

// Update translator version
let translatorVersion = JSON.parse(fs.readFileSync("package.json").toString("utf-8")).version;
let translatorVersion = JSON.parse(fs.readFileSync("../package.json").toString("utf-8")).version;
if (!translatorVersion) {
console.log("Failed to read translator version from package.json file");
process.exit(1);
Expand All @@ -61,46 +59,182 @@ templates.forEach((template) => {
});
});

// Load known schemas from file
const knownSchemas = translate.knownSchemas;

// Iterate over each template
templates.forEach((template) => {
// Prepare csv/json data
let csvData = [];

// Iterate over each schema
for (let [crc, schema] of Object.entries(knownSchemas)) {
// Process schema.mapData - replace \n with \
schema.mapData = schema.mapData.split("\n").join(" \\");

const templateStr = fs.readFileSync(template.path, 'utf8');
const newStr = getSchemaReplacement(templateStr, crc, schema);
const newFilename = path.join(template.outputDir, getFilename(schema));

// Write modified template to file
fs.writeFileSync(newFilename, newStr, 'utf8');

// Prepare csv row data
csvData.push([newFilename, schema.name, schema.versions, crc, template.name]);
}
// Prepare csv row data with header row
let csvData = [['Filename', 'Product Application', 'Versions', 'CRCs']];

// Group knownSchemas by name
const groupedSchemas = {};
for (let [crc, schema] of Object.entries(knownSchemas)) {
if (!(schema.name in groupedSchemas)) {
groupedSchemas[schema.name] = [];
}
groupedSchemas[schema.name].push({crc,schema});
}

// Iterate over each group of knownSchemas with the same name
for (let [name, schemas] of Object.entries(groupedSchemas)) {
// Check if all mapData are identical within the group
const uniqueMapData = new Set(schemas.map(({schema}) => schema.mapData));
if (uniqueMapData.size > 1) {
// If mapData is not identical, process each schema separately
for (const {crc,schema} of schemas) {
// Skip processing if versions are missing or less than R11
if (!schema.versions || schema.versions.trim() === "" || !schema.versions.match(/R\d{2,}/)) {
console.log(`Skipping schema with CRC ${crc} because versions are missing or less than R11.`);
continue;
}

// Extract version numbers and ensure they are at least R11
const versions = schema.versions.split(" ");
const versionNumbers = versions.map(v => parseInt(v.slice(1))); // Extract and parse version numbers
const minVersion = Math.min(...versionNumbers); // Find the minimum version
if (minVersion < 11) {
console.log(`Skipping schema with CRC ${crc} because versions are less than R11.`);
continue;
}

// Process schema.mapData - replace \n with +
schema.mapData = schema.mapData.replace(/\r\n|\r|\n/g, " + ").slice(0, -3);

const templateStr = fs.readFileSync(template.path, 'utf8');
const newStr = getSchemaReplacement(templateStr, crc, schema);
const newFilename = path.join(template.outputDir, getFilename(schema));

// Write modified template to file
fs.writeFileSync(newFilename, newStr, 'utf8');
// Prepare schema data JSON
const schemaJSON = JSON.stringify({
[crc]: {
name: schema.name,
versions: schema.versions,
mapData: schema.mapData
}
}, null, 4)
.replace(/"(\w+)":/g, '$1:')
.replace(/\n/g, '\n '); // Add indentation

// Replace placeholder in template with schema data JSON
const replacedTemplate = newStr.replace(/\/{3} DO NOT CHANGE THE BELOW[\s\S]*\/{3} END DO NOT CHANGE THE ABOVE/g, `/// DO NOT CHANGE THE BELOW - IT IS REPLACED AUTOMATICALLY WITH KNOWN SCHEMA\n var schema = \n ${schemaJSON};\n /// END DO NOT CHANGE THE ABOVE`);


// Write modified template to file
fs.writeFileSync(newFilename, replacedTemplate, 'utf8');

// Prepare csv row data
const csvRow = [
`${path.join(template.outputDir, path.basename(newFilename))}`,
`${schema.name.replace(/,/g, '-')}`,
`${schema.versions}`,
`${crc}`
];

csvData.push(csvRow);
}
} else {
// If mapData is identical, process only one schema for the group
const {crc,schema} = schemas[0];
// Skip processing if versions are missing or less than R11
if (!schema.versions || schema.versions.trim() === "" || !schema.versions.match(/R\d{2,}/)) {
console.log(`Skipping schema with CRC ${crc} because versions are missing or less than R11.`);
continue;
}

// Extract version numbers and ensure they are at least R11
const versions = schema.versions.split(" ");
const versionNumbers = versions.map(v => parseInt(v.slice(1))); // Extract and parse version numbers
const minVersion = Math.min(...versionNumbers); // Find the minimum version
if (minVersion < 11) {
console.log(`Skipping schema with CRC ${crc} because versions are less than R11.`);
continue;
}

// Process schema.mapData - replace \n with +
schema.mapData = schema.mapData.replace(/\r\n|\r|\n/g, " + ").slice(0, -3);

// Log if mapData matches
console.log(`MapData matched for schema: ${schema.name}`);

const templateStr = fs.readFileSync(template.path, 'utf8');
const newStr = getSchemaReplacement(templateStr, crc, schema);
const newFilename = path.join(template.outputDir, getFilename(schema));

// Write modified template to file
fs.writeFileSync(newFilename, newStr, 'utf8');

// Prepare schema data JSON
const schemaJSON = JSON.stringify({
[crc]: {
name: schema.name,
versions: schema.versions,
mapData: schema.mapData
}
}, null, 4)
.replace(/"(\w+)":/g, '$1:')
.replace(/\n/g, '\n '); // Add indentation

// Replace placeholder in template with schema data JSON
const replacedTemplate = newStr.replace(/\/{3} DO NOT CHANGE THE BELOW[\s\S]*\/{3} END DO NOT CHANGE THE ABOVE/g, `/// DO NOT CHANGE THE BELOW - IT IS REPLACED AUTOMATICALLY WITH KNOWN SCHEMA\n var schema = \n ${schemaJSON};\n /// END DO NOT CHANGE THE ABOVE`);

// Write modified template to file
fs.writeFileSync(newFilename, replacedTemplate, 'utf8');

// Prepare csv row data
const csvRow = [
`${path.join(template.outputDir, path.basename(newFilename))}`,
`${schema.name.replace(/,/g, '-')}`,
`${schema.versions}`,
`${schemas.map(({ crc }) => crc).join(', ')}`
];

csvData.push(csvRow);
}
}

// Convert csvData to csv string and write to file
const csvString = csvData.map(row => row.join(',')).join('\n');
fs.writeFileSync(path.join(template.outputDir, 'index.csv'), csvString);
const csvString = csvData.map(row => row.join('|')).join('\n');
const csvFilename = `1-dots-basic-${template.name}-translators-index.csv`;
fs.writeFileSync(path.join(template.outputDir, csvFilename), csvString);
});

function getSchemaReplacement(template, crc, schema) {
// Substitute in the new CRC schema and translator version
return template.replace(/rulesCrc32: \d+/, `rulesCrc32: ${crc}`)
.replace(/\/{3} DO NOT CHANGE THE BELOW[\s\S]*\/{3} END DO NOT CHANGE THE ABOVE/g, JSON.stringify(schema, null, 2))
return template.replace(/rulesCrc32: \d+/, `rulesCrc32: ${crc}`)
.replace(/\/{3} DO NOT CHANGE THE BELOW[\s\S]*\/{3} END DO NOT CHANGE THE ABOVE/g, `/// DO NOT CHANGE THE BELOW - IT IS REPLACED AUTOMATICALLY WITH KNOWN SCHEMAS\n${JSON.stringify(schema, null, 2)}\n /// END DO NOT CHANGE THE ABOVE`)
.replace(/###VERSION###/g, translatorVersion);
}

function getFilename(schema) {
// Prepare filename as requested
// Extract versions
const versions = schema.versions.split(" ").sort();
const vFrom = versions[0];
const vTo = versions[versions.length - 1];
return `${schema.name.replace(' ', '-')}-${vFrom}-to-${vTo}.js`;

// Replace commas with dashes in the schema name
const nameWithoutCommas = schema.name.replace(/,/g, '-');

// Check if there's only one version
if (versions.length === 1) {
return `${nameWithoutCommas}-${vFrom}.js`;
}

// Ensure versions are consecutive
const allVersions = versions.map(v => parseInt(v.slice(1))).sort((a, b) => a - b);
const lowestVersion = allVersions[0];
const highestVersion = allVersions[allVersions.length - 1];
const missingVersions = [];
for (let i = lowestVersion + 1; i < highestVersion; i++) {
if (!allVersions.includes(i)) {
missingVersions.push(`R${i}`);
}
}

// Construct filename
if (missingVersions.length === 0) {
return `${nameWithoutCommas}-${vFrom}-to-${vTo}.js`;
} else {
const allVersions = [vFrom, ...missingVersions, vTo].map(v => v.slice(1)).sort((a, b) => a - b);
return `${nameWithoutCommas}-${allVersions.map(v => `R${v}`).join('-')}.js`;
}
}