Extended and normalized key names in GoogleFitEvent type

This commit is contained in:
Fabio Manganiello 2019-03-16 19:28:47 +01:00
parent 87d63d7cb3
commit f201f039e9
2 changed files with 19 additions and 7 deletions

View file

@ -3,6 +3,7 @@ import time
from platypush.backend import Backend from platypush.backend import Backend
from platypush.context import get_plugin from platypush.context import get_plugin
from platypush.message.event.google.fit import GoogleFitEvent from platypush.message.event.google.fit import GoogleFitEvent
from platypush.utils import camel_case_to_snake_case
class GoogleFitBackend(Backend): class GoogleFitBackend(Backend):
@ -58,22 +59,28 @@ class GoogleFitBackend(Backend):
while not self.should_stop(): while not self.should_stop():
last_timestamp = float(get_plugin('variable'). last_timestamp = float(get_plugin('variable').
get(self._last_timestamp_varname).output. get(self._last_timestamp_varname).output.
get(self._last_timestamp_varname)) or 0 get(self._last_timestamp_varname) or 0)
for data_source in self.data_sources: for data_source in self.data_sources:
new_last_timestamp = last_timestamp new_last_timestamp = last_timestamp
for dp in get_plugin('google.fit').get_data( for dp in get_plugin('google.fit').get_data(
user_id=self.user_id, data_source_id=data_source).output: user_id=self.user_id, data_source_id=data_source).output:
dp_time = dp.get('startTime', 0) dp_time = dp.pop('startTime', 0)
if 'dataSourceId' in dp:
del dp['dataSourceId']
if dp_time > last_timestamp: if dp_time > last_timestamp:
self.bus.post(GoogleFitEvent( self.bus.post(GoogleFitEvent(
user_id=self.user_id, data_source_id=data_source, user_id=self.user_id, data_source_id=data_source,
data_type=dp.get('dataTypeName'), data_type=dp.pop('dataTypeName'),
start_time=dp.get('startTime'), start_time=dp_time,
end_time=dp.get('endTime'), end_time=dp.pop('endTime'),
modified_time=dp.get('modifiedTime'), modified_time=dp.pop('modifiedTime'),
values=dp.get('values'))) values=dp.pop('values'),
**{ camel_case_to_snake_case(k): v
for k,v in dp.items() }
))
if dp_time > new_last_timestamp: if dp_time > new_last_timestamp:
new_last_timestamp = dp_time new_last_timestamp = dp_time

View file

@ -5,6 +5,7 @@ import importlib
import inspect import inspect
import logging import logging
import os import os
import re
import signal import signal
import socket import socket
import ssl import ssl
@ -229,5 +230,9 @@ def get_mime_type(resource):
mime = magic.Magic(mime=True) mime = magic.Magic(mime=True)
return mime.from_file(resource) return mime.from_file(resource)
def camel_case_to_snake_case(string):
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', string)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et: