-
Notifications
You must be signed in to change notification settings - Fork 4
/
devices.py
267 lines (235 loc) · 7.94 KB
/
devices.py
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# -*- coding: utf-8 -*-
import math
import pygame
from sqlManage import SQLManagement
class device:
typeStrToNum = {
"lamp" : 0,
"airCondition" : 1,
"TV" : 2,
"computer" : 3,
"charger" : 4,
"heater" : 5,
"door" : 6,
"window" : 7,
"other" : 8
}
# typeNumToStr = {
# 0 : "lamp",
# 1 : "airCondition",
# 2 : "TV",
# 3 : "computer",
# 4 : "charger",
# 5 : "heater",
# 6 : "door",
# 7 : "window",
# 8 : "other"
# }
def __init__(self, code = 0, deviceName = 'None', deviceType = 'other', statu = 0, value = -1, posX = 0, posY = 0, ID = -1):
self.code = code
self.name = deviceName
self.deviceType = deviceType
self.statu = statu
self.value = value
self.posX = posX
self.posY = posY
self.ID = -1
self.imageOn = pygame.image.load('./pic/' + self.getType() + '_on.png')
self.imageOff = pygame.image.load('./pic/' + self.getType() + '_off.png')
def getCode(self):
return self.code
def setCode(self, code):
self.code = code
def setID(self, ID):
self.ID = ID
def getID(self):
return self.ID
def getName(self):
return self.name
def setName(self, deviceName):
self.name = deviceName
def getStatu(self):
if(self.statu == 0):
return False
else:
return True
def turnOn(self):
self.statu = 1
# print self.name + ' has been turn on'
def turnOff(self):
self.statu = 0
# print self.name + ' has been turn off'
def getValue(self):
return self.value
def setValue(self, value):
self.value = value
def getType(self):
return self.deviceType
def setType(self, Type):
if(self.typeStrToNum.get(Type) == 'None'):
self.deviceType = 'other'
else:
self.deviceType = Type
def setPos(self, posX, posY):
self.posX = posX
self.posY = posY
def getPosX(self):
return self.posX
def getPosY(self):
return self.posY
def getImage(self):
if(self.getStatu()):
return self.imageOn
else:
return self.imageOff
def calDistance(self, posX, posY):
dx = self.posX - posX
dy = self.posY - posY
dis = math.sqrt(dx*dx + dy*dy)
return dis
def setRandomDevicePos(self, roomLeft, roomRight, roomTop, roomBottom):
import random
if(self.deviceType == 'lamp'):
tempPosX = int((roomLeft+roomRight)/2)
tempPosY = int((roomTop+roomBottom)/2)
self.setPos(tempPosX, tempPosY)
# print 'lamp', self.getPosX(), self.getPosY()
if(self.deviceType == 'airCondition'):
direction = random.randint(1, 4)
if(direction == 1):
tempPosX = roomLeft
tempPosY = random.randint(roomTop, roomBottom)
if(direction == 2):
tempPosX = roomRight
tempPosY = random.randint(roomTop, roomBottom)
if(direction == 3):
tempPosY = roomTop
tempPosX = random.randint(roomLeft, roomRight)
if(direction == 4):
tempPosY = roomBottom
tempPosX = random.randint(roomLeft, roomRight)
self.setPos(tempPosX, tempPosY)
# print 'airCondition', self.getPosX(), self.getPosY()
if(self.deviceType == 'TV'):
direction = random.randint(1, 4)
if(direction == 1):
tempPosX = roomLeft
tempPosY = random.randint(roomTop, roomBottom)
if(direction == 2):
tempPosX = roomRight
tempPosY = random.randint(roomTop, roomBottom)
if(direction == 3):
tempPosY = roomTop
tempPosX = random.randint(roomLeft, roomRight)
if(direction == 4):
tempPosY = roomBottom
tempPosX = random.randint(roomLeft, roomRight)
self.setPos(tempPosX, tempPosY)
# print 'TV', self.getPosX(), self.getPosY()
if(self.deviceType == 'computer'):
tempPosX = random.randint(roomLeft, roomRight)
tempPosY = random.randint(roomTop, roomBottom)
self.setPos(tempPosX, tempPosY)
# print 'computer', self.getPosX(), self.getPosY()
if(self.deviceType == 'charger'):
tempPosX = random.randint(roomLeft, roomRight)
tempPosY = random.randint(roomTop, roomBottom)
self.setPos(tempPosX, tempPosY)
# print 'charger', self.getPosX(), self.getPosY()
if(self.deviceType == 'heater'):
tempPosX = random.randint(roomLeft, roomRight)
tempPosY = random.randint(roomTop, roomBottom)
self.setPos(tempPosX, tempPosY)
# print 'heater', self.getPosX(), self.getPosY()
if(self.deviceType == 'door'):
direction = random.randint(1, 4)
if(direction == 1):
tempPosX = roomLeft
tempPosY = random.randint(roomTop, roomBottom)
if(direction == 2):
tempPosX = roomRight
tempPosY = random.randint(roomTop, roomBottom)
if(direction == 3):
tempPosY = roomTop
tempPosX = random.randint(roomLeft, roomRight)
if(direction == 4):
tempPosY = roomBottom
tempPosX = random.randint(roomLeft, roomRight)
self.setPos(tempPosX, tempPosY)
# print 'door', self.getPosX(), self.getPosY()
if(self.deviceType == 'window'):
direction = random.randint(1, 4)
tempPosX = int( (roomLeft+roomRight)/2 )
tempPosY = int( (roomTop+roomBottom)/2 )
if(direction == 1):
tempPosX = roomLeft
tempPosY = random.randint(roomTop, roomBottom)
if(direction == 2):
tempPosX = roomRight
tempPosY = random.randint(roomTop, roomBottom)
if(direction == 3):
tempPosY = roomTop
tempPosX = random.randint(roomLeft, roomRight)
if(direction == 4):
tempPosY = roomBottom
tempPosX = random.randint(roomLeft, roomRight)
self.setPos(tempPosX, tempPosY)
# print 'window', self.getPosX(), self.getPosY()
if(self.deviceType == 'other'):
tempPosX = random.randint(roomLeft, roomRight)
tempPosY = random.randint(roomTop, roomBottom)
self.setPos(tempPosX, tempPosY)
# print 'other: ', self.getPosX(), self.getPosY()
def getTypeAbbr(self):
if(self.deviceType == 'computer'):
return 'P'
return self.deviceType[0]
# 从数据库中读取数据(待验证)
def readInfoFromDB(self, ID = None, sqlMana = None):
if(ID == None or sqlMana == None):
return False
else:
# 读数据(待验证)
sql = 'select * from device where ID = %d' %ID
res = sqlMana.query(sql)
if(len(res) < 1):
return False
else:
temp = res[0]
self.ID = temp.get("ID")
self.deviceType = temp.get("deviceType")
self.name = str(temp.get("name"))
self.statu = temp.get("statu")
self.value = temp.get("value")
self.posX = temp.get("posX")
self.posY = temp.get("posY")
self.code = temp.get("code")
self.imageOn = pygame.image.load('./pic/' + self.getType() + '_on.png')
self.imageOff = pygame.image.load('./pic/' + self.getType() + '_off.png')
return True
# 向数据库中存储该设备信息
def saveInfoToDB(self, roomID = None, sqlMana = None):
if(roomID == None or sqlMana == None):
return False
else:
# 存数据
sql = 'select * from device where ID = %d' %self.ID
res = sqlMana.query(sql)
if(len(res) > 0):
sql = "update device set roomID= %d, deviceType = '%s', name = '%s', statu = %d, value = %lf, posX = %lf, posY = %lf, code = %d where ID = %d" %(roomID, self.deviceType, self.name, self.statu, self.value, self.posX, self.posY, self.code, self.ID)
sqlMana.update(sql)
else:
sql = "insert into device (roomID, deviceType, name, statu, value, posX, posY, code) values (%d, '%s', '%s', %d, %lf, %lf, %lf, %d)" %(roomID, self.deviceType, self.name, self.statu, self.value, self.posX, self.posY, self.code)
sqlMana.insert(sql)
temp = sqlMana.query('select @@identity as newID')
self.ID = temp[0].get('newID')
return True
# 获取设备信息,用于保存记录
def getInfo(self):
tempStr = 'device_%03d_' %self.code
res = {
tempStr + 'ID' : int(self.ID),
tempStr + 'statu' : int(self.statu),
tempStr + 'value' : float(self.value)
}
return res