unetjs 1.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,21 +1,19 @@
1
- JavaScript Helper Library for UnetStack
2
- ==========================
1
+ # JavaScript Helper Library for UnetStack
3
2
 
4
3
  The JavaScript Helper Library for UnetStack is a JavaScript library that enables controlling of a UnetStack Node from JavaScript, both Browser-based (over WebSockets) and Node.JS (TCP).
5
4
 
6
5
  The library contains helper methods, commonly used [Messages](https://fjage.readthedocs.io/en/latest/messages.html) and [Services](https://fjage.readthedocs.io/en/latest/services.html) in Unet, and an implementation of the [UnetSocket API](https://unetstack.net/handbook/unet-handbook_unetsocket_api.html), which is a high-level [Socket-like](https://en.wikipedia.org/wiki/Network_socket) API for communicating over an Unet.
7
6
 
8
-
9
7
  ## Installation
10
8
 
11
9
  ```sh
12
10
  $ npm install unetjs
11
+ ...
13
12
  ```
14
13
 
15
14
  ## Documentation
16
15
 
17
- The API documentation of the latest version of unet.js is published at https://github.com/org-arl/unet-contrib/tree/master/unetsocket/js/docs
18
-
16
+ The API documentation of the latest version of unet.js is published at [https://github.com/org-arl/unet-contrib/tree/master/unetsocket/js/docs](https://github.com/org-arl/unet-contrib/tree/master/unetsocket/js/docs)
19
17
 
20
18
  ## Usage
21
19
 
@@ -57,10 +55,46 @@ rxNtf instanceof UnetMessages.DatagramReq; // returns true;
57
55
 
58
56
  ### UnetSocket
59
57
 
60
- The UnetSocket API is a high-level API exposed by UnetStack to allow users to communicate over an Unet. It is a socket-style API, which allows a user to send [Datagrams]() to specific nodes, or as a broadcast, and similarly listen to Datagrams from specific nodes, etc. A detailed explanation of UnetSocket API can be found in the [Unet Handbook](https://unetstack.net/handbook/unet-handbook_unetsocket_api.html)
58
+ The UnetSocket API is a high-level API exposed by UnetStack to allow users to communicate over an Unet. It is a socket-style API, which allows a user to send [Datagrams](https://unetstack.net/handbook/unet-handbook_datagram_service.html) to specific nodes, or as a broadcast, and similarly listen to Datagrams from specific nodes, etc. A detailed explanation of UnetSocket API can be found in the [Unet Handbook](https://unetstack.net/handbook/unet-handbook_unetsocket_api.html)
61
59
 
62
60
  The JavaScript version of the UnetSocket API allows a user to connect to a node in an Unet from a browser/Node.JS-based application and communicate with other nodes in the Unet. The Datagrams received on those nodes could be consumed by other instances of the UnetSocket, either directly on the node, or on a remote [Gateway](https://fjage.readthedocs.io/en/latest/remote.html#interacting-with-agents-using-a-gateway) connected to that node.
63
61
 
62
+ ### Caching Parameter Responses
63
+
64
+ The UnetSocket API allows a user to cache responses to parameter requests. This is useful in a scenario where the parameters aren't changing very often, and the user wants to reduce the round trip time for parameter requests.
65
+
66
+ UnetSocket API acheives this using two mechanism, firstly, it can request ALL of the parameters from an Agent instead of just one, and then cache the responses. This is called the `greedy` mode. The greedy mode is enabled by default but can be disabled by setting the `greedy` property to `false` in the `CachingAgentID` constructor.
67
+
68
+ Secondly, the UnetSocket API caches the responses to parameter requests for a limited time. If the user requests the same parameter again, the UnetSocket will return the cached response. The time to cache a response is set by the `cacheTime` property in the `CachingAgentID` constructor.
69
+
70
+ ```js
71
+ import {UnetMessages, Gateway} from 'unetjs'
72
+ let gw = new Gateway({...});
73
+ let nodeinfo = await gw.agentForService(Services.NODE_INFO); // returns a CachingAgentID by default.
74
+ let cLoc = nodeinfo.get('location'); // this will request all the Parameters from the Agent, and cache the responses.
75
+ ...
76
+ cLoc = nodeinfo.get('location', maxage=5000); // this will return the cached response if it was called within 5000ms of the original request.
77
+ ...
78
+ cLoc = nodeinfo.get('location', maxage=0); // this will force the Gateway to request the parameter again.
79
+ ```
80
+
81
+ ```js
82
+ import {UnetMessages, Gateway} from 'unetjs'
83
+ let gw = new Gateway({...});
84
+ let nodeinfo = await gw.agentForService(Services.NODE_INFO); // returns a CachingAgentID by default.
85
+ let nonCachingNodeInfo = await gw.agentForService(Services.NODE_INFO, false); // returns an AgentID without caching (original fjage.js functionality).
86
+ let cLoc = nonCachingNodeInfo.get('location'); // this will request the `location` parameter from the Agent.
87
+ ...
88
+ cLoc = nonCachingNodeInfo.get('location'); // this will request the `location` parameter from the Agent again.
89
+
90
+ let nonGreedyNodeInfo = await gw.agentForService(Services.NODE_INFO, true, false); // returns an CachingAgentID that's not greedy.
91
+ let cLoc = nonGreedyNodeInfo.get('location'); // this will request the `location` parameter from the Agent.
92
+ ...
93
+ cLoc = nonCachingNodeInfo.get('location'); // this will request the `location` parameter from the cache.
94
+ ...
95
+ let cLoc = nonGreedyNodeInfo.get('heading'); // this will request the `heading` parameter from the Agent.
96
+ ```
97
+
64
98
  ### Importing/Modules
65
99
 
66
100
  A distribution-ready bundle is available for types of module systems commonly used in the JS world. Examples of how to use it for the different module systems are available in the [examples](/examples) directory.
@@ -92,6 +126,7 @@ const gw = new Gateway({
92
126
  ```
93
127
 
94
128
  ### [UMD](dist)
129
+
95
130
  ```js
96
131
  <script src="unet.min.js"></script>
97
132
  <script>
@@ -102,4 +137,4 @@ const gw = new Gateway({
102
137
  pathname: '/ws/'
103
138
  });
104
139
  </script>
105
- ```
140
+ ```