yeg
2026-09-09 7100c8a4f7dd3fc7d53ffead4475d7a8abc51c12
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package com.battery.business.web.device;
 
import java.math.BigDecimal;
import java.util.List;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import com.ruoyi.common.annotation.Anonymous;
import com.battery.device.simulator.BtsDevelopmentSimulator;
import com.battery.device.simulator.BtsDevelopmentSimulator.StateSnapshot;
import com.battery.device.api.discovery.DeviceDiscoveryCandidateStore;
import com.battery.device.api.model.DeviceWorkStatus;
import com.battery.device.simulator.BtsSimulatorStateStore.CollectorMetricState;
import com.battery.device.simulator.DevelopmentCollectorSimulation.CollectorRealtimeSnapshot;
 
/** 本机开发模拟器的配置与观测 API,不向设备发送控制命令。 */
@Anonymous
@RestController
@Profile("dev-simulator")
public class BtsDevelopmentSimulatorController
{
    private final BtsDevelopmentSimulator simulator;
    private final DeviceDiscoveryCandidateStore candidateStore;
    private final com.battery.business.runtime.DevelopmentCollectorStatusSynchronizer statusSynchronizer;
 
    @Autowired
    public BtsDevelopmentSimulatorController(BtsDevelopmentSimulator simulator,
            DeviceDiscoveryCandidateStore candidateStore,
            com.battery.business.runtime.DevelopmentCollectorStatusSynchronizer statusSynchronizer)
    {
        this.simulator = simulator;
        this.candidateStore = candidateStore;
        this.statusSynchronizer = statusSynchronizer;
    }
 
    /** 保留独立模拟器测试和工具调用的构造入口,不触发数据库同步。 */
    public BtsDevelopmentSimulatorController(BtsDevelopmentSimulator simulator,
            DeviceDiscoveryCandidateStore candidateStore)
    {
        this(simulator, candidateStore, null);
    }
 
    /** 返回模拟器配置页;匿名放行后仍由本机拦截器限制来源。 */
    @GetMapping(value = "/dev/bts-simulator.html", produces = MediaType.TEXT_HTML_VALUE)
    public Resource page()
    {
        return new ClassPathResource("static/dev/bts-simulator.html");
    }
 
    /** 返回模拟器、虚拟设备、通道和最近请求的当前快照。 */
    @GetMapping("/dev/bts-simulator/state")
    public StateSnapshot state()
    {
        return simulator.snapshot();
    }
 
    /** 人工清空模拟配置并恢复默认检测柜和采集器。 */
    @PostMapping("/dev/bts-simulator/reset")
    public StateSnapshot reset()
    {
        simulator.reset();
        candidateStore.removeAdapter(BtsDevelopmentSimulator.ADAPTER_CODE);
        return simulator.snapshot();
    }
 
    /** 模拟现场人工统一开关全部检测柜和采集器,不发送任何设备控制命令。 */
    @PostMapping("/dev/bts-simulator/power")
    public StateSnapshot setPower(@RequestBody SimulatorPowerCommand command)
    {
        if (command == null || command.poweredOn() == null)
            throw badRequest(new IllegalArgumentException("poweredOn must be true or false"));
        simulator.setAllDevicesPoweredOn(command.poweredOn());
        synchronizeOnlineStatusIfConfigured();
        return simulator.snapshot();
    }
 
    /** 添加一台虚拟设备。 */
    @PostMapping("/dev/bts-simulator/devices")
    public StateSnapshot createDevice(@RequestBody CreateDeviceCommand command)
    {
        try
        {
            if (command.model() == null || command.model().isBlank())
                simulator.addDevice(command.ip(), command.channelCount());
            else
                simulator.addNewareDevice(command.ip(), command.model());
            return simulator.snapshot();
        }
        catch (IllegalArgumentException exception)
        {
            throw badRequest(exception);
        }
    }
 
    /** 修改指定虚拟设备的地址、通道数和在线状态。 */
    @PutMapping("/dev/bts-simulator/devices/{deviceId}")
    public StateSnapshot updateDevice(@PathVariable int deviceId, @RequestBody DeviceStateCommand command)
    {
        try
        {
            simulator.updateDevice(deviceId, command.ip(), command.channelCount(), command.online());
            if (command.model() != null && !command.model().isBlank())
                simulator.configureNewareProfile(deviceId, command.model());
            return simulator.snapshot();
        }
        catch (IllegalArgumentException exception)
        {
            throw badRequest(exception);
        }
    }
 
    /** 删除指定虚拟设备。 */
    @DeleteMapping("/dev/bts-simulator/devices/{deviceId}")
    public StateSnapshot deleteDevice(@PathVariable int deviceId)
    {
        try
        {
            simulator.deleteDevice(deviceId);
            return simulator.snapshot();
        }
        catch (IllegalArgumentException exception)
        {
            throw badRequest(exception);
        }
    }
 
    /** 添加一台与检测柜分开配置的虚拟采集器。 */
    @PostMapping("/dev/bts-simulator/collectors")
    public StateSnapshot createCollector(@RequestBody CreateCollectorCommand command)
    {
        try
        {
            simulator.addCollector(command.ip(), command.channelCount());
            return simulator.snapshot();
        }
        catch (IllegalArgumentException exception)
        {
            throw badRequest(exception);
        }
    }
 
    /** 修改虚拟采集器的地址、通道数和在线状态。 */
    @PutMapping("/dev/bts-simulator/collectors/{collectorId}")
    public StateSnapshot updateCollector(@PathVariable int collectorId,
            @RequestBody CollectorStateCommand command)
    {
        try
        {
            simulator.updateCollector(collectorId, command.ip(), command.channelCount(), command.online());
            synchronizeOnlineStatusIfConfigured();
            return simulator.snapshot();
        }
        catch (IllegalArgumentException exception)
        {
            throw badRequest(exception);
        }
    }
 
    /** 保存稳定 S 通道的固定测量值及自定义指标,不产生新的成功观测。 */
    @PutMapping("/dev/bts-simulator/collectors/{collectorId}/channels/{channelCode}")
    public StateSnapshot configureCollectorChannel(@PathVariable int collectorId,
            @PathVariable String channelCode, @RequestBody CollectorChannelCommand command)
    {
        try
        {
            List<CollectorMetricState> metrics = command.metrics().stream()
                    .map(metric -> new CollectorMetricState(metric.metricCode(), metric.value(), metric.unit()))
                    .toList();
            simulator.configureCollectorChannel(collectorId, channelCode, metrics);
            return simulator.snapshot();
        }
        catch (IllegalArgumentException | NullPointerException exception)
        {
            throw badRequest(new IllegalArgumentException("Invalid collector channel metrics", exception));
        }
    }
 
    /** 请求采集器当前实时快照;不可访问或离线时返回空测量。 */
    @GetMapping("/dev/bts-simulator/collectors/{collectorId}/realtime")
    public CollectorRealtimeSnapshot collectorRealtime(@PathVariable int collectorId)
    {
        try
        {
            return simulator.collectorRealtime(collectorId);
        }
        catch (IllegalArgumentException exception)
        {
            throw badRequest(exception);
        }
    }
 
    /** 删除指定虚拟采集器。 */
    @DeleteMapping("/dev/bts-simulator/collectors/{collectorId}")
    public StateSnapshot deleteCollector(@PathVariable int collectorId)
    {
        try
        {
            simulator.deleteCollector(collectorId);
            return simulator.snapshot();
        }
        catch (IllegalArgumentException exception)
        {
            throw badRequest(exception);
        }
    }
 
    /** 修改指定虚拟设备通道的在线和工作状态。 */
    @PutMapping("/dev/bts-simulator/devices/{deviceId}/channels/{channelId}")
    public StateSnapshot updateChannel(@PathVariable int deviceId, @PathVariable int channelId,
            @RequestBody ChannelStateCommand command)
    {
        try
        {
            simulator.updateChannel(deviceId, channelId, command.online(), command.workStatus());
            return simulator.snapshot();
        }
        catch (IllegalArgumentException exception)
        {
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, exception.getMessage(), exception);
        }
    }
 
    /** 保存单通道的当前观测参数和工步方案,不改变设备在线或工作状态。 */
    @PutMapping("/dev/bts-simulator/devices/{deviceId}/channels/{channelId}/configuration")
    public StateSnapshot configureChannel(@PathVariable int deviceId, @PathVariable int channelId,
            @RequestBody com.battery.device.simulator.BtsSimulatorStateStore.ChannelConfiguration configuration)
    {
        try {
            simulator.configureChannel(deviceId, channelId, configuration);
            return simulator.snapshot();
        }
        catch (IllegalArgumentException exception) { throw badRequest(exception); }
    }
 
    /** 修改软件可访问性,设备配置继续保留。 */
    @PutMapping("/dev/bts-simulator/software")
    public StateSnapshot software(@RequestBody SoftwareCommand command)
    {
        if (command == null || command.accessible() == null)
            throw badRequest(new IllegalArgumentException("accessible must be true or false"));
        simulator.setSoftwareAccessible(command.accessible());
        synchronizeOnlineStatusIfConfigured();
        return simulator.snapshot();
    }
 
    /** 修改采集器通道当前工作状态。 */
    @PutMapping("/dev/bts-simulator/collectors/{collectorId}/channels/{channelCode}/status")
    public StateSnapshot collectorChannelStatus(@PathVariable int collectorId, @PathVariable String channelCode,
            @RequestBody CollectorChannelStatusCommand command)
    {
        try
        {
            simulator.updateCollectorChannelStatus(collectorId, channelCode, command.workStatus());
            return simulator.snapshot();
        }
        catch (IllegalArgumentException exception) { throw badRequest(exception); }
    }
 
    private ResponseStatusException badRequest(IllegalArgumentException exception)
    {
        return new ResponseStatusException(HttpStatus.BAD_REQUEST, exception.getMessage(), exception);
    }
 
    private void synchronizeOnlineStatusIfConfigured()
    {
        if (statusSynchronizer != null) statusSynchronizer.synchronizeOnlineStatus();
    }
 
    public record CreateDeviceCommand(String ip, int channelCount, String model)
    {
        public CreateDeviceCommand(String ip, int channelCount)
        {
            this(ip, channelCount, null);
        }
    }
 
    public record SimulatorPowerCommand(Boolean poweredOn) { }
 
    public record DeviceStateCommand(String ip, int channelCount, boolean online, String model)
    {
        public DeviceStateCommand(String ip, int channelCount, boolean online)
        {
            this(ip, channelCount, online, null);
        }
    }
 
    public record ChannelStateCommand(boolean online, DeviceWorkStatus workStatus) { }
 
    public record CreateCollectorCommand(String ip, int channelCount) { }
 
    public record CollectorStateCommand(String ip, int channelCount, boolean online) { }
 
    public record CollectorChannelCommand(List<CollectorMetricCommand> metrics) { }
 
    public record CollectorMetricCommand(String metricCode, BigDecimal value, String unit) { }
 
    public record SoftwareCommand(Boolean accessible) { }
 
    public record CollectorChannelStatusCommand(DeviceWorkStatus workStatus) { }
}