Nodejs order of execution

hello

i am working on factory testing of a product based on a particle p1

the factory is going to use the particle cli. instead of calling the particle cli commands sequentially i though i just make my own nodejs script calling the particle commands. i have little experience with nodejs and here is my problem: the particle identify and the particle flash in the code below work separately but not as in the code below. i understand why that is (it starts the “flash” while the “identify” is still in progress) - how do you make nodejs wait till the first command is finished before starting the second? i ask the question here because all the solutions i find googling do not seem to work in this case: await, async module, then (this actually works but i cannot figure out how to use that to do what i want)

thanks
frank

"use strict"
  
const SerialCommands = require('particle-cli/dist/cmd/serial');
const FlashCommand = require('particle-cli/dist/cmd/flash');

global.verboseLevel = 1;

// command is found in cli/serial.js
// args are found in cmd/serial.js

// particle identify
new SerialCommands().identifyDevice({port: undefined});

// particle flash
new FlashCommand().flash('p1-bootloader@1.5.2+lto.bin', 'p1-bootloader@1.5.2+lto.bin', [], {usb: false, serial: true, factory: false, force: false, target: undefined, port: undefined, yes: true})

// more commands...

Depending on your needs I would recommend writing your script in either Bash or Python rather than NodeJS. (Not trying to upset anyone, but Bash and Python scripts are simply easier to develop and most *nix systems come with bash and python installed.)

In a Bash script you can just list out the commands that you would normally enter in a terminal, so you could use the particle command directly.

In Python you could use the subprocess module to run Particle CLI processes.

thanks nrobinson2000

that was also my initial thought. unfortunately we are limited by the factory to use windows and vb.net. since i do not have any experience with that either, i thought i prepare all in nodejs (which we have to use due to particle-cli). got this far and now stuck on something which seems trivial…

cheers
frank

1 Like

okay, figured out how to make it work
(thanks to: https://stackoverflow.com/questions/62991384/sequential-processing-in-nodejs)

"use strict"
  
const SerialCommands = require('particle-cli/dist/cmd/serial');
const FlashCommand = require('particle-cli/dist/cmd/flash');

global.verboseLevel = 1;

// command is found in cli/serial.js
// args are found in cmd/serial.js

// particle identify
// new SerialCommands().identifyDevice({port: undefined});

// particle flash
//new FlashCommand().flash('p1-bootloader@1.5.2+lto.bin', 'p1-bootloader@1.5.2+lto.bin', [], {usb: false, serial: true, factory: false, force: false, target: undefined, port: undefined, yes: true});



(async function() {

        await new SerialCommands().identifyDevice({port: undefined})

        await new FlashCommand().flash('p1-bootloader@1.5.2+lto.bin', 'p1-bootloader@1.5.2+lto.bin', [], {usb: false, serial: true, factory: false, force: false, target: undefined, port: undefined, yes: true})
})()
1 Like

@boddeke be aware that these are not considered public APIs - meaning we make no guarantees about their stability as opposed to the CLI interface which we do aim to keep stable from version to version. these internal interfaces will likely change in the future and your script will break or operate in strange ways.

thanks for the head up… that is what i expect. factory system will not upgrade during production - so that is no issue

feature request: it would however be very nice to have the ability to use / extend the particle code in this way to use in the factory. i cannot image we are the only ones running into these problems (see my other posts)

Since you already adding node.js to your Windows, can always use python on windows as long you keep all your “\” and “/” in order, (os.path is great for this), and even making it compatible with Linux for future usage. On top of that, you avoid problems of using non-exposed APIs with subprocess as @m_m and @nrobinson2000 mentioned.

Avoid work for the future @boddeke :wink:

1 Like

Avoid work for the future @boddeke :wink:

that is certainly the plan, always: minimize work now + work later (lazy is good)

though stripping one layer of the particle tools to get the basic functions is easier than interfacing the actual commands (which might also change) in some 3rd language

for the next particle based product i actually hope for better factory tools from particle