1172 return [], err |
1172 return [], err |
1173 |
1173 |
1174 clientsList = ast.literal_eval(out.decode("utf-8")) |
1174 clientsList = ast.literal_eval(out.decode("utf-8")) |
1175 return clientsList, "" |
1175 return clientsList, "" |
1176 |
1176 |
|
1177 ################################################################## |
|
1178 ## Methods below implement NTP related methods |
|
1179 ################################################################## |
|
1180 |
|
1181 def hasNetworkTime(self): |
|
1182 """ |
|
1183 Public method to check the availability of network time functions. |
|
1184 |
|
1185 @return flag indicating the availability of network time functions |
|
1186 @rtype bool |
|
1187 """ |
|
1188 command = """ |
|
1189 def has_ntp(): |
|
1190 try: |
|
1191 import ntptime |
|
1192 return True |
|
1193 except ImportError: |
|
1194 return False |
|
1195 |
|
1196 print(has_ntp()) |
|
1197 del has_ntp |
|
1198 """ |
|
1199 out, err = self._interface.execute(command, mode=self._submitMode) |
|
1200 if err: |
|
1201 raise OSError(self._shortError(err)) |
|
1202 return out.strip() == b"True" |
|
1203 |
|
1204 def setNetworkTime(self, server="0.pool.ntp.org", tzOffset=0, timeout=10): |
|
1205 """ |
|
1206 Public method to set the time to the network time retrieved from an |
|
1207 NTP server. |
|
1208 |
|
1209 @param server name of the NTP server to get the network time from |
|
1210 (defaults to "0.pool.ntp.org") |
|
1211 @type str (optional) |
|
1212 @param tzOffset offset with respect to UTC (defaults to 0) |
|
1213 @type int (optional) |
|
1214 @param timeout maximum time to wait for a server response in seconds |
|
1215 (defaults to 10) |
|
1216 @type int |
|
1217 @return tuple containing a flag indicating success and an error string |
|
1218 @rtype tuple of (bool, str) |
|
1219 """ |
|
1220 command = """ |
|
1221 def set_ntp_time(server, tz_offset, timeout): |
|
1222 import network |
|
1223 import ntptime |
|
1224 import machine |
|
1225 |
|
1226 if not network.WLAN(network.STA_IF).isconnected(): |
|
1227 return False |
|
1228 |
|
1229 ntptime.host = server |
|
1230 ntptime.timeout = timeout |
|
1231 ntptime.settime() |
|
1232 |
|
1233 rtc = machine.RTC() |
|
1234 t = list(rtc.datetime()) |
|
1235 t[4] += tz_offset |
|
1236 rtc.datetime(t) |
|
1237 |
|
1238 return True |
|
1239 |
|
1240 try: |
|
1241 print({{ |
|
1242 'result': set_ntp_time({0}, {1}, {2}), |
|
1243 'error': '', |
|
1244 }}) |
|
1245 except Exception as err: |
|
1246 print({{ |
|
1247 'result': False, |
|
1248 'error': str(err), |
|
1249 }}) |
|
1250 del set_ntp_time |
|
1251 """.format( |
|
1252 repr(server), tzOffset, timeout |
|
1253 ) |
|
1254 out, err = self._interface.execute( |
|
1255 command, mode=self._submitMode, timeout=(timeout + 2) * 1000 |
|
1256 ) |
|
1257 if err: |
|
1258 return False, err |
|
1259 else: |
|
1260 res = ast.literal_eval(out.decode("utf-8")) |
|
1261 return res["result"], res["error"] |
|
1262 |
1177 ############################################################################ |
1263 ############################################################################ |
1178 ## RP2 only methods below |
1264 ## RP2 only methods below |
1179 ############################################################################ |
1265 ############################################################################ |
1180 |
1266 |
1181 @pyqtSlot() |
1267 @pyqtSlot() |