]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/sensors.h
String coversion added to the sensors for debugging purpoces
[stg.git] / projects / stargazer / plugins / other / smux / sensors.h
1 #ifndef __SENSORS_H__
2 #define __SENSORS_H__
3
4 #include <map>
5
6 #include "stg/users.h"
7 #include "stg/tariffs.h"
8 #include "stg/user_property.h"
9
10 #include "stg/ObjectSyntax.h"
11
12 #include "value2os.h"
13 #include "types.h"
14
15 class Sensor {
16     public:
17         virtual bool GetValue(ObjectSyntax_t * objectSyntax) const = 0;
18 #ifdef DEBUG
19         virtual std::string ToString() const = 0;
20 #endif
21 };
22
23 typedef std::map<OID, Sensor *> Sensors;
24
25 class TotalUsersSensor : public Sensor {
26     public:
27         TotalUsersSensor(const USERS & u) : users(u) {}
28         virtual ~TotalUsersSensor() {}
29
30         bool GetValue(ObjectSyntax_t * objectSyntax) const
31         {
32         ValueToOS(users.GetUserNum(), objectSyntax);
33         return true;
34         }
35
36 #ifdef DEBUG
37         std::string ToString() const
38         { std::string res; x2str(users.GetUserNum(), res); return res; }
39 #endif
40
41     private:
42         const USERS & users;
43 };
44
45 class UsersSensor : public Sensor {
46     public:
47         UsersSensor(USERS & u) : users(u) {}
48         virtual ~UsersSensor() {}
49
50         bool GetValue(ObjectSyntax_t * objectSyntax) const;
51 #ifdef DEBUG
52         std::string ToString() const;
53 #endif
54
55     private:
56         USERS & users;
57
58         virtual bool UserPredicate(USER_PTR userPtr) const = 0;
59 };
60
61 class ConnectedUsersSensor : public UsersSensor {
62     public:
63         ConnectedUsersSensor(USERS & u) : UsersSensor(u) {}
64         virtual ~ConnectedUsersSensor() {}
65
66     private:
67         bool UserPredicate(USER_PTR userPtr) const
68         { return userPtr->GetConnected(); }
69 };
70
71 class AuthorizedUsersSensor : public UsersSensor {
72     public:
73         AuthorizedUsersSensor(USERS & u) : UsersSensor(u) {}
74         virtual ~AuthorizedUsersSensor() {}
75
76     private:
77         bool UserPredicate(USER_PTR userPtr) const
78         { return userPtr->GetAuthorized(); }
79 };
80
81 class AlwaysOnlineUsersSensor : public UsersSensor {
82     public:
83         AlwaysOnlineUsersSensor(USERS & u) : UsersSensor(u) {}
84         virtual ~AlwaysOnlineUsersSensor() {}
85
86     private:
87         bool UserPredicate(USER_PTR userPtr) const
88         { return userPtr->GetProperty().alwaysOnline; }
89 };
90
91 class NoCashUsersSensor : public UsersSensor {
92     public:
93         NoCashUsersSensor(USERS & u) : UsersSensor(u) {}
94         virtual ~NoCashUsersSensor() {}
95
96     private:
97         bool UserPredicate(USER_PTR userPtr) const
98         { return userPtr->GetProperty().cash < 0; }
99 };
100
101 class DisabledDetailStatsUsersSensor : public UsersSensor {
102     public:
103         DisabledDetailStatsUsersSensor(USERS & u) : UsersSensor(u) {}
104         virtual ~DisabledDetailStatsUsersSensor() {}
105
106     private:
107         bool UserPredicate(USER_PTR userPtr) const
108         { return userPtr->GetProperty().disabledDetailStat; }
109 };
110
111 class DisabledUsersSensor : public UsersSensor {
112     public:
113         DisabledUsersSensor(USERS & u) : UsersSensor(u) {}
114         virtual ~DisabledUsersSensor() {}
115
116     private:
117         bool UserPredicate(USER_PTR userPtr) const
118         { return userPtr->GetProperty().disabled; }
119 };
120
121 class PassiveUsersSensor : public UsersSensor {
122     public:
123         PassiveUsersSensor(USERS & u) : UsersSensor(u) {}
124         virtual ~PassiveUsersSensor() {}
125
126     private:
127         bool UserPredicate(USER_PTR userPtr) const
128         { return userPtr->GetProperty().passive; }
129 };
130
131 class CreditUsersSensor : public UsersSensor {
132     public:
133         CreditUsersSensor(USERS & u) : UsersSensor(u) {}
134         virtual ~CreditUsersSensor() {}
135
136     private:
137         bool UserPredicate(USER_PTR userPtr) const
138         { return userPtr->GetProperty().credit > 0; }
139 };
140
141 class FreeMbUsersSensor : public UsersSensor {
142     public:
143         FreeMbUsersSensor(USERS & u) : UsersSensor(u) {}
144         virtual ~FreeMbUsersSensor() {}
145
146     private:
147         bool UserPredicate(USER_PTR userPtr) const
148         { return userPtr->GetProperty().freeMb > 0; }
149 };
150
151 class TariffChangeUsersSensor : public UsersSensor {
152     public:
153         TariffChangeUsersSensor(USERS & u) : UsersSensor(u) {}
154         virtual ~TariffChangeUsersSensor() {}
155
156     private:
157         bool UserPredicate(USER_PTR userPtr) const
158         { return userPtr->GetProperty().nextTariff.ConstData().empty(); }
159 };
160
161 class TotalTariffsSensor : public Sensor {
162     public:
163         TotalTariffsSensor(const TARIFFS & t) : tariffs(t) {}
164         virtual ~TotalTariffsSensor() {}
165
166         bool GetValue(ObjectSyntax_t * objectSyntax) const
167         {
168         ValueToOS(tariffs.GetTariffsNum(), objectSyntax);
169         return true;
170         }
171
172 #ifdef DEBUG
173         std::string ToString() const
174         { std::string res; x2str(tariffs.GetTariffsNum(), res); return res; }
175 #endif
176
177     private:
178         const TARIFFS & tariffs;
179 };
180
181 template <typename T>
182 class ConstSensor : public Sensor {
183     public:
184         ConstSensor(const T & v) : value(v) {}
185         virtual ~ConstSensor() {}
186
187         bool GetValue(ObjectSyntax * objectSyntax) const
188         { return ValueToOS(value, objectSyntax); }
189
190 #ifdef DEBUG
191         std::string ToString() const
192         { std::string res; x2str(value, res); return res; }
193 #endif
194
195     private:
196         T value;
197 };
198
199 template <>
200 inline
201 std::string ConstSensor<std::string>::ToString() const
202 {
203 return value;
204 }
205
206 #endif