diff --git a/components/AirportWeather.coffee b/components/AirportWeather.coffee new file mode 100644 index 0000000..83d65ec --- /dev/null +++ b/components/AirportWeather.coffee @@ -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 diff --git a/package.json b/package.json index 2bf945c..72f329b 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/spec/AirportWeather.yaml b/spec/AirportWeather.yaml new file mode 100644 index 0000000..a390f63 --- /dev/null +++ b/spec/AirportWeather.yaml @@ -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