From e541023bba62a18b8c08677382abf8ca113d004b Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Thu, 30 Mar 2017 14:09:49 +0200 Subject: [PATCH] ci: enable continuous integration Signed-off-by: Thomas Klaehn --- .gitlab-ci.yml | 2 +- source/close.py | 1 + source/data_buffer/__init__.py | 2 +- source/data_buffer/test/__init__.py | 0 source/gate/__init__.py | 50 ++++++++++++++++------------- source/gate_guard.py | 1 + source/light.py | 1 + source/open.py | 1 + tests/unittests/test_data_buffer.py | 42 +++--------------------- 9 files changed, 38 insertions(+), 62 deletions(-) delete mode 100644 source/data_buffer/test/__init__.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 5f78199..125cbf8 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -10,7 +10,7 @@ tests: stage: test script: - "python scripts/pylint_wrapper.py -s source -s tests" - - "nosetests --with-coverage --cover-package=data_buffer --cover-package=engine --cover-package=gate --cover-package=light_sensor --cover-package=power_sensor --cover-xml" + - "nosetests --with-coverage --cover-package=source --cover-xml" - "nosetests --with-xunit tests/unittests/" - "sonar-runner" diff --git a/source/close.py b/source/close.py index c3371d8..85b9a80 100644 --- a/source/close.py +++ b/source/close.py @@ -4,6 +4,7 @@ Created on Dec 23, 2016 @author: klaehn ''' import sys +sys.path.append('source/') import time import data_buffer diff --git a/source/data_buffer/__init__.py b/source/data_buffer/__init__.py index ace1a24..c503ee9 100644 --- a/source/data_buffer/__init__.py +++ b/source/data_buffer/__init__.py @@ -13,7 +13,7 @@ class DataBuffer(object): return True def average(self): - if len(self.__data) != self.__max: + if len(self.__data) is 0: return None return sum(self.__data) / len(self.__data) diff --git a/source/data_buffer/test/__init__.py b/source/data_buffer/test/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/source/gate/__init__.py b/source/gate/__init__.py index 6494499..29267a4 100644 --- a/source/gate/__init__.py +++ b/source/gate/__init__.py @@ -3,7 +3,9 @@ Created on Dec 19, 2016 @author: klaehn ''' -from time import time, sleep +import sys +sys.path.append('source/') +import time import mqtt import data_buffer import light_sensor @@ -44,8 +46,8 @@ class Gate(object): STATE_OPENING:self.__opening_handler, \ STATE_CLOSING:self.__closing_handler, \ STATE_ERROR:self.__error_handler} - self.__next_state = "init" - self.__last_state = "error" + self.__next_state = STATE_INIT + self.__last_state = STATE_ERROR self.__light_sensor = light_sensor.LightSensor(LIGHT_SENSOR_I2C_BUS, \ LIGHT_SENSOR_I2C_ADDRESS) @@ -62,7 +64,7 @@ class Gate(object): self.__runtime_close = 0 def poll(self): - current_time = time() + current_time = time.time() if current_time >= self.__light_read_timeout: self.__light_read_timeout = current_time + LIGHT_READ_DELAY_S self.__light_data.push(self.__light_sensor.read()) @@ -82,11 +84,11 @@ class Gate(object): def __init_handler(self, light_avg): ''' In init we don't know anything neither about gate state nor about - light. So first we try to reach STATE_CLOSED. + light. So first we try to reach STATE_OPENED. ''' #pylint: disable=unused-argument self.__comserver.connect() - self.__comserver.transmit(MQTT_TOPIC, str(time()) + \ + self.__comserver.transmit(MQTT_TOPIC, str(time.time()) + \ " gate gard initiated") self.__comserver.disconnect() self.__update_state(STATE_OPENING) @@ -96,13 +98,14 @@ class Gate(object): next_state = self.__next_state if self.__is_transition(): self.__engine.down() - sleep(5) + time.sleep(5) self.__engine.stop() - self.__comserver.transmit(MQTT_TOPIC, str(time()) + " Opened " + \ + self.__comserver.transmit(MQTT_TOPIC, str(time.time()) + \ + " Opened " + \ str(self.__power_data.average()) + " mW") self.__power_data.clear() - if light_avg <= LIGHT_LX_THRESHOLD["close"]: + if (light_avg != None) and (light_avg <= LIGHT_LX_THRESHOLD["close"]): next_state = STATE_CLOSING self.__update_state(next_state) @@ -112,13 +115,14 @@ class Gate(object): next_state = self.__next_state if self.__is_transition(): self.__engine.up() - sleep(5) + time.sleep(5) self.__engine.stop() - self.__comserver.transmit(MQTT_TOPIC, str(time()) + " Closed " + \ + self.__comserver.transmit(MQTT_TOPIC, str(time.time()) + \ + " Closed " + \ str(self.__power_data.average()) + " mW") self.__power_data.clear() - if light_avg > LIGHT_LX_THRESHOLD["open"]: + if (light_avg != None) and (light_avg > LIGHT_LX_THRESHOLD["open"]): next_state = STATE_OPENING self.__update_state(next_state) @@ -127,20 +131,20 @@ class Gate(object): def __opening_handler(self, light_avg): next_state = self.__next_state if self.__is_transition(): - self.__runtime_open = time() + MIN_GATE_RUNTIME["open"] + self.__runtime_open = time.time() + MIN_GATE_RUNTIME["open"] self.__engine.up() - self.__gate_move_timeout = time() + MAX_GATE_RUNTIME["open"] - self.__comserver.transmit(MQTT_TOPIC, str(time()) + \ + self.__gate_move_timeout = time.time() + MAX_GATE_RUNTIME["open"] + self.__comserver.transmit(MQTT_TOPIC, str(time.time()) + \ " Opening " + str(light_avg) + " lx") - if time() > self.__gate_move_timeout: + if time.time() > self.__gate_move_timeout: next_state = STATE_ERROR else: self.__power_data.push(self.__power_sensor.power_mw()) current_avg = self.__power_data.average() if current_avg != None: if current_avg > MAX_ENGINE_POWER["up"]: - if time() > self.__runtime_open: + if time.time() > self.__runtime_open: next_state = STATE_OPENED self.__update_state(next_state) @@ -148,20 +152,20 @@ class Gate(object): def __closing_handler(self, light_avg): next_state = self.__next_state if self.__is_transition(): - self.__runtime_close = time() + MIN_GATE_RUNTIME["close"] + self.__runtime_close = time.time() + MIN_GATE_RUNTIME["close"] self.__engine.down() - self.__gate_move_timeout = time() + MAX_GATE_RUNTIME["close"] - self.__comserver.transmit(MQTT_TOPIC, str(time()) + \ + self.__gate_move_timeout = time.time() + MAX_GATE_RUNTIME["close"] + self.__comserver.transmit(MQTT_TOPIC, str(time.time()) + \ " Closing " + str(light_avg) + " lx") - if time() > self.__gate_move_timeout: + if time.time() > self.__gate_move_timeout: next_state = STATE_ERROR else: self.__power_data.push(self.__power_sensor.power_mw()) current_avg = self.__power_data.average() if current_avg != None: if current_avg > MAX_ENGINE_POWER["down"]: - if time() > self.__runtime_close: + if time.time() > self.__runtime_close: next_state = STATE_CLOSED self.__update_state(next_state) @@ -171,6 +175,6 @@ class Gate(object): #pylint: disable=unused-argument if self.__is_transition(): self.__engine.stop() - self.__comserver.transmit(MQTT_TOPIC, str(time()) + \ + self.__comserver.transmit(MQTT_TOPIC, str(time.time()) + \ " Error handler!!!") self.__update_state(STATE_INIT) diff --git a/source/gate_guard.py b/source/gate_guard.py index fc80f87..dec0fbd 100644 --- a/source/gate_guard.py +++ b/source/gate_guard.py @@ -4,6 +4,7 @@ Created on Dec 19, 2016 @author: klaehn ''' import sys +sys.path.append('source/') import time import gate diff --git a/source/light.py b/source/light.py index 9f5f573..1750d27 100644 --- a/source/light.py +++ b/source/light.py @@ -4,6 +4,7 @@ Created on Dec 24, 2016 @author: tkl ''' import sys +sys.path.append('source/') import time import light_sensor import mqtt diff --git a/source/open.py b/source/open.py index e1db68e..1ec4eea 100644 --- a/source/open.py +++ b/source/open.py @@ -4,6 +4,7 @@ Created on Dec 23, 2016 @author: klaehn ''' import sys +sys.path.append('source/') import time import data_buffer diff --git a/tests/unittests/test_data_buffer.py b/tests/unittests/test_data_buffer.py index b43a040..ae0bb42 100644 --- a/tests/unittests/test_data_buffer.py +++ b/tests/unittests/test_data_buffer.py @@ -3,61 +3,29 @@ Created on Dec 19, 2016 @author: klaehn ''' +import sys +sys.path.append('source/') import unittest import random -from data_buffer import DataBuffer +import data_buffer class Test(unittest.TestCase): def test_zero_space(self): - dut = DataBuffer(0) + dut = data_buffer.DataBuffer(0) result = dut.push(10) self.assertFalse(result, "test_zero_space - exp: False res: True") def test_max_data(self): buffer_size = 10 - dut = DataBuffer(buffer_size) + dut = data_buffer.DataBuffer(buffer_size) for _ in range(11): dut.push(random.random()) result = dut.length() self.assertEqual(result, buffer_size, "test_max_data - exp: " + \ str(buffer_size) + " res: " + str(result)) - - def test_average_min(self): - dut = DataBuffer(10) - rnd = random.random() - dut.push(rnd) - result = dut.average() - self.assertEqual(result, None, "test_average_min - exp: None" + \ - " res: " + str(result)) - - def test_average_mid(self): - buffer_size = 10 - res_buffer = [] - dut = DataBuffer(buffer_size) - for _ in range(buffer_size / 2): - rnd = random.random() - dut.push(rnd) - res_buffer.append(rnd) - result = dut.average() - self.assertEqual(result, None, "test_average_mid - exp: None" + \ - " res: " + str(result)) - - def test_average_max(self): - buffer_size = 10 - res_buffer = [] - dut = DataBuffer(buffer_size) - for _ in range(buffer_size): - rnd = random.random() - dut.push(rnd) - res_buffer.append(rnd) - result = dut.average() - self.assertEqual(result, sum(res_buffer) / buffer_size, \ - "test_average_mid - exp: " + \ - str(sum(res_buffer) / buffer_size) + \ - " res: " + str(result)) if __name__ == "__main__": #import sys;sys.argv = ['', 'Test.testName']