{"id":3984,"date":"2019-04-24T09:34:46","date_gmt":"2019-04-24T09:34:46","guid":{"rendered":"https:\/\/grodansparadis.com\/wordpress\/?p=3984"},"modified":"2019-04-24T09:54:49","modified_gmt":"2019-04-24T09:54:49","slug":"howto-read-a-vscp-mdf-file-with-node-js","status":"publish","type":"post","link":"https:\/\/grodansparadis.com\/wordpress\/?p=3984","title":{"rendered":"Howto: Read a #VSCP mdf file with node.js"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <a href=\"https:\/\/grodansparadis.gitbooks.io\/the-vscp-specification\/vscp_module_description_file.html\">Module Description file<\/a> is a XML file that all VSCP devices must have and which describe the device. It can either be stored on the device itself or more common linked by the device and stored on some external server storage. Software that wants to configure a device can fetch this file to get the knowledge to do so. Typically a user interface use this file to guide a user through device configuration.  A good thing is that one software can handle and configure any device.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A sample MDF file is <a href=\"http:\/\/www.eurosource.se\/ntc10KA_3.xml\">here<\/a> an <a href=\"http:\/\/www.eurosource.se\/paris_010.xml\">here<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Read a mdf file from a device, parse it and display the module name.  A device can consist of several modules, there will always be one and <strong>result.vscp.module[0]<\/strong> will always refer to the first.  Most devices contains only one module. You can get the number of modules with <strong>result.vscp.module.length<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Below are some Javascript examples on how to get information from the MDF-file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Display the module name<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst axios = require(&#039;axios&#039;);\nconst xml2js = require(&#039;xml2js&#039;);\n\nlet parser = new xml2js.Parser();\n\naxios.get(&#039;https:\/\/www.eurosource.se\/ntc10KA_3.xml&#039;)  \n  .then((response) =&gt; {\n      parser.parseString(response.data,\n                         (err, result) =&gt; {\n        console.dir(result.vscp.module&#x5B;0].name);    \n      });\n  })  \n  .catch((err) =&gt; {    \n    console.log(err);  \n  });\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Similar to above sample <strong>get a link to the manual for a device<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.log(result.vscp.module&#x5B;0].manual&#x5B;0].$.path);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>List all registers<\/strong> with<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.dir(result.vscp.module&#x5B;0].registers&#x5B;0].reg);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Iterate through all registers an display there names<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; highlight: [5,6,7]; title: ; notranslate\" title=\"\">\naxios.get(&#039;https:\/\/www.eurosource.se\/ntc10KA_3.xml&#039;)\n  .then((response) =&gt; {\n    parser.parseString(response.data, (err, result) =&gt; {\n      for (let reg of result.vscp.module&#x5B;0].registers&#x5B;0].reg) {\n        console.log(reg.name&#x5B;0]._);\n      }\n    });\n\n  })\n  .catch((err) =&gt; {\n    console.log(err);\n  });\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>List register descriptions<\/strong> with<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.log(reg.description&#x5B;0]._);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">You get the <strong>language code for a register name<\/strong> or a register description with<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.log(reg.name&#x5B;0].$.lang);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>List abstractions<\/strong> with<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; highlight: [6]; title: ; notranslate\" title=\"\">\naxios.get(&#039;https:\/\/www.eurosource.se\/ntc10KA_3.xml&#039;)\n  .then((response) =&gt; {\n    parser.parseString(response.data, (err, result) =&gt; {\n      for (let reg of result.vscp.module&#x5B;0].registers&#x5B;0].reg) {\n        console.dir(result.vscp.module&#x5B;0].abstractions&#x5B;0].abstraction);\n      }\n    });\n\n  })\n  .catch((err) =&gt; {\n    console.log(err);\n  });\n\n\n\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>List number of events<\/strong> the module can generate and the events<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.dir(&#039;# events: &#039; + result.vscp.module&#x5B;0].events&#x5B;0].event.length);\nconsole.dir(result.vscp.module&#x5B;0].events&#x5B;0].event);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">If you want an url to a <strong>picture of the module<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.log(result.vscp.module&#x5B;0].picture&#x5B;0].$.path);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">If you want the <strong>manual<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.log(result.vscp.module&#x5B;0].manual&#x5B;0].$.path);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">or a <strong>firmware image<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconsole.log(result.vscp.module&#x5B;0].firmware&#x5B;0].$.path);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>The number of firmware images available<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; highlight: [1,4]; title: ; notranslate\" title=\"\">\naxios.get(&#039;http:\/\/www.eurosource.se\/paris_010.xml&#039;)\n  .then((response) =&gt; {\n    parser.parseString(response.data, (err, result) =&gt; {\n  console.log(result.vscp.module&#x5B;0].firmware.length);\n    });\n  })\n  .catch((err) =&gt; {\n    console.log(err);\n  });\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>The release date for a specific firmware<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconsole.log(result.vscp.module&#x5B;0].firmware&#x5B;3].$.date);\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The way this works should be obvious by now.  <strong>Enjoy!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Module Description file is a XML file that all VSCP devices must have and which describe the device. It can either be stored on the device itself or more common linked by the device and stored on some external server storage. Software that wants to configure a device can fetch this file to get [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[12,53,13],"tags":[],"class_list":["post-3984","post","type-post","status-publish","format-standard","hentry","category-general","category-howtos","category-vscp"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4raCZ-12g","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/grodansparadis.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3984","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/grodansparadis.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/grodansparadis.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/grodansparadis.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/grodansparadis.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3984"}],"version-history":[{"count":20,"href":"https:\/\/grodansparadis.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3984\/revisions"}],"predecessor-version":[{"id":4004,"href":"https:\/\/grodansparadis.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3984\/revisions\/4004"}],"wp:attachment":[{"href":"https:\/\/grodansparadis.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/grodansparadis.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/grodansparadis.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}