def getCurrentDevices(req): if not crab.checkToken(req): return crab.responseMsg(0, "Your identity is not identified!") try: sql = "SELECT * FROM device WHERE user_id={}".format(crab.getUserId()) res = devicePojo(crab.sqlExe(sql)) return crab.responseDate(0, res) except: return crab.responseMsg(1, "Get device information failure!")
def delDevice(req): if not crab.checkToken(req): return crab.responseMsg(0, "Your identity is not identified!") values = req.get_json() device_id = int(values['device_id']) try: crab.sqlExe("DELETE FROM device WHERE device_id={}".format(device_id)) return crab.responseMsg(0, "Delete device information successfully") except: return crab.responseMsg(1, "Delete device information failure!")
def delSensor(req): if not crab.checkToken(req): return crab.responseMsg(0, "Your identity is not identified!") values = req.get_json() sensor_id = int(values['sensor_id']) try: crab.sqlExe("DELETE FROM sensor WHERE sensor_id={}".format(sensor_id)) return crab.responseMsg(0, "Delete sensor information successfully") except: return crab.responseMsg(1, "Delete sensor information failure!")
def logout(req): token = req.headers["Authorization"] if not crab.checkToken(token): return crab.responseMsg(1, "Your token had overtime!") try: newToken = crab.getRandomStr() sqll = "UPDATE user SET token=\"{}\" WHERE token=\"{}\"".format( newToken, token) crab.sqlExe(sqll) return crab.responseMsg(0, "Logout successfully!") except: return crab.responseMsg(1, "Logout failure!")
def addDevice(req): if not crab.checkToken(req): return crab.responseMsg(0, "Your identity is not identified!") userId = crab.getUserId() values = req.get_json() try: sql="INSERT INTO device(device_name,device_type,place,is_control,is_public,user_id) VALUES("\ "\"{}\",\"{}\",\"{}\",{},{},{})".format(values['device_name'],values['device_type'],values['place'], values['is_control'],values['is_public'],userId,) crab.sqlExe(sql) return crab.responseMsg(0, "Add device successfully!") except: return crab.responseDate(1, "Add device missing field,pleass cheking!")
def getDeviceAndsensorList(req): if not crab.checkToken(req): return crab.responseMsg(0, "Your identity is not identified!") sql = "SELECT * FROM device,sensor WHERE device.device_id=sensor.device_id" try: values = req.get_json() sql += crab.getByConditions(values) except: sql = sql try: res = deviceSensorPojo(crab.sqlExe(sql)) return crab.responseDate(0, res) except: return crab.responseMsg(1, "Get device information failure!")
def getDevices(req): if not crab.checkToken(req): return crab.responseMsg(0, "Your identity is not identified!") sql = "SELECT * FROM device" try: values = req.get_json() sql += (" WHERE" + crab.getByConditions(values)) except: sql = sql try: res = devicePojo(crab.sqlExe(sql)) return crab.responseDate(0, res) except: return crab.responseMsg(1, "Get device information failure!")
def getSensorAndDataList(req): if not crab.checkToken(req): return crab.responseMsg(0, "Your identity is not identified!") sql = "SELECT * FROM sensor,monitor WHERE sensor.sensor_id=monitor.sensor_id" try: values = req.get_json() sql += crab.getByConditions(values) except: sql = sql try: res = sensorPojo(crab.sqlExe(sql)) return crab.responseDate(0, res) except: return crab.responseMsg(1, "Get sensor and data information failure!")
def addSensor(req): if not crab.checkToken(req): return crab.responseMsg(0, "Your identity is not identified!") userId = crab.getUserId() values = req.get_json() try: sql="INSERT INTO sensor(device_id,identification,sensor_name,sensor_type,unit,value_max,value_min," \ "is_warn,sort) VALUES("\ "{},\"{}\",\"{}\",\"{}\",\"{}\",{},{},{},{})".format(values['device_id'],values['identification'], values['sensor_name'],values['sensor_type'],values['unit'],values['value_max'], values['value_min'],values['is_warn'],values['sort']) crab.sqlExe(sql) return crab.responseMsg(0, "Add sensor successfully!") except: return crab.responseDate(1, "Add sensor missing field,pleass cheking!")
def editDevice(req): if not crab.checkToken(req): return crab.responseMsg(0, "Your identity is not identified!") values = req.get_json() device_id = values['device_id'] res = devicePojo( crab.sqlExe( "SELECT * FROM device WHERE device_id=\"{}\"".format(device_id))) if len(res) == 0: return crab.responseMsg(1, "Hava not this deviceId!") else: try: res = res[0] for i in values: res[i] = values[i] sql="UPDATE device SET device_name=\"{}\", device_type=\"{}\", place=\"{}\", is_control={}, is_public={} WHERE " \ "device_id={}".format(res['device_name'],res['device_type'],res['place'],res['is_control'],res['is_public'],res['device_id']) crab.sqlExe(sql) return crab.responseMsg(0, "Edit device information successfully!") except: return crab.responseMsg(1, "Edit device information failure!")
def editSensor(req): if not crab.checkToken(req): return crab.responseMsg(0, "Your identity is not identified!") values = req.get_json() sensor_id = values['sensor_id'] res = sensorPojo( crab.sqlExe( "SELECT * FROM sensor WHERE sensor_id=\"{}\"".format(sensor_id))) if len(res) == 0: return crab.responseMsg(1, "Have not this sensorId!") else: try: res = res[0] for i in values: res[i] = values[i] sql="UPDATE sensor SET device_id={}, identification=\"{}\", sensor_name=\"{}\", sensor_type=\"{}\"," \ " unit=\"{}\",value_max={},value_min={},is_warn={},sort={} WHERE " \ "sensor_id={}".format(res['device_id'],res['identification'], res['sensor_name'],res['sensor_type'],res['unit'],res['value_max'], res['value_min'],res['is_warn'],res['sort'],sensor_id) crab.sqlExe(sql) return crab.responseMsg(0, "Edit sensor information successfully!") except: return crab.responseMsg(1, "Edit sensor information failure!")