• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

traintastic / traintastic / 23088148108

14 Mar 2026 12:40PM UTC coverage: 26.661% (-0.2%) from 26.84%
23088148108

push

github

reinder
[cbus] added input support for short/long events

0 of 133 new or added lines in 4 files covered. (0.0%)

587 existing lines in 20 files now uncovered.

8246 of 30929 relevant lines covered (26.66%)

185.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

45.21
/server/src/board/tile/rail/sensorrailtile.cpp
1
/**
2
 * This file is part of Traintastic,
3
 * see <https://github.com/traintastic/traintastic>.
4
 *
5
 * Copyright (C) 2020-2026 Reinder Feenstra
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
 */
21

22
#include "sensorrailtile.hpp"
23
#include "../../../world/world.hpp"
24
#include "../../../core/attributes.hpp"
25
#include "../../../core/method.tpp"
26
#include "../../../core/objectproperty.tpp"
27
#include "../../../hardware/input/inputcontroller.hpp"
28
#include "../../../utils/sensor.hpp"
29
#include "../../../utils/category.hpp"
30
#include "../../../utils/displayname.hpp"
31

32
SensorRailTile::SensorRailTile(World& world, std::string_view _id) :
2✔
33
  StraightRailTile(world, _id, TileId::RailSensor),
34
  InputConsumer(static_cast<Object&>(*this), world),
35
  name{this, "name", id, PropertyFlags::ReadWrite | PropertyFlags::Store | PropertyFlags::ScriptReadOnly},
2✔
36
  type{this, "type", SensorType::OccupancyDetector, PropertyFlags::ReadWrite | PropertyFlags::Store,
4✔
37
    [this](SensorType /*value*/)
2✔
38
    {
UNCOV
39
      if(input() && input()->value != TriState::Undefined)
×
40
      {
UNCOV
41
        inputValueChanged(input()->value == TriState::True, input());
×
42
      }
UNCOV
43
    }},
×
44
  invert{this, "invert", false, PropertyFlags::ReadWrite | PropertyFlags::Store,
4✔
45
    [this](bool /*value*/)
2✔
46
    {
UNCOV
47
      if(input() && input()->value != TriState::Undefined)
×
48
      {
UNCOV
49
        inputValueChanged(input()->value == TriState::True, input());
×
50
      }
UNCOV
51
    }},
×
52
  state{this, "state", SensorState::Unknown, PropertyFlags::ReadOnly | PropertyFlags::StoreState | PropertyFlags::ScriptReadOnly}
2✔
53
  , simulateTrigger{*this, "simulate_trigger",
4✔
54
      [this]()
2✔
55
      {
UNCOV
56
        if(input()) /*[[likely]]*/
×
57
        {
UNCOV
58
          input()->interface->inputSimulateChange(input()->channel, input()->location(), SimulateInputAction::Toggle);
×
59
        }
UNCOV
60
      }}
×
61
  , onStateChanged{*this, "on_state_changed", EventFlags::Scriptable}
4✔
62
{
63
  const bool editable = contains(m_world.state.value(), WorldState::Edit);
2✔
64

65
  Attributes::addEnabled(name, editable);
2✔
66
  Attributes::addDisplayName(name, DisplayName::Object::name);
2✔
67
  m_interfaceItems.add(name);
2✔
68

69
  Attributes::addCategory(type, Category::general);
2✔
70
  Attributes::addEnabled(type, editable);
2✔
71
  Attributes::addValues(type, sensorTypeValues);
2✔
72
  m_interfaceItems.add(type);
2✔
73

74
  InputConsumer::addInterfaceItems(m_interfaceItems);
2✔
75

76
  Attributes::addCategory(invert, Category::input);
2✔
77
  Attributes::addEnabled(invert, editable);
2✔
78
  m_interfaceItems.add(invert);
2✔
79

80
  Attributes::addObjectEditor(state, false);
2✔
81
  Attributes::addValues(state, sensorStateValues);
2✔
82
  m_interfaceItems.add(state);
2✔
83

84
  Attributes::addEnabled(simulateTrigger, false);
2✔
85
  Attributes::addObjectEditor(simulateTrigger, false);
2✔
86
  m_interfaceItems.add(simulateTrigger);
2✔
87

88
  m_interfaceItems.add(onStateChanged);
2✔
89
}
2✔
90

91
//! \todo Remove in v0.4
UNCOV
92
void SensorRailTile::load(WorldLoader& loader, const nlohmann::json& data)
×
93
{
UNCOV
94
  if(data["type"] == "occupy_detector")
×
95
  {
UNCOV
96
    nlohmann::json dataCopy = data;
×
97
    dataCopy["type"] = "occupancy_detector";
×
98
    StraightRailTile::load(loader, dataCopy);
×
99
  }
×
100
  else
101
  {
UNCOV
102
    StraightRailTile::load(loader, data);
×
103
  }
UNCOV
104
}
×
105

UNCOV
106
void SensorRailTile::loaded()
×
107
{
UNCOV
108
  StraightRailTile::loaded();
×
109
  InputConsumer::loaded();
×
110
  updateSimulateTriggerEnabled();
×
111
}
×
112

113
void SensorRailTile::destroying()
2✔
114
{
115
  StraightRailTile::destroying();
2✔
116
}
2✔
117

UNCOV
118
void SensorRailTile::worldEvent(WorldState worldState, WorldEvent worldEvent)
×
119
{
UNCOV
120
  StraightRailTile::worldEvent(worldState, worldEvent);
×
121
  InputConsumer::worldEvent(worldState, worldEvent);
×
122

UNCOV
123
  const bool editable = contains(worldState, WorldState::Edit);
×
124

UNCOV
125
  Attributes::setEnabled(name, editable);
×
126
  Attributes::setEnabled(type, editable);
×
127
  Attributes::setEnabled(invert, editable);
×
128
  updateSimulateTriggerEnabled();
×
129
}
×
130

UNCOV
131
void SensorRailTile::inputValueChanged(bool value, const std::shared_ptr<Input>& /*input*/)
×
132
{
UNCOV
133
  const auto newState = toSensorState(type, toTriState(value != invert.value()));
×
134
  if(state != newState)
×
135
  {
UNCOV
136
    state.setValueInternal(newState);
×
137
    fireEvent(onStateChanged, newState, shared_ptr<SensorRailTile>());
×
138
  }
UNCOV
139
}
×
140

UNCOV
141
void SensorRailTile::updateSimulateTriggerEnabled()
×
142
{
UNCOV
143
  Attributes::setEnabled(simulateTrigger, contains(m_world.state, WorldState::Online | WorldState::Simulation) /*&& input*/);
×
144
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc