openwrt: home side changes
This commit is contained in:
parent
c0111bf4d3
commit
2960f9f09a
@ -66,9 +66,11 @@ class WebAPIClient:
|
||||
})
|
||||
|
||||
def log_openwrt(self,
|
||||
lines: List[Tuple[int, str]]):
|
||||
lines: List[Tuple[int, str]],
|
||||
access_point: int):
|
||||
return self._post('log/openwrt/', {
|
||||
'logs': stringify(lines)
|
||||
'logs': stringify(lines),
|
||||
'ap': access_point
|
||||
})
|
||||
|
||||
def get_sensors_data(self,
|
||||
|
@ -37,13 +37,14 @@ class BotsDatabase(MySQLDatabase):
|
||||
self.commit()
|
||||
|
||||
def add_openwrt_logs(self,
|
||||
lines: List[Tuple[datetime, str]]):
|
||||
lines: List[Tuple[datetime, str]],
|
||||
access_point: int):
|
||||
now = datetime.now()
|
||||
with self.cursor() as cursor:
|
||||
for line in lines:
|
||||
time, text = line
|
||||
cursor.execute("INSERT INTO openwrt (log_time, received_time, text) VALUES (%s, %s, %s)",
|
||||
(time.strftime(datetime_fmt), now.strftime(datetime_fmt), text))
|
||||
cursor.execute("INSERT INTO openwrt (log_time, received_time, text, ap) VALUES (%s, %s, %s, %s)",
|
||||
(time.strftime(datetime_fmt), now.strftime(datetime_fmt), text, access_point))
|
||||
self.commit()
|
||||
|
||||
def add_sound_hits(self,
|
||||
|
@ -2,16 +2,15 @@
|
||||
import os
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Tuple, List
|
||||
from argparse import ArgumentParser
|
||||
from home.config import config
|
||||
from home.database import SimpleState
|
||||
from home.api import WebAPIClient
|
||||
from typing import Tuple, List
|
||||
|
||||
log_file = '/var/log/openwrt.log'
|
||||
|
||||
f"""
|
||||
This script is supposed to be run by cron every 5 minutes or so.
|
||||
It looks for new lines in {log_file} and sends them to remote server.
|
||||
It looks for new lines in log file and sends them to remote server.
|
||||
|
||||
OpenWRT must have remote logging enabled (UDP; IP of host this script is launched on; port 514)
|
||||
|
||||
@ -41,16 +40,22 @@ def parse_line(line: str) -> Tuple[int, str]:
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
config.load('openwrt_logger')
|
||||
parser = ArgumentParser()
|
||||
parser.add_argument('--file', type=str, required=True,
|
||||
help='openwrt log file')
|
||||
parser.add_argument('--access-point', type=int, required=True,
|
||||
help='access point number')
|
||||
|
||||
arg = config.load('openwrt_logger', parser=parser)
|
||||
|
||||
state = SimpleState(file=config['simple_state']['file'],
|
||||
default={'seek': 0, 'size': 0})
|
||||
|
||||
fsize = os.path.getsize(log_file)
|
||||
fsize = os.path.getsize(arg.file)
|
||||
if fsize < state['size']:
|
||||
state['seek'] = 0
|
||||
|
||||
with open(log_file, 'r') as f:
|
||||
with open(arg.file, 'r') as f:
|
||||
if state['seek']:
|
||||
# jump to the latest read position
|
||||
f.seek(state['seek'])
|
||||
@ -75,4 +80,4 @@ if __name__ == '__main__':
|
||||
lines.append((0, line))
|
||||
|
||||
api = WebAPIClient()
|
||||
api.log_openwrt(lines)
|
||||
api.log_openwrt(lines, arg.access_point)
|
||||
|
@ -154,8 +154,10 @@ class WebAPIServer(http.HTTPServer):
|
||||
|
||||
try:
|
||||
logs = data['logs']
|
||||
ap = int(data['ap'])
|
||||
except KeyError:
|
||||
logs = ''
|
||||
ap = 0
|
||||
|
||||
# validate it
|
||||
logs = json.loads(logs)
|
||||
@ -173,7 +175,7 @@ class WebAPIServer(http.HTTPServer):
|
||||
line[1]
|
||||
))
|
||||
|
||||
BotsDatabase().add_openwrt_logs(lines)
|
||||
BotsDatabase().add_openwrt_logs(lines, ap)
|
||||
return self.ok()
|
||||
|
||||
async def GET_recordings_list(self, req: http.Request):
|
||||
|
Loading…
x
Reference in New Issue
Block a user