def getCloudEnes(list,rtc,secrets): url = "http://ec2-34-245-7-230.eu-west-1.compute.amazonaws.com:8086/query?db=newtest&u="+secrets[0]+"&p="+secrets[1]+"&pretty=true" for thing in list: timeNow = rtc.now() year = timeNow[0] month = timeNow[1] day = timeNow[2] hour = timeNow[3] curTime = [year,month,day,hour] name = thing.getName() query = '&q=SELECT+last(totalEne)+FROM+"Measurement"+WHERE+"id"=\''+str(thing.getID())+"\'" #print(query) try: resp = urequests.get(url+query) #print(resp.status_code) jsonOut = resp.json() resp.close() #print(jsonOut) #print("Aika:",jsonOut["results"][0]["series"][0]["values"][0][0]) #print("Arvo:",jsonOut["results"][0]["series"][0]["values"][0][1]) lastTime = parseStringToTime(jsonOut["results"][0]["series"][0]["values"][0][0]) if lastTime[0]==curTime[0] and lastTime[1]==curTime[1] and lastTime[2]==curTime[2] and lastTime[3]==curTime[3]: ene = jsonOut["results"][0]["series"][0]["values"][0][1] thing.setCurHourEne(ene) setMeasTime(measTime,curTime) except: print("Query failed.") pass
def openPhases(phases): resp = urequests.get('http://salty-mountain-85076.herokuapp.com/api/phases') phasesData = resp.json() for phase in phasesData: newPhase = mainPhase(phase['name'],phase['id'],phase['commandBits'],int(phase['maxCurrent'])) phases.append(newPhase) return resp
def openLoads(loads): resp = urequests.get('http://salty-mountain-85076.herokuapp.com/api/loads') loadData = resp.json() for nuload in loadData: newLoad = load(nuload['name'],nuload['id'],nuload['commandBits'],nuload['relayPin'],int(nuload['maxCurrent']),int(nuload['phase']), int(nuload['priority'])) loads.append(newLoad) return resp
def cloudThread(threadLoads,threadPhases): global maxHour global startto while True: #controllimuuttujien hakeminen if startto: try: #if True: url = 'http://salty-mountain-85076.herokuapp.com/api/loads' resp = urequests.get(url) threadData = resp.json() resp.close() for threadUnit in threadData: for threadLoad in threadLoads: if threadLoad.getID()==threadUnit['id']: threadVal = int(threadUnit['contValue']) threadPrio = int(threadUnit['priority']) threadLoad.setPriority(threadPrio) if threadVal==1: threadLoad.relayManualOpen() elif threadVal==0: threadLoad.relayManualClose() break; except: print("Error getting manualCont values.") pass #maksimi tuntitehon hakeminen try: url = 'http://salty-mountain-85076.herokuapp.com/api/phases' resp = urequests.get(url) threadData = resp.json() resp.close() for threadUnit in threadData: for threadPhase in threadPhases: if threadPhase.getID()==threadUnit['id']: threadVal = int(threadUnit['phaseMax']) maxHour = threadVal break; except: print("Error getting max hourpower values.") pass sleep(1)
def _get_request(url: str, headers: dict) -> (int, bytes): """ Send a http get request to the backend. :param url: the backend service URL :param headers: the headers for the request :return: the backend response status code, the backend response content (body) """ r = requests.get(url=url, headers=headers) status_code = r.status_code content = r.content r.close() return status_code, content
def enrollStation(self): if (len( [item for item in os.listdir("/flash") if "enroll.state" == item]) > 0): enrollState = open("../enroll.state", "r") self.enrollUUID = enrollState.read() enrollState.close() req = requests.get( "http://%s/lib/enroll_device.php?verify_uuid=%s" % (self.host, self.enrollUUID)) if (req.text.rstrip("\n") == self.stationName): print("[I] [serverConnector] Enrolment status: OK") self.enrollment = True else: print("[E] [serverConnector] Enrolment status: INVALID") print("[E] [serverConnector] Removing Enrolment File...") os.remove('../enroll.state') self.enrollStation() else: print("[I] [serverConnector] Enrolment status: Not Enrolled!") print("[I] [serverConnector] Enrolling Device...") j = ujson.loads(open("../settings.json").read()) r = requests.post("http://%s/lib/enroll_device.php" % self.host, json=j) if r.text != "": enrollState = open("../enroll.state", "w+") enrollState.write(r.text.rstrip("\n")) enrollState.close() print("[I] [serverConnector] UUID: [" + open("../enroll.state").read() + "]") print("[I] [serverConnector] Enrollment complete !") self.enrollment = True else: print( "[E] [serverConnector] Enrollment Failed (No Response from RPI)" )
def getCloudMaxHourPower(secrets,thing): ene = 0 try: url = "http://ec2-34-245-7-230.eu-west-1.compute.amazonaws.com:8086/query?db=newtest&u="+secrets[0]+"&p="+secrets[1]+"&pretty=true" query = '&q=SELECT+max(totalEne)+FROM+"Measurement"+WHERE+"id"=\''+str(thing.getID())+'\'+AND+time>=now()-365d' resp = urequests.get(url+query) #print(resp.status_code) jsonOut = resp.json() resp.close() #print(jsonOut) ene = jsonOut["results"][0]["series"][0]["values"][0][1] newtime = parseStringToTime(jsonOut["results"][0]["series"][0]["values"][0][0]) print("") #tällä hetkellä lisää 3h koska utc, mutta ei pysy samana koska kesä- ja talviaika print("The greatest hourly power in the last 12 months was: "+str(ene)+", and this hour was in: "+str(newtime[2])+"."+str(newtime[1])+"."+str(newtime[0])+" "+str(newtime[3]+3)+"-"+str(newtime[3]+1+3)) except: pass print("") return ene
def _get_device_information(self): """" Gets the device information. """ return requests.get(self.endpoint, headers={})
def download(self): """ Downloads the Amazon Root Certificate """ return requests.get(self.root_ca_url).content
def getTime(self): r = requests.get( "http://%s/lib/be_connections.php?operation=timestamp" % self.host) currentTime = r.text.rstrip("\n") r.close() return (currentTime)