beaconState =
await
module.
get_beacon
()
;
Apart from a few exceptions, most
XXX_async
redundant methods have been removed as well, as
they would have introduced confusion on the proper way of handling asynchronous behaviors. It is
however very simple to get an
async
method to invoke a callback upon completion, using the
returned Promise object. For instance, you can replace:
module.
get_beacon_async
(
callback, myContext
)
;
by
module.
get_beacon
()
.
then
(function(
res
)
{
callback
(
myContext, module, res
)
;
})
;
In some cases, it might be desirable to get a sensor value using a method identical to the old
synchronous methods (without using Promises), even if it returns a slightly outdated cached value
since I/O is not possible. For this purpose, the EcmaScript library introduce new classes called
synchronous proxies
. A synchronous proxy is an object that mirrors the most recent state of the
connected class, but can be read using regular synchronous function calls. For instance, instead of
writing:
async
function
logInfo
(
module
)
{
console.
log
(
'Name: '
+
await
module.
get_logicalName
())
;
console.
log
(
'Beacon: '
+
await
module.
get_beacon
())
;
}
...
logInfo
(
myModule
)
;
...
you can use:
function
logInfoProxy
(
moduleSyncProxy
)
{
console.
log
(
'Name: '
+moduleProxy.
get_logicalName
())
;
console.
log
(
'Beacon: '
+moduleProxy.
get_beacon
())
;
}
logInfoSync
(await
myModule.
get_syncProxy
())
;
You can also rewrite this last asynchronous call as:
myModule.
get_syncProxy
()
.
then
(
logInfoProxy
)
;
7.3. Control of the Latitude function
A few lines of code are enough to use a Yocto-GPS. Here is the skeleton of a JavaScript code
snipplet to use the Latitude function.
import
{
YAPI, YErrorMsg, YLatitude
}
from
'yoctolib-es'
;
// Get access to your device, through the VirtualHub running locally
await
YAPI.
RegisterHub
(
'127.0.0.1'
)
;
var
latitude = YLatitude.
FindLatitude
(
"YGNSSMK1-123456.latitude"
)
;
// Check that the module is online to handle hot-plug
if(await
latitude.
isOnline
())
{
// Use latitude.get_currentValue()
[
...
]
}
Let us look at these lines in more details.
7. Using Yocto-GPS with JavaScript / EcmaScript
40
www.yoctopuce.com
Summary of Contents for Yocto-GPS
Page 1: ...Yocto GPS User s guide...
Page 2: ......
Page 6: ...22 Characteristics 557 Blueprint 559 Index 561...
Page 10: ...4 www yoctopuce com...
Page 14: ...8 www yoctopuce com...
Page 18: ...12 www yoctopuce com...
Page 22: ...16 www yoctopuce com...
Page 38: ...32 www yoctopuce com...
Page 42: ...36 www yoctopuce com...
Page 54: ...48 www yoctopuce com...
Page 72: ...66 www yoctopuce com...
Page 92: ...86 www yoctopuce com...
Page 122: ...116 www yoctopuce com...
Page 132: ...126 www yoctopuce com...
Page 564: ...22 Characteristics 558 www yoctopuce com...
Page 565: ......
Page 566: ......