Skip to content

Commit f161708

Browse files
committed
Initial commit.
0 parents  commit f161708

14 files changed

+979
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.pyc
2+
*.swp
3+
*.tgz
4+
lib/
5+
package/
6+
SHA256SUMS

CODE_OF_CONDUCT.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Community Participation Guidelines
2+
3+
This repository is governed by Mozilla's code of conduct and etiquette guidelines.
4+
For more details, please read the
5+
[Mozilla Community Participation Guidelines](https://www.mozilla.org/about/governance/policies/participation/).
6+
7+
## How to Report
8+
For more information on how to report violations of the Community Participation Guidelines, please read our '[How to Report](https://www.mozilla.org/about/governance/policies/participation/reporting/)' page.
9+
10+
<!--
11+
## Project Specific Etiquette
12+
13+
In some cases, there will be additional project etiquette i.e.: (https://bugzilla.mozilla.org/page.cgi?id=etiquette.html).
14+
Please update for your project.
15+
-->

LICENSE

Lines changed: 373 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# tide-calendar-adapter
2+
3+
Tide calendar adapter for Mozilla WebThings Gateway.
4+
5+
# Configuration
6+
7+
To configure the adapter, navigate to _Settings -> Add-ons_ in the user interface and click the _Configure_ button on the entry for this adapter.
8+
9+
## Stations
10+
11+
Each station is a numeric station ID. You can find the station ID by searching on [this website](https://tidesandcurrents.noaa.gov/) for the station closest to you.
12+
13+
## Unit
14+
15+
This is the tide level unit you want, either feet (english) or meters (metric).
16+
17+
# Requirements
18+
19+
If you're running this add-on outside of the official gateway image for the Raspberry Pi, i.e. you're running on a development machine, you'll need to do the following (adapt as necessary for non-Ubuntu/Debian):
20+
21+
```
22+
sudo apt install python3-dev libnanomsg-dev
23+
sudo pip3 install nnpy
24+
sudo pip3 install git+https://github.com/mozilla-iot/gateway-addon-python.git
25+
```

main.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""Tide calendar adapter for Mozilla WebThings Gateway."""
2+
3+
from os import path
4+
import functools
5+
import gateway_addon
6+
import signal
7+
import sys
8+
import time
9+
10+
sys.path.append(path.join(path.dirname(path.abspath(__file__)), 'lib'))
11+
12+
from pkg.tide_calendar_adapter import TideCalendarAdapter # noqa
13+
14+
15+
_API_VERSION = {
16+
'min': 2,
17+
'max': 2,
18+
}
19+
_ADAPTER = None
20+
21+
print = functools.partial(print, flush=True)
22+
23+
24+
def cleanup(signum, frame):
25+
"""Clean up any resources before exiting."""
26+
if _ADAPTER is not None:
27+
_ADAPTER.close_proxy()
28+
29+
sys.exit(0)
30+
31+
32+
if __name__ == '__main__':
33+
if gateway_addon.API_VERSION < _API_VERSION['min'] or \
34+
gateway_addon.API_VERSION > _API_VERSION['max']:
35+
print('Unsupported API version.')
36+
sys.exit(0)
37+
38+
signal.signal(signal.SIGINT, cleanup)
39+
signal.signal(signal.SIGTERM, cleanup)
40+
_ADAPTER = TideCalendarAdapter(verbose=True)
41+
42+
# Wait until the proxy stops running, indicating that the gateway shut us
43+
# down.
44+
while _ADAPTER.proxy_running():
45+
time.sleep(2)

package.json

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"name": "tide-calendar-adapter",
3+
"display_name": "Tide Calendar",
4+
"version": "0.1.0",
5+
"description": "Tide calendar for Mozilla WebThings Gateway",
6+
"author": "Mozilla IoT",
7+
"main": "main.py",
8+
"keywords": [
9+
"mozilla",
10+
"iot",
11+
"adapter",
12+
"tide",
13+
"noaa"
14+
],
15+
"homepage": "https://github.com/mozilla-iot/tide-calendar-adapter",
16+
"license": "MPL-2.0",
17+
"repository": {
18+
"type": "git",
19+
"url": "https://github.com/mozilla-iot/tide-calendar-adapter.git"
20+
},
21+
"bugs": {
22+
"url": "https://github.com/mozilla-iot/tide-calendar-adapter/issues"
23+
},
24+
"files": [
25+
"LICENSE",
26+
"SHA256SUMS",
27+
"lib",
28+
"main.py",
29+
"pkg/__init__.py",
30+
"pkg/tide_calendar_adapter.py",
31+
"pkg/tide_calendar_device.py",
32+
"pkg/tide_calendar_property.py",
33+
"pkg/util.py"
34+
],
35+
"moziot": {
36+
"api": {
37+
"min": 2,
38+
"max": 2
39+
},
40+
"plugin": true,
41+
"exec": "python3 {path}/main.py",
42+
"config": {
43+
"stations": [],
44+
"unit": "ft"
45+
},
46+
"schema": {
47+
"type": "object",
48+
"required": [
49+
"stations",
50+
"unit"
51+
],
52+
"properties": {
53+
"stations": {
54+
"type": "array",
55+
"description": "List of NOAA station IDs",
56+
"items": {
57+
"type": "integer"
58+
}
59+
},
60+
"unit": {
61+
"type": "string",
62+
"description": "Water level unit system",
63+
"enum": [
64+
"english",
65+
"metric"
66+
]
67+
}
68+
}
69+
}
70+
}
71+
}

package.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
version=$(grep version package.json | cut -d: -f2 | cut -d\" -f2)
6+
7+
# Clean up from previous releases
8+
rm -rf *.tgz package
9+
rm -f SHA256SUMS
10+
rm -rf lib
11+
12+
# Prep new package
13+
mkdir lib
14+
mkdir package
15+
16+
# Pull down Python dependencies
17+
pip3 install -r requirements.txt -t lib --no-binary pyHS100 --prefix ""
18+
19+
# Put package together
20+
cp -r lib pkg LICENSE package.json *.py package/
21+
find package -type f -name '*.pyc' -delete
22+
find package -type d -empty -delete
23+
24+
# Generate checksums
25+
cd package
26+
sha256sum *.py pkg/*.py LICENSE > SHA256SUMS
27+
find lib -type f -exec sha256sum {} \; >> SHA256SUMS
28+
cd -
29+
30+
# Make the tarball
31+
tar czf "tide-calendar-adapter-${version}.tgz" package

pkg/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Make this a package."""

pkg/tide_calendar_adapter.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""Tide calendar adapter for Mozilla WebThings Gateway."""
2+
3+
from gateway_addon import Adapter, Database
4+
5+
from .tide_calendar_device import TideCalendarDevice, StationException
6+
7+
8+
class TideCalendarAdapter(Adapter):
9+
"""Adapter for NOAA tide calendars."""
10+
11+
def __init__(self, verbose=False):
12+
"""
13+
Initialize the object.
14+
15+
verbose -- whether or not to enable verbose logging
16+
"""
17+
self.name = self.__class__.__name__
18+
Adapter.__init__(self,
19+
'tide-calendar-adapter',
20+
'tide-calendar-adapter',
21+
verbose=verbose)
22+
23+
self.pairing = False
24+
self.start_pairing()
25+
26+
def start_pairing(self, timeout=None):
27+
"""
28+
Start the pairing process.
29+
30+
timeout -- Timeout in seconds at which to quit pairing
31+
"""
32+
if self.pairing:
33+
return
34+
35+
self.pairing = True
36+
37+
database = Database('tide-calendar-adapter')
38+
if not database.open():
39+
return
40+
41+
config = database.load_config()
42+
database.close()
43+
44+
if not config or 'stations' not in config or 'unit' not in config:
45+
return
46+
47+
unit = config['unit']
48+
49+
for station in config['stations']:
50+
_id = 'tide-calendar-{}'.format(station)
51+
if _id not in self.devices:
52+
try:
53+
device = TideCalendarDevice(self, _id, station, unit)
54+
except StationException as e:
55+
print(e)
56+
else:
57+
self.handle_device_added(device)
58+
59+
self.pairing = False
60+
61+
def cancel_pairing(self):
62+
"""Cancel the pairing process."""
63+
self.pairing = False

0 commit comments

Comments
 (0)