-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvacancy.rb
73 lines (59 loc) · 1.98 KB
/
vacancy.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
class Vacancy
require '../arduino/dependence_provider'
require 'rubygems'
include DependenceProvider
dependence = Vacancy.new
@db = dependence.connection
sp = dependence.serial_port
AVAILABLE = "0"
RESTRICTED = "1"
BUSY = "2"
def self.get_vacancy(cod_arduino)
@db[:vacancies].filter(:cod_arduino => cod_arduino).order(:id)
end
def self.update_vacancy(cod_arduino, status)
@db[:vacancies].filter(:cod_arduino => cod_arduino).update(:status => status)
end
def self.update_status_controll(cod_arduino)
@db.transaction do
vacancy = get_vacancy(cod_arduino).last
old_status = vacancy[:status]
update_vacancy(cod_arduino, AVAILABLE)
@db[:status_controlls].filter(:vacancy_id => vacancy[:id], :time_end => nil).update(:time_end => Time.now, :current_status => AVAILABLE, :old_status => old_status)
end
end
def self.save_status_controll(cod_arduino)
@db.transaction do
vacancy = get_vacancy(cod_arduino).last
update_vacancy(cod_arduino, BUSY)
@db[:status_controlls].insert(:vacancy_id => vacancy[:id], :timebegin => Time.now, :current_status => BUSY)
end
end
while true do
msg = sp.gets
p msg
unless msg.nil?
cod_arduino = msg.split(":")
vacancy = get_vacancy(cod_arduino[0]).last
if !vacancy.nil?
status = @db[:status_controlls].filter(:vacancy_id => vacancy[:id]).order(:id).last
if status.nil?
save_status_controll(cod_arduino[0])
elsif !status.nil?
if vacancy[:status] != RESTRICTED
if status[:time_end].nil?
self.update_status_controll(cod_arduino[0])
if cod_arduino[1] == BUSY
save_status_controll(cod_arduino[0])
end
end
if !status[:time_end].nil? and cod_arduino[1] == BUSY
save_status_controll(cod_arduino[0])
end
end
end
end
end
sleep(1)
end
end