Please disable Adblockers and enable JavaScript for domain CEWebS.cs.univie.ac.at! We have NO ADS, but they may interfere with some of our course material.
Name: tests/personas/monitor.rb
1: | #!/usr/bin/ruby |
2: | require 'riddl/client' |
3: | require 'pp' |
4: | require 'xml/smart' |
5: | require 'json' |
6: | require 'yaml' |
7: | require 'uri' |
8: | |
9: | def get_rule(notification,cat_event, rules) |
10: | |
11: | rules.each{ |r| |
12: | if (/^#{r['trigger']['label']} \(\d*\)/ =~ notification['cpee_label'] || r['trigger']['label'].nil? || r['trigger']['label'].empty? ) \ |
13: | && (notification['orgmodel'] == r['trigger']['target'] || r['trigger']['target'].nil? || r['trigger']['label'].empty? ) \ |
14: | && cat_event == r['trigger']['event'] \ |
15: | && r['active'] == true then |
16: | |
17: | pp 'yes' |
18: | return r |
19: | end |
20: | } |
21: | return nil |
22: | end |
23: | |
24: | #ON Event |
25: | def on_task_add(notification,rule) |
26: | if rule['action']['event'] == 'user/take' |
27: | do_task_take(notification,rule['action']['user']) |
28: | end |
29: | end |
30: | |
31: | def on_user_take(notification,rule) |
32: | case rule['action']['event'] |
33: | when 'user/giveback' |
34: | do_task_giveback(notification) |
35: | when 'task/finish' |
36: | do_task_finish(notification,rule) |
37: | end |
38: | |
39: | end |
40: | def on_user_giveback(notification,rule) |
41: | if rule['action']['event'] == 'user/giveback' |
42: | do_task_take(notification,rule['action']['user']) |
43: | end |
44: | end |
45: | |
46: | #DO Action |
47: | def do_task_giveback(notification) |
48: | put_to_wl("/#{notification['domain']}/#{notification['user']}/tasks/#{notification['callback_id']}",[Riddl::Parameter::Simple.new("operation","giveback"),]) |
49: | end |
50: | |
51: | def do_task_take(notification,user) |
52: | put_to_wl("/#{notification['domain']}/#{user}/tasks/#{notification['callback_id']}",[Riddl::Parameter::Simple.new("operation","take"),]) |
53: | pp "did user/take" |
54: | end |
55: | def do_task_finish(notification,rule) |
56: | parameters = YAML.load_file('./parameters/'+rule['action']['data']) |
57: | pp parameters |
58: | put_parameters = Array.new |
59: | parameters['data'].each{ |k,v| |
60: | put_parameters << Riddl::Parameter::Simple.new(k,v) |
61: | pp "key #{k} value #{v}" |
62: | } |
63: | |
64: | srv = Riddl::Client.new(notification['cpee_callback']) |
65: | status, response = srv.put put_parameters |
66: | pp status |
67: | if status == 200 then |
68: | resource_url = URI.escape("/#{notification['domain']}/#{notification['user']}/tasks/#{notification['callback_id']}") |
69: | srv = Riddl::Client.new('http://coms.wst.univie.ac.at:9300') |
70: | res = srv.resource(resource_url) |
71: | status, response = res.delete |
72: | pp status |
73: | end |
74: | end |
75: | |
76: | def put_to_wl(resource_url,parameters) |
77: | resource_url = URI.escape(resource_url) |
78: | srv = Riddl::Client.new('http://coms.wst.univie.ac.at:9300') |
79: | res = srv.resource(resource_url) |
80: | status, response = res.put parameters |
81: | pp resource_url |
82: | pp status |
83: | end |
84: | |
85: | def main() |
86: | domain = "Virtual%20Business%201" |
87: | |
88: | path = File.dirname(__FILE__) |
89: | rules = Array.new |
90: | |
91: | #load all the YAMLS in pat |
92: | Dir.glob(path + '/rules/*.yml') do |yml_file| |
93: | rules << YAML.load_file(yml_file) |
94: | end |
95: | srv = Riddl::Client.new('http://coms.wst.univie.ac.at:9300') |
96: | res = srv.resource("/Virtual%20Business%201/notifications/subscriptions/") |
97: | status, response = res.post [ |
98: | Riddl::Parameter::Simple.new("url","http://coms.wst.univie.ac.at"), |
99: | Riddl::Parameter::Simple.new("topic","user"), |
100: | Riddl::Parameter::Simple.new("events","take,giveback,finish"), |
101: | Riddl::Parameter::Simple.new("topic","task"), |
102: | Riddl::Parameter::Simple.new("events","add"), |
103: | |
104: | |
105: | ] |
106: | |
107: | key = response.first.value |
108: | pp key |
109: | res = srv.resource("/#{domain}/notifications/subscriptions/#{key}/ws/") |
110: | begin |
111: | res.ws do |conn| |
112: | |
113: | conn.stream do |msg| |
114: | puts msg |
115: | puts '--------------' |
116: | topic, event, notification, cat_event = nil |
117: | XML::Smart.string(msg.data) do |doc| |
118: | topic = doc.find('string(/event/topic)') |
119: | event = doc.find('string(/event/event)') |
120: | notification = JSON.parse(doc.find("string(/event/notification)")) |
121: | |
122: | cat_event = topic.to_s+'/'+event.to_s |
123: | |
124: | #select rule |
125: | |
126: | |
127: | end |
128: | rule = get_rule(notification, cat_event, rules) |
129: | unless rule.nil? then |
130: | case cat_event |
131: | when 'task/add' |
132: | on_task_add(notification,rule) |
133: | when 'user/take' |
134: | on_user_take(notification,rule) |
135: | when 'user/giveback' |
136: | on_user_giveback(notification,rule) |
137: | end |
138: | end |
139: | end |
140: | |
141: | conn.errback do |e| |
142: | puts "Got error: #{e}" |
143: | end |
144: | |
145: | end |
146: | rescue Interrupt => e |
147: | pp "close" |
148: | srv = Riddl::Client.new('http://coms.wst.univie.ac.at:9300') |
149: | res = srv.resource("/Virtual%20Business%201/notifications/subscriptions/#{key}") |
150: | status, response = res.delete |
151: | pp status |
152: | end |
153: | end |
154: | main() |