Skip to content

Commit

Permalink
Set time support in Application
Browse files Browse the repository at this point in the history
Adds a convenience function to set the time from a python datetime
object. The time needs to be converted to TAI format which is now 35.
This number needs to be bumbed if more leap seconds are added to UTC.
  • Loading branch information
Tigge committed Apr 6, 2015
1 parent 650696f commit 9111374
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
23 changes: 21 additions & 2 deletions ant/fs/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from __future__ import absolute_import, print_function, division

import array
import datetime
import logging
import threading

Expand All @@ -42,7 +43,7 @@
AuthenticateCommand, AuthenticateResponse, DisconnectCommand,
UploadRequest, UploadResponse, UploadDataCommand, UploadDataResponse,
EraseRequestCommand, EraseResponse)
from ant.fs.commandpipe import CreateFile, Response
from ant.fs.commandpipe import CreateFile, Response, Time, TimeResponse
from ant.fs.file import Directory
from ant.fs.commons import crc

Expand Down Expand Up @@ -82,11 +83,15 @@ def __init__(self, error, errno=None):
AntFSException.__init__(self, error, errno)


class AntFSCreateFileException():
class AntFSCreateFileException(AntFSException):
def __init__(self, error, errno=None):
AntFSException.__init__(self, error, errno)


class AntFSTimeException(AntFSException):
def __init__(self, error, errno=None):
AntFSException.__init__(self, error, errno)

class Application:
_serial_number = 1337
_frequency = 19 # 0 to 124, x - 2400 (in MHz)
Expand Down Expand Up @@ -332,6 +337,20 @@ def download_directory(self, callback=None):
data = self.download(0, callback)
return Directory.parse(data)

def set_time(self, time=datetime.datetime.utcnow()):
"""
:param time: datetime in UTC, or None to set to current time
"""
utc_tai_diff_seconds = 35
offset = time - datetime.datetime(1989, 12, 31, 0, 0, 0)
t = Time(int(offset.total_seconds()) + utc_tai_diff_seconds, 0xffffffff, 0)
self._send_commandpipe(t.get())

result = self._get_commandpipe()

if result.get_response() != TimeResponse.Response.OK:
raise AntFSTimeException("Failed to set time", result.get_response())

def erase(self, index):
self._send_command(EraseRequestCommand(index))
response = self._get_command()
Expand Down
33 changes: 14 additions & 19 deletions examples/antfs_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@

from __future__ import absolute_import, print_function

from ant.fs.manager import Application, AntFSAuthenticationException
from ant.fs.commandpipe import Time
import logging
import datetime

from ant.fs.manager import Application, AntFSAuthenticationException, AntFSTimeException

class Listener(Application):

class Listener(Application):
def __init__(self):
Application.__init__(self)

Expand All @@ -39,33 +37,30 @@ def setup_channel(self, channel):
channel.set_rf_freq(50)
channel.set_search_waveform([0x53, 0x00])
channel.set_id(0, 0x01, 0)

channel.open()

print("Lister: Searching for devices...")

def on_link(self, beacon):
print("Lister: Link", beacon.get_serial(), beacon.get_descriptor())
self.link()
return True

def on_authentication(self, beacon):
print("Lister: Auth", self.authentication_serial())
try:
self.authentication_pair("ANT-FS List")
return True
except AntFSAuthenticationException as e:
return False

def on_transport(self, beacon):
x = datetime.datetime.now() - datetime.datetime(1989, 12, 31, 0, 0, 0)
t = Time(int(x.total_seconds()), 0xffffffff, 0)

self._send_commandpipe(t.get())
result = self._get_commandpipe()

print("Time, response:", result.get_request_id(), result.get_response())

try:
self.set_time()
except AntFSTimeException:
print("Could not set time")

print("Listener: Transport")
directory = self.download_directory()
print("Directory version: ", directory.get_version())
Expand All @@ -76,7 +71,6 @@ def on_transport(self, beacon):


def main():

logging.basicConfig()

try:
Expand All @@ -89,7 +83,8 @@ def main():
finally:
print("Stop")
# a.stop()



if __name__ == "__main__":
main()

0 comments on commit 9111374

Please sign in to comment.