Print out the platform names of all claimed devices for a user.
Print out a message when a button is pushed on a device.
Detect nearby devices and print out their name, if set, and platform.
-
device
: the representation of a wireless device and its variables.
-
variables
: devices have a set of variables that are either of three types, d
, s
, and meta
. d
variables can be set either by the device or via the API, and are pushed to the device. s
variables can be set either by the device or via the API, but are not pushed to the device. meta
variables can not be set by neither the device nor the via the API, and contain meta information about the device. Each variable has a value and a timestamp. The values and timestamps of variables are directly accessible via the device
object. For example, the value of the s
variable called name
is accessed via device.s.name.value
and its timestamp as device.s.name.time
. Variables are set with the thsq.setVariable()
method.
-
unique
: each device is identified by a unique identity, contained in the meta.unique
device variable. The thsq
module uses this unique number when referring to a device.
-
update
: when the thsq
module sees that a device has been updated, it will post the new variables in an update
object, which contains all the new values of the variables.
-
user
: a user account and its associated data. A user account has a login name, a set of devices, and a data structure that can be used to store user-specific application data.
-
token
: an API token. API tokens give access for a specific user and can be used as an alternative to login/password pairs for running server-side applications.
-
Nearby device: Thingsquare devices with BLE transmitters send out a short-ranged BLE beacon that is picked up by nearby smartphones and laptops. This beacon is encrypted and changes over time. When a beacon is picked up by a smartphone or laptop, the Thingsquare system knows that the device and the smartphone or laptop are in close proximity of each other. This is typically used during installation phases, when devices are deployed.
-
Network device: when a Thingsquare device and a smartphone or laptop are on the same physical network (WiFi, Ethernet), the smartphone or laptop sends out an encrypted message over the network that the devices pick up. The Thingsquare system can then determine that the device and the smartphone or laptop are on the same network. This is used to prove proximity in a similar way as with BLE beacons, but can also be used for devices that does not have BLE beacon capabilities.
-
thsq.login(username, password, callback)
- login with the username provided by the username
argument and the password provided by the password
argument. The callback
will be called with a string that indicates the result:
login-ok
: the user was successfully logged in.login-fail
: the user could not be logged in.
-
thsq.logout(callback)
- log out the currently logged in user. The callback function callback
will be called when the user has been logged out.
-
thsq.userSignup(username, password, callback)
- sign up a new user with the username username
and password password
. The callback function callback
is called with a string that indicates the result of the operation:
signup-ok
: a new user account was successfully created.signup-fail-already-exists
: a user account with the same name already exists.signup-fail-no-email
: the username was not an email address.
-
thsq.userResendConfirmationEmail(callback)
- request a new user account confirmation email to be sent. The calback function callback
will be called with a string that indicates the result of the operation:
resend-ok
: a user confirmation email was successfully sent.resend-fail
: the user confirmation email could not be sent.
-
thsq.userSendPasswordRecoveryEmail(username, callback)
- request a password recover email to be send to the user with the user name username
. The password recover email will contain a password token that later can be used as a parameter to the recoverNewPassword()
function. The callback function callback
will be called with a string that indicates the result of the operation:
recover-ok
: a password reset email was successfully sentrecover-fail
: a password reset email could not be sent.
-
thsq.recoverNewPassword(username, passwordtoken, newpassword, callback)
- set a new password for the user account. The username
is the username of the user, and must match the username that previously requested the password reset, the passwordtoken
is a password token that was previously generated as a result of a call to userSendPasswordRecoveryEmail()
, newpassword
is the new password, and callback
is a callback function that gets called with a string that indicates the result of the operation:
recover-ok
: the new password was successfully set.recover-fail
: the new password was not set.
-
thsq.getUser(callback)
- get the user data and application data associated with the user account. The callback function callback
will be called with an object that represents the user information. The user object has the following fields:
login
: the user login name.data
: the application user data that was previously stored with storeUserData()
.
-
thsq.storeUserData(data, callback)
- store new user data for the user account. The argument data
is an object that holds the data to be stored and the callback function callback
will be called once the data has been stored, with a string that indicates the result of the operation:
user-data-ok
: user data was successfully stored.user-data-fail
: user data could not be stored.user-fail
: no user account was logged in.
-
thsq.createAccessToken(callback)
- create a new user API access token for the logged in user. The function callback
will be called with a string that contains the new access token, or an error message that indicates the result of the operation:
user-token-fail
: user token could not be created.
-
thsq.deleteAccessToken(token, callback)
- delete a user API access token. The token
argument should be a token that was previously created with createAccessToken()
. The callback function callback
will be called with a string that indicates the result of the operation:
user-token-deleted
: the user API access token was successfully deleted.user-token-fail
: the user API access token could not be deleted.
-
thsq.claimDevice(unique, callback)
- claim a device for the logged in user account. This makes the device available for this user only. Any updated for the device will be received as device-updated
events. The unique
argument is the unique device identifier and callback
is a callback function that is called to indicate the result of the operation:
- device ID: the device was successfully claimed
claim-fail
: the device could not be claimedclaim-fail-no-auth
: bad user account
-
thsq.removeDevice(unique, callback)
- remove the device from the user account. The user will no longer have access to its data. The unique
argument is the device's unique identifier and callback
is a callback function that gets called with a string that indicates the result of the operation:
delete-ok
: the device was successfully removeddelete-fail
: the device could not be removed
-
thsq.shareDevice(unique, username, callback)
- share the device with another user. This makes the device also be available for the other user. The unique
argument is the device's unique identifier, the username
argument is the username of the user with which the device should be shared, and callback
is a callback function that is called with a string that indicates the result of the operation:
add-user-ok
: the new user was successfully addedclaim-fail
: the new user could not be added
-
thsq.getVariable(unique, type, variable, callback)
- get the current value of a specified variable. The unique
argument is the unique identifier for the device, the type
can be either d
, s
, or meta
, and variable
is the name of the variable. The callback
function will be called with an object that contains the value
and time
for the variable, or undefined
if the variable does not exist.
-
thsq.getVariableValue(unique, type, variable, fallback, callback)
- get the current value of a specified variable, and with a fallback if the value is not defined. The unique
argument is the unique identifier for the device, the type
can be either d
, s
, or meta
, and variable
is the name of the variable. The callback
function will be called with two arguments: the first is the value of the variable, or fallback
if the value is not defined, and the second is an object that contains the value
and time
for the variable, or undefined
if the variable does not exist.
-
thsq.getVariableStringValue(unique, type, variable, fallback, callback)
- like thsq.getVariableValue()
, except the value will be provided as a string.
-
thsq.getVariableNumberValue(unique, type, variable, fallback, callback)
- like thsq.getVariableValue()
, except the value will be provided as a number. If the value was NaN
, the fallback
value is provided instead.
-
thsq.getVariableBufferValue(unique, type, variable, fallback, callback)
- like thsq.getVariableValue()
, except the value will be provided as a Buffer
object. (Node.js version only.)
-
thsq.setVariable(unique, type, variable, value, [options,] callback)
- set a device variable. The unique
argument is the unique identifier for the device, the type
can be either d
or s
, variable
is the name of the variable, and value
is the value of the variable. The optional options
argument can take one { timestamp: timestamp }
value, which sets a specific timestamp, in seconds since the Unix epoch, for the variable. The callback
function will be called with an object that indicates the result of the operation:
device-ok
: the variable could be setdevice-fail
: the variable could not be set
-
thsq.deleteVariable(unique, type, variable, callback)
- delete a device variable. The unique
argument is the unique identifier for the device, the type
can be either d
or s
, and variable
is the name of the variable. The callback
function will be called with an object that indicates the result of the operation:
delete-ok
: the variable could be deleteddelete-fail
: the variable could not be deleted
-
thsq.getVariableHistory(unique, type, variable, [options,] callback)
- get the variable history for a specific variable. The unique
argument is the unique identifier for the device, the type
can be either d
, s
, or meta
, and variable
is the name of the variable. The optional options
argument is used to delimit the number of history items to be returned, per below. The callback
function will be called with an array of items from the data history, with each item as per below.
The options
argument determines what history elements are returned. They are either returned starting from a given timestamp, or starting from a given logical history element id. If no timestamp or element id is provided, the most recent history is returned.
startid
: request history elements from given idstarttime
: request history elements from given timestampnum
: the number of elements to request in total (default: 1000
)chunksize
: the maximum number of elements to retrieve per request (default: 1000
)progress(num)
: a callback function that gets called on each request, with the num
argument being the number of history elements read so far (default: none).
The callback will receive an array with objects with the following properties:
value
: the value of the data object. For binary data, this is a Javascript object with the field type
set to the string Buffer
. In this case, the data
field will be an array of integers that represent the binary data value.time
: the timestamp of the value
-
thsq.addHistoryListener(unique, type, name, options, listener)
- adds a history listener for the given device and variable. This function returns a unique history listener id that can later be used to remove the history listener via thsq.clearHistoryListener()
.
The options
parameter is the same as for thsq.getVariableHistory()
.
The listener will receive the following parameters:
device
: the device objectunique
: the device uniquetype
: the variable type e.g. s
variable
: the variable name e.g. button
data
: the variable value object containing time and variable value
-
thsq.clearHistoryListener(id)
- clears a previously registered history listener.
-
thsq.clearAllHistoryListeners()
- clears all previously registered history listeners.
-
thsq.sendCommand(unique, command, [options,] callback)
- send a command to the device identified by unique
. The command
is a string that will be sent to the device. Commands are sent in a best-effort fashion and there is no guarantee that it will be received by the device. The callback function callback
will be called with a string that indicates the result of the API call, but does not indicate anything regarding the command propagation itself.
Possible results for the callback
function are:
device-ok
: command was sent towards the devicedevice-fail
: the command could not be sent
The possible options
are:
sendcallback
: a function that gets called when the command has been sent towards the devicesendtimeout
: the number of milliseconds to wait before giving up on the send callback (default 60000 ms)sendtimeoutcallback
: a function that gets called when the sendtimeout
occursackedcallback
: a function that gets called when the command has been acked by the deviceackedtimeout
: the number of milliseconds to wait after the command has been sent before giving up on the acked callback (default 10000 ms)ackedtimeoutcallback
: a function that gets called when the ackedtimeout
occurspriority
: the priority of the command (0
: default urgency, 1
: urgent, 2
: most urgent)lifetime
: the time, in milliseconds, that the command should be on the command queue before being removed
-
thsq.getDevice(unique, callback)
- get the device object for the unique identifier unique
. The callback function callback
will be called with the device object, or undefined
if the device does not exist.
-
thsq.getDevicelist(callback)
- get all devices claimed by the user. The callback function callback
will receive a Javascript object that is indexed by defice ID and where each item represents a device.
-
thsq.deviceStateName(state)
- returns a human-readable string representation of a nearby device's state, as given by the state
argument that has previously been received received via the device-nearby-seen
, device-nearby-first-seen
, and device-unknown-nearby-seen
events.
-
thsq.deviceEUI(device)
- returns the EUI for the device in the device
argument.
-
thsq.deviceId(unique, callback)
- receives the device ID for the device with unique provided in the unique
argument as a callback to the callback(deviceiq)
function.
-
thsq.devicePlatform(device)
- returns the platform for the device in the device
argument.
-
thsq.deviceunique(device)
- get the device unique of a device.
-
thsq.isacked(variable)
- returns true if a variable is acked by the device.
-
thsq.valuestring(value)
- convert a variable value to string value.
-
thsq.valuebuffer(val)
- convert a variable value to a Buffer value.
-
thsq.valuecbor(val)
- parse a CBOR Buffer value to a Javascript object.
-
thsq.devicename(device)
- get the device name of a device.
-
thsq.deviceidstring(devicedata, str)
- get the substring str
from the s.id
variable of a device.
-
thsq.devicefwver(devicedata)
- get the device firmware timestamp version of a device.
-
thsq.devicefwid(devicedata)
- get the device SDK version ID of a device.
-
thsq.devicefreq(devicedata)
- get the device frequency mode (fcc
or etsi
) of a device.
-
thsq.devicepower(devicedata)
- get the device power mode (low
or high
) of a device.
-
thsq.variablevalue(device, type, name, fallback)
- get the variable value for a device variable with type type
and name name
. If the variable does not exist, return fallback
.
-
thsq.variablevaluestring(device, type, name, fallback)
: - get the variable value as a string for a device variable with type type
and name name
. If the variable does not exist, return fallback
.
-
thsq.variablevaluebuffer(device, type, name, fallback)
: - get the variable value as a Buffer for a device variable with type type
and name name
. If the variable does not exist, return fallback
.
-
thsq.variablevaluecbor(device, type, name, fallback)
: - get the variable value as a Javascript object decoded from the CBOR (https://datatracker.ietf.org/doc/html/rfc8949) value in the device variable with type type
and name name
. If the variable is undefined, return fallback
instead.
-
thsq.variablevaluejson(device, type, name, fallback)
: - get the variable value as a Javascript object decoded from the JSON value in the device variable with type type
and name name
. If the variable is undefined, or if the variable contains unparseable JSON, return fallback
instead.
-
thsq.variableage(device, type, name, fallback)
- get the variable age for a device variable with type type
and name name
. If the variable does not exist, return fallback
.
-
thsq.variabletime(device, type, name, fallback)
- get the variable time for a device variable with type type
and name name
. If the variable does not exist, return fallback
.
-
thsq.ackedvariablevalue(device, type, name, fallback)
- get the acknowledged variable (most recently known value) value for a device variable with type type
and name name
. If the variable does not exist, return fallback
.
-
thsq.ackedvariablevaluebuffer(device, type, name, fallback)
: - get the acknowledged variable (most recently known value) value as a Buffer for a device variable with type type
and name name
. If the variable does not exist, return fallback
.
-
thsq.ackedvariablevaluestring(device, type, name, fallback)
: - get the acknowledged variable (most recently known value) value as a string for a device variable with type type
and name name
. If the variable does not exist, return fallback
.
-
thsq.variablevaluestringnum(device, type, name, num, fallback)
: - get the num
th value of a comma separated variable, as a string.
-
thsq.variablevaluenumbernum(device, type, name, num, fallback)
- get the num
th value of a comma separated variable, as a Number.
-
thsq.currentmode(device)
: - get the current device mode (master
, feather
, or deadleaf
) for a device. Returns an array with [0] being the mode and [1] being the wake-up rate, if the mode is deadleaf
.
-
thsq.reachable(device)
- get the number of milliseconds until the device will be reachable, in case of a deadleaf
device.
Each device may have a position. A position is a { lat: lat, lng: lng }
object that is set with the thsq.setdeviceposition()
method. The position is retreieved with the thsq.deviceposition()
method. If the device has no position, the method returns undefined
.