Skip to content

Commit

Permalink
Extend readme: data items + connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
megahoneybadger committed Dec 9, 2019
1 parent 4900823 commit 9af185e
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 2,083 deletions.
160 changes: 157 additions & 3 deletions README.md
Expand Up @@ -190,8 +190,8 @@ In this case timeout values will be the following:
| ------------ | ------------ |
| T3 | 45 |
| T5 | 10 |
| T6 | 5 |
| T7 | 10 |
| T6 | 5 |
| T7 | 10 |
| T8 | 5 |
| LT | 0 (*if value is zero driver does not send link test request) |

Expand Down Expand Up @@ -223,7 +223,161 @@ And here is a final sample for connection configuration:
.build());
##### Connection
TODO :)
The next step is to create a connection object:

const conn = new Connection(config);

Connection object supports a few events which are important for the message exchange:
- *established*
Physical connection has been established and the driver entered the selected state. This event means that we are ready for HSMS message exchange.
- *dropped*
Connection has been dropped and the driver entered not connected state.
- *recv*
New message has been received.
- *timeout*
Driver fired a control timeout (e.g. connection did not receive a reply).
- *error*
Error occured when handling a message.

Here is the example:

conn
.on("established", p => console.log( `connection established: ${p.ip}:${p.port}` ))
.on("dropped", () => console.log(`connection dropped`))
.on("recv", m => console.log(`${m.toString()}`))

##### Data Items
The main exchange unit within HSMS universe is a message. But every message consists of building blocks called data items. Every data item represents a value (or array of values) and data type. The driver supports the following types:

| Data Type | Description |
| ------------ | ------------ |
|I1 | 1 byte integer (signed) |
| I2 | 2 byte integer (signed) |
| I4 | 4 byte integer (signed) |
| I8 | 8 byte integer (signed) |
| U1 | 1 byte integer (unsigned) |
| U2 | 2 byte integer (unsigned) |
| U4 | 4 byte integer (unsigned) |
| U8 | 8 byte integer (signed) |
| F4 | 4 byte floating point |
| F8 | 8 byte floating point |
| A | ASCII |
| List | list |

Here is a example how to create data items:

const {
Config,
ConnectionMode,
Timers,
ItemFormat,
DataItem} = require('hsms-driver')
const i1 = DataItem.i1( "item-name-1", 12 );
const i1arr = DataItem.i1( "item-name-2", 12, 121, -8, "7" );

console.log( i1.toString() ); // I1 item-name-1 [12]
console.log( i1.name ); // item-name-1
console.log( i1.format ); // 100 or ItemFormat.U1
console.log( i1.value ); // 12

console.log( i1arr.toString() ); // I1<4> item-name-2 [12,121,-8,7]
console.log( i1arr.name ); // item-name-2
console.log( i1arr.format ); // 100 or ItemFormat.I1
console.log( i1arr.value ); // Array(4) [12, 121, -8, 7]

const i2 = DataItem.i2( "item-name-3", 12 );
const i2arr = DataItem.i2( "item-name-4", 12, 121, -8, "7" );

console.log( i2.toString() ); // I2 item-name-3 [12]
console.log( i2.name ); // item-name-3
console.log( i2.format ); // 104 or ItemFormat.I2
console.log( i2.value ); // 12

console.log( i2arr.toString() ); // I2<4> item-name-4 [12,121,-8,7]
console.log( i2arr.name ); // item-name-4
console.log( i2arr.format ); // 104 or ItemFormat.I2
console.log( i2arr.value ); // Array(4) [12, 121, -8, 7]

const i4 = DataItem.i4( "item-name-3", 54312432 );
const i4arr = DataItem.i4( "item-name-4", 43213212, 0x2121, "-817543" );

console.log( i4.toString() ); // I4 item-name-3 [54312432]
console.log( i4.name ); // item-name-3
console.log( i4.format ); // 112 or ItemFormat.I4
console.log( i4.value ); // 54312432

console.log( i4arr.toString() ); // I4<3> item-name-4 [43213212,8481,-817543]
console.log( i4arr.name ); // item-name-4
console.log( i4arr.format ); // 112 or ItemFormat.I4
console.log( i4arr.value ); // Array(3) [43213212, 8481, -817543]

const f4 = DataItem.f4( "item-name-5", 54312432 );
const f4arr = DataItem.f4( "item-name-6", 43213212, 0x2121, "-817543" );

console.log( f4.toString() ); // F4 item-name-5 [54312432]
console.log( f4.name ); // item-name-5
console.log( f4.format ); // 144 or ItemFormat.F4
console.log( f4.value ); // 54312432

console.log( f4arr.toString() ); // F4<3> item-name-6 [43213212,8481,-817543]
console.log( f4arr.name ); // item-name-6
console.log( f4arr.format ); // 144 or ItemFormat.F4
console.log( f4arr.value ); // Array(3) [43213212, 8481, -817543]

const a1 = DataItem.a( "item-name-9", "very long string", 5 );
const a2 = DataItem.a( "item-name-10", "short", 10 );


console.log( a1.toString() ); // item-name-9 [very ]
console.log( a1.name ); // item-name-9
console.log( a1.format ); // 64 or ItemFormat.A
console.log( a1.value ); //'very '
console.log( a1.size ); // 5

console.log( a2.toString() ); // A<10> item-name-10 [short ]
console.log( a2.name ); // item-name-10
console.log( a2.format ); // 64 or ItemFormat.A
console.log( a2.value ); 'short '
console.log( a2.size ); // 10

const list = DataItem.list( "list-1",
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 ) )
);

console.log( list.toString() );
// List list-1
// A<5> item-name-9 [very ]
// I2 item-name-3 [12]
// List child-1
// U2 age [12]
// A<30> name [John Smith ]
console.log( list.name ); // list-1
console.log( list.format ); // 0 or ItemFormat.List
console.log( list.value ); // Array(3) [DataItem, DataItem, DataItem]

Pay attention:
- numeric types and lists do not have size (array length is not an item size)
- numeric types support single value items as well as array value items
- strings require a size: if a given string value is shorter than a size it will be padded with spaces and vice versa if it is longer it will be trimmed















Expand Down
19 changes: 0 additions & 19 deletions dist/hsms-driver.js

This file was deleted.

2,006 changes: 0 additions & 2,006 deletions dist/main.js

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
@@ -1,14 +1,14 @@
{
"name": "hsms-driver",
"version": "0.2.11",
"version": "0.2.12",
"description": "JavaScript driver for HSMS protocol.",
"main": "index.js",
"scripts": {
"test": "mocha --recursive",
"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": "nde index.js",
"start": "node index.js",
"lint": "eslint ./src --fix",
"build": "webpack -p"
},
Expand Down
89 changes: 36 additions & 53 deletions test-suite.js
Expand Up @@ -26,6 +26,35 @@ const {
ExpectedSelectReqError } = require( './src/utils/errors/custom-errors' )

try {
const a1 = DataItem.a( "item-name-9", "very long string", 5 );
const a2 = DataItem.a( "item-name-10", "short", 10 );


console.log( a1.toString() );
console.log( a1.name );
console.log( a1.format );
console.log( a1.value );
console.log( a1.size );

console.log( a2.toString() );
console.log( a2.name );
console.log( a2.format );
console.log( a2.value );
console.log( a2.size );

var list = DataItem.list( "list-1",
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 ) )
);

console.log( list.toString() );
console.log( list.name );
console.log( list.format );
console.log( list.value );


const item = DataItem
.builder
Expand Down Expand Up @@ -75,29 +104,13 @@ try {
var index = 0;

conn
.on("dropped", () => {
console.log(`client connection has been dropped`);
})
// .on( "connected", ( p ) => {
// console.log( `connected: ${p.ip}:${p.port}` );
// conn.send( m )
// } )
.on("error", (err) => console.log(`encountered error: ${err}`))
.on("established", (r) => {
conn.send(new SeparateReq());
console.log( "connection established" )

//conn.send(new DeselectRsp());
conn.send( new SeparateReq() )
})
.on("recv", (m) => {
console.log(`c rec[${m.toLongString()}]`);
})
.on( "timeout", t => console.log( `timeout t${t}` ) );
.on("established", p => console.log( `connection established: ${p.ip}:${p.port}` ))
.on("dropped", () => console.log(`connection dropped`))
.on("recv", m => console.log( m.toString() ))
.on("timeout", (t, m) => console.log( `t${t}` ) );

//setTimeout( () => , 200 );

conn.removeAllListeners( "recv" );

const server = new Connection(Config
.builder
Expand All @@ -109,6 +122,9 @@ try {
.build());

server
.on("established", () => {
setTimeout( () => server.send( m ), 1000);
} )
.on("dropped", () => {
console.log(`server connection has been dropped`);
})
Expand All @@ -126,38 +142,5 @@ catch (err) {
console.log(err);
}

console.log("end");
//--------------------
const {
DataItem,
DataMessage,
Config,
ConnectionMode,
Connection,
Timers } = require('hsms-driver')

let m = DataMessage
.builder
.device( 1 )
.stream( 1 )
.replyExpected( true )
.func( 1 )
.items(
DataItem.f4( "temp", 12.1 ),
DataItem.f8( "temp", 14.53 ) )
.build();

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

conn
.on("dropped", () => console.log(`client connection has been dropped`))
.on("established", p => conn.send( m ))
.on("recv", m => console.log(`${m.toLongString()}`))

conn.start();

0 comments on commit 9af185e

Please sign in to comment.