Skip to content

Commit

Permalink
fix: pc example, deploy script
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Dec 24, 2019
1 parent 1bf14b1 commit 5bd18ee
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 24 deletions.
8 changes: 8 additions & 0 deletions Makefile
@@ -1,2 +1,10 @@
test:
python -m unittest discover -s tests/

package: package-dist package-upload

package-dist:
python ./setup.py sdist

package-upload:
twine upload dist/*
82 changes: 82 additions & 0 deletions examples/minimal.py
@@ -0,0 +1,82 @@
# library
from prometheus_express import check_network, start_http_server, CollectorRegistry, Counter, Gauge, Router
#from prometheus_express.i2c import scan_i2c_bus

#def check_network(eth):
# print('check')
# return True

# system
import board
import busio
import digitalio
import random
import time

# hardware
import neopixel
import wiznet

# colors
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# set up networking
spi = busio.SPI(clock=board.SCK, MOSI=board.MOSI, MISO=board.MISO)

eth = wiznet.WIZNET5K(spi, board.D10, board.D11)
eth.dhcp = True

# initialize the LEDs
led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT

rgb = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)

time.sleep(0.5)
led.value = check_network(eth)

def main():
ready = False

registry = CollectorRegistry(namespace='prom_express')
metric_c = Counter('test_counter',
'a test counter', registry=registry)
metric_g = Gauge('test_gauge',
'a test gauge', registry=registry)

router = Router([
('GET', '/favicon.ico', lambda headers, body: {
'status': '200 OK',
'type': 'image/png;base64',
'content': 'iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAFklEQVQI12N8HmPBxMPHxMTDx8TNBwAUNwHSqFS0zAAAAABJRU5ErkJggg==',
}),
('GET', '/metrics', lambda headers, body: {
'status': '200 OK',
'content': '\r\n'.join(registry.render()),
}),
])
server = False

rgb[0] = RED # starting
while not ready:
ready = check_network(eth)
led.value = ready

rgb[0] = BLUE # connected
while not server:
server = start_http_server(8080, address=eth.ifconfig()[0])

rgb[0] = GREEN # ready
while True:
metric_c.inc(random.randint(0, 50))
metric_g.set(random.randint(0, 5000))
try:
server.accept(router)
except OSError as err:
print('Error accepting request: {}'.format(err))
server = start_http_server(8080, address=eth.ifconfig()[0])


main()
11 changes: 11 additions & 0 deletions examples/pc-python-3.py
Expand Up @@ -12,6 +12,17 @@
import socket
import time

class CPythonNetwork():
connected = True

def __init__(self, socket):
self.socket = socket

def ifconfig(self):
hostname = self.socket.gethostname()
ip_addr = self.socket.gethostbyname(hostname)
return (ip_addr, 0, 0, 0)

# colors
RED = (255, 0, 0)
GREEN = (0, 255, 0)
Expand Down
27 changes: 6 additions & 21 deletions prometheus_express/utils.py
Expand Up @@ -12,31 +12,16 @@ def check_network(eth):

return True


def scan_i2c_bus(i2c, timeout=10):
attempt=0

while not i2c.try_lock():
def scan_i2c_bus(bus, timeout):
attempt = 0
while not bus.try_lock():
if attempt < timeout:
attempt += 1
pass
else:
return False

print('I2C devices:', [
hex(x) for x in i2c.scan()
hex(x) for x in bus.scan()
])

i2c.unlock()
return True

class CPythonNetwork(object):
connected = True

def __init__(self, socket):
self.socket = socket

def ifconfig(self):
hostname = self.socket.gethostname()
ip_addr = self.socket.gethostbyname(hostname)
return (ip_addr, 0, 0, 0)
bus.unlock()
return True
2 changes: 1 addition & 1 deletion scripts/deploy.sh
Expand Up @@ -7,5 +7,5 @@ PROGRAM_FILE="${3}"

mkdir "${DEVICE_LIB}"
rsync -avh "${LIBRARY_PATH}/neopixel.mpy" "${DEVICE_LIB}"
rsync -avh --exclude-from="scripts/deploy-exclude" "./prometheus_express" "${DEVICE_LIB}"
rsync -avh --exclude-from="scripts/deploy-exclude" "./prometheus_express" "${DEVICE_PATH}"
cp -v "./examples/${PROGRAM_FILE}.py" "${DEVICE_PATH}/code.py"
1 change: 1 addition & 0 deletions scripts/serial.sh
@@ -0,0 +1 @@
sudo screen /dev/ttyACM0 115200
4 changes: 2 additions & 2 deletions setup.py
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="prometheus_express",
version="0.0.1",
version="0.0.2",
author="Sean Sube",
author_email="seansube@gmail.com",
description="Prometheus client/server for CircuitPython Express ARM devices",
Expand All @@ -22,4 +22,4 @@
"Operating System :: OS Independent",
],
python_requires='>=3.0',
)
)

0 comments on commit 5bd18ee

Please sign in to comment.