示例#1
0
def request_device_identification(io_device, address):
    # identification_request: bytes = bytes([0x02, 0xFE, 0x00, 0x05, 0x08, 0x01, 0x0B, 0xd4, 0x9c, 0x03])
    identification_frame = RequestFrame(address, 0x0B01)

    test_the_frame = Frame(frame_data=identification_frame.get_frame_data())
    print(test_the_frame)
    # ensure the crc is right
    if not test_the_frame.isValid:
        exit(1)
    io_device.write(identification_frame.get_frame_data())
    identification_response = Frame(io_source=io_device)
    if identification_response.isValid:
        print(identification_response)
        as_string = [chr(x) for x in identification_response.get_framedata()]
        print(as_string)
        print(FrameDecoder().decode_all(identification_response.get_data()))
示例#2
0
 def test_filelogger_no_file_logging_if_disabled_in_config(self):
     with tempfile.TemporaryDirectory() as temp_dir:
         file_name = temp_dir + '/test.csv'
         file_logger = FileLogger(
             json.loads('{"enabled": false, "path": "%s"}' % file_name),
             None)
         file_logger.log_data(Frame(frame_data=TestRemeha.raw_test_data))
         file_logger.close()
         assert not os.path.exists(file_name)
示例#3
0
 def test_filelogger_uses_config_path_if_provided(self):
     with tempfile.TemporaryDirectory() as temp_dir:
         file_name = temp_dir + '/test.csv'
         file_logger = FileLogger(
             json.loads('{"enabled": true, "path": "%s"}' % file_name),
             None)
         file_logger.log_data(Frame(frame_data=TestRemeha.raw_test_data))
         file_logger.close()
         assert os.path.exists(file_name)
示例#4
0
 def test_filelogger_uses_filename_if_provided(self):
     with mock.patch('remeha.csv') as csv_mock:
         with tempfile.TemporaryDirectory() as temp_dir:
             file_name = temp_dir + '/test.csv'
             file_logger = FileLogger(None, file_name)
             file_logger.log_data(
                 Frame(frame_data=TestRemeha.raw_test_data))
             file_logger.close()
             assert os.path.exists(file_name)
             csv_mock.writer.assert_called()
示例#5
0
 def test_filelogger_commandline_parameter_overwrites_config_path(self):
     with tempfile.TemporaryDirectory() as temp_dir:
         expected_file_name = temp_dir + '/test.csv'
         not_expected_file_name = temp_dir + '/test2.csv'
         file_logger = FileLogger(
             json.loads('{"enabled": true, "path": "%s"}' %
                        not_expected_file_name), expected_file_name)
         file_logger.log_data(Frame(frame_data=TestRemeha.raw_test_data))
         file_logger.close()
         assert not os.path.exists(not_expected_file_name)
         assert os.path.exists(expected_file_name)
示例#6
0
def log_remeha(source_serial, destination_filename, mqtt_freq, config):
    ser = serial.Serial(source_serial,
                        9600,
                        timeout=10,
                        parity='N',
                        bytesize=8,
                        stopbits=1
                        )
    if not ser.isOpen():
        sys.exit("Could not open serial: " + source_serial)

    log_db = DatabaseLogger(config)
    log_mqtt = LogToMQtt(config, mqtt_freq, lambda message: log_db.log_manual(message))
    log_file = FileLogger(config.get('file_logger'), destination_filename)

    clean_up_handler = atexit.register(clean_up, [log_db, log_mqtt, log_file])
    sample_data_request = bytes([0x02, 0xFE, 0x01, 0x05, 0x08, 0x02, 0x01, 0x69, 0xAB, 0x03])
    last_frame_was_valid = True
    runtime_seconds = 0
    try:
        while True:
            # sys.stdout.flush()
            ser.write(sample_data_request)

            frame = Frame(io_source=ser)
            if frame.isValid:
                last_frame_was_valid = True
                log_file.log_data(frame)
                log_mqtt.log(frame, runtime_seconds)
                log_db.log_data(frame)

                while ser.inWaiting():
                    unknown_data = ser.read(ser.inWaiting())
                    print("Error unknown data: " + unknown_data.hex())
                    time.sleep(1)
            else:
                if not last_frame_was_valid:
                    sys.exit("Two consecutive read errors")
                print("Sleep and retry")
                time.sleep(10)
                ser.close()
                time.sleep(10)
                ser.open()
                last_frame_was_valid = False
            runtime_seconds += 1
            time.sleep(1)

    finally:
        log_db.close()
        log_file.close()
        atexit.unregister(clean_up_handler)