The core javascript libraries providing an abstract interface to access
- the VSCP event communication bus (independent on physical layer),
- and variables, stored in the VSCP daemon database.
The communication to the VSCP daemon takes place via websocket and REST.
In general its recommended to use a released version. If you know what to do, use the trunk, but don't complain later on. ;-)
If you like, you can bind this repository as git submodule to your own one.
- Add it e.g. to the existing folder
js
viagit submodule add https://github.com/grodansparadis/vscp-js.git js/vscp-js
, which will create the subfoldervscp-js
and clones the repository there. - The previous step will create an additional file, called
.gitmodules
, which contains the submodule informations. - Commit the changes now to your repository.
Note, the submodule is not automatically updated with a git pull
, you have to explicit call git pull --recursive
or git submodule update --recursive --remote
.
Include the following javascript files to your HTML:
<!-- VSCP websocket authentication encryption/decryption -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/aes-js/3.1.0/index.min.js"></script>
<!-- VSCP common core library -->
<script type="text/javascript" src="js/vscp-js/vscp.js"></script>
<!-- VSCP class definitions -->
<script type="text/javascript" src="js/vscp-js/vscp_class.js"></script>
<!-- VSCP type definitions -->
<script type="text/javascript" src="js/vscp-js/vscp_type.js"></script>
<!-- VSCP websocket client library -->
<script type="text/javascript" src="js/vscp-js/vscpws.js"></script>
The websocket API supports callbacks and Promise to handle the asynchronous communication. Only the first two of the following examples will shows both possibilities. All others continoue using promises.
Example using callbacks:
var vscpClient = new vscp.ws.Client();
vscpClient.connect({
url: "ws://localhost:8884/ws1",
userName: "admin",
password: "secret",
vscpkey: "2DBB079A38985AF00EBEEFE22F9FFA0E7F72DF06EBE44563EDF4A1073CABC7D4",
onSuccess: function() {
// Implement your code here ...
},
onError: function() {
// Implement your code here ...
}
});
Example using native promises:
var vscpClient = new vscp.ws.Client();
vscpClient.connect({
url: "ws://localhost:8884/ws1",
userName: "admin",
password: "secret",
vscpkey: "2DBB079A38985AF00EBEEFE22F9FFA0E7F72DF06EBE44563EDF4A1073CABC7D4",
})
.then(function() {
// Implement your code here ...
})
.catch(function() {
// Implement your code here ...
});
Example using callbacks:
vscpClient.disconnect({
onSuccess: function(client) {
// Implement your code here ...
}
});
Example using native promises:
vscpClient.disconnect()
.then(function() {
// Implement your code here ...
})
.catch(function() {
// Implement your code here ...
});
vscpClient.sendEvent({
event: new vscp.Event({
vscpClass: vscp.constants.classes.VSCP_CLASS1_CONTROL,
vscpType: vscp.constants.types.VSCP_TYPE_CONTROL_TURNON,
vscpPriority: vscp.constants.priorities.PRIORITY_3_NORMAL,
vscpData: [ 0, 1, 12 ]
})
})
.then(function() {
console.info("TURNON event sent.");
// Implement your code here ...
})
.catch(function() {
console.error("Failed to send TURNON event.");
// Implement your code here ...
});
First add a event listener to the client.
// Catch all events and show them in the trace tab.
var eventListener = function(client, evt) {
// Implement your code here ...
};
// Add event listener
vscpClient.addEventListener(eventListener);
In the second step, start listening.
// Start receiving VSCP events
vscpClient.start()
.then(function() {
console.info("Receiving VSCP events started.");
// Implement your code here ...
})
.catch(function() {
console.error("Failed to start receiving VSCP events.");
// Implement your code here ...
});
Include the following javascript files to your HTML:
<!-- jQuery used for ajax calls -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- VSCP common core library -->
<script type="text/javascript" src="js/vscp-js/vscp.js"></script>
<!-- VSCP REST client library -->
<script type="text/javascript" src="js/vscp-js/vscprest.js"></script>
The websocket API supports callbacks and Promise to handle the asynchronous communication. Only the first two of the following examples will shows both possibilities. All others continoue using promises.
Example using callbacks:
var vscpClient = new vscp.rest.Client({
baseUrl: "http://localhost:8884"
});
vscpClient.openSession({
user: "admin",
password: "secret",
onSuccess: function() {
// Implement your code here ...
},
onError: function() {
// Implement your code here ...
}
});
Example using native promises:
var vscpClient = new vscp.rest.Client({
baseUrl: "http://localhost:8884"
});
vscpClient.openSession({
user: "admin",
password: "secret"
})
.then(function(){
// Implement your code here ...
})
.catch(function(){
// Implement your code here ...
});
Example using callbacks:
vscpClient.closeSession({
onSuccess: function() {
// Implement your code here ...
},
onError: function() {
// Implement your code here ...
}
});
Example using native promises:
vscpClient.closeSession()
.then(function(){
// Implement your code here ...
})
.catch(function() {
// Implement your code here ...
});
vscpRestClient.sendEvent({
event: new vscp.Event({
vscpClass: vscp.constants.classes.VSCP_CLASS1_INFORMATION,
vscpType: vscp.constants.types.VSCP_TYPE_INFORMATION_NODE_HEARTBEAT,
vscpPriority: vscp.constants.priorities.PRIORITY_3_NORMAL,
vscpData: [ 138, 0, 255 ]
})
})
.then(function(data) {
// Implement your code here ...
})
.catch(function(data) {
// Implement your code here ...
});
Reading a single event:
vscpClient.readEvent()
.then(function(data) {
// Event received?
if (0 < data.response.count) {
var evt = data.response.event[0];
// Implement your code here ...
}
})
.catch(function(data) {
// Implement your code here ...
})
Reading 10 events:
vscpClient.readEvent({
count: 10
})
.then(function(data) {
var index = 0;
var evt = null;
// Event received?
if (0 < data.response.count) {
for(index = 0; index < data.response.count; ++index) {
evt = data.response.event[index];
// Implement your code here ...
}
}
})
.catch(function(data) {
// Implement your code here ...
})
Copyright © 2015-2021 Andreas Merkle
Copyright © 2012-2021 The VSCP Project