def setUp(self): tracer = Tracer(16) tracer.get_command_bytes = Mock(return_value=fixture_data) tracer.add_crc = Mock(return_value=fixture_data) tracer.verify_crc = Mock(return_value=True) tracer.get_result = Mock(return_value=Command(0x12, fixture_data)) self.ts = TracerSerial(tracer, None)
def load_on(): try: port = FakePort(fake_load_on) tracer = Tracer(0x16) t_ser = TracerSerial(tracer, port) t_ser.send_command(0xAA, 0x01, 0x01) #data = t_ser.receive_result(13) data = t_ser.receive_result() load_state = data.load_state return render_template('load_on.html', load_state=load_state) except (IndexError, IOError) as e: return jsonify({'error': str(e)}), 503
def load_on(): try: port = Serial('/dev/ttyAMA0', 9600, timeout=1) tracer = Tracer(0x16) t_ser = TracerSerial(tracer, port) t_ser.send_command(0xAA, 0x01, 0x01) #data = t_ser.receive_result(13) data = t_ser.receive_result() port.close() load_state = data.load_state return render_template('load_on.html', load_state=load_state) except (IndexError, IOError) as e: port.reset_input_buffer() port.reset_output_buffer() return jsonify({'error': str(e)}), 503
def get_data(): try: port = Serial('/dev/ttyAMA0', 9600, timeout=1) port.reset_input_buffer() port.reset_output_buffer() tracer = Tracer(0x16) t_ser = TracerSerial(tracer, port) t_ser.send_command(0xA0) #sleep(1) #data = t_ser.receive_result(36) data = t_ser.receive_result() port.close() # operating parameters batt_voltage = data.batt_voltage batt_full_voltage = data.batt_full_voltage batt_overdischarge_voltage = data.batt_overdischarge_voltage batt_temp = data.batt_temp pv_voltage = data.pv_voltage charge_current = data.charge_current load_on = data.load_on load_amps = data.load_amps load_overload = data.load_overload load_short = data.load_short batt_overdischarge = data.batt_overdischarge batt_full = data.batt_full batt_overload = data.batt_overload batt_charging = data.batt_charging return render_template('data.html', batt_voltage=batt_voltage, batt_full_voltage=batt_full_voltage, batt_overdischarge_voltage=batt_overdischarge_voltage, batt_temp=batt_temp, pv_voltage=pv_voltage, charge_current=charge_current, load_on=load_on, load_amps=load_amps, load_overload=load_overload, load_short=load_short, batt_overdischarge=batt_overdischarge, batt_full=batt_full, batt_overload=batt_overload, batt_charging=batt_charging) except (IndexError, IOError) as e: port.reset_input_buffer() port.reset_output_buffer() return jsonify({'error': str(e)}), 503
def setUp(self): tracer = Tracer(16) tracer.get_command_bytes=Mock(return_value=fixture_data) tracer.add_crc = Mock(return_value=fixture_data) tracer.verify_crc = Mock(return_value=True) tracer.get_result = Mock(return_value=Command(0x12, fixture_data)) self.ts = TracerSerial(tracer, None)
class TestTracerSerial(TestCase): def setUp(self): tracer = Tracer(16) tracer.get_command_bytes = Mock(return_value=fixture_data) tracer.add_crc = Mock(return_value=fixture_data) tracer.verify_crc = Mock(return_value=True) tracer.get_result = Mock(return_value=Command(0x12, fixture_data)) self.ts = TracerSerial(tracer, None) def test_to_bytes(self): command = Command(0x12) command.decode_result = Mock() result = self.ts.to_bytes(command) self.assertEqual( bytearray(b'\xAA\x55\xAA\x55\xAA\x55\xEB\x90\xEB\x90\xEB\x90' + fixture_data), result) def test_from_bytes(self): result = self.ts.from_bytes( bytearray(b'\xEB\x90\xEB\x90\xEB\x90\x00\x12\x03' + fixture_data + b'\x00\x00\x7A')) self.assertEqual(fixture_data, result.data) def test_receive_result(self): class FakePort(object): def __init__(self, data): self.data = data read_idx = 0 def read(self, count=1): result = self.data[self.read_idx:self.read_idx + count] self.read_idx += count return result self.ts.port = FakePort(full_query_result) self.ts.from_bytes = Mock(return_result=QueryResult(query_result)) # make the actual call result = self.ts.receive_result() self.assertNotEqual(None, result) self.ts.from_bytes.assert_called_with(full_query_result)
def get_data(): try: port = FakePort(fake) tracer = Tracer(0x16) t_ser = TracerSerial(tracer, port) t_ser.send_command(0xA0) #data = t_ser.receive_result(36) data = t_ser.receive_result() # operating parameters batt_voltage = data.batt_voltage batt_full_voltage = data.batt_full_voltage batt_overdischarge_voltage = data.batt_overdischarge_voltage batt_temp = data.batt_temp pv_voltage = data.pv_voltage charge_current = data.charge_current load_on = data.load_on load_amps = data.load_amps load_overload = data.load_overload load_short = data.load_short batt_overdischarge = data.batt_overdischarge batt_full = data.batt_full batt_overload = data.batt_overload batt_charging = data.batt_charging return render_template( 'data.html', batt_voltage=batt_voltage, batt_full_voltage=batt_full_voltage, batt_overdischarge_voltage=batt_overdischarge_voltage, batt_temp=batt_temp, pv_voltage=pv_voltage, charge_current=charge_current, load_on=load_on, load_amps=load_amps, load_overload=load_overload, load_short=load_short, batt_overdischarge=batt_overdischarge, batt_full=batt_full, batt_overload=batt_overload, batt_charging=batt_charging) except (IndexError, IOError) as e: return jsonify({'error': str(e)}), 503
class TestTracerSerial(TestCase): def setUp(self): tracer = Tracer(16) tracer.get_command_bytes=Mock(return_value=fixture_data) tracer.add_crc = Mock(return_value=fixture_data) tracer.verify_crc = Mock(return_value=True) tracer.get_result = Mock(return_value=Command(0x12, fixture_data)) self.ts = TracerSerial(tracer, None) def test_to_bytes(self): command = Command(0x12) command.decode_result = Mock() result = self.ts.to_bytes(command) self.assertEqual(bytearray(b'\xAA\x55\xAA\x55\xAA\x55\xEB\x90\xEB\x90\xEB\x90' + fixture_data), result) def test_from_bytes(self): result = self.ts.from_bytes(bytearray(b'\xEB\x90\xEB\x90\xEB\x90\x00\x12\x03' + fixture_data + b'\x00\x00\x7A')) self.assertEqual(fixture_data, result.data) def test_receive_result(self): class FakePort(object): def __init__(self, data): self.data = data read_idx = 0 def read(self, count=1): result = self.data[self.read_idx:self.read_idx+count] self.read_idx += count return result self.ts.port = FakePort(full_query_result) self.ts.from_bytes = Mock(return_result=QueryResult(query_result)) # make the actual call result = self.ts.receive_result() self.assertNotEqual(None, result) self.ts.from_bytes.assert_called_with(full_query_result)
from flask import Flask, jsonify from time import sleep from serial import Serial import sys sys.path.append('/app/tracer/python') from tracer import Tracer, TracerSerial, QueryCommand port = Serial('/dev/ttyAMA0', 9600, timeout=1) port.flushInput() port.flushOutput() tracer = Tracer(0x16) t_ser = TracerSerial(tracer, port) query = QueryCommand() # Rest API app = Flask(__name__) @app.route('/', methods=['GET']) def get_data(): try: t_ser.send_command(query) data = t_ser.receive_result() return jsonify(batt_voltage=data.batt_voltage, pv_voltage=data.pv_voltage, charge_current=data.charge_current, load_amps=data.load_amps) except (IndexError, IOError) as e: port.flushInput()
from time import sleep from serial import Serial import sys sys.path.append('/home/pi/tracer/python') from tracer import Tracer, TracerSerial, QueryCommand port = Serial('/dev/ttyAMA0', 9600, timeout = 1) port.flushInput() port.flushOutput() tracer = Tracer(0x16) t_ser = TracerSerial(tracer, port) query = QueryCommand() try: while 1: try: t_ser.send_command(query) data = t_ser.receive_result() except (IndexError, IOError) as e: print(e) port.flushInput() port.flushOutput() sleep(4) continue print('Battery Voltage: {0:0.1f}V'.format(data.batt_voltage)) print('Solar Panel Voltage: {0:0.1f}V'.format(data.pv_voltage)) print('Charging Current: {0:0.2f}A'.format(data.charge_current)) print('Load Current: {0:0.2f}A\n'.format(data.load_amps)) sleep(4)
def read(self, count=1): result = self.data[self.read_idx:self.read_idx + count] self.read_idx += count return result def write(self, data): return len(data) if not fake: ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1) else: ser = FakePort(fake) tracer = Tracer(0x16) t_ser = TracerSerial(tracer, ser) t_ser.send_command(0xA0) result = t_ser.receive_result() #print("result.data:\n", result.data) #print(type(result.data)) #print("result:\n", result) #print(type(result)) #data = str(result) #print("str(result):\n", data) #print(type(str(result))) #for i in data: # print(i) print("Raw bytes: %s" % ", ".join(map(lambda a: "%0X" % (a), result.data))) print
def init(): global tracer_serial, query port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=1) tracer = Tracer(0x16) tracer_serial = TracerSerial(tracer, port) query = QueryCommand()
#fake = bytearray(b'\xEB\x90\xEB\x90\xEB\x90\x00\xA0\x18\xD2\x04\xD3\x04\x00\x00\x0E\x00\x53\x04\xA5\x05\x01\x00\x00\x1F\x00\x00\x00\x01\x33\x0A\x00\x00\x99\x5B\x7F') class FakePort(object): def __init__(self, data): self.data = data read_idx = 0 def read(self, count=1): result = self.data[self.read_idx:self.read_idx+count] self.read_idx += count return result def write(self, data): return len(data) if not fake: ser = serial.Serial('/dev/ttyAMA0', 9600, timeout = 1) else: ser = FakePort(fake) tracer = Tracer(0x16) t_ser = TracerSerial(tracer, ser) query = QueryCommand() t_ser.send_command(query) result = t_ser.receive_result() print "Raw bytes: %s" % ", ".join(map(lambda a: "%0X" % (a), result.data)) print formatted = str(result).replace('{', '{\n') formatted = formatted.replace('}', '\n}') print formatted.replace(', ', '\n')