def get(self, pduip, tower, outlet): if g.user.username == 'root': pdu = PduDetails.query.filter_by(ip=pduip).first() if pdu is None: return {'Error': 'pdu doesn"t exist'} else: pdu_access_string = pdu.access_string else: pdu = PduDetails.query.filter_by(ip=pduip).first() if pdu is None: return {'Error': 'pdu doesn"t exist'} relation = UserPdus.query.filter_by(userid=g.user.id, pduid=pdu.id).first() if relation is None: return {'message': 'User does not have neccesary permission'} pdu_access_string = pdu.access_string pdu = Pdu_obj(pduip, 161, pdu_access_string) state = pdu.get_outlet_status(tower, outlet) amperage = pdu.get_outlet_amperage(tower, outlet) if state == 'Error': state = 'Unable to fetch data' if amperage == 'Error': amperage = 'unable to fetch amperage' return {'state': state, 'amperage': amperage}
def get(self, outletid): if g.user.username == 'root': outlet = outlet_details(outletid) else: role = check_outlet_permission(g.user.id, outletid) if role is False: return {'message': 'User does not have neccesary permission'} else: outlet = outlet_details(outletid) pdu = Pdu_obj(outlet[0], 161, outlet[1]) state = pdu.get_outlet_status(outlet[2], outlet[3]) amperage = pdu.get_outlet_amperage(outlet[2], outlet[3]) states = [] if state == 'Error': states.append("unable to get data") else: states.append(state) if amperage == 'Error': amperage = 'unable to fetch data' else: amperage = amperage state_dict = {} state_dict[str(outlet[0])+" "+str(outlet[2])+" "+str(outlet[3])] = states[0] state_dict['amperage'] = amperage return {'Status': state_dict}
def get(self, groupid): if g.user.username == 'root': outlets = query_group(groupid) else: role = UserOutletsGroups.query.filter_by(userid=g.user.id, outletgroupid=groupid).first() if role is None: return {'message': 'User does not have necessary permission'} else: outlets = query_group(groupid) states = [] amperages = [] for outlet in outlets: pdu = Pdu_obj(outlet[0], 161, outlet[1]) state = pdu.get_outlet_status(outlet[2], outlet[3]) amperage = pdu.get_outlet_amperage(outlet[2], outlet[3]) if state == 'Error': states.append("unable to get data") else: states.append(state) if amperage == 'Error': amperages.append('unable to get data') else: amperages.append(amperage) state_dict = {} for i in range(len(outlets)): state_dict[str(outlets[i][0])+" "+str(outlets[i][2])+" "+str(outlets[i][3])] = states[i] amperage_dict = {} for i in range(len(outlets)): amperage_dict[str(outlets[i][0])+" "+str(outlets[i][2])+" "+str(outlets[i][3])] = amperages[i] return {'Status': state_dict, 'amperages': amperage_dict}