-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScript.cs
260 lines (215 loc) · 9.12 KB
/
Script.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
string _script_name = "Zephyr Industries PubSub Controller";
string _script_version = "1.0.2";
string _script_title = null;
string _script_title_nl = null;
const string PUBSUB_ID = "zi.pubsub";
const int PANELS_DEBUG = 0;
const int PANELS_WARN = 1;
const int SIZE_PANELS = 2;
const string CHART_TIME = "PubSub Exec Time";
const string CHART_LOAD = "PubSub Instr Load";
const string CHART_EVENTS_RX = "PubSub Events Rx";
const string CHART_EVENTS_TX = "PubSub Events Tx";
List<string> _panel_tags = new List<string>(SIZE_PANELS) { "@PubSubDebugDisplay", "@PubSubWarningDisplay" };
/* Genuine global state */
int _cycles = 0;
// event_name => list of subscriber programmable blocks
Dictionary<string, HashSet<IMyProgrammableBlock>> _subscriptions = new Dictionary<string, HashSet<IMyProgrammableBlock>>();
List<List<IMyTextPanel>> _panels = new List<List<IMyTextPanel>>(SIZE_PANELS);
List<string> _panel_text = new List<string>(SIZE_PANELS) { "", "", };
double _time_total = 0.0, _last_run_time_ms_tally = 0.0;
int _events_rx = 0, _events_tx = 0;
/* Reused single-run state objects, only global to avoid realloc/gc-thrashing */
// FIXME: _chart here? _panel?
MyCommandLine _command_line = new MyCommandLine();
HashSet<IMyProgrammableBlock> _subscribers;
public Program() {
_script_title = $"{_script_name} v{_script_version}";
_script_title_nl = $"{_script_name} v{_script_version}\n";
for (int i = 0; i < SIZE_PANELS; i++) {
_panels.Add(new List<IMyTextPanel>());
}
FindPanels();
if (!Me.CustomName.Contains(_script_name)) {
// Update our block to include our script name
Me.CustomName = $"{Me.CustomName} - {_script_name}";
}
Log(_script_title);
// Only for chart updates
Runtime.UpdateFrequency |= UpdateFrequency.Update100;
}
public void Save() {
}
public void Main(string argument, UpdateType updateSource) {
try {
// Tally up all invocation times and record them as one on the non-command runs.
_last_run_time_ms_tally += Runtime.LastRunTimeMs;
if ((updateSource & UpdateType.Update100) != 0) {
_cycles++;
ProcessCommand($"event {PUBSUB_ID} datapoint.issue \"{CHART_TIME}\" {TimeAsUsec(_last_run_time_ms_tally)}");
if (_cycles > 1) {
_time_total += _last_run_time_ms_tally;
if (_cycles == 201) {
Warning($"Total time after 200 cycles: {_time_total}ms.");
}
}
_last_run_time_ms_tally = 0.0;
ClearPanels(PANELS_DEBUG);
Log(_script_title_nl);
if ((_cycles % 30) == 0) {
FindPanels();
ProcessCommand($"event {PUBSUB_ID} dataset.create \"{CHART_TIME}\" \"us\"");
ProcessCommand($"event {PUBSUB_ID} dataset.create \"{CHART_LOAD}\" \"%\"");
ProcessCommand($"event {PUBSUB_ID} dataset.create \"{CHART_EVENTS_RX}\" \"\"");
ProcessCommand($"event {PUBSUB_ID} dataset.create \"{CHART_EVENTS_TX}\" \"\"");
}
double load = (double)Runtime.CurrentInstructionCount * 100.0 / (double)Runtime.MaxInstructionCount;
ProcessCommand($"event {PUBSUB_ID} datapoint.issue \"{CHART_LOAD}\" {load}");
// Slightly sneaky, push the counts for sending rx/tx themseles onto the next cycle.
int rx = _events_rx, tx = _events_tx;
_events_rx = 0;
_events_tx = 0;
ProcessCommand($"event {PUBSUB_ID} datapoint.issue \"{CHART_EVENTS_RX}\" {rx}");
ProcessCommand($"event {PUBSUB_ID} datapoint.issue \"{CHART_EVENTS_TX}\" {tx}");
//long load_avg = (long)Chart.Find(CHART_LOAD).Avg;
//long time_avg = (long)Chart.Find(CHART_TIME).Avg;
//Log($" [Avg ] Load {load_avg}% in {time_avg}us");
// Start at T-1 - exec time hasn't been updated yet.
//for (int i = 1; i < 16; i++) {
/* FIXME:
long load = (long)Chart.Find(CHART_LOAD).Datapoint(-i);
long time = (long)Chart.Find(CHART_TIME).Datapoint(-i);
Log($" [T-{i,-2}] Load {load}% in {time}us");
*/
//}
// FIXME: events/subscribers Log($"Charts: {Chart.Count}, DrawBuffers: {Chart.BufferCount}");
Log($"[Cycle {_cycles}]\n Events received: {rx}, Events transmitted: {tx}");
FlushToPanels(PANELS_DEBUG);
}
//if ((updateSource & (UpdateType.Trigger | UpdateType.Terminal)) != 0) {
if (argument != null) {
ProcessCommand(argument);
}
} catch (Exception e) {
string mess = $"An exception occurred during script execution.\nException: {e}\n---";
Log(mess);
Warning(mess);
FlushToPanels(PANELS_DEBUG);
throw;
}
}
public void ProcessCommand(string argument) {
if (_command_line.TryParse(argument)) {
string command = _command_line.Argument(0);
if (command == null) {
Log("No command specified");
} else if (command == "event") {
ProcessEvent(argument);
} else {
Log($"Unknown command {command}");
}
}
}
// Format: event <source> <event> <event data...>
// eg: event zi.bar-charts pubsub.subscribe datapoint.issue <entity_id>
// event zi.inv-display datapoint.issue "Max Stored Power" 6.7
public void ProcessEvent(string argument) {
string source = _command_line.Argument(1);
string event_name = _command_line.Argument(2);
//Warning($"Received event '{event_name}' from source '{source}'.");
_events_rx++;
if (_subscriptions.TryGetValue(event_name, out _subscribers)) {
foreach (IMyProgrammableBlock block in _subscribers) {
//Warning($"Sending event '{event_name}' to '{block.CustomName}'.");
block.TryRun(argument);
_events_tx++;
}
}
if (event_name == "pubsub.subscribe" || event_name == "pubsub.register") {
// eg: event zi.bar-charts pubsub.subscribe datapoint.issue <entity_id>
string subscription = _command_line.Argument(3);
long entity_id = long.Parse(_command_line.Argument(4), System.Globalization.CultureInfo.InvariantCulture);
// register new listener
IMyProgrammableBlock block = (IMyProgrammableBlock)GridTerminalSystem.GetBlockWithId(entity_id);
if (block != null) {
AddSubscriber(subscription, block);
}
} else if (event_name == "pubsub.unsubscribe" || event_name == "pubsub.unregister") {
// eg: event zi.bar-charts pubsub.unsubscribe datapoint.issue <entity_id>
string subscription = _command_line.Argument(3);
long entity_id = long.Parse(_command_line.Argument(4), System.Globalization.CultureInfo.InvariantCulture);
// unregister listener
IMyProgrammableBlock block = (IMyProgrammableBlock)GridTerminalSystem.GetBlockWithId(entity_id);
if (block != null) {
RemoveSubscriber(subscription, block);
}
}
}
public void AddSubscriber(string event_name, IMyProgrammableBlock subscriber) {
HashSet<IMyProgrammableBlock> subscribers;
if (_subscriptions.TryGetValue(event_name, out subscribers)) {
subscribers.Add(subscriber);
} else {
subscribers = new HashSet<IMyProgrammableBlock>() { subscriber };
_subscriptions.Add(event_name, subscribers);
}
}
public void RemoveSubscriber(string event_name, IMyProgrammableBlock subscriber) {
HashSet<IMyProgrammableBlock> subscribers;
if (_subscriptions.TryGetValue(event_name, out subscribers)) {
subscribers.Remove(subscriber);
}
}
public double TimeAsUsec(double t) {
//return (t * 1000.) / TimeSpan.TicksPerMillisecond;
return t * 1000.0;
}
public void FindPanels() {
for (int i = 0; i < SIZE_PANELS; i++) {
_panels[i].Clear();
GridTerminalSystem.GetBlocksOfType<IMyTextPanel>(_panels[i], block => block.CustomName.Contains(_panel_tags[i]) && block.IsSameConstructAs(Me));
for (int j = 0, szj = _panels[i].Count; j < szj; j++) {
_panels[i][j].ContentType = ContentType.TEXT_AND_IMAGE;
_panels[i][j].Font = "Monospace";
_panels[i][j].FontSize = 0.5F;
_panels[i][j].TextPadding = 0.5F;
_panels[i][j].Alignment = TextAlignment.LEFT;
}
}
}
public void ClearAllPanels() {
for (int i = 0; i < SIZE_PANELS; i++) {
ClearPanels(i);
}
}
public void ClearPanels(int kind) {
_panel_text[kind] = "";
}
public void WritePanels(int kind, string s) {
_panel_text[kind] += s;
}
public void PrependPanels(int kind, string s) {
_panel_text[kind] = s + _panel_text[kind];
}
public void FlushToAllPanels() {
for (int i = 0; i < SIZE_PANELS; i++) {
FlushToPanels(i);
}
}
public void FlushToPanels(int kind) {
for (int i = 0, sz = _panels[kind].Count; i < sz; i++) {
if (_panels[kind][i] != null) {
_panels[kind][i].WriteText(_panel_text[kind], false);
}
}
}
public void Log(string s) {
WritePanels(PANELS_DEBUG, s + "\n");
Echo(s);
}
public void Warning(string s) {
// Never clear buffer and and always immediately flush.
// Prepend because long text will have the bottom hidden.
PrependPanels(PANELS_WARN, $"[{DateTime.Now,11:HH:mm:ss.ff}] {s}\n");
FlushToPanels(PANELS_WARN);
}