def get(self, owner_id): #get the list of devices owned by a particular user if not (session.get('logged_in') == True): return jsonify({ 'msg': "Session Expired. Pls login again.", 'result': False }) elif (session.get('logged_in') == True and session.get('user_id') != str(owner_id)): return jsonify({ 'msg': "Unauthorized Access Attempt", 'result': False }) devicelist = Device.getDevicesByOwner(owner_id) if (isinstance(devicelist, list)): devicelistofdicts = [device.toDict() for device in devicelist] response = {} response['devicelist'] = devicelistofdicts response['result'] = True return jsonify(response) else: response = {} response['msg'] = devicelist response['result'] = False return jsonify(response)
def get(self, owner_id): devicelist = Device.getDevicesByOwner(owner_id) if (isinstance(devicelist, str)): response = {} response['msg'] = devicelist response['result'] = False return jsonify(response) deviceidlist = [ device.getid() for device in devicelist if device.getstate() == True ] if (deviceidlist == []): response = {} response['msg'] = "No Devices with ongoing Sessions found." response['result'] = False return jsonify(response) devicedatalist = {} for deviceid in deviceidlist: dbkey = "device:" + deviceid dbval = self.db.get(dbkey) if (dbval != None): devicedatalist[deviceid] = literal_eval(dbval) response = {} response['devicedatalist'] = devicedatalist response['result'] = True return jsonify(response)
def delete(self, device_id): d = Device.getDeviceById(device_id) if not (isinstance(d, Device)): response = {} response['msg'] = d response['result'] = False return jsonify(response) r = Device.deleteDeviceById(device_id) if (r == False): response = {} response['msg'] = "Device Deletion Unsuccessful." response['result'] = False return jsonify(response) else: response = {} response['msg'] = "Device deleted successfully." response['result'] = True return jsonify(response)
def get(self, device_id): d = Device.getDeviceById(device_id) if (isinstance(d, Device)): response = d.toDict() response['result'] = True return jsonify(response) else: response = {} response['msg'] = d response['result'] = True return jsonify(response)
def post(self, device_id): post_data = request.get_json() d = Device.getDeviceById(device_id) if not (isinstance(d, Device)): response = {} response['msg'] = d response['result'] = False return jsonify(response) r = Device.updateDeviceStateById(device_id, post_data.get('state')) if (r == False): response = {} response['msg'] = "Device State Update Unsuccessful." response['result'] = False return jsonify(response) else: response = {} response['msg'] = "Device State Updated Successfully." response['result'] = True return jsonify(response)
def post(self, device_id): post_data = request.get_json() d = Device.getDeviceById(device_id) if not (isinstance(d, Device)): response = {} response['msg'] = d response['result'] = False return jsonify(response) d.setname(post_data.get('name')) r = d.updateDevice() if (isinstance(r, str)): response = {} response['msg'] = r response['result'] = False return jsonify(response) else: response = {} response['msg'] = "Device Updated Successfully." response['result'] = True return jsonify(response)
def post(self, owner_id): #adds a new device post_data = request.get_json() d = Device() d.setname(post_data.get('name')) d.setowner(owner_id) d.generateid() d.generateauthtoken() r = d.addDevice() if (isinstance(r, str)): response = {} response['msg'] = r response['result'] = False return jsonify(response) else: response = {} response['msg'] = "Device Successfully Added." response['result'] = True return jsonify(response)
def getBill(self, user_id): us = UserSetting.getSetting(user_id, 'lastbilldate') us_dict = us.toDict() lastbilldate = us_dict['value'] print(lastbilldate) rows = Session.getEnergyConsumedPerDeviceByOwner( user_id, lastbilldate=lastbilldate) if (isinstance(rows, list)): totalenergyc_completedsessions = self.calcTotalEnergyConsumption( rows) else: totalenergyc_completedsessions = 0.0 devicelist = Device.getDevicesByOwner(user_id) if (isinstance(devicelist, list)): activedevicelist = [ device for device in devicelist if device.getstate() == True ] if (activedevicelist == []): totalenergyc_ongoingsessions = 0.0 else: totalenergyc_ongoingsessions = 0.0 for device in activedevicelist: dbkey = "device:" + device.getid() dbval = self.db.get(dbkey) if (dbval != None): devicertdata = literal_eval(dbval) totalenergyc_ongoingsessions += devicertdata['energyc'] found = False for i in range(len(rows)): if (rows[i]['id'] == device.getid()): rows[i]['rtenergyc'] = devicertdata['energyc'] found = True break if (found == False): d = {} d['id'] = device.getid() d['name'] = device.getname() d['rtenergyc'] = devicertdata['energyc'] rows.append(d) else: totalenergyc_ongoingsessions = 0.0 print(rows) totalenergyc = totalenergyc_completedsessions + totalenergyc_ongoingsessions d = {} d['totalenergyc'] = totalenergyc d['totalenergyc_completedsessions'] = totalenergyc_completedsessions d['totalenergyc_ongoingsessions'] = totalenergyc_ongoingsessions d['devicelist'] = rows return d
def get(self): if (request.args.get('user') != None): user_id = request.args.get('user') elif (request.args.get('device') != None): deviceid = request.args.get('device') device = Device.getDeviceById(deviceid) user_id = device.getowner() else: return jsonify({'result': False}) #realtimebill=RealtimeBillAPI().getBill(user_id) #lastbilldate=UserSetting.getSetting(user_id,'lastbilldate') lastbilldate_updated = UserSetting.updateLastBillDate(user_id) if (lastbilldate_updated == True): #session energyc split #realtime bill -> stored bill pass realtimebill = RealtimeBillAPI().getBill(user_id) energyc_now = realtimebill['totalenergyc'] energyc_quota = float( UserSetting.getSetting(user_id, 'energyc_quota').toDict()['value']) billingcycle = UserSetting.getSetting(user_id, 'billingcycle').toDict()['value'] billstartdate, billenddate = UserSetting.getBillingPeriodDates( user_id, billingcycle) notification_generated = False if (energyc_now > energyc_quota): alreadynotified = False billingcycle = UserSetting.getSetting( user_id, 'billingcycle').toDict()['value'] billstartdate, billenddate = UserSetting.getBillingPeriodDates( user_id, billingcycle) user_notifications = Notification.getNotificationsByUserIdAndDates( user_id, billstartdate, billenddate) if (isinstance(user_notifications, list)): user_notifications_d = [n.toDict() for n in user_notifications] for n in user_notifications_d: if (n['type'] == 'ENERGYC_QUOTA_EXCEEDED'): alreadynotified = True if (alreadynotified == False): new_notification = Notification() new_notification.setuserid(user_id) new_notification.settype('ENERGYC_QUOTA_EXCEEDED') n_msg = "Your devices have exceeded the energy consumption quota for the billing period \ " + billstartdate + " to " + billenddate + "." new_notification.setmsg(n_msg) notification_generated = new_notification.addNotification() response = {} response['lastbilldate_updated'] = lastbilldate_updated response['notification_generated'] = notification_generated response['billstartdate'] = billstartdate response['billenddate'] = billenddate response['energyc_quota'] = energyc_quota response['result'] = True return jsonify(response)