Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Airport weather #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions components/AirportWeather.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
msgflo = require 'msgflo-nodejs'
MetarFetcher = require('metar-taf').MetarFetcher
metarFetcher = new MetarFetcher
metarParser = require('metar')

getWeather = (station, callback) ->
metarFetcher.getData(station)
.then (data) ->
clean = data.split("\n")[1]
callback null, metarParser clean
, (err) ->
callback err

Participant = (client, role) ->
station = null
definition =
id: role
component: 'AirportWeather'
icon: 'plane'
label: 'Fetch weather data for an airport'
inports: [
id: 'icao'
type: 'string'
hidden: false
,
id: 'fetch'
type: 'bang'
hidden: false
,
id: 'temperature'
type: 'float'
hidden: true
,
id: 'pressure'
type: 'float'
hidden: true
]
outports: [
id: 'temperature'
type: 'float'
hidden: false
,
id: 'pressure'
type: 'float'
hidden: false
,
id: 'error'
type: 'object'
hidden: false
]
process = (inport, indata, callback) ->
if inport in ['temperature', 'pressure']
# Forward to outport
return callback inport, null, indata
unless inport in ['icao', 'fetch']
return callback 'error', new Error "Unknown port name"
if inport is 'icao'
station = indata
if inport is 'fetch' and station is null
return callback 'error', new Error "No weather station provided"
getWeather station, (err, weather) ->
return callback 'error', err if err
participant.send 'pressure', weather.altimeterInHpa
callback 'temperature', null, weather.temperature

participant = new msgflo.participant.Participant client, definition, process, role
return participant

module.exports = Participant
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"dependencies": {
"coffee-script": "^1.10.0",
"fbp": "^1.5.0",
"metar": "^1.0.0",
"metar-taf": "0.0.2",
"mqtt": "~2.8.0",
"msgflo": "^0.10.19",
"msgflo-nodejs": "^0.10.5"
Expand Down
17 changes: 17 additions & 0 deletions spec/AirportWeather.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: 'Oslo airport weather'
topic: AirportWeather
fixture:
type: 'fbp'
data: |
INPORT=weather.ICAO:ICAO
OUTPORT=weather.TEMPERATURE:TEMPERATURE
weather(AirportWeather)
cases:
-
name: 'fetching weather'
assertion: 'should return a temperature'
inputs:
icao: ENGM
expect:
temperature:
type: number