#!/usr/bin/env python

import nose
import time
import threading

from nose.tools import eq_
from mock import MagicMock

from pylacrosse import (LaCrosse, LaCrosseSensor)

def static_vars(**kwargs):
    def decorate(func):
        for k in kwargs:
             setattr(func, k, kwargs[k])
        return func
    return decorate

class TestLacrosse(object):

    def test_refresh(self):
        mock_readline = MagicMock()

        def side_effect_fct():
            if not hasattr(side_effect_fct, "counter"):
                side_effect_fct.counter = 0
            vals = [
                'OK 9 1 1 4 150 106',
                'OK 9 2 1 4 150 106',
                'OK 9 2 1 4 150 106',
            ]

            if side_effect_fct.counter >= len(vals):
                v = ""
            else:
                v = vals[side_effect_fct.counter]
            side_effect_fct.counter += 1
            return v

        mock_readline.side_effect = side_effect_fct

        mock_cb_1 = MagicMock(return_value=None)
        mock_cb_2 = MagicMock(return_value=None)

        l = LaCrosse('/dev/ttyTEST', 115200)
        l._serial.readline = mock_readline
        l.register_callback(1, mock_cb_1)
        l.register_callback(2, mock_cb_2)

        l._start_worker()
        time.sleep(0.01)
        l._stop_worker()

        eq_(mock_cb_1.call_count, 1)
        eq_(mock_cb_2.call_count, 2)

class TestLaCrosseSensor(object):

    def test_init(self):
        s = LaCrosseSensor('OK 9 1 1 4 150 66')
        eq_(s.sensorid, 1)
        eq_(s.sensortype, 1)
        eq_(s.temperature, 17.4)
        eq_(s.humidity, 66)
        eq_(s.new_battery, False)
        eq_(s.low_battery, False)

@@ -1381,16 +1381,732 @@
 nt, 2)%0A%0A
+    def test_get_info(self):%0A        info = LaCrosse._parse_info('%5BLaCrosseITPlusReader.10.1s (RFM12B f:0 r:17241)%5D')%0A        eq_(info%5B'name'%5D, 'LaCrosseITPlusReader')%0A        eq_(info%5B'version'%5D, '10.1s')%0A%0A        info = LaCrosse._parse_info('%5BLaCrosseITPlusReader.10.1s (RFM12B f:0 r:17241)%5D')%0A        eq_(info%5B'rfm1name'%5D, 'RFM12B')%0A        eq_(info%5B'rfm1frequency'%5D, '0')%0A        eq_(info%5B'rfm1datarate'%5D, '17241')%0A%0A        info = LaCrosse._parse_info('%5BLaCrosseITPlusReader.10.1s (RFM12B f:0 t:10~3)%5D')%0A        eq_(info%5B'rfm1name'%5D, 'RFM12B')%0A        eq_(info%5B'rfm1frequency'%5D, '0')%0A        eq_(info%5B'rfm1datarate'%5D, None)%0A        eq_(info%5B'rfm1toggleinterval'%5D, '10')%0A        eq_(info%5B'rfm1togglemask'%5D, '3')%0A%0A
 class Te

add test