Commit ea340750 authored by loelkes's avatar loelkes
Browse files

WIP Version 1

parent bef4eb4d
#!/usr/bin/python
# -*- coding: utf-8 -*-
from http_API import API
from toolbox import Util
from hosted import node, device
from datetime import datetime
import time
class Freifunk(object):
def __init__(self, url, node_id):
self.util = Util()
self.api_url = API(url=url)
self.api_url.format = 'json'
self.lastRefresh = 0
self.node_id = node_id
class FreifunkNode(object):
def __init__(self, data):
self.data = data
self.node_id = 0
self.node_data = False
self.human_readable = True
self.supported_versions = [2]
def find_node(self, id='', data=False):
data = data or self.api_url.response['nodes']
self.node_data = next(node for node in data if node['nodeinfo']['node_id'] == self.node_id)
def check_version(self):
if self.data['data']['version'] not in self.supported_versions:
return False
else:
return True
def select(self, id):
self.node_id = id
self.node_data = next(node for node in self.data['nodes'] \
if node['nodeinfo']['node_id'] == self.node_id)
# From https://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size
# See also https://en.wikipedia.org/wiki/Binary_prefix#yobi
def HRB(self, num, suffix='B'):
# Convert to Humand Readable Bits or Bytes (HRB)
for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
......@@ -85,11 +85,11 @@ class Freifunk(object):
return self.node_data['statistics']['gateway6']
@property
def total_processes(self):
def total_proc(self):
return self.node_data['statistics']['processes']['total']
@property
def running_processes(self):
def running_proc(self):
return self.node_data['statistics']['processes']['running']
@property
......@@ -122,18 +122,34 @@ class Freifunk(object):
def traffic(self):
return 'Tx: {} | Rx: {}'.format(self.tx_bytes, self.rx_bytes)
### Infobeamer package part
# --------------------------------------------------------------------------
# Node information
# --------------------------------------------------------------------------
def refresh(self):
if time.time() - self.lastRefresh > 60:
self.api_url.query()
self.find_node()
node['/stream'](dict(line=self.util.line(self.traffic, height=20)))
self.lastRefresh = time.time()
else:
pass
@property
def hostname(self):
return self.node_data['nodeinfo']['owner']
@property
def is_vpn(self):
return self.node_data['nodeinfo']['vpn']
@property
def processors(self):
return self.node_data['nodeinfo']['hardware']['nproc']
def update(self):
self.util.reset()
self.refresh()
self.util.update()
@property
def model(self):
return self.node_data['nodeinfo']['hardware']['model']
@property
def longitude(self):
return self.node_data['node_info']['location']['longitude']
@property
def latitude(self):
return self.node_data['node_info']['location']['latitude']
@property
def position(self):
return [self.longitude, self.latitude]
......@@ -51,16 +51,16 @@ end
-- create the content
function content()
-- -- get the single lines to be written
for i, button in ipairs(buttons) do
width = 0 -- font:width(button.text.string, button.text.h)
for j, shape in ipairs(button.shapes) do
plain_color = resource.create_colored_texture(shape.r,shape.g,shape.b,shape.a)
plain_color:draw(shape.x1, shape.y1, shape.x2+width, shape.y2)
end
font:write(button.text.x, button.text.y, button.text.string, button.text.h, button.text.r, button.text.g, button.text.b, button.text.a)
end
-- for i, button in ipairs(buttons) do
-- width = 0 -- font:width(button.text.string, button.text.h)
-- for j, shape in ipairs(button.shapes) do
-- plain_color = resource.create_colored_texture(shape.r,shape.g,shape.b,shape.a)
-- plain_color:draw(shape.x1, shape.y1, shape.x2+width, shape.y2)
-- end
-- font:write(button.text.x, button.text.y, button.text.string, button.text.h, button.text.r, button.text.g, button.text.b, button.text.a)
-- end
for i, line in ipairs(text) do
font:write(line.x, line.y, line.string, line.h, line.r, line.g, line.b, line.a)
font:write(line.x, line.y, line.string, line.h, text_color.r, text_color.g, text_color.b, text_color.a)
end
end
......@@ -68,6 +68,7 @@ function node.render()
gl.clear(background_color.r, background_color.g, background_color.b, background_color.a) -- clear the backgrount
st()
if on then
content()
streamContent()
end
end
......@@ -6,18 +6,46 @@ import traceback
from hosted import CONFIG, NODE
CONFIG.restart_on_update()
from freifunk import Freifunk
from freifunk import FreifunkNode
from http_API import API
from toolbox import Util
from hosted import node, device
from datetime import datetime
import time
class Service(object):
def __init__(self, url, node_id):
self.util = Util()
self.api_url = API(url=url)
self.api_url.format = 'json'
self.lastRefresh = 0
self.node_id = node_id
def refresh(self):
if time.time() - self.lastRefresh > 60:
self.util.reset()
self.api_url.query()
self.node = FreifunkNode(self.api_url.response)
self.node.select(self.node_id)
self.util.text('Freifunk Node {}'.format(self.node.model), 20, 20, 100)
self.lastRefresh = time.time()
else:
pass
def update(self):
self.refresh()
self.util.update()
ff = Freifunk(CONFIG['api_url'],CONFIG['node_id'])
def run(self):
while 1:
try:
self.update()
time.sleep(1)
except Exception:
traceback.print_exc()
time.sleep(10)
def main():
while 1:
try:
ff.update()
time.sleep(10)
except Exception:
traceback.print_exc()
time.sleep(10)
serv = Service(CONFIG['api_url'],CONFIG['node_id'])
if __name__ == "__main__":
main()
serv.run()
# Version: 17 Juni 2019
import time, json, pytz
from datetime import datetime
from hosted import node
......@@ -18,7 +20,10 @@ class Util(object):
self.lines, self.rectangles, self.buttons = [], [], []
self.touch_areas, self.lineHeight = [], 32
self.offset, self.margin = dict(x=0, y=0), dict(x=20, y=20)
self.colour = dict(button=Colour(1, 1, 1, 1), border=Colour(1, 0, 0, 1), text=Colour(1, 1, 1, 1))
self.colour = dict(
button=Colour(1, 1, 1, 1),
border=Colour(1, 0, 0, 1),
text=Colour(1, 1, 1, 1))
self.timeFormat = '%Y-%m-%d %H:%M:%S'
# Touch utils.
......@@ -47,6 +52,9 @@ class Util(object):
line.update(colour or self.colour['text']._asdict())
return line
def text(self, string, x, y, height):
self.lines.append(dict(string=string, x=x, y=y, h=height))
# Datetime tools
def current_time(self, timestamp=None):
now = datetime.utcnow() if not timestamp else datetime.utcfromtimestamp(timestamp)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment