Skip to content

Commit c476ef9

Browse files
committed
added method to sync time, updated docstrings
1 parent 887eab7 commit c476ef9

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

servercom/implementations/circuitpy.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""CircuitPython implementation of the CubeServer API Wrapper Library"""
22

33
from .common import *
4+
from ..timetools import Time
45

56
import ssl
67
import wifi
@@ -379,6 +380,9 @@ def request(
379380
raise last_error
380381

381382
def get_status(self) -> GameStatus:
383+
"""Gives the team access to their score and the current unix time.
384+
Most teams probably won't need this.
385+
"""
382386
if self.v:
383387
print("Getting status...")
384388
resp = self.request('GET', '/status',
@@ -389,7 +393,16 @@ def get_status(self) -> GameStatus:
389393
print(f"It is {resp_json['unix_time']} seconds since the epoch.")
390394
return GameStatus(Time(resp_json['unix_time']), resp_json['status']['score'])
391395

396+
def sync_time(self) -> bool:
397+
"""Syncs the current clock against the server"""
398+
status = self.get_status()
399+
if status:
400+
Time.set_time(status.time)
401+
return True
402+
return False
403+
392404
def post(self, point: DataPoint) -> bool:
405+
"""Posts a DataPoint object to the server"""
393406
if self.v:
394407
print("Posting datapoint!")
395408
return self.request(
@@ -401,6 +414,7 @@ def post(self, point: DataPoint) -> bool:
401414
).code == 201
402415

403416
def email(self, msg: Email) -> bool:
417+
"""Sends an email to the team"""
404418
if self.v:
405419
print(f"Sending email {msg.subject}...")
406420
return self.request(

servercom/implementations/cpy.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,27 +339,44 @@ def request(
339339
raise last_error
340340

341341
def get_status(self) -> GameStatus:
342+
"""Gives the team access to their score and the current unix time.
343+
Most teams probably won't need this.
344+
"""
342345
resp = self.request('GET', '/status')
343346
resp_json = loads(resp[1])
344347
return GameStatus(Time(resp_json['unix_time']), resp_json['status']['score'])
348+
349+
def sync_time(self) -> bool:
350+
"""Syncs the current clock against the server"""
351+
status = self.get_status()
352+
if status:
353+
Time.set_time(status.time)
354+
return True
355+
return False
345356

346357
def post(self, point: DataPoint) -> bool:
358+
"""Posts a DataPoint object to the server"""
347359
return self.request(
348360
'POST',
349361
'/data',
350362
point.dumps(),
351363
content_type = 'application/json',
352-
headers=['User-Agent: CPython, dude!']
364+
headers=['User-Agent: CPythonDude']
353365
).code == 201
354366
def email(self, msg: Email) -> bool:
367+
"""Sends an email to the team"""
355368
return self.request(
356369
'POST',
357370
'/email',
358371
msg.dumps(),
359372
content_type = 'application/json',
360-
headers=['User-Agent: CPython, dude!']
373+
headers=['User-Agent: CPythonDude']
361374
).code == 201
362375
def code_update(reset=True):
376+
"""THIS FEATURE IS NOT IMPLEMENTED FOR CPython.
377+
Checks for a code update from the server.
378+
Restarts the microcontroller and run the new code if it is available
379+
"""
363380
pass
364381
def __exit__(self):
365382
if self.v:

servercom/timetools.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ class Time(Immutable):
2626

2727
# Class Methods:
2828
@classmethod
29-
def set_time(cls, unix_timestamp: int) -> None:
29+
def set_unix_time(cls, unix_timestamp: int) -> None:
3030
cls._timeset = (int(monotonic()), int(unix_timestamp))
3131
cls._time_offset = int(unix_timestamp - monotonic())
3232

33+
@classmethod
34+
def set_time(cls, current_time: 'Time') -> None:
35+
cls.set_unix_time(current_time.seconds)
36+
3337
@classmethod
3438
def from_unix_time(cls, unix_time: int) -> int:
3539
"""Returns a monotonic timestamp from a unix timestamp"""

0 commit comments

Comments
 (0)