misc: small fix or general refactoring i did not bother commenting
This commit is contained in:
parent
d20f6c8a52
commit
68d7686a03
pwnagotchi
@ -284,12 +284,13 @@ class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer):
|
|||||||
|
|
||||||
self.run('events.clear')
|
self.run('events.clear')
|
||||||
|
|
||||||
logging.debug("event polling started ...")
|
|
||||||
while True:
|
while True:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
new_shakes = 0
|
new_shakes = 0
|
||||||
|
|
||||||
|
logging.debug("polling events ...")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
s = self.session()
|
s = self.session()
|
||||||
self._update_uptime(s)
|
self._update_uptime(s)
|
||||||
|
@ -89,6 +89,8 @@ class Automata(object):
|
|||||||
return self._epoch.any_activity
|
return self._epoch.any_activity
|
||||||
|
|
||||||
def next_epoch(self):
|
def next_epoch(self):
|
||||||
|
logging.debug("agent.next_epoch()")
|
||||||
|
|
||||||
was_stale = self.is_stale()
|
was_stale = self.is_stale()
|
||||||
did_miss = self._epoch.num_missed
|
did_miss = self._epoch.num_missed
|
||||||
|
|
||||||
|
@ -3,6 +3,20 @@ import requests
|
|||||||
from requests.auth import HTTPBasicAuth
|
from requests.auth import HTTPBasicAuth
|
||||||
|
|
||||||
|
|
||||||
|
def decode(r, verbose_errors=True):
|
||||||
|
try:
|
||||||
|
return r.json()
|
||||||
|
except Exception as e:
|
||||||
|
if r.status_code == 200:
|
||||||
|
logging.error("error while decoding json: error='%s' resp='%s'" % (e, r.text))
|
||||||
|
else:
|
||||||
|
err = "error %d: %s" % (r.status_code, r.text.strip())
|
||||||
|
if verbose_errors:
|
||||||
|
logging.info(err)
|
||||||
|
raise Exception(err)
|
||||||
|
return r.text
|
||||||
|
|
||||||
|
|
||||||
class Client(object):
|
class Client(object):
|
||||||
def __init__(self, hostname='localhost', scheme='http', port=8081, username='user', password='pass'):
|
def __init__(self, hostname='localhost', scheme='http', port=8081, username='user', password='pass'):
|
||||||
self.hostname = hostname
|
self.hostname = hostname
|
||||||
@ -13,27 +27,14 @@ class Client(object):
|
|||||||
self.url = "%s://%s:%d/api" % (scheme, hostname, port)
|
self.url = "%s://%s:%d/api" % (scheme, hostname, port)
|
||||||
self.auth = HTTPBasicAuth(username, password)
|
self.auth = HTTPBasicAuth(username, password)
|
||||||
|
|
||||||
def _decode(self, r, verbose_errors=True):
|
|
||||||
try:
|
|
||||||
return r.json()
|
|
||||||
except Exception as e:
|
|
||||||
if r.status_code == 200:
|
|
||||||
logging.error("error while decoding json: error='%s' resp='%s'" % (e, r.text))
|
|
||||||
else:
|
|
||||||
err = "error %d: %s" % (r.status_code, r.text.strip())
|
|
||||||
if verbose_errors:
|
|
||||||
logging.info(err)
|
|
||||||
raise Exception(err)
|
|
||||||
return r.text
|
|
||||||
|
|
||||||
def session(self):
|
def session(self):
|
||||||
r = requests.get("%s/session" % self.url, auth=self.auth)
|
r = requests.get("%s/session" % self.url, auth=self.auth)
|
||||||
return self._decode(r)
|
return decode(r)
|
||||||
|
|
||||||
def events(self):
|
def events(self):
|
||||||
r = requests.get("%s/events" % self.url, auth=self.auth)
|
r = requests.get("%s/events" % self.url, auth=self.auth)
|
||||||
return self._decode(r)
|
return decode(r)
|
||||||
|
|
||||||
def run(self, command, verbose_errors=True):
|
def run(self, command, verbose_errors=True):
|
||||||
r = requests.post("%s/session" % self.url, auth=self.auth, json={'cmd': command})
|
r = requests.post("%s/session" % self.url, auth=self.auth, json={'cmd': command})
|
||||||
return self._decode(r, verbose_errors=verbose_errors)
|
return decode(r, verbose_errors=verbose_errors)
|
||||||
|
@ -66,9 +66,9 @@ class AsyncAdvertiser(object):
|
|||||||
|
|
||||||
def _adv_poller(self):
|
def _adv_poller(self):
|
||||||
while True:
|
while True:
|
||||||
logging.debug("polling pwngrid-peer for peers ...")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
logging.debug("polling pwngrid-peer for peers ...")
|
||||||
|
|
||||||
grid_peers = grid.peers()
|
grid_peers = grid.peers()
|
||||||
new_peers = {}
|
new_peers = {}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user