forked from mb003/HomeTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
houses.py
359 lines (316 loc) · 11.2 KB
/
houses.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# -*- coding: utf-8 -*-
import time
import random
from rooms import room
from sqlManage import SQLManagement
from temperature import temperatureSimulator
class house:
# roomDict = {
# "livingRoom" : 0,
# "masterBedroom" : 100,
# "secondBedroom" : 200,
# "thirdBedroom" : 300,
# "bathroom" : 400,
# "kitchen" : 500,
# "diningRoom" : 600,
# "studyRoom" : 700
# }
# 初始化函数
def __init__(self, ID = -1, roomList = [], width = 30, height = 20,currentTime = time.mktime(time.strptime("2015 1 1 00:00:00", "%Y %m %d %H:%M:%S"))):
print('in __init__ of house')
self.ID = ID
self.width = width
self.height = height
self.roomList = []
self.createHouseStructure(roomList)
self.initRoomAndDevice()
# 时间
self.currentTime = currentTime
self.simT = temperatureSimulator(self.currentTime)
#获取温度
def getSimT(self):
return self.simT
def iterateT(self, timeSlot):
self.simT.iterate(self.currentTime, timeSlot)
# 时间迭代器迭代一次
def iterateTime(self, timeSlot):
self.currentTime = self.currentTime + timeSlot
#获取当前时间
def getCurrentTime(self):
return self.currentTime
#格式化获取当前时间
def getCurrentTimeStr(self):
return time.strftime("%H:%M:%S", time.localtime(self.getCurrentTime()))
#设置当前时间
def setCurrentTime(self, currentTime):
self.currentTime = currentTime
def getID(self):
return self.ID
def getWidth(self):
return self.width
def getHeight(self):
return self.height
def getRoomList(self):
return self.roomList
# 生成房间结构
def createHouseStructure(self, roomList):
print('in createHouseStructure')
self.roomList = []
roomNum = len(roomList)
i=0
for roomName in roomList:
tempPosX = self.width//(roomNum)
tempPosY = self.height
newRoom = room(-1, roomName, i*tempPosX, (i+1)*tempPosX,0, tempPosY)#, 0)
i+=1
self.roomList.append(newRoom)
# print('end of seed create')
# =============================================================================
# endFlag = False
# while(not endFlag):
# endFlag = True
# for tempRoom in self.roomList:
# # right add
# flag = True
# newRight = tempRoom.getRight() + 1
# if(newRight >= self.width):
# flag = False
# for tempY in range(tempRoom.getTop(), tempRoom.getBottom() + 1):
# for existRoom in self.roomList:
# if(existRoom.isInRoom(newRight,tempY)):
# flag = False
# break;
# if(newRight-tempRoom.getLeft()>self.width/3):
# flag = False
# break;
# if(flag == False):
# break;
# if(flag):
# tempRoom.setRight(newRight)
# endFlag = False
# # bottom add
# flag = True
# newBottom = tempRoom.getBottom() + 1
# if(newBottom >= self.height):
# flag = False
# for tempX in range(tempRoom.getLeft(), tempRoom.getRight()+1):
# for existRoom in self.roomList:
# if(existRoom.isInRoom(tempX, newBottom)):
# flag = False
# break;
# if(newBottom - tempRoom.getTop() > self.height/3):
# flag = False
# break;
# if(flag == False):
# break;
# if(flag):
# tempRoom.setBottom(newBottom)
# endFlag = False
# # print('loop')
#
# flag = True
# for tempRoom in self.roomList:
# if ( tempRoom.getSquare() < (self.height * self.width)/roomNum/4):
# flag = False
# if (not flag):
# self.createHouseStructure(roomList)
# =============================================================================
# 为各房间添加设备
def initRoomAndDevice(self):
for tempRoom in self.roomList:
tempRoom.initDeviceList()
# 判断是否存在某房间
def roomExist(self, roomType):
for tempRoom in self.roomList:
if(tempRoom.getType() == roomType):
return True
return False
# 在住房中添加房屋
def appendRoom(self, ID, roomType, centerPosX, centerPosY, width, height):
newRoom = room(-1, roomType, centerPosX, centerPosY, width, height)
self.roomList.append(newRoom)
return
# 打印房间信息及房间结构草图
def printHouseInfo(self):
for tempRoom in self.roomList:
tempRoom.printInfo
# print("House Map:")
houseMap = [ [' ' for col in range(self.width)] for row in range(self.height)]
for i in range(self.width):
for j in range(self.height):
count = 0
for tempRoom in self.roomList:
if ( tempRoom.isInRoom(i,j) and ( j == tempRoom.getTop() or j == tempRoom.getBottom() or i == tempRoom.getLeft() or i == tempRoom.getRight()) ):
# houseMap[j][i] = count
houseMap[j][i] = tempRoom.getType()[0]
count = count + 1
# print('Len of roomList: ', len(self.roomList))
for tempRoom in self.roomList:
# print('Room Type: ', tempRoom.getType(), len(tempRoom.deviceList))
for tempDevice in tempRoom.deviceList:
# print('Device Type: ', tempDevice.getType())
x = tempDevice.getPosX()
y = tempDevice.getPosY()
houseMap[y][x] = tempDevice.getTypeAbbr()
# for i in range(self.height):
# for j in range(self.width):
# print(houseMap[i][j],)
# print('')
return houseMap
# 开启指定房间内指定设备类型的设备,返回设备列表
def turnOnDeviceByType(self, roomType, deviceType):
for tempRoom in self.roomList:
if ( tempRoom.getType() == roomType ):
return tempRoom.turnOnDeviceByType(deviceType)
return []
# 关闭指定房间内指定设备类型的设备,返回设备列表
def turnOffDeviceByType(self, roomType, deviceType):
for tempRoom in self.roomList:
if ( tempRoom.getType() == roomType ):
return tempRoom.turnOffDeviceByType(deviceType)
return []
# 开启指定房间内指定设备名的设备,返回对应设备
def turnOnDeviceByName(self, roomType, deviceName):
for tempRoom in self.roomList:
if ( tempRoom.getType() == roomType ):
return tempRoom.turnOnDeviceByName(deviceName)
return None
# 关闭指定房间内指定设备名的设备,返回对应设备
def turnOffDeviceByName(self, roomType, deviceName):
for tempRoom in self.roomList:
if ( tempRoom.getType() == roomType ):
return tempRoom.turnOffDeviceByName(deviceName)
return None
# 设置指定房间内指定设备类型的设备的值,返回设备列表
def setDeviceValueByType(self, roomType, deviceType, value):
for tempRoom in self.roomList:
if ( tempRoom.getType() == roomType ):
return tempRoom.setDeviceValueByType(deviceType, value)
return []
# 获取指定坐标所处的房间
def getRoomByPos(self, posX, posY):
for tempRoom in self.roomList:
if ( tempRoom.isInRoom(posX, posY) ):
return tempRoom
return None
# 关闭指定类型的全部设备
def turnOffAllByDeviceType(self, deviceType, exceptRoomTypeList = []):
for tempRoom in self.roomList:
flag = True
for exceptRoomType in exceptRoomTypeList:
if tempRoom.getType() == exceptRoomType:
flag = False
if (flag):
tempRoom.turnOffDeviceByType(deviceType)
# 打开属于某个人的指定类型的设备
def turnOnAllByDeviceTypeAndOnwner(self, ManID, deviceType):
for tempRoom in self.roomList:
tempRoom.turnOnDeviceByTypeAndOwner(deviceType,ManID)
# 关闭属于某个人的指定类型的设备
def turnOffAllByDeviceTypeAndOnwner(self, ManID, deviceType):
for tempRoom in self.roomList:
tempRoom.turnOffDeviceByTypeAndOwner(deviceType,ManID)
# 开启指定房间内 某人 指定设备类型的设备,返回设备列表
def turnOnDeviceByTypeOfMan(self, roomType, deviceType, ID):
for tempRoom in self.roomList:
if ( tempRoom.getType() == roomType ):
return tempRoom.turnOnDeviceByTypeAndOwner(deviceType,ID)
return []
# 关闭指定房间内 某人 的指定设备类型的设备,返回设备列表
def turnOffDeviceByTypeOfMan(self, roomType, deviceType, ID):
for tempRoom in self.roomList:
if ( tempRoom.getType() == roomType ):
return tempRoom.turnOffDeviceByTypeAndOwner(deviceType,ID)
return []
# 获得距离最近的指定类型设备
def getNearDevice(self, posX, posY, deviceType):
import math
desDevice = None
mindis = float('inf')
for tempRoom in self.roomList:
for tempDevice in tempRoom.getDeviceList():
if (tempDevice.getType == deviceType):
dx = posX - tempDevice.getPosX()
dy = posY - tempDevice.getPosY()
dis = math.sqrt( dx*dx + dy*dy )
if (mindis > dis):
mindis = dis
desDevice = tempDevice
return desDevice
# 打开距离最近的指定类型设备,返回对应设备
def turnOnNearDevice(self, posX, posY, deviceType):
tempDevice = self.getNearDevice(posX, posY, deviceType)
if ( tempDevice == None ):
return None
else:
tempDevice.turnOn()
return tempDevice
# 关闭距离最近的指定类型设备,返回对应设备
def turnOffNearDevice(self, posX, posY, deviceType):
tempDevice = self.getNearDevice(posX, posY, deviceType)
if ( tempDevice == None ):
return None
else:
tempDevice.turnOff()
return tempDevice
# 设定距离最近的指定设备的值,返回对应设备
def setNearDeviceValue(self, posX, posY, deviceType, value):
tempDevice = self.getNearDevice(posX, posY, deviceType)
if ( tempDevice == None ):
return None
else:
tempDevice.setValue(value)
return tempDevice
# 向数据库中存储住房信息
def saveInfoToDB(self, humanID = None, sqlMana = None):
if(humanID == None or sqlMana == None):
return False
else:
flag = True
sql = 'select * from house where ID = %d' %self.ID
res = sqlMana.query(sql)
if(len(res) > 0):
sql = "update house set humanID= %d, width = %lf, height = %lf where ID = %d" %(humanID, self.width, self.height, self.ID)
sqlMana.update(sql)
else:
sql = "insert into house (humanID, width, height) values (%d, %lf, %lf)" %(humanID, self.width, self.height)
sqlMana.insert(sql)
temp = sqlMana.query('select @@identity as newID')
self.ID = temp[0].get('newID')
for tempRoom in self.roomList:
flag = tempRoom.saveInfoToDB(self.ID, sqlMana) and flag
return flag
# 从数据库中读取住房信息(待验证)
def readInfoFromDB(self, ID = None, sqlMana = None):
if( ID == None or sqlMana == None):
return False
else:
# 读数据(待验证)
sql = 'select * from house where ID = %d' %ID
res = sqlMana.query(sql)
if(len(res) < 1):
return False
else:
temp = res[0]
self.ID = int(temp.get("ID"))
self.width = float(temp.get("width"))
self.height = float(temp.get("height"))
self.roomList = []
self.initRoomListFromDB(sqlMana)
return True
# 通过数据库初始化房间列表(待验证)
def initRoomListFromDB(self, sqlMana = None):
sql = 'select ID from room where houseID = %d' %self.ID
res = sqlMana.query(sql)
for temp in res:
tempRoom = room()
tempRoom.readInfoFromDB(temp.get('ID'), sqlMana)
self.roomList.append(tempRoom)
# 获得住房全部信息,用于保存记录
def getInfo(self, currentTime):
res = dict()
for tempRoom in self.roomList:
res = dict(res.items() + tempRoom.getInfo(currentTime).items())
return res
def setCurrentTime(self, currentTime):
self.currentTime = currentTime