3 This file is part of R²C²
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
8 #ifndef LIBR2C2_CENTRALSTATION_H_
9 #define LIBR2C2_CENTRALSTATION_H_
11 #include <msp/net/streamsocket.h>
12 #include <msp/time/timestamp.h>
17 class CentralStation: public Driver
28 operator bool() const;
33 typedef std::map<std::string, std::string> AttribMap;
34 typedef std::map<unsigned, AttribMap> ObjectMap;
40 operator bool() const;
46 NERROR_UNKNOWNCOMMAND = 10,
47 NERROR_UNKNOWNARG = 11,
48 NERROR_UNKNOWNPARAM = 12,
49 NERROR_UNKNOWNID = 15,
51 NERROR_NOTALLOWED = 22,
52 NERROR_NOCONTROL = 25,
99 Msp::Time::TimeStamp off_timeout;
104 typedef std::map<unsigned, unsigned> AddressMap;
105 typedef std::map<unsigned, Locomotive> LocoMap;
106 typedef std::map<unsigned, Turnout> TurnoutMap;
107 typedef std::map<unsigned, Sensor> SensorMap;
109 Msp::Net::StreamSocket socket;
110 std::list<std::string> cmd_queue;
111 unsigned pending_commands;
114 std::string in_buffer;
116 AddressMap loco_addr;
119 AddressMap turnout_addr;
120 bool turnouts_synced;
122 std::vector<unsigned> s88;
126 CentralStation(const std::string &);
129 virtual void set_power(bool);
130 virtual bool get_power() const { return power; }
131 virtual void halt(bool);
132 virtual bool is_halted() const { return halted; }
134 virtual const char *enumerate_protocols(unsigned) const;
135 virtual unsigned get_protocol_speed_steps(const std::string &) const;
137 virtual void add_loco(unsigned, const std::string &, const VehicleType &);
138 virtual void set_loco_speed(unsigned, unsigned);
139 virtual void set_loco_reverse(unsigned, bool);
140 virtual void set_loco_function(unsigned, unsigned, bool);
142 virtual void add_turnout(unsigned, const TrackType &);
143 virtual void set_turnout(unsigned, unsigned);
144 virtual unsigned get_turnout(unsigned) const;
146 virtual void add_sensor(unsigned);
147 virtual void set_sensor(unsigned, bool) { }
148 virtual bool get_sensor(unsigned) const;
151 virtual void flush();
154 void command(const std::string &, bool = false);
156 void process_reply(const Message &);
157 void process_event(const Message &);
158 void process_object(unsigned, const Message::AttribMap &);
159 Protocol map_protocol(const std::string &) const;
161 unsigned map_address(const std::map<unsigned, T> &, const AddressMap &, unsigned) const;
162 void skip(std::string::iterator &, const std::string::iterator &, const std::string &) const;
163 std::string parse_token(std::string::iterator &, const std::string::iterator &, const std::string &) const;
164 Tag parse_tag(std::string::iterator &, const std::string::iterator &) const;
165 Message parse_message(std::string::iterator &, const std::string::iterator &) const;