Using the Resource data client
The ResourceDataClient
is a service that can retrieve resource data in an easy and efficient manner.
You can create a client instance using the createResourceDataClient()
method on the component context.
const client = context.createResourceDataClient();
Retrieving resources
To start retrieving resource data (for example the device name) you can use the query()
method to set up a listener. What's nice about this is that when the data is updated, the listener will be called again with the latest data.
const client = context.createResourceDataClient();
client.query([
{ selector: 'Agent', fields: ['name'] }
], result => {
console.log(result);
// result: [{ data: { name: 'CNC Machine' }, query: { fields: ['name'], selector: 'Agent' } }]
});
When a component's lifecycle ends, the component context is automatically destroyed, effectively calling destroy()
on any ResourceDataClient created via that context. This will in turn cancel all query listeners that were set up from these clients.
However, you can choose to manually destroy the client instance or cancel a specific query listener. A use case for the latter may be to stop listening for changes as soon as you receive a first result.
const client = context.createResourceDataClient();
const cancelQuery = client.query([
{ selector: 'MyUser', fields: ['name'] }
], result => {
console.log('Hello, ' + result[0].data.name + '!');
// result: "Hello, John Doe!"
// Stops the listener, because the name was needed just once.
cancelQuery();
});
The next example showcases a query which combines data from two different resources:
const client = context.createResourceDataClient();
client.query([
{ selector: 'Agent', fields: ['custom'] },
{ selector: 'CustomPropertyList', fields: ['name', 'scopeType', 'slug'] }
], results => {
const agent = results[0].data;
const customProps = results[1].data;
const agentCustomProps = customProps.map(prop => ({
name: prop.name,
value: agent.custom[prop.scopeType + prop.slug],
}));
console.log(agentCustomProps);
// result: [{ name: 'Project number', value: '100' }]
});
Resource query selectors
The ResourceDataClient
is limited in the kind of resources and fields it can retrieve. Below you can find the currently supported selectors and their allowed fields.
Selector | Allowed fields | Description | API endpoint used |
---|---|---|---|
Agent | activeVpnSession.rscServer.name, activeVpnSession.rscServer.publicId, activeVpnSession.vpnAddress, connectedUsers.name, connectedUsers.publicId, custom, dataSources.name, dataSources.publicId, dataSources.slug, description, lastSeenAgentUserAgent.firmwareVersion, lastSeenAgentUserAgent.hardware, lastSeenAgentUserAgent.hardwareVersion, lastSeenAgentUserAgent.publicId, location.name, loggingMdrServer.name, loggingMdrServer.publicId, loggingMqttChangedOn, macAddress, mdrServer.name, mdrServer.publicId, memberships.group.publicId, memberships.publicId, mqttChangedOn, name, permissions, publicId, rateLimiting.configMqttCommandCounter, rateLimiting.configMqttCommandLastHit, rateLimiting.configMqttCommandLimit, rateLimiting.configMqttDataCounter, rateLimiting.configMqttDataLastHit, rateLimiting.configMqttDataLimit, rateLimiting.configOpenvpnCounter, rateLimiting.configOpenvpnLastHit, rateLimiting.configOpenvpnLimit, serialNumber, servers.custom, servers.name, servers.publicId, servers.serviceGroup, servers.type, type.name, type.publicId | The current Agent (i.e. the Agent for the Page that is currently open, or the Agent for the Card that the component is placed on). | Agent |
Asset | custom, description, memberships.group.publicId, memberships.publicId, name, permissions, publicId, resourceVariant.publicId | The current Asset (i.e. the Asset for the Page that is currently open, or the Asset for the Card that the component is placed on). | Asset |
AssetAppConfig | publicId, values, stateValues | The current AssetAppConfig. Can be used to store app configuration or state for an Asset | AssetAppConfig |
AssetAppConfigList | publicId, values, stateValues | Lists the current AssetAppConfig and all AssetAppConfigs descendant to the current asset. | AssetAppConfig |
Branding | logo, publicId, theme.logoHasBackgroundColor | The currently active branding. | CompanyBranding |
CustomPropertyList | flags, name, publicId, resourceType, resourceVariant, scopeType, slug | All Custom Properties for the current Company. | CustomPropertyList |
GroupList | name, publicId, type.name, type.order, type.publicId | All Groups in the current Company. | GroupList |
MyCompany | name, permissions, publicId | The currently active Company. | MyCompany |
MyUser | custom, name, publicId, support, unreadMessagesCount | The currently logged in User. | MyUser |
PagePage | order, publicId | The individual page within a document-style page (only applicable to reports). | PagePage |
PageVersion | pagePages.order, pagePages.publicId, publicId, title | The current Page (i.e. the Page that is currently open, or the Card that the component is placed on). | PageVersion |
UserList | name, publicId | All users of the current Company | UserList |
Please reach out to us by sending an e-mail to [email protected], if you are missing items on this list and you would like to use the resource data client to retrieve data for other resource types or allowed fields.
Updating resources
The ResourceDataClient
can also be used to update resources. To update a resource you can use the update()
method to create a Promise that completes when the resource has been updated.
const client = context.createResourceDataClient();
client.update({
selector: 'AssetAppConfig',
data: {
stateValues: { item: 5 },
}
})
Resource update selectors
The ResourceDataClient
is limited in the kind of resources it can update. Below you can find the currently supported selectors and their allowed fields.
Selector | Allowed fields | Description | API endpoint used |
---|---|---|---|
AssetAppConfig | publicId, values, stateValues | The current AssetAppConfig. Can be used to store app configuration or state for an Asset | AssetAppConfig |
Please reach out to us by sending an e-mail to [email protected], if you are missing items on this list and you would like to use the resource data client to update data for other resource types.
Template rendering
The ResourceDataClient
has built-in support for template rendering. Components can make use of Mustache templating via the render
method. The renderer will resolve fields from the given template and re-render the template when a resource is updated. Fields can be referenced via [selector].[field]
. Below is an example of a component that references the name
field of the Agent
resource.
const client = context.createResourceDataClient();
client.render([
{ template: 'Device name: {{agent.name}}' },
], ([output]) => {
console.log(output);
// result: 'Device name: CNC Machine'
});
Updated 10 months ago