Begin refactoring things to be easier to test

master
Ryan Rix 2016-03-13 07:28:46 +00:00
parent 19a81cc9e1
commit da4a4e802a
5 changed files with 114 additions and 0 deletions

0
lightrix/__init__.py Normal file
View File

46
lightrix/matrix-client.py Normal file
View File

@ -0,0 +1,46 @@
#!/bin/env python
from matrix_client.client import MatrixClient
class Client:
def __init__(self, homeserver, username, password, room_id, strip=None):
self.rooms = {}
self.room_ids = []
self.state = {}
if strip:
self.strip = strip
self.strip.begin()
self.client = MatrixClient(homeserver)
self.token = self.client.login_with_password(username=username,
password=password)
self.rooms = {}
self.room_ids = []
self.join_room(room_id)
self.rooms[room_id].send_text("Started up, set to timebase.")
self.refresh_states()
self.set_from_state()
return
def add_listener(self, callback):
self.client.add_listener(callback)
def join_room(self, room_id):
room = self.client.join_room(room_id)
self.rooms[room_id] = room
self.room_ids.append(room_id)
return
def refresh_states(self):
for room in self.rooms:
self.state[room.id] = self.client.api.get_room_state(room.id)
def listen(self):
while(True):
self.client.listen_for_events(timeout=30000)
return

30
lightrix/mock.py Normal file
View File

@ -0,0 +1,30 @@
# lightrix mock interface for testing
import colorsys
class MockStrip(object):
def __init__(self, count=48, pin=18, freq_hz=800000,
dma=10, brightness=255, invert=False):
self.led_count = count
self.led_pin = pin
self.led_freq_hz = freq_hz
self.led_dma = dma
self.led_brightness = brightness
self.led_invert = invert
for led in range(self.led_count):
self.strip.append(dict())
def setPixel(self, ix, hue, saturation, lightness):
rgb = colorsys.hls_to_rgb(hue, lightness, saturation)
self.strip[ix]['r'] = rgb[0]
self.strip[ix]['g'] = rgb[1]
self.strip[ix]['b'] = rgb[2]
def getPixel(self, ix):
return self.strip[ix]
def begin(self):
pass

16
lightrix/neopixel.py Normal file
View File

@ -0,0 +1,16 @@
# lightrix mock interface for testing
class Neopixel(object):
def __init__(self):
self.led_count = 48
self.led_pin = 18
self.led_freq_hz = 800000
self.led_dma = 10
self.led_brightness = 255
self.led_invert = False
self.strip = np.Adafruit_NeoPixel(self.led_count, self.led_pin, self.led_freq_hz,
self.led_dma, self.led_invert, self.led_brightness)

22
test-client.py Normal file
View File

@ -0,0 +1,22 @@
import lightrix.matrix_client as mc
import argparse
import os
import sys
import yaml
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Pipe in to and out of a Matrix room")
parser.add_argument('--config_path', "-c", default="~/.mcatrc")
parser.add_argument('--room', "-r")
args = parser.parse_args()
if args.room is None:
print("Require --room argument")
sys.exit(1)
if not os.path.exists(args.config_path):
print("Configuration file doesn't exist, please create one")
sys.exit(1)
mcat = None
with open(args.config_path) as f:
lightrix = mc.Client(yaml.safe_load(f), args.room)
lightrix.listen()