Skip to content

Commit

Permalink
Add status property.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrstegeman committed Apr 24, 2020
1 parent 7135678 commit 3d1ee98
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 21 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@
}
},
"short_name": "Tide",
"version": "0.4.0"
"version": "0.4.1"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tide-calendar-adapter",
"display_name": "Tide Calendar",
"version": "0.4.0",
"version": "0.4.1",
"description": "Tide calendar for Mozilla WebThings Gateway",
"author": "Mozilla IoT",
"main": "main.py",
Expand Down
118 changes: 99 additions & 19 deletions pkg/tide_calendar_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def __init__(self, adapter, _id, station_id, unit):

self.get_station_info()

self.next_high_tide = None
self.next_low_tide = None
self.high_tides = []
self.low_tides = []

if self.have_tide_predictions:
self.properties['lowTideTime'] = TideCalendarProperty(
Expand Down Expand Up @@ -113,6 +113,23 @@ def __init__(self, adapter, _id, station_id, unit):
False
)

self.properties['status'] = TideCalendarProperty(
self,
'status',
{
'title': 'Status',
'type': 'string',
'enum': [
'low',
'high',
'rising',
'falling',
],
'readOnly': True,
},
''
)

if self.have_water_levels:
self._type = ['MultiLevelSensor']
self.properties['currentLevel'] = TideCalendarProperty(
Expand Down Expand Up @@ -204,7 +221,7 @@ def poll(self):
'product': 'predictions',
'application': 'NOS.COOPS.TAC.WL',
'begin_date': '{}{:02d}{:02d}'.format(now.year, now.month, now.day), # noqa
'range': '24',
'range': '36',
'datum': 'MLLW',
'station': '{}'.format(self.station_id),
'time_zone': 'lst_ldt',
Expand All @@ -224,6 +241,9 @@ def poll(self):
print('Invalid tide prediction data for station {}'
.format(self.station_id))
else:
self.high_tides = []
self.low_tides = []

set_high = False
set_low = False

Expand All @@ -233,27 +253,42 @@ def poll(self):
'%Y-%m-%d %H:%M'
)

if parsed >= now:
if prediction['type'] == 'H' and not set_high:
self.next_high_tide = parsed
set_high = True
if parsed < now:
continue

level = round(float(prediction['v']), 1)
timestamp = prediction['t'].split(' ')[1]

if prediction['type'] == 'H':
self.high_tides.append({
'datetime': parsed,
'level': level,
'timestamp': timestamp,
})

if not set_high:
self.properties['highTideLevel'].update(
round(float(prediction['v']), 1)
level
)
self.properties['highTideTime'].update(
prediction['t'].split(' ')[1]
timestamp
)
elif prediction['type'] == 'L' and not set_low:
self.next_low_tide = parsed
set_low = True

set_high = True
elif prediction['type'] == 'L':
self.low_tides.append({
'datetime': parsed,
'level': level,
'timestamp': timestamp,
})

if not set_low:
self.properties['lowTideLevel'].update(
round(float(prediction['v']), 1)
level
)
self.properties['lowTideTime'].update(
prediction['t'].split(' ')[1]
timestamp
)
set_low = True

if self.have_water_levels:
url = 'https://tidesandcurrents.noaa.gov/api/datagetter'
Expand Down Expand Up @@ -300,14 +335,59 @@ def check_events(self):
microsecond=0
)

if self.next_high_tide is not None:
if now == self.next_high_tide:
status_set = False

if len(self.high_tides) > 0:
next_high_tide = self.high_tides[0]['datetime']

if now == next_high_tide:
self.properties['highTide'].update(True)
self.properties['status'].update('high')
status_set = True
else:
if next_high_tide < now:
self.high_tides.pop(0)

if len(self.high_tides) > 0:
next_high_tide = self.high_tides[0]
self.properties['highTideLevel'].update(
next_high_tide['level']
)
self.properties['highTideTime'].update(
next_high_tide['timestamp']
)

self.properties['highTide'].update(False)

if self.next_low_tide is not None:
if now == self.next_low_tide:
if len(self.low_tides) > 0:
next_low_tide = self.low_tides[0]['datetime']

if now == next_low_tide:
self.properties['lowTide'].update(True)
self.properties['status'].update('low')
status_set = True
else:
if next_low_tide < now:
self.low_tides.pop(0)

if len(self.low_tides) > 0:
next_low_tide = self.low_tides[0]
self.properties['lowTideLevel'].update(
next_low_tide['level']
)
self.properties['lowTideTime'].update(
next_low_tide['timestamp']
)

self.properties['lowTide'].update(False)

if len(self.high_tides) > 0 and \
len(self.low_tides) > 0 and \
not status_set:
next_low_tide = self.low_tides[0]
next_high_tide = self.high_tides[0]

if next_low_tide['datetime'] < next_high_tide['datetime']:
self.properties['status'].update('falling')
else:
self.properties['status'].update('rising')

0 comments on commit 3d1ee98

Please sign in to comment.