Skip to content

Commit

Permalink
Add tests for messages with replies.
Browse files Browse the repository at this point in the history
  • Loading branch information
megahoneybadger committed Dec 17, 2019
1 parent e34dac1 commit 37d1d69
Show file tree
Hide file tree
Showing 8 changed files with 530 additions and 225 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Expand Up @@ -12,7 +12,7 @@ report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.seed git
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
Expand Down Expand Up @@ -40,9 +40,6 @@ build/Release
# Dependency directories
node_modules/
jspm_packages/
coverage/
dist/
.vscode/

# TypeScript v1 declaration files
typings/
Expand Down
17 changes: 17 additions & 0 deletions .vscode/launch.json
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/test-suite.js"
}
]
}
85 changes: 76 additions & 9 deletions README.md
@@ -1,5 +1,6 @@



![](https://www.semi.org/themes/custom/semi/logo.svg)

[![Build Status](https://api.travis-ci.com/megahoneybadger/hsms.svg?branch=master)](https://travis-ci.com/megahoneybadger/hsms) [![Coverage Status](https://coveralls.io/repos/github/megahoneybadger/hsms/badge.svg?branch=master)](https://coveralls.io/github/megahoneybadger/hsms?branch=master)
Expand Down Expand Up @@ -435,8 +436,8 @@ Lists:
DataItem.a( "item-name-9", "very long string", 5 ),
DataItem.i2( "item-name-3", 12 ),
DataItem.list( "child-1",
DataItem.u2( "age", 12 ),
DataItem.a( "name", "John Smith", 30 ) )
DataItem.u2( "age", 12 ),
DataItem.a( "name", "John Smith", 30 ) )
);

console.log( list.toString() );
Expand Down Expand Up @@ -506,12 +507,13 @@ Here is the example on how to create your first data message:
When message building is over we are ready to send it. But before, let's summarize all the steps you have to take to send the message:

- create a configuration object where you specify network and timeout details
- create a connection based on the configuration, subscribe to events you need and call a method 'start'
- create a connection based on the configuration, subscribe to events you need and call a method 'start'
- create a data message with required stream and function numbers, add data items
- send the message via 'send' method of the created connection object

And here is the full sample: as soon as two driver gets connected a passive connection sends a data message and does not expect a reply.
Let's analyze a few examples:

*Example #1: send a message which does not need a reply.*

const {
DataItem,
Expand All @@ -537,7 +539,7 @@ And here is the full sample: as soon as two driver gets connected a passive conn
.builder
.device( 1 )
.stream( 5 )
.replyExpected( false)
.replyExpected( false /*!!!*/)
.func( 1 )
.items(
DataItem.f4( "temperature", 12.1 ),
Expand All @@ -553,18 +555,83 @@ And here is the full sample: as soon as two driver gets connected a passive conn
.build());

server
.on("established", p => server.send( m ));
.on("established", p => server.send( m ));

server.start();
conn.start();

As soon as two driver instances gets connected a passive connection sends a data message and does not expect a reply.

TODO:
message callbacks and messages with replies
*Example #2: send a message and get a reply.*

const {
DataItem,
DataMessage,
Config,
Message,
ConnectionMode,
Connection } = require('hsms-driver')

const conn = new Connection(Config
.builder
.ip("127.0.0.1")
.port(7000)
.mode(ConnectionMode.Active)
.build());

conn
.on("established", p => console.log( `connection established: ${p.ip}:${p.port}` ))
.on("dropped", () => console.log(`connection dropped`))
.on("timeout", (t, m) => console.log( `client t${t}` ) )
.on( "recv", m => {
if( m.kind == Message.Type.DataMessage){
console.log( `client recv ${m.toLongString()}` );
switch( m.toString() ){
case "S1F1":
conn.send( DataMessage
.builder
.reply( m )
.items(
DataItem.a( "name", "bob", 10 ),
DataItem.u2( "age", 12 ),
DataItem.list( "hobbies",
DataItem.a( "hobby-1", "basketball", 10 ),
DataItem.a( "hobby-2", "books", 15 )))
.build() )
break;
}
}
} );

let m = DataMessage
.builder
.device( 1 )
.stream( 1 )
.func( 1 )
.items(
DataItem.a( "name", "alice", 10 ),
DataItem.u2( "age", 10 ))
.build();

const server = new Connection(Config
.builder
.ip("127.0.0.1")
.port(7000)
.mode(ConnectionMode.Passive)
.build());

server
.on("established", p => server.send( m ))
.on("timeout", (t, m) => console.log( `server t${t}` ) )
.on( "recv", m => {
if( m.kind == Message.Type.DataMessage){
console.log( `server recv ${m.toLongString()}` );
}
} );

server.start();
conn.start();

As soon as two drivers instances gets connected a passive connection sends a data message and receives a reply.

TODO: message callback example, control messages etc.
7 changes: 3 additions & 4 deletions package.json
@@ -1,17 +1,16 @@
{
"name": "hsms-driver",
"version": "0.2.14",
"version": "0.2.15",
"description": "JavaScript driver for HSMS protocol.",
"main": "index.js",
"scripts": {
"test": "mocha --recursive",
"testc": "npm test -- --grep \"Data Message\"",
"testc": "npm test -- --grep \"Communication active\"",
"test-with-coverage": "nyc --reporter=text mocha --recursive",
"cover": "node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha -- --recursive test/*",
"start": "node index.js",
"lint": "eslint ./src --fix",
"build": "webpack -p",
"debug": "node --nolazy --inspect-brk=9229 test-suite.js"
"build": "webpack -p"
},
"author": "",
"license": "Apache-2.0",
Expand Down
42 changes: 34 additions & 8 deletions test-suite.js
Expand Up @@ -3,6 +3,7 @@ const {
DataItem,
DataMessage,
Config,
Message,
ConnectionMode,
Connection } = require('./src/hsms')

Expand All @@ -16,19 +17,36 @@ const conn = new Connection(Config
conn
.on("established", p => console.log( `connection established: ${p.ip}:${p.port}` ))
.on("dropped", () => console.log(`connection dropped`))
.on("recv", m => console.log( m.toLongString() ))
.on("timeout", (t, m) => console.log( `t${t}` ) );
.on("timeout", (t, m) => console.log( `client t${t}` ) )
.on( "recv", m => {
if( m.kind == Message.Type.DataMessage){
console.log( `client recv ${m.toLongString()}` );

switch( m.toString() ){
case "S1F1":
conn.send( DataMessage
.builder
.reply( m )
.items(
DataItem.a( "name", "bob", 10 ),
DataItem.u2( "age", 12 ),
DataItem.list( "hobbies",
DataItem.a( "hobby-1", "basketball", 10 ),
DataItem.a( "hobby-2", "books", 15 )))
.build() )
break;
}
}
} );

let m = DataMessage
.builder
.device( 1 )
.stream( 5 )
.replyExpected( false)
.stream( 1 )
.func( 1 )
.items(
DataItem.f4( "temperature", 12.1 ),
DataItem.f8( "pressure", 14.53 ),
DataItem.a( "description", "this is a long sensor description", 50 ))
DataItem.a( "name", "alice", 10 ),
DataItem.u2( "age", 10 ))
.build();

const server = new Connection(Config
Expand All @@ -39,7 +57,15 @@ const server = new Connection(Config
.build());

server
.on("established", p => server.send( m ));
.on("established", p => server.send( m ))
.on("timeout", (t, m) => console.log( `server t${t}` ) )
.on( "recv", m => {
if( m.kind == Message.Type.DataMessage){
console.log( `server recv ${m.toLongString()}` );
}
} );



server.start();
conn.start();
Expand Down

0 comments on commit 37d1d69

Please sign in to comment.