npm package: @samtec-ash/virtuintestcomm
Description: Virtuin Test Platform communication utilities for broadcasting, direct, etc communication over RabbitMQ.
Author: Adam Page
Latest version: 0.9.8


# ![Virtuin Logo](https://s3.amazonaws.com/virtuin-static-images/64x64.png) Virtuin Test Comm  

## Overview

Virtuin Test Comm. provides utilities for broadcasting, direct, etc communication over RabbitMQ.  
The following classes are provided:

* __VirtuinTestPublisher__: Used by active running test to update status and results to subscribers.

* __VirtuinTestSubscriber__: Used by any process wanting to receive test status/result updates.

* __VirtuinTestViewClient__: Used by test view (e.g. web-server) to send requests to test view server.

* __VirtuinTestViewServer__: Used primarily by active test to listen for view requests.  

* __VirtuinTestDispatcher__: Used by Virtuin GUI to dispatch tests to a test environment server.

* __VirtuinTestHandler__:  Used within test environment to handle receiving test start/stop/status/clear requests.  
Not recommended to be used alone. Instead use [VirtuinTestServer](https://bitbucket.org/samteccmd/virtuintestserver#readme).


### Build

```bash
yarn run prepublish
```

### Installation

```bash
yarn add @samtec-ash/virtuintestcomm
```

### Examples

##### VirtuinTestPublisher

```javascript
import {
  VirtuinTestPublisher
} from '@samtec-hub/virtuintestcomm';

publisher = new VirtuinTestPublisher(
  'DebugStation',
  'DebugTest',
  'DebugUUID'
);

await publisher.open('localhost');
publisher.updateStatus(
  state='STARTING',
  progress=0
);
await publisher.publish(
  message='Just warming up.',
  error=undefined,
  customDict={a=1}
);
publisher.updateStatus(
  state='FINISHED',
  progress=100
);
await publisher.publish(
  message='All done.',
  error=undefined,
  results=[{type='scalar', name='Temp', unit='C', value='28'}],
  customDict={a=2}
);
await publisher.close();
```
##### VirtuinTestSubscriber

```javascript
import {
  VirtuinTestSubscriber
} from '@samtec-hub/virtuintestcomm';

subscriber = new VirtuinTestSubscriber('DebugStation');
await subscriber.open('localhost');
let subscribeTag;
const subscribeCB = async (err, data) => {
  if (err) {
    console.log(`Received error ${err}`);
  } else if (data) {
    if (data.state === 'STARTED') {
      console.log('Test started');
    } else if (data.progress === 100 || data.state === 'FINISHED') {
      console.log('Test finished');
      await subscriber.unsubscribe(subscribeTag);
      await subscriber.close();
    }
  }  
};
subscribeTag = await subscriber.subscribe(subscribeCB);
```

##### VirtuinTestViewServer

```javascript
import {
  VirtuinTestViewServer
} from '@samtec-hub/virtuintestcomm';

viewServer = new VirtuinTestViewServer('DebugStation', 'DebugTest');
await viewServer.open('localhost');

let listenTag;
const listenCB = async (err, data) => {
  if (err) {
    console.log(`Received error ${err}`);
  } else if (data) {
    if (typeof data.operation === 'string') {
      switch(data.operation.toUpperCase()) {
        case 'FOO': {
          console.log('Doing FOO');
          return { success: true, operation: 'FOO' };
        }
        case 'BAR': {
          console.log('Doing BAR');
          return { success: true, operation: 'BAR' };
        }
        case 'EXIT': {          
          setTimeout(() => { viewServer.close(); }, 500);
          return { success: true, operation: 'EXIT' };
        }
        default: {
          console.log('Operation unknown');
          return { success: false };
        }
      }
    }
  }  
};
listenTag = await viewServer.listen(listenCB);
```

##### VirtuinTestViewClient

```javascript
import {
  VirtuinTestViewClient
} from '@samtec-hub/virtuintestcomm';

viewClient = new VirtuinTestViewClient('DebugStation', 'DebugTest');
await viewClient.open('localhost');
viewClient.write({ operation: 'FOO' });
viewClient.close();
```

### API

[API HTML](docs/index.html)  
[API MD](docs/api.md)