1366 advType = ADV_SCAN_IND |
1366 advType = ADV_SCAN_IND |
1367 scanResults[address].update(advType, res["rssi"], res["advertisement"]) |
1367 scanResults[address].update(advType, res["rssi"], res["advertisement"]) |
1368 |
1368 |
1369 return scanResults, "" |
1369 return scanResults, "" |
1370 |
1370 |
|
1371 ################################################################## |
|
1372 ## Methods below implement NTP related methods |
|
1373 ################################################################## |
|
1374 |
|
1375 def hasNetworkTime(self): |
|
1376 """ |
|
1377 Public method to check the availability of network time functions. |
|
1378 |
|
1379 @return flag indicating the availability of network time functions |
|
1380 @rtype bool |
|
1381 """ |
|
1382 command = """ |
|
1383 def has_ntp(): |
|
1384 try: |
|
1385 import adafruit_ntp |
|
1386 if hasattr(adafruit_ntp, 'NTP'): |
|
1387 return True |
|
1388 except ImportError: |
|
1389 pass |
|
1390 |
|
1391 return False |
|
1392 |
|
1393 print(has_ntp()) |
|
1394 del has_ntp |
|
1395 """ |
|
1396 out, err = self._interface.execute(command, mode=self._submitMode) |
|
1397 if err: |
|
1398 raise OSError(self._shortError(err)) |
|
1399 return out.strip() == b"True" |
|
1400 |
|
1401 def setNetworkTime(self, server="0.pool.ntp.org", tzOffset=0, timeout=10): |
|
1402 """ |
|
1403 Public method to set the time to the network time retrieved from an |
|
1404 NTP server. |
|
1405 |
|
1406 @param server name of the NTP server to get the network time from |
|
1407 (defaults to "0.pool.ntp.org") |
|
1408 @type str (optional) |
|
1409 @param tzOffset offset with respect to UTC (defaults to 0) |
|
1410 @type int (optional) |
|
1411 @param timeout maximum time to wait for a server response in seconds |
|
1412 (defaults to 10) |
|
1413 @type int |
|
1414 @return tuple containing a flag indicating success and an error string |
|
1415 @rtype tuple of (bool, str) |
|
1416 """ |
|
1417 command = """ |
|
1418 def set_ntp_time(server, tz_offset, timeout): |
|
1419 import rtc |
|
1420 import socketpool |
|
1421 import wifi |
|
1422 |
|
1423 import adafruit_ntp |
|
1424 |
|
1425 |
|
1426 r = wifi.radio |
|
1427 if r.ipv4_address is None: |
|
1428 return False |
|
1429 |
|
1430 pool = socketpool.SocketPool(r) |
|
1431 ntp = adafruit_ntp.NTP( |
|
1432 pool, server=server, tz_offset=tz_offset, socket_timeout=timeout |
|
1433 ) |
|
1434 rtc.RTC().datetime = ntp.datetime |
|
1435 return True |
|
1436 |
|
1437 try: |
|
1438 print({{ |
|
1439 'result': set_ntp_time({0}, {1}, {2}), |
|
1440 'error': '', |
|
1441 }}) |
|
1442 except Exception as err: |
|
1443 print({{ |
|
1444 'result': False, |
|
1445 'error': str(err), |
|
1446 }}) |
|
1447 del set_ntp_time |
|
1448 """.format( |
|
1449 repr(server), tzOffset, timeout |
|
1450 ) |
|
1451 out, err = self._interface.execute( |
|
1452 command, mode=self._submitMode, timeout=(timeout + 2) * 1000 |
|
1453 ) |
|
1454 if err: |
|
1455 return False, err |
|
1456 else: |
|
1457 res = ast.literal_eval(out.decode("utf-8")) |
|
1458 return res["result"], res["error"] |
|
1459 |
1371 |
1460 |
1372 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber): |
1461 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber): |
1373 """ |
1462 """ |
1374 Function to instantiate a MicroPython device object. |
1463 Function to instantiate a MicroPython device object. |
1375 |
1464 |