def post(self, device_id):

        s = Session(device_id)
        session_id = s.addSession()
        if (isinstance(session_id, int)):
            response = {}
            response['result'] = True
            response['sid'] = session_id
            return jsonify(response)
        else:
            response = {}
            response['result'] = False
            return jsonify(response)
    def delete(self, device_id):
        s = Session.getSessionsByDevice(device_id)
        if not (isinstance(s, list)):
            response = {}
            response['msg'] = s
            response['result'] = False
            return jsonify(response)

        r = Session.deleteSessionsByDevice(device_id)
        if (r == False):
            response = {}
            response['msg'] = "Sessions Deletion Unsuccessful."
            response['result'] = False
            return jsonify(response)
        else:
            response = {}
            response['msg'] = "Sessions deleted successfully."
            response['result'] = True
            return jsonify(response)
 def get(self, session_id):
     s = Session.getSessionById(session_id)
     if (isinstance(s, Session)):
         response = s.toDict()
         response['result'] = True
         return jsonify(response)
     else:
         response = {}
         response['msg'] = s
         response['result'] = True
         return jsonify(response)
    def delete(self, session_id):

        s = Session.getSessionById(session_id)
        if not (isinstance(s, Session)):
            response = {}
            response['msg'] = s
            response['result'] = False
            return jsonify(response)

        r = Session.deleteSessionById(session_id)
        if (r == False):
            response = {}
            response['msg'] = "Session Deletion Unsuccessful."
            response['result'] = False
            return jsonify(response)
        else:
            response = {}
            response['msg'] = "Session deleted successfully."
            response['result'] = True
            return jsonify(response)
    def get(self, device_id):

        sessionlist = Session.getSessionsByDevice(device_id)
        if (isinstance(sessionlist, list)):
            sessionlistofdicts = [session.toDict() for session in sessionlist]
            response = {}
            response['sessionlist'] = sessionlistofdicts
            response['result'] = True
            return jsonify(response)
        else:
            response = {}
            response['msg'] = sessionlist
            response['result'] = False
            return jsonify(response)
    def post(self, session_id):

        post_data = request.get_json()
        s = Session()
        s.setid(session_id)
        s.setenergyconsumed(post_data.get('energyc'))
        r = s.updateSession()
        if (r == False):
            response = {}
            response['result'] = False
            return jsonify(response)
        else:
            response = {}
            response['result'] = True
            return jsonify(response)
    def get(self, user_id):

        startdate = request.args.get('startdate')
        enddate = request.args.get('enddate')

        r = Session.getEnergyConsumedPerDeviceByOwner(user_id, startdate,
                                                      enddate)
        response = {}
        if (isinstance(r, list)):
            response['devicelist'] = r
            response['totalenergyc'] = self.calcTotalEnergyConsumption(r)
            response['result'] = True
            return jsonify(response)
        else:
            response['msg'] = r
            response['result'] = False
            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