diff --git a/modules/bus.js b/modules/bus.js index ef96a8b5431161aa8aaa7f6efc43f33ec2670d94..64060fff8d9ad31aa254225afc9787eae9dcd91a 100644 --- a/modules/bus.js +++ b/modules/bus.js @@ -11,9 +11,12 @@ var ENTRIES = 6; /// VARS var CACHED = null; -var TEMPLATE = ''; -fs.readFile('modules/bus/template.mustache', 'utf-8', function (err, data) { - TEMPLATE = data; +var TEMPLATES = {}; +fs.readFile('modules/bus/outer.mustache', 'utf-8', function (err, data) { + TEMPLATES.outer = data; +}); +fs.readFile('modules/bus/inner.mustache', 'utf-8', function (err, data) { + TEMPLATES.inner = data; }); /// FUNS @@ -24,7 +27,7 @@ function pad(n, width, z) { } function fetchData(stop, cb) { - var url = "http://62.154.206.87/efaws2/default/XML_DM_REQUEST?sessionID=0&requestID=0&language=de&useRealtime=1&coordOutputFormat=WGS84[DD.ddddd]&locationServerActive=1&mode=direct&dmLineSelectionAll=1&depType=STOPEVENTS&useAllStops=1&command=null&type_dm=stop&name_dm="+CITY+' '+stop+"&itdTime=2315&itdDate=20151116&mId=efa_rc2" + var url = "http://62.154.206.87/efaws2/default/XML_DM_REQUEST?sessionID=0&requestID=0&language=de&useRealtime=1&coordOutputFormat=WGS84[DD.ddddd]&locationServerActive=1&mode=direct&dmLineSelectionAll=1&depType=STOPEVENTS&useAllStops=1&command=null&type_dm=stop&name_dm="+CITY+' '+stop+"&mId=efa_rc2" httpreq.get(url, {binary: true}, function (err, res) { try { cb(XML.parse(iconv.decode(res.body, 'latin1'))); @@ -69,12 +72,13 @@ function update(io) { var done = 0; var context = []; var innerGetData = function (stop, i) { + var ns = normalizeStop(stop); getData(stop, ENTRIES, function (deps) { - context[i] = {stop: stop, deps: deps}; - if (++done >= STOPS.length) { - io.emit('bus', Mustache.render(TEMPLATE, context)); + try { + context[i] = {stop: stop, normalizedStop: ns, deps: deps}; + io.emit('bus.'+ns, Mustache.render(TEMPLATES.inner, context[i])); CACHED = context; - } + } catch (e) {console.log(e);} // calculate when to update next var d = new Date(); var hour = d.getHours(); @@ -91,9 +95,17 @@ function update(io) { }); } +function normalizeStop(stop) { + return stop.replace(/[^a-zA-Z0-9_]/g,''); +} + module.exports = function (io) { update(io); io.on('connect', function (sock) { - sock.emit('bus', Mustache.render(TEMPLATE, CACHED)); + sock.emit('bus', Mustache.render(TEMPLATES.outer, CACHED)); + for (var i in CACHED) { + sock.emit('bus.'+CACHED[i].normalizedStop, + Mustache.render(TEMPLATES.inner, CACHED[i])); + } }); } diff --git a/modules/bus/inner.mustache b/modules/bus/inner.mustache new file mode 100644 index 0000000000000000000000000000000000000000..2c30bc3e70ccc7649f4d1df25ba70f2a828325d7 --- /dev/null +++ b/modules/bus/inner.mustache @@ -0,0 +1,22 @@ +<tr> + <th class="title" colspan="4"> + <h3><img src="/modules/bus/bsv.svg"> {{stop}}</h3> + </th> +</tr> +<tr> + <th>Time</th> + <th>Line</th> + <th>Pltf</th> + <th>Direction</th> +</tr> +{{#deps}} + <tr> + <td class="time"> + <span>{{hour}}:{{minute}}</span> + (<font class="until"></font>) + </td> + <td>{{{renderedLine}}}</td> + <td>{{platform}}</td> + <td>{{dir}}</td> + </tr> +{{/deps}} diff --git a/modules/bus/outer.mustache b/modules/bus/outer.mustache new file mode 100644 index 0000000000000000000000000000000000000000..7f60d24ee06685506288890e61107749e8b34a0b --- /dev/null +++ b/modules/bus/outer.mustache @@ -0,0 +1,13 @@ +<link rel="stylesheet" href="/modules/bus/bus.css"> +<script type="text/javascript" src="/modules/bus/bus.js"></script> +<table id="busTable"> + <colgroup> + <col width="140px"> + <col width="80px"> + <col> + <col> + </colgroup> + {{#.}} + <tbody data-infodisplay-outlet="{{normalizedStop}}"></tbody> + {{/.}} +</table> diff --git a/modules/bus/template.mustache b/modules/bus/template.mustache deleted file mode 100644 index 5250c599d189e447d01c80570dac13257cb556d5..0000000000000000000000000000000000000000 --- a/modules/bus/template.mustache +++ /dev/null @@ -1,34 +0,0 @@ -<link rel="stylesheet" href="/modules/bus/bus.css"> -<script type="text/javascript" src="/modules/bus/bus.js"></script> -<table id="busTable"> - <colgroup> - <col width="140px"> - <col width="80px"> - <col> - <col> - </colgroup> - {{#.}} - <tr> - <th class="title" colspan="4"> - <h3><img src="/modules/bus/bsv.svg"> {{stop}}</h3> - </th> - </tr> - <tr> - <th>Time</th> - <th>Line</th> - <th>Pltf</th> - <th>Direction</th> - </tr> - {{#deps}} - <tr> - <td class="time"> - <span>{{hour}}:{{minute}}</span> - (<font class="until"></font>) - </td> - <td>{{{renderedLine}}}</td> - <td>{{platform}}</td> - <td>{{dir}}</td> - </tr> - {{/deps}} - {{/.}} -</table>