-
Notifications
You must be signed in to change notification settings - Fork 0
/
havag.rb
116 lines (90 loc) · 3.13 KB
/
havag.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env ruby
# encoding: utf-8
require 'httpclient'
class HavagResponseError < IOError; end
class Havag
def initialize
@hclnt = HavagClient.new('http://83.221.237.42:20010')
end
#private method
def get(body)
hres = @hclnt.request(body)
return hres
end
def getNextTrains(station)
body = [0x63, 0x00, 0x01, 0x6d, 0x00, 0x14, 0x67, 0x65,
0x74, 0x44, 0x65, 0x70, 0x61, 0x72, 0x74, 0x75,
0x72, 0x65, 0x73, 0x46, 0x6f, 0x72, 0x53, 0x74,
0x6f, 0x70, 0x53, 0x00]
body.push(station.length)
station.bytes{ |b| body.push(b) }
body.push(0x7a)
result = Array.new
self.get(body).to_a.each do |train|
result.push({:line => train[0],
:destination => train[1],
:departure_real => DateTime.strptime(train[2], '%Y.%m.%d.%H:%M:%S'),
:departure_scheduled => DateTime.strptime(train[3], '%Y.%m.%d.%H:%M:%S'),
:low_floor => train[4],
:cab_id => train[6],
:station => train[10]})
end
#return self.get(body).to_a
return result.sort { |x, y| x[:departure_real] <=> y[:departure_real] }
end
#Ergebnis ev. als Enumeration für vorhandene Eingabe der getNextTrains-method
def getAllStations
#Gibt alle Haltestellen der Havag zurück
body = [0x63, 0x00, 0x01, 0x6d, 0x00, 0x0c, 0x67, 0x65,
0x74, 0x53, 0x74, 0x6f, 0x70, 0x43, 0x6f, 0x64,
0x65, 0x73, 0x7a]
return self.get(body).to_a
end
end
class HavagClient
@@headers = [['Content-Type', 'text/xml']]
def initialize(server)
@server = server
@clnt = HTTPClient.new
end
def request(bytestream)
res = @clnt.post(@server + "/init/rtpi", (bytestream.map{ |c| c.chr}).join, @@headers)
return HavagResponse.new(res.content)
end
end
class HavagResponse < String
@@rsplit = [0x56, 0x74, 0x00]
@@csplit = [0x53, 0x00]
def to_a
data = Array.new
output = Array.new
self.encode!("ISO-8859-15", "UTF-8")
self.bytes{ |b| data.push(b) }
while data.length > 3 do
if data[0] == @@rsplit[0] and data[1] == @@rsplit[1] and data[2] == @@rsplit[2] then
data.shift(3)
output.push(Array.new)
next
end
if data[0] == @@csplit[0] and data[1] == @@csplit[1] then
if output.empty? then
raise HavagResponseError.new('no dataset marker found while already retriving data')
end
data.shift(2)
value = String.new
(1..data.shift).each do |c|
value += data.shift.chr
end
output.last.push(value.encode("UTF-8", "ISO-8859-15"))
next
end
data.shift
end
return output
end
end
if __FILE__ == $0
havag = Havag.new
#puts havag.getNextTrains("Talstraße")
puts havag.getAllStations
end