Skip to content

Commit

Permalink
Merge pull request #29 from MeshAddicts/refactor-20240708
Browse files Browse the repository at this point in the history
Refactor 20240708
  • Loading branch information
kevinelliott authored Jul 10, 2024
2 parents 63d5759 + 13d4e3c commit c2e7bd4
Show file tree
Hide file tree
Showing 26 changed files with 1,736 additions and 1,213 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ node_modules/
.env

backups/*
caddy
output/*
mosquitto/data/*
postgres/data/*
Expand Down
12 changes: 12 additions & 0 deletions Caddyfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
debug
}

localhost {
root * /srv
encode gzip
file_server
log {
output file /var/log/caddy.log
}
}
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ Build. Be sure to specify a related version number and suffix (this example `dev
scripts/docker-build.sh 0.0.1dev5
```

### Running via Docker Compose while developing

```sh
docker compose -f docker-compose-dev.yml up --build --force-recreate
```

You will need to CTRL-C and run again if you make any changes to the python code, but not if you only make changes to
the templates.

### Release

Tag the release using git and push up the tag. The image will be build by GitHub automatically (see: https://github.com/MeshAddicts/meshinfo/actions/workflows/docker.yml).
Expand Down
12 changes: 11 additions & 1 deletion config.json.sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"broker": {
"enabled": true,
"host": "mqtt.meshtastic.org",
"port": 1883,
"client_id_prefix": "meshinfo-dev",
Expand All @@ -18,6 +19,7 @@
"timezone": "America/Los_Angeles",
"start_time": null,
"intervals": {
"enrich": 60,
"data_save": 60,
"render": 5
}
Expand Down Expand Up @@ -46,6 +48,14 @@
"token": "token",
"channel": "1247618108810596392",
"webhook": "webhook"
},
"geocoding": {
"enabled": false,
"provider": "geocode.maps.co",
"geocode.maps.co": {
"api_key": "XXXXXXXXXXXXXXXX"
}
}
}
},
"debug": false
}
34 changes: 34 additions & 0 deletions data_renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python3

import json

from encoders import _JSONEncoder

class DataRenderer:
def __init__(self, config, data):
self.config = config
self.data = data

def render(self):
self.save_file("chat.json", self.data.chat)
print(f"Saved {len(self.data.chat['channels']['0']['messages'])} chat messages to file ({self.config['paths']['data']}/chat.json)")

nodes = {}
for id, node in self.data.nodes.items():
if id.startswith('!'):
id = id.replace('!', '')
nodes[id] = node

self.save_file("nodes.json", self.data.nodes)
print(f"Saved {len(nodes)} nodes to file ({self.config['paths']['data']}/nodes.json)")

self.save_file("telemetry.json", self.data.telemetry)
print(f"Saved {len(self.data.telemetry)} telemetry to file ({self.config['paths']['data']}/telemetry.json)")

self.save_file("traceroutes.json", self.data.traceroutes)
print(f"Saved {len(self.data.traceroutes)} traceroutes to file ({self.config['paths']['data']}/traceroutes.json)")

def save_file(self, filename, data):
print(f"Saving {filename}")
with open(f"{self.config['paths']['data']}/{filename}", "w", encoding='utf-8') as f:
json.dump(data, f, indent=2, sort_keys=True, cls=_JSONEncoder)
52 changes: 52 additions & 0 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
services:
caddy:
image: caddy:latest
ports:
- 80:80
- 443:443
volumes:
- ./caddy/data:/data/caddy
- ./Caddyfile.dev:/etc/caddy/Caddyfile
- ./output/static-html:/srv
- ./public/images:/srv/images
environment:
- CADDY_AGREE=true
restart: always

postgres:
image: postgres:latest
volumes:
- ./postgres/data:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=password
restart: always

mqtt:
container_name: mqtt
image: eclipse-mosquitto:latest
ports:
- 1883:1883
volumes:
- ./mosquitto/data:/mosquitto/data:rw
- ./mosquitto/config:/mosquitto/config:rw
restart: always

meshinfo:
build:
context: .
dockerfile: Dockerfile
volumes:
- ./config.json:/app/config.json
- ./output:/app/output
- ./templates:/app/templates
environment:
- PYTHONUNBUFFERED=1
- MQTT_HOST=mqtt
- MQTT_PORT=1883
- MQTT_USERNAME=meshinfo
- MQTT_PASSWORD=m3sht4st1c
restart: always
depends_on:
- caddy
- postgres
- mqtt
60 changes: 30 additions & 30 deletions encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,36 @@
import json

class _JSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, (datetime.date, datetime.datetime)):
return obj.astimezone().isoformat()
if isinstance(obj, str):
return obj.replace('!', '')
return obj
def default(self, obj):
if isinstance(obj, datetime.datetime):
return obj.astimezone().isoformat()
if isinstance(obj, datetime.timedelta):
return None
return obj

class _JSONDecoder(json.JSONDecoder):
def __init__(self, *args, **kwargs):
json.JSONDecoder.__init__(
self, object_hook=self.object_hook, *args, **kwargs)
def __init__(self, *args, **kwargs):
json.JSONDecoder.__init__(
self, object_hook=self.object_hook, *args, **kwargs)

def object_hook(self, obj):
ret = {}
for key, value in obj.items():
if key in {'last_seen'}:
ret[key] = datetime.datetime.fromisoformat(value)
elif key in {'id'}:
if isinstance(value, str):
ret[key] = value.replace('!', '')
else:
ret[key] = value
elif key in {'sender'}:
if isinstance(value, str):
ret[key] = value.replace('!', '')
else:
ret[key] = value
else:
if isinstance(value, str):
ret[key] = value.replace('!', '')
else:
ret[key] = value
return ret
def object_hook(self, obj):
ret = {}
for key, value in obj.items():
if key in {'last_seen', 'last_geocoding'}:
ret[key] = datetime.datetime.fromisoformat(value)
elif key in {'id'}:
if isinstance(value, str):
ret[key] = value.replace('!', '')
else:
ret[key] = value
elif key in {'sender'}:
if isinstance(value, str):
ret[key] = value.replace('!', '')
else:
ret[key] = value
else:
if isinstance(value, str):
ret[key] = value.replace('!', '')
else:
ret[key] = value
return ret
Loading

0 comments on commit c2e7bd4

Please sign in to comment.