Project: IotCenter Author: panjanek File: database.py [(MIT License)](https://github.com/panjanek/IotCenter) [View Source Project](https://github.com/panjanek/IotCenter/tree/master/manager/database.py)

def getDevicesData(self):
with self.lock:
conn = sqlite3.connect(self.dbFile)
c = conn.cursor()
devicesData = {}
for row in c.execute(“SELECT devices.id, devices.hex, max(readings.time) FROM devices JOIN readings ON devices.id = readings.deviceId GROUP BY devices.id, devices.hex”):
did = row[0]
devDict = {“deviceId”:str(row[1].encode(‘UTF-8’)), “lastContact”:row[2]}
devDict[“values”] = {}
c2 = conn.cursor()
for sensor in c2.execute(“SELECT sensors.name, readings.numberValue, addresses.ip, addresses.protocol FROM readings JOIN sensors ON readings.sensorId = sensors.id JOIN addresses ON readings.addressId = addresses.id WHERE readings.time=? AND deviceId=?”, (devDict[“lastContact”],did)):
devDict[“address”] = sensor[2]
devDict[“protocol”] = sensor[3]
devDict[“values”][sensor[0]] = sensor[1]
devicesData[devDict[“deviceId”]] = devDict
conn.close()
return devicesData

  • SQL
    260 引用
  • python

    Python (发音:[ paiθ(ə)n; (US) paiθɔn ]n. 蟒蛇,巨蛇 ),是一种面向对象的解释性的计算机程序设计语言,也是一种功能强大而完善的通用型语言,已经具有十多年的发…

    7952 引用 • 22 回帖 • 2 关注
感谢    赞同    分享    收藏    关注    反对    举报    ...