Skip to content

Commit

Permalink
Support monitor offset
Browse files Browse the repository at this point in the history
Closes #4
  • Loading branch information
kiliankoe committed Aug 24, 2016
1 parent a780a18 commit b8500e8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,20 @@ Monitor public transport in the VVO/DVB network right from within Alfred. Powere

Download [here](https://github.com/kiliankoe/alfred_dvb/releases/latest).

### Usage

- `dvb helmholtz`

Gives you all connections from Helmholtzstraße.

- `dvb prager in 10`

Gives you all connections from Prager Straße in 10 minutes. Optional text after the amount of minutes is ignored, so you could also enter `dvb prager in 10 minutes` if you prefer.

### Problems?

Please [report an issue](https://github.com/kiliankoe/alfred_dvb/issues/new) if something isn't working as expected or you have a question/feature request.

### Credits

Bus icon by [icons8.com](https://icons8.com).
33 changes: 20 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ moment.locale('de')

var args = process.argv.slice(2)

// TODO: Decide on if this is a monitor request somehow
displayMonitor(args)
var offset = 0
var stop = args[0]

function displayMonitor (stop, timeOffset = 0, numResults = 6) {
dvb.monitor(args[0], timeOffset, numResults)
var offsetMatch = args[0].match(/in (\d+)/)
if (offsetMatch !== null && offsetMatch.length > 0) {
offset = offsetMatch[1]
stop = args[0].split('in')[0]
}

monitor(stop, offset).then(function (output) {
console.log(JSON.stringify(output))
})

function monitor (stop, timeOffset = 0, numResults = 6) {
return dvb.monitor(stop, timeOffset, numResults)
.then(function (data) {
var items = {'items': []}

Expand All @@ -19,11 +29,10 @@ function displayMonitor (stop, timeOffset = 0, numResults = 6) {
'title': 'Haltestelle nicht gefunden 🤔',
'subtitle': 'Vielleicht ein Tippfehler?'
})
console.log(JSON.stringify(items))
return
return items
}

data.forEach(function (con) {
items.items = data.map(function (con) {
var timeText
if (con.arrivalTimeRelative === 0) {
timeText = ' jetzt'
Expand All @@ -32,23 +41,21 @@ function displayMonitor (stop, timeOffset = 0, numResults = 6) {
} else {
timeText = ' in ' + con.arrivalTimeRelative + ' Minuten'
}
items.items.push({
return {
'title': con.line + ' ' + con.direction + timeText,
'subtitle': moment().add(con.arrivalTimeRelative, 'm').format('dddd, HH:mm [Uhr]'),
'icon': {
'path': 'transport_icons/' + con.mode.name + '.png'
}
})
}
})

console.log(JSON.stringify(items))
return items
})
.catch(function (err) {
var items = {'items': [{
'title': 'Unerwarteter Fehler 😲',
'subtitle': err.message,
}]}
console.log(JSON.stringify(items))
throw err
return items
})
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alfred_dvb",
"version": "0.2.1",
"version": "0.3.0",
"description": "Monitor public transport in the VVO/DVB network from Alfred",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit b8500e8

Please sign in to comment.