From b87baa1f89a086a39004768ce5c6990560636183 Mon Sep 17 00:00:00 2001 From: Henri Bergius Date: Fri, 30 Jun 2017 20:14:15 +0200 Subject: [PATCH 1/3] Initial participant for fetching Oslo airport weather --- package.json | 2 + participants/AirportWeather.coffee | 61 ++++++++++++++++++++++++++++++ spec/AirportWeather.yaml | 17 +++++++++ 3 files changed, 80 insertions(+) create mode 100644 participants/AirportWeather.coffee create mode 100644 spec/AirportWeather.yaml 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/participants/AirportWeather.coffee b/participants/AirportWeather.coffee new file mode 100644 index 0000000..c63f244 --- /dev/null +++ b/participants/AirportWeather.coffee @@ -0,0 +1,61 @@ +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: '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 is 'icao' + return callback 'error', new Error "Unknown port name" + getWeather indata, (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/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 From 77d740b04b57ac9bf8213568082d5d909e4da9ae Mon Sep 17 00:00:00 2001 From: Henri Bergius Date: Fri, 30 Jun 2017 20:18:58 +0200 Subject: [PATCH 2/3] Add a 'bang' port for triggering fetch periodically --- participants/AirportWeather.coffee | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/participants/AirportWeather.coffee b/participants/AirportWeather.coffee index c63f244..83d65ec 100644 --- a/participants/AirportWeather.coffee +++ b/participants/AirportWeather.coffee @@ -22,6 +22,10 @@ Participant = (client, role) -> id: 'icao' type: 'string' hidden: false + , + id: 'fetch' + type: 'bang' + hidden: false , id: 'temperature' type: 'float' @@ -48,9 +52,13 @@ Participant = (client, role) -> if inport in ['temperature', 'pressure'] # Forward to outport return callback inport, null, indata - unless inport is 'icao' + unless inport in ['icao', 'fetch'] return callback 'error', new Error "Unknown port name" - getWeather indata, (err, weather) -> + 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 From 211ca1f1956ed3cfd85e3aa03c7d41a6dd8d4405 Mon Sep 17 00:00:00 2001 From: Henri Bergius Date: Fri, 30 Jun 2017 22:12:40 +0200 Subject: [PATCH 3/3] Move to components dir --- {participants => components}/AirportWeather.coffee | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {participants => components}/AirportWeather.coffee (100%) diff --git a/participants/AirportWeather.coffee b/components/AirportWeather.coffee similarity index 100% rename from participants/AirportWeather.coffee rename to components/AirportWeather.coffee