MLopezJ / LWM2M-JSONSchema

LWM2M protocol From XML to JSON Schema
0 stars 0 forks source link

Implement mandatory instance in LwM2M definition #13

Closed MLopezJ closed 1 year ago

MLopezJ commented 1 year ago

Same as MultipleInstances https://github.com/MLopezJ/LWM2M-JSONSchema/issues/11 , the mandatory property from the registry should be considered in the type definition of the LwM2M object.

<LWM2M xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.openmobilealliance.org/tech/profiles/LWM2M-v1_1.xsd">
  <Object ObjectType="MODefinition">
        ...
        <MultipleInstances>Multiple</MultipleInstances>
        <Mandatory>Mandatory</Mandatory>
        <Resources>
            <Item ID="0">
                           ....
            </Item>
    </Object>
</LWM2M>
MLopezJ commented 1 year ago

Mandatory Instances : 12

list of mandatory registries:

 0, 1, 10315, 10316, 10318, 10319, 10326, 10329, 10330, 10331, 10354, 3,

Optional Instances : 290

list of optional registries:

10, 10241, 10242, 10243, 10244, 10245, 10246, 10247, 10248, 10249, 10250,
  10251, 10252, 10253, 10254, 10255, 10256, 10257, 10258, 10259, 10260, 10262,
  10263, 10264, 10265, 10266, 10267, 10268, 10269, 10270, 10271, 10272, 10273,
  10274, 10275, 10276, 10277, 10278, 10279, 10280, 10281, 10282, 10283, 10284,
  10286, 10290, 10291, 10292, 10299, 10300, 10308, 10309, 10311, 10313, 10314,
  10320, 10322, 10323, 10324, 10327, 10328, 10332, 10333, 10334, 10335, 10336,
  10337, 10338, 10339, 10340, 10341, 10342, 10343, 10344, 10345, 10346, 10347,
  10348, 10349, 10350, 10351, 10352, 10353, 10355, 10356, 10357, 10358, 10359,
  10360, 10361, 10362, 10363, 10364, 10365, 10366, 10368, 10369, 10371, 10374,
  10375, 10376, 10377, 10378, 11, 12, 13, 14, 15, 16, 18830, 18831, 19, 2, 20,
  2048, 2049, 2050, 2051, 2052, 2053, 2054, 2055, 2056, 2057, 21, 22, 23, 24,
  25, 26, 27, 28, 3200, 3201, 3202, 3203, 3300, 3301, 3302, 3303, 3304, 3305,
  3306, 3308, 3310, 3311, 3312, 3313, 3314, 3315, 3316, 3317, 3318, 3319, 3320,
  3321, 3322, 3323, 3324, 3325, 3326, 3327, 3328, 3329, 3330, 3331, 3332, 3333,
  3334, 3335, 3336, 3337, 3338, 3339, 3340, 3341, 3342, 3343, 3344, 3345, 3346,
  3347, 3348, 3349, 3350, 3351, 3352, 3353, 3354, 3355, 3356, 3357, 3358, 3359,
  3360, 3361, 3362, 3363, 3364, 3365, 3366, 3367, 3368, 3369, 3370, 3371, 3372,
  3373, 3374, 3375, 3376, 3377, 3378, 3379, 3380, 3381, 3382, 3383, 3384, 3385,
  3386, 3387, 3388, 3389, 3390, 3391, 3392, 3393, 3394, 3395, 3396, 3397, 3398,
  3399, 3400, 3401, 3402, 3403, 3404, 3405, 3406, 3407, 3408, 3410, 3411, 3412,
  3413, 3414, 3415, 3416, 3417, 3418, 3419, 3420, 3421, 3423, 3424, 3425, 3426,
  3427, 3428, 3429, 3430, 3431, 3432, 3433, 3434, 3435, 3436, 3437, 3438, 3439,
  3441, 3442, 4, 5, 500, 501, 502, 503, 504, 505, 507, 508, 509, 510, 6, 7, 8,
  9,

Script used to get the data


//About: get how many of the objects are mandatory instance and how many are optional instance

import path from "path";
import fs from "fs";
import { readFile, writeFile } from "fs/promises";

const EXTENSION = ".json";
const dirpath = path.join("./lwm2m-registry-json");

const mandatoryInstance: { mandatory: boolean; object: string }[] = [];
const optionalInstance: { mandatory: boolean; object: string }[] = [];

/**
 * Check if registry is defined as Mandatory or Optional
 * @param Lwm2mRegistry
 * @returns { mandatory: boolean; object: string }[]
 */
const getInfo = (
  Lwm2mRegistry: any
): { mandatory: boolean; object: string } => {
  const id: string = Lwm2mRegistry.ObjectID[0];

  const isMandatory = Lwm2mRegistry.Mandatory[0] === "Mandatory" ? true : false;

  return { mandatory: isMandatory, object: id };
};

/**
 * Read file
 * @param element
 */
const readJson = async (element: any) => {
  const fileName = element.split(".")[0];
  if (
    !["Common", "DDF", "LWM2M_senml_units", "LWM2M-v1_1", "LWM2M"].includes(
      fileName
    )
  ) {
    console.log(`-- processing element ${fileName} --`);
    const jsonPath = `./lwm2m-registry-json/${fileName}.json`;
    const json = JSON.parse(await readFile(jsonPath, "utf-8"));
    const instanceType = getInfo(json.LWM2M.Object[0]);
    instanceType.mandatory
      ? mandatoryInstance.push(instanceType)
      : optionalInstance.push(instanceType);
  }
};

fs.readdir(dirpath, async (err, files) => {
  for (const file of files) {
    if (path.extname(file) === EXTENSION) {
      await readJson(file);
    }
  }
  const mandatory = mandatoryInstance.map((element) => element.object);
  await writeFile("./mandatoryInstance.ts", `const mandatory = [${mandatory}]`);
  const optional = optionalInstance.map((element) => element.object);
  await writeFile("./optionalInstance.ts", `const optional = [${optional}]`);
});