本文整理汇总了Python中heartbeat.Heartbeat.set_token方法的典型用法代码示例。如果您正苦于以下问题:Python Heartbeat.set_token方法的具体用法?Python Heartbeat.set_token怎么用?Python Heartbeat.set_token使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类heartbeat.Heartbeat
的用法示例。
在下文中一共展示了Heartbeat.set_token方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from heartbeat import Heartbeat [as 别名]
# 或者: from heartbeat.Heartbeat import set_token [as 别名]
class Sensor:
"""
Defines sensor logic.
It reads the distance of object continuously and calls services on the server on different events to notify server.
Events are :
* OBJECT_WITHIN_THRESHOLD1 : Notify server about an object which just showed up in the not-very-close distance.
* OBJECT_WITHIN_THRESHOLD2 : Notify server about an object which just showed up in the close distance.
It is smart enough to not to send same event over and over again. Also, if there is a very close event for a very
long time, it doesn't broadcast the event to the server.
"""
def __init__(self):
if MOCK_HARDWARE:
from hardware_mock import HardwareMock
self._hardware = HardwareMock()
else:
from hardware import Hardware
self._hardware = Hardware()
if MOCK_HEARTBEAT:
self._heartbeat = HeartbeatMock()
else:
self._heartbeat = Heartbeat()
if MOCK_DATA:
self._sensor_service = SensorServiceMock()
self._registration_service = RegistrationServiceMock()
else:
self._sensor_service = SensorService()
self._registration_service = RegistrationService()
@staticmethod
def _current_time_in_millis():
# see http://stackoverflow.com/questions/5998245/get-current-time-in-milliseconds-in-python
# about getting the current time in millis
return int(round(time.time() * 1000))
def _start(self):
log.info("Starting program...")
# first of all, check all the required settings
# noinspection PyBroadException
try:
self._sensor_info = self._registration_service.get_sensor_info_sync()
except:
log.exception("Unable to register! Exiting program")
return
if not self._sensor_info:
log.critical("Unable to get sensor info. Exiting program!")
# give the token to services
self._sensor_service.set_token(self._sensor_info.token)
self._heartbeat.set_token(self._sensor_info.token)
# since server settings are all good, send a heartbeat about starting the sensor program
self._heartbeat.sendSync(Constants.HEARTBEAT_STARTING)
# initialize hardware.
# noinspection PyBroadException
try:
log.info("Initializing hardware GPIO...")
self._hardware.initialize_gpio()
except:
# do not care about clean up, since hardware does it itself
log.exception("Unable to initialize hardware GPIO. Exiting program!")
# send heartbeat die with message
self._heartbeat.sendSync(Constants.HEARTBEAT_DIE, "Unable to initialize GPIO.", sys.exc_info())
return
# send the sync heartbeat afterwards to not to mix GPIO initialization exceptions with heartbeat exceptions
self._heartbeat.sendSync(Constants.HEARTBEAT_GPIO_INITIALIZED)
# start heartbeat here
self._heartbeat.start_heartbeat_thread()
previous_broadcasted_event_type = None
previous_broadcasted_event_time = 0
# start measuring
while True:
event_type = None
try:
distance = self._hardware.measure()
except:
# do not care about clean up, since hardware itself does it
# update heartbeat status so that server knows there is something wrong with the measurement
self._heartbeat.sendSync(Constants.HEARTBEAT_DIE, "Error during measure.", sys.exc_info())
# since long time if that is the case
log.exception("Error during measurement!")
# re-raise and eventually exit the program
raise
if distance < self._sensor_info.threshold1:
if distance >= self._sensor_info.threshold2:
# we have event type 1
event_type = Constants.EVENT_TYPE_OBJECT_WITHIN_THRESHOLD1
#.........这里部分代码省略.........