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

[WIP] Arduino code generator #229

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,6 @@ typings/

out/

newmanResponses.json
newmanResponses.json

dummyFile*
66 changes: 66 additions & 0 deletions codegens/arduino/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
.DS_Store
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Prevent IDE stuff
.idea
.vscode
*.sublime-*

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
.coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

out/
76 changes: 76 additions & 0 deletions codegens/arduino/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
### NPM Specific: Disregard recursive project files
### ===============================================
/.editorconfig
/.gitmodules
/test

### Borrowed from .gitignore
### ========================

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Prevent IDE stuff
.idea
.vscode
*.sublime-*

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
.coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

snippet.swift

out/
66 changes: 66 additions & 0 deletions codegens/arduino/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# codegen-arduino

> Converts Postman-SDK Request into code snippet for [Arduino](https://www.arduino.cc/en/Guide/Introduction) code.

#### Prerequisites
To run Code-Gen, ensure that you have NodeJS >= v8. A copy of the NodeJS installable can be downloaded from

## Using the Module
The module will expose an object which will have property `convert` which is the function for converting the Postman-SDK request to HTTP spec and `getOptions` function which returns an array of supported options.

### convert function
Convert function takes three parameters

* `request` - Postman-SDK Request Object

* `options` - options is an object which has following properties
* `trimRequestBody` - Trim request body fields

* `callback` - callback function with first parameter as error and second parameter as string for code snippet


##### Example:
```js
var request = new sdk.Request('www.example.com'), //using postman sdk to create request
options = {};
convert(request, options, function(error, snippet) {
if (error) {
console.error(error);
return;
}
console.log(snippet)
});
```
### getOptions function

This function returns a list of options supported by this codegen.

#### Example
```js
var options = getOptions();

console.log(options);
// output
// [
// {
// name: 'Trim request body fields',
// id: 'trimRequestBody',
// type: 'boolean',
// default: false,
// description: "Remove white space and additional lines that may affect the server's response"
// },
// {
// name: 'WiFi Library',
// id: 'arduinoWifiLibrary',
// type: 'string',
// default: 'WiFiNINA',
// description: 'The Wifi library your Arduino is using, supported values are WiFiNINA|WiFi101|WiFi'
// }
// ]
```
### Guidelines for using generated snippet

* Since Postman-SDK Request object doesn't provide complete path of the file, it needs to be manually inserted in case of uploading a file.

* This module doesn't support cookies.

6 changes: 6 additions & 0 deletions codegens/arduino/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let converter = require('./lib/converter');

module.exports = {
getOptions: converter.getOptions,
convert: converter.convert
};
100 changes: 100 additions & 0 deletions codegens/arduino/lib/converter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
const utils = require('./util'),
httpCodegen = require('./../../http/index');

/**
* Used in order to get additional options for generation of C# code snippet (i.e. Include Boilerplate code)
*
* @module getOptions
*
* @returns {Array} Additional options specific to generation of http code snippet
*/
function getOptions () {
return [{
name: 'Trim request body fields',
id: 'trimRequestBody',
type: 'boolean',
default: false,
description: 'Remove white space and additional lines that may affect the server\'s response'
},
{
name: 'WiFi Library',
id: 'arduinoWifiLibrary',
type: 'string',
default: 'WiFiNINA',
description: 'The Wifi library your Arduino is using, supported values are WiFiNINA|WiFi101|WiFi'
}];
}

/**
* Converts a Postman SDK request to HTTP message
*
* @param {Object} request - Postman SDK request
* @param {Object} options - Options for converter
* @param {Boolean} options.trimRequestBody - determines whether to trim the body or not
* @param {String} options.arduinoWifiLibrary - determines the wifi library used. Default: WiFiNINA
* @param {Function} callback callback
* @returns {Function} returns the snippet with the callback function.
*/
function convert (request, options, callback) {
httpCodegen.convert(request, options, (error, httpSnippet) => {
const clientSnippet = utils.getClientHttpSnippet(httpSnippet),
port = utils.getPort(request),
host = utils.getHost(request);

let wifiLibrary = 'WiFiNINA',
arduinoSnippet = '';
if (['WiFi', 'WiFi101'].includes(options.wifiLibrary)) {
wifiLibrary = options.wifiLibrary;
}

arduinoSnippet += '#include <SPI.h>\n';
arduinoSnippet += `#include <${wifiLibrary}.h>\n`;
arduinoSnippet += '\n';
arduinoSnippet += 'char ssid[] = "myNetwork"; // your network SSID (name)\n';
arduinoSnippet += 'char pass[] = "myPassword"; // your network password\n';
arduinoSnippet += '\n';
arduinoSnippet += 'int status = WL_IDLE_STATUS;\n';
arduinoSnippet += `char server[] = "${host}";\n`;
arduinoSnippet += '\n';
arduinoSnippet += 'WiFiClient client;\n';
arduinoSnippet += '\n';
arduinoSnippet += 'void setup() {\n';
arduinoSnippet += ' Serial.begin(9600);\n';
arduinoSnippet += ' Serial.println("Attempting to connect to WPA network...");\n';
arduinoSnippet += ' Serial.print("SSID: ");\n';
arduinoSnippet += ' Serial.println(ssid);\n';
arduinoSnippet += '\n';
arduinoSnippet += ' status = WiFi.begin(ssid, pass);\n';
arduinoSnippet += ' if ( status != WL_CONNECTED) {\n';
arduinoSnippet += ' Serial.println("Couldn\'t get a wifi connection");\n';
arduinoSnippet += ' while(true);\n';
arduinoSnippet += ' }\n';
arduinoSnippet += ' else {\n';
arduinoSnippet += ' Serial.println("Connected to wifi");\n';
arduinoSnippet += ' Serial.println("\nStarting connection...");\n';
arduinoSnippet += ` if (client.connect(server, ${port})) {\n`;
arduinoSnippet += ' Serial.println("connected");\n';
arduinoSnippet += clientSnippet;
arduinoSnippet += ' }\n';
arduinoSnippet += ' }\n';
arduinoSnippet += '}\n';
arduinoSnippet += '\n';
arduinoSnippet += 'void loop() {\n';
arduinoSnippet += ' printResponse();\n';
arduinoSnippet += '}\n';
arduinoSnippet += '\n';
arduinoSnippet += 'void printResponse() {\n';
arduinoSnippet += ' while (client.available()) {\n';
arduinoSnippet += ' char c = client.read();\n';
arduinoSnippet += ' Serial.write(c);\n';
arduinoSnippet += ' }\n';
arduinoSnippet += '}\n';

return callback(error, arduinoSnippet);
});
}

module.exports = {
getOptions: getOptions,
convert: convert
};
Loading