-
Notifications
You must be signed in to change notification settings - Fork 0
/
devitation_generator.rb
142 lines (119 loc) · 3.13 KB
/
devitation_generator.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
require 'rubygems'
require 'rufus/scheduler'
require 'httparty'
require 'json'
require 'xmlsimple'
require 'htmlentities'
# define uri of the deviation api
class DevitationApi
include HTTParty
#base_uri 'http://localhost:8888/v1'
base_uri 'http://api.av.vikel.se/v1'
end
# define uri of vasttrafik stop list
class GetStopList
include HTTParty
base_uri 'https://api.trafiklab.se/samtrafiken/resrobot'
end
class Stop
attr_accessor :name, :x, :y
def initialize(name, x, y)
@name = name
@x = x
@y = y
end
end
# container for stop list with abiltiy to retrieve random stop
class StopList
@stops = Array.[]
def initialize
params = {"key" => "8300d2f62d1a0db69a7c9117dbd8768f",
"centerX" => "11.981211",
"centerY" => "57.709792",
"radius" => "30000", # max radius
"coordSys" => "WGS84",
"apiVersion" => "2.1" }
out = GetStopList.get('/StationsInZone', :query => params)
puts out
xml = XmlSimple.xml_in(out.response.body)
tmp_stops = []
xml['location'].each do |item|
stop = Stop.new(item['name'][0], item['x'], item['y'])
tmp_stops.push(stop)
end
@stops = tmp_stops
end
def random_stop
return @stops.sample
end
end
class Deviation
# needs to be backed up with some "real data" -> vasttrafik!?
@@comments = ["olycka", "motor skada", "vet inte vafor", "alltid versenat"]
@@transports = ["BUS", "TRAIN", "SUBWAY"]
@@stops = StopList.new
attr_accessor :comment, :line, :vehicle, :latitude, :longitude, :transport, :source
def initialize(randomize)
if randomize == true
stop = @@stops.random_stop
@comment = stop.name << " - " << @@comments.sample
@line = rand(10) + 1
@vehicle = rand(500) + 1
@latitude = stop.x
@longitude = stop.y
@transport = @@transports.sample
@source = "crowd"
end
end
def to_hash()
dev = Hash.new
if @comment != nil
dev['comment'] = @comment
end
if @line != nil
dev['line'] = @line
end
if @vehicle != nil
dev['vehicle'] = @vehicle
end
if @latitude != nil
dev['latitude'] = @latitude
end
if @longitude != nil
dev['longitude'] = @longitude
end
if @transport != nil
dev['transport'] = @transport
end
if @source != nil
dev['source'] = @source
end
return dev
end
end
class DeviationGenerator
def initialize(api, interval = '1s')
@scheduler = Rufus::Scheduler.start_new
@api = api
@interval = interval
end
def createRandomDeviation
@scheduler.every @interval do
begin
dev = Deviation.new(true)
params = dev.to_hash
puts 'Creating new deviation ' << params.to_s
out = DevitationApi.post('/deviations/', :body => params)
id = JSON.parse( out.response.body )['id']
puts 'Ceated deviation ' << id
rescue Exception => msg
puts "Something went wrong"
end
end
@scheduler.join
end
end
# put things together
api = DevitationApi.new
generator = DeviationGenerator.new(api)
generator.createRandomDeviation()