forked from ossforumjp-app-IoT/rubyiot_server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.rb
243 lines (192 loc) · 4.61 KB
/
models.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
module Echonet
Defs = JSON::parse(open("public/echonet-device-objects.json").read)
module ClassGroup
def class_group_name
unless self.class_group_code
return nil
end
if Defs[self.class_group_code].class == Hash
return Defs[self.class_group_code]["class_group_name"]
else
return nil
end
end
end
module Class
def class_name
unless self.class_group_code
return nil
end
unless self.class_code
return nil
end
unless Defs[self.class_group_code].class == Hash
return nil
end
unless Defs[self.class_group_code][self.class_code].class == Hash
return nil
end
Defs[self.class_group_code][self.class_code]["class_name"]
end
end
module Property
def definitions(def_name)
unless self.class_group_code
return nil
end
unless self.class_code
return nil
end
unless self.property_code
return nil
end
unless Defs[self.class_group_code].class == Hash
return nil
end
unless Defs[self.class_group_code][self.class_code].class == Hash
return nil
end
unless Defs[self.class_group_code][self.class_code][self.property_code].class == Hash
return nil
end
Defs[self.class_group_code][self.class_code][self.property_code][def_name]
end
def property_name
self.definitions("property_name")
end
end
end
class User < ActiveRecord::Base
has_many :user_gateway_relations
def password=(s)
self.password_hash = Digest::SHA256.hexdigest(s)
end
end
class Gateway < ActiveRecord::Base
has_many :user_gateway_relations
has_many :devices
end
class UserGatewayRelation < ActiveRecord::Base
belongs_to :user
belongs_to :gateway
end
class Device < ActiveRecord::Base
include Echonet::ClassGroup
include Echonet::Class
belongs_to :gateway
has_many :device_properties
end
class DeviceProperty < ActiveRecord::Base
include Echonet::ClassGroup
include Echonet::Class
include Echonet::Property
belongs_to :device
has_many :sensor_datas
has_many :sensor_hourly_datas
has_many :sensor_alerts
has_many :monitor_ranges
has_many :operations
def ldelete
self.deleted_at = Time.now
end
def lrestore
self.deleted_at = nil
end
def self.lwhere(w)
DeviceProperty.where(deleted_at: nil).where(w)
end
def self.lexists?(w)
DeviceProperty.where(deleted_at: nil).exists?(w)
end
end
class SensorData < ActiveRecord::Base
self.table_name = 'sensor_datas'
belongs_to :device_property
end
class SensorHourlyData < ActiveRecord::Base
self.table_name = 'sensor_hourly_datas'
belongs_to :device_property
end
class MonitorRange < ActiveRecord::Base
belongs_to :device_property
end
class SensorAlert < ActiveRecord::Base
belongs_to :device_property
end
class Operation < ActiveRecord::Base
belongs_to :device_property
@@queue = {}
def push
if self.device_property_id
dpid = self.device_property_id.to_s
else
return false
end
if self.value
v = self.value
else
return false
end
unless @@queue.has_key?(dpid)
@@queue[dpid] = []
end
if self.save
@@queue[dpid].push( { id: self.id, value: v } )
return true
else
return false
end
end
def self.pop(device_property_id)
Operation.init_queue
dpid = device_property_id.to_s
unless @@queue.has_key?(dpid)
@@queue[dpid] = []
end
if @@queue[dpid].length > 0
op = @@queue[dpid].shift
Operation.find(op[:id])
else
nil
end
end
private
def self.init_queue
w = "created_at > '#{(Time.now - 60).strftime("%Y-%m-%d %H:%M:%S")}'"
w += " AND (status IS NULL OR status = '')"
objs = Operation.where(w)
unless objs.empty?
objs.each { |obj|
dpid = obj.device_property_id.to_s
unless @@queue.has_key?(dpid)
@@queue[dpid] = []
end
if @@queue[dpid].empty?
@@queue[dpid].push( { id: obj.id, value: obj.value } )
end
}
end
end
end
class Door
@@data = [
{status: false, pet: false, updated_at: Time.now},
{status: false, pet: true, updated_at: Time.now},
]
attr_accessor :id, :status, :updated_at
def self.find(door_id)
return nil if door_id.to_i < 0
door = Door.new
door.id = door_id.to_i
door.status = @@data[door_id.to_i][:status]
return door
end
def self.where(door_id: -1)
return find(door_id)
end
def save
return nil if !@id || @id.to_i < 0
@@data[@id.to_i][:status] = @status
return self
end
end