Ejemplo n.º 1
0
    def test_get_mileage_no_mileage_found(self):
        """
        If no mileage is found in the setup file, the odometer should start from 0
        """

        with patch("devices.odometer.SetupFile.get_value") as m_get_value:
            m_get_value.side_effect = AttributeError
            odo = Odometer()

        assert odo.get_mileage() == {"km": 0, "miles": 0}
Ejemplo n.º 2
0
    def test_get_mileage(self, unit, value, expected_km, expected_miles):
        """
        Checks that the odometer reads the correct mileage after setting the value in the json setup file.
        """

        with patch("devices.odometer.SetupFile.get_value") as m_get_value:
            m_get_value.return_value = {"value": value, "unit": unit}
            odo = Odometer()

        assert odo.get_mileage() == {"km": expected_km, "miles": expected_miles}
Ejemplo n.º 3
0
    def test_save_not_enough_distance_traveled(self):
        """
        The mileage should not increase if more than one km more is not reached
        """

        with freeze_time("00:00:00"):
            # setting the starting mileage
            with patch("devices.odometer.SetupFile.get_value") as get_value:
                get_value.return_value = {"value": 0}
                odo = Odometer()

        assert odo.get_mileage() == {"km": 0, "miles": 0}  # we are clean

        with freeze_time("00:00:30"):
            # after 30 secs we will notify the odometer that we have been going to 100km/h
            with patch("devices.odometer.SetupFile.update_key") as m_update_key:
                odo.save(100)
                # checking that we couldn't reach one km more so no update to the setup file is needed
                m_update_key.assert_not_called()

        # the mileage will remain the same
        assert odo.get_mileage() == {"km": 0, "miles": 0}
Ejemplo n.º 4
0
    def test_save(self):
        """
        Checks that the mileage increases after been some time running to a certain speed.
        """

        with freeze_time("00:00:00"):
            # setting the starting mileage
            with patch("devices.odometer.SetupFile.get_value") as get_value:
                get_value.return_value = {"value": 0}
                odo = Odometer()

        assert odo.get_mileage() == {"km": 0, "miles": 0}  # we are clean

        with freeze_time("01:00:00"):
            # after an hour we will notify the odometer that we have been going to 100km/h
            with patch("devices.odometer.SetupFile.update_key") as m_update_key:
                odo.save(100)
                # checking that we tried to save the new mileage into the setup file
                m_update_key.assert_called_with("odo", {"value": 100})

        # after an hour we should be in 100km mileage
        assert odo.get_mileage() == {"km": 100, "miles": 62}
Ejemplo n.º 5
0
 def _init_resources(self):
     self.time = Time()
     self.odo = Odometer()
     self.kpro = Kpro()
Ejemplo n.º 6
0
class Backend:
    _instance = None

    def __init__(self):
        self._init_websocket()
        self._load_user_preferences()
        self._init_resources()

    @staticmethod
    def _init_websocket():
        websocket_started = False
        while not websocket_started:
            try:
                app.run()
                websocket_started = app._started
            except AlreadyRunningError:
                app.stop()
            except (ConnectionRefusedError, TimeoutError):
                # Crossbar router not yet accepting connections, let's try again
                pass

    def _init_resources(self):
        self.time = Time()
        self.odo = Odometer()
        self.kpro = Kpro()

    def _load_user_preferences(self):
        """
        In order to read only once from the setup file
        we will load in memory some user preferences that we are gonna use later on.
        """
        self.setup_file = SetupFile()

        self.style = Style(
            self.setup_file.json.get("style").get("tpsLowerThreshold"),
            self.setup_file.json.get("style").get("tpsUpperThreshold"),
            self.setup_file.json.get("style").get("elapsedSeconds"),
        )

        self.iat_unit = self.setup_file.json.get("iat",
                                                 {}).get("unit", "celsius")
        self.ect_unit = self.setup_file.json.get("ect",
                                                 {}).get("unit", "celsius")
        self.vss_unit = self.setup_file.json.get("vss", {}).get("unit", "kmh")
        self.o2_unit = self.setup_file.json.get("o2", {}).get("unit", "afr")
        self.odo_unit = self.setup_file.json.get("odo", {}).get("unit", "km")
        self.map_unit = self.setup_file.json.get("map", {}).get("unit", "bar")
        self.an0_unit = self.setup_file.json.get("an0",
                                                 {}).get("unit", "volts")
        self.an1_unit = self.setup_file.json.get("an1",
                                                 {}).get("unit", "volts")
        self.an2_unit = self.setup_file.json.get("an2",
                                                 {}).get("unit", "volts")
        self.an3_unit = self.setup_file.json.get("an3",
                                                 {}).get("unit", "volts")
        self.an4_unit = self.setup_file.json.get("an4",
                                                 {}).get("unit", "volts")
        self.an5_unit = self.setup_file.json.get("an5",
                                                 {}).get("unit", "volts")
        self.an6_unit = self.setup_file.json.get("an6",
                                                 {}).get("unit", "volts")
        self.an7_unit = self.setup_file.json.get("an7",
                                                 {}).get("unit", "volts")

        self.an0_formula = self.setup_file.get_formula("an0")
        self.an1_formula = self.setup_file.get_formula("an1")
        self.an2_formula = self.setup_file.get_formula("an2")
        self.an3_formula = self.setup_file.get_formula("an3")
        self.an4_formula = self.setup_file.get_formula("an4")
        self.an5_formula = self.setup_file.get_formula("an5")
        self.an6_formula = self.setup_file.get_formula("an6")
        self.an7_formula = self.setup_file.get_formula("an7")

    def update(self):
        """ load the websocket with updated info """
        if not self.kpro.status:  # if kpro is down try to reconnect
            self.kpro.find_and_connect()
        self.odo.save(self.kpro.vss["kmh"])
        self.style.update(self.kpro.tps)
        publish(
            "data",
            {
                "bat": self.kpro.bat,
                "gear": self.kpro.gear,
                "iat": self.kpro.iat[self.iat_unit],
                "tps": self.kpro.tps,
                "ect": self.kpro.ect[self.ect_unit],
                "rpm": self.kpro.rpm,
                "vss": self.kpro.vss[self.vss_unit],
                "o2": self.kpro.o2[self.o2_unit],
                "cam": self.kpro.cam,
                "mil": self.kpro.mil,
                "fan": self.kpro.fanc,
                "bksw": self.kpro.bksw,
                "flr": self.kpro.flr,
                "eth": self.kpro.eth,
                "scs": self.kpro.scs,
                "fmw": self.kpro.firmware,
                "map": self.kpro.map[self.map_unit],
                "an0": self.an0_formula(
                    self.kpro.analog_input(0))[self.an0_unit],
                "an1": self.an1_formula(
                    self.kpro.analog_input(1))[self.an1_unit],
                "an2": self.an2_formula(
                    self.kpro.analog_input(2))[self.an2_unit],
                "an3": self.an3_formula(
                    self.kpro.analog_input(3))[self.an3_unit],
                "an4": self.an4_formula(
                    self.kpro.analog_input(4))[self.an4_unit],
                "an5": self.an5_formula(
                    self.kpro.analog_input(5))[self.an5_unit],
                "an6": self.an6_formula(
                    self.kpro.analog_input(6))[self.an6_unit],
                "an7": self.an7_formula(
                    self.kpro.analog_input(7))[self.an7_unit],
                "time": self.time.get_time(),
                "odo": self.odo.get_mileage()[self.odo_unit],
                "style": self.style.status,
                "ver": __version__,
            },
        )

    def _setup(self):
        return self.setup_file.load_setup()

    def _save(self, new_setup):
        self.setup_file.save_setup(new_setup)
        self.setup_file.rotate_screen(new_setup["screen"]["rotate"])
        publish(
            "refresh")  # refresh the frontend so the new changes are applied
        self._load_user_preferences()  # refresh the backend too

    def _reset(self):
        self.setup_file.reset_setup()
        publish(
            "refresh")  # refresh the frontend so the new changes are applied
        self._load_user_preferences()  # refresh the backend too

    @classmethod
    def get(cls):
        """ get the running instance of Backend class or instantiate it for first time """
        if cls._instance is None:
            cls._instance = cls()
        return cls._instance

    @classmethod
    def setup(cls):
        return cls.get()._setup()

    @classmethod
    def save(cls, new_setup):
        cls.get()._save(new_setup)

    @classmethod
    def reset(cls):
        cls.get()._reset()
Ejemplo n.º 7
0
class Backend:
    def __init__(self):
        self._load_user_preferences()
        self._init_resources()
        self._init_websocket()

    def stop(self):
        self.websocket.stop()

    def _init_websocket(self):
        self.websocket = Websocket(self)

    def _init_resources(self):
        self.time = Time()
        self.odo = Odometer()
        self.kpro = Kpro()

    def _load_user_preferences(self):
        """
        In order to read only once from the setup file
        we will load in memory some user preferences that we are gonna use later on.
        """
        self.setup_file = SetupFile()

        self.style = Style(
            self.setup_file.json.get("style").get("tpsLowerThreshold"),
            self.setup_file.json.get("style").get("tpsUpperThreshold"),
            self.setup_file.json.get("style").get("elapsedSeconds"),
        )

        self.iat_unit = self.setup_file.json.get("iat",
                                                 {}).get("unit", "celsius")
        self.ect_unit = self.setup_file.json.get("ect",
                                                 {}).get("unit", "celsius")
        self.vss_unit = self.setup_file.json.get("vss", {}).get("unit", "kmh")
        self.o2_unit = self.setup_file.json.get("o2", {}).get("unit", "afr")
        self.odo_unit = self.setup_file.json.get("odo", {}).get("unit", "km")
        self.map_unit = self.setup_file.json.get("map", {}).get("unit", "bar")
        self.an0_unit = self.setup_file.json.get("an0",
                                                 {}).get("unit", "volts")
        self.an1_unit = self.setup_file.json.get("an1",
                                                 {}).get("unit", "volts")
        self.an2_unit = self.setup_file.json.get("an2",
                                                 {}).get("unit", "volts")
        self.an3_unit = self.setup_file.json.get("an3",
                                                 {}).get("unit", "volts")
        self.an4_unit = self.setup_file.json.get("an4",
                                                 {}).get("unit", "volts")
        self.an5_unit = self.setup_file.json.get("an5",
                                                 {}).get("unit", "volts")
        self.an6_unit = self.setup_file.json.get("an6",
                                                 {}).get("unit", "volts")
        self.an7_unit = self.setup_file.json.get("an7",
                                                 {}).get("unit", "volts")

        self.an0_formula = self.setup_file.get_formula("an0")
        self.an1_formula = self.setup_file.get_formula("an1")
        self.an2_formula = self.setup_file.get_formula("an2")
        self.an3_formula = self.setup_file.get_formula("an3")
        self.an4_formula = self.setup_file.get_formula("an4")
        self.an5_formula = self.setup_file.get_formula("an5")
        self.an6_formula = self.setup_file.get_formula("an6")
        self.an7_formula = self.setup_file.get_formula("an7")

    def update(self):
        """ load the websocket with updated info """
        if not self.kpro.status:  # if kpro is down try to reconnect
            self.kpro.find_and_connect()
        self.odo.save(self.kpro.vss["kmh"])
        self.style.update(self.kpro.tps)
        return {
            "bat": self.kpro.bat,
            "gear": self.kpro.gear,
            "iat": self.kpro.iat[self.iat_unit],
            "tps": self.kpro.tps,
            "ect": self.kpro.ect[self.ect_unit],
            "rpm": self.kpro.rpm,
            "vss": self.kpro.vss[self.vss_unit],
            "o2": self.kpro.o2[self.o2_unit],
            "cam": self.kpro.cam,
            "mil": self.kpro.mil,
            "fan": self.kpro.fanc,
            "bksw": self.kpro.bksw,
            "flr": self.kpro.flr,
            "eth": self.kpro.eth,
            "scs": self.kpro.scs,
            "fmw": self.kpro.firmware,
            "map": self.kpro.map[self.map_unit],
            "an0": self.an0_formula(self.kpro.analog_input(0))[self.an0_unit],
            "an1": self.an1_formula(self.kpro.analog_input(1))[self.an1_unit],
            "an2": self.an2_formula(self.kpro.analog_input(2))[self.an2_unit],
            "an3": self.an3_formula(self.kpro.analog_input(3))[self.an3_unit],
            "an4": self.an4_formula(self.kpro.analog_input(4))[self.an4_unit],
            "an5": self.an5_formula(self.kpro.analog_input(5))[self.an5_unit],
            "an6": self.an6_formula(self.kpro.analog_input(6))[self.an6_unit],
            "an7": self.an7_formula(self.kpro.analog_input(7))[self.an7_unit],
            "time": self.time.get_time(),
            "odo": self.odo.get_mileage()[self.odo_unit],
            "style": self.style.status,
            "ver": __version__,
        }

    def setup(self):
        """Return the current setup"""
        return self.setup_file.load_setup()

    def save(self, new_setup):
        """Save a new setup"""
        self.setup_file.save_setup(new_setup)
        self.setup_file.rotate_screen(new_setup["screen"]["rotate"])
        self._load_user_preferences()  # refresh the backend too

    def reset(self):
        """Reset to the default setup"""
        self.setup_file.reset_setup()
        self._load_user_preferences()  # refresh the backend too
Ejemplo n.º 8
0
from devices.kpro import Kpro
from devices.time import Time
from devices.odometer import Odometer

from mapper import Mapper

while True:
    try:
        run()
        break
    except:
        continue

kpro = Kpro()
time = Time()
odo = Odometer()
map = Mapper()

iat_unit = map.get_unit('iat')
ect_unit = map.get_unit('ect')
vss_unit = map.get_unit('vss')
o2_unit = map.get_unit('o2')
map_unit = map.get_unit('map')
an0_unit = map.get_unit('an0')
an0_formula = map.get_formula('an0')
an1_unit = map.get_unit('an1')
an1_formula = map.get_formula('an1')
an2_unit = map.get_unit('an2')
an2_formula = map.get_formula('an2')
an3_unit = map.get_unit('an3')
an3_formula = map.get_formula('an3')
Ejemplo n.º 9
0
import random
from time import sleep

from autobahn_sync import publish, run

from devices.formula import Formula
from devices.odometer import Odometer
from devices.time import Time

run()

time = Time()
odo = Odometer()
while True:
    odo.save(random.randint(0, 200))
    publish(
        'com.app.idea', {
            'bat': random.uniform(0, 15),
            'map': {
                'bar': random.uniform(0, 2),
                'mbar': random.uniform(0, 2),
                'psi': random.uniform(0, 2)
            },
            'gear': random.randint(0, 6),
            'iat': {
                'celsius': random.randint(0, 50),
                'fahrenheit': random.randint(0, 50)
            },
            'tps': random.randint(0, 100),
            'ect': {
                'celsius': random.randint(0, 50),
Ejemplo n.º 10
0
import random
from time import sleep

from autobahn_sync import publish, run

from devices.formula import Formula
from devices.odometer import Odometer
from devices.time import Time

run()

time = Time()
odo = Odometer()
while True:
    odo.save(random.randint(0, 200))
    publish('com.app.idea', {'bat': random.uniform(0, 15),
                             'map': {'bar': random.uniform(0, 2), 'mbar': random.uniform(0, 2), 'psi': random.uniform(0, 2)},
                             'gear': random.randint(0, 6),
                             'iat': {'celsius': random.randint(0, 50), 'fahrenheit': random.randint(0, 50)},
                             'tps': random.randint(0, 100),
                             'ect': {'celsius': random.randint(0, 50), 'fahrenheit': random.randint(0, 50)},
                             'rpm': random.randint(0, 9000),
                             'vss': {'kmh': random.randint(0, 200), 'mph': random.randint(0, 200)},
                             'o2': {'afr': random.uniform(0, 20), 'lambda': random.uniform(0, 1)},
                             'cam': random.randint(0, 50),
                             'mil': random.choice([True, False]),
                             'bksw': random.choice([True, False]),
                             'an0': Formula.psi_to_bar(Formula.ebay_150_psi(random.uniform(0, 5))),
                             'an1': Formula.autometer_2246(random.uniform(0, 5)),
                             'an2': random.uniform(0, 5),
                             'an3': random.uniform(0, 5),
Ejemplo n.º 11
0
@register(u"reset")
def reset():
    setup_file.reset_setup()


while True:
    try:
        run()
        break
    except Exception:
        continue

time = Time()
setup_file = SetupFile()
odo = Odometer()
style = Style(
    setup_file.json.get("style").get("tpsLowerThreshold"),
    setup_file.json.get("style").get("tpsUpperThreshold"),
    setup_file.json.get("style").get("elapsedSeconds"),
)
kpro = Kpro()

iat_unit = setup_file.json.get("iat", {}).get("unit", "celsius")
ect_unit = setup_file.json.get("ect", {}).get("unit", "celsius")
vss_unit = setup_file.json.get("vss", {}).get("unit", "kmh")
o2_unit = setup_file.json.get("o2", {}).get("unit", "afr")
odo_unit = setup_file.json.get("odo", {}).get("unit", "km")
map_unit = setup_file.json.get("map", {}).get("unit", "bar")
an0_unit = setup_file.json.get("an0", {}).get("unit", "volts")
an1_unit = setup_file.json.get("an1", {}).get("unit", "volts")
Ejemplo n.º 12
0
@register(u'save')
def save(new_setup):
    setup_file.save_setup(new_setup)
    setup_file.rotate_screen(new_setup['screen']['rotate'])


while True:
    try:
        run()
        break
    except Exception:
        continue

time = Time()
setup_file = SetupFile()
odo = Odometer()
kpro = Kpro()

iat_unit = setup_file.json.get('iat', {}).get('unit', 'celsius')
ect_unit = setup_file.json.get('ect', {}).get('unit', 'celsius')
vss_unit = setup_file.json.get('vss', {}).get('unit', 'kmh')
o2_unit = setup_file.json.get('o2', {}).get('unit', 'afr')
odo_unit = setup_file.json.get('odo', {}).get('unit', 'km')
map_unit = setup_file.json.get('map', {}).get('unit', 'bar')
an0_unit = setup_file.json.get('an0', {}).get('unit', 'volts')
an1_unit = setup_file.json.get('an1', {}).get('unit', 'volts')
an2_unit = setup_file.json.get('an2', {}).get('unit', 'volts')
an3_unit = setup_file.json.get('an3', {}).get('unit', 'volts')
an4_unit = setup_file.json.get('an4', {}).get('unit', 'volts')
an5_unit = setup_file.json.get('an5', {}).get('unit', 'volts')
an6_unit = setup_file.json.get('an6', {}).get('unit', 'volts')