1350 res["adv_type"], res["rssi"], res["advertisement"] |
1350 res["adv_type"], res["rssi"], res["advertisement"] |
1351 ) |
1351 ) |
1352 |
1352 |
1353 return scanResults, "" |
1353 return scanResults, "" |
1354 |
1354 |
|
1355 ################################################################## |
|
1356 ## Methods below implement NTP related methods |
|
1357 ################################################################## |
|
1358 |
|
1359 def hasNetworkTime(self): |
|
1360 """ |
|
1361 Public method to check the availability of network time functions. |
|
1362 |
|
1363 @return flag indicating the availability of network time functions |
|
1364 @rtype bool |
|
1365 """ |
|
1366 if self.hasCircuitPython(): |
|
1367 self.__createCpyDevice() |
|
1368 return self.__cpyDevice.hasNetworkTime() |
|
1369 |
|
1370 command = """ |
|
1371 def has_ntp(): |
|
1372 try: |
|
1373 import ntptime |
|
1374 return True |
|
1375 except ImportError: |
|
1376 return False |
|
1377 |
|
1378 print(has_ntp()) |
|
1379 del has_ntp |
|
1380 """ |
|
1381 out, err = self._interface.execute(command, mode=self._submitMode) |
|
1382 if err: |
|
1383 raise OSError(self._shortError(err)) |
|
1384 return out.strip() == b"True" |
|
1385 |
|
1386 def setNetworkTime(self, server="0.pool.ntp.org", tzOffset=0, timeout=10): |
|
1387 """ |
|
1388 Public method to set the time to the network time retrieved from an |
|
1389 NTP server. |
|
1390 |
|
1391 @param server name of the NTP server to get the network time from |
|
1392 (defaults to "0.pool.ntp.org") |
|
1393 @type str (optional) |
|
1394 @param tzOffset offset with respect to UTC (defaults to 0) |
|
1395 @type int (optional) |
|
1396 @param timeout maximum time to wait for a server response in seconds |
|
1397 (defaults to 10) |
|
1398 @type int |
|
1399 @return tuple containing a flag indicating success and an error string |
|
1400 @rtype tuple of (bool, str) |
|
1401 """ |
|
1402 if self.hasCircuitPython(): |
|
1403 return self.__cpyDevice.setNetworkTime( |
|
1404 server=server, tzOffset=tzOffset, timeout=timeout |
|
1405 ) |
|
1406 |
|
1407 command = """ |
|
1408 def set_ntp_time(server, tz_offset, timeout): |
|
1409 import network |
|
1410 import ntptime |
|
1411 import machine |
|
1412 |
|
1413 if not network.WLAN(network.STA_IF).isconnected(): |
|
1414 return False |
|
1415 |
|
1416 ntptime.host = server |
|
1417 ntptime.timeout = timeout |
|
1418 ntptime.settime() |
|
1419 |
|
1420 rtc = machine.RTC() |
|
1421 t = list(rtc.datetime()) |
|
1422 t[4] += tz_offset |
|
1423 rtc.datetime(t) |
|
1424 |
|
1425 return True |
|
1426 |
|
1427 try: |
|
1428 print({{ |
|
1429 'result': set_ntp_time({0}, {1}, {2}), |
|
1430 'error': '', |
|
1431 }}) |
|
1432 except Exception as err: |
|
1433 print({{ |
|
1434 'result': False, |
|
1435 'error': str(err), |
|
1436 }}) |
|
1437 del set_ntp_time |
|
1438 """.format( |
|
1439 repr(server), tzOffset, timeout |
|
1440 ) |
|
1441 out, err = self._interface.execute( |
|
1442 command, mode=self._submitMode, timeout=(timeout + 2) * 1000 |
|
1443 ) |
|
1444 if err: |
|
1445 return False, err |
|
1446 else: |
|
1447 res = ast.literal_eval(out.decode("utf-8")) |
|
1448 return res["result"], res["error"] |
|
1449 |
1355 |
1450 |
1356 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber): |
1451 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber): |
1357 """ |
1452 """ |
1358 Function to instantiate a MicroPython device object. |
1453 Function to instantiate a MicroPython device object. |
1359 |
1454 |