Pu Zhibing
22 小时以前 4fcd1f89d42fef05fb6592d30f168f3bfc6728d6
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
package com.ruoyi.integration.iotda.utils.listener;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.chargingPile.api.dto.CheckChargingStrategyDTO;
import com.ruoyi.chargingPile.api.feignClient.AccountingStrategyDetailClient;
import com.ruoyi.chargingPile.api.feignClient.ChargingPileClient;
import com.ruoyi.chargingPile.api.model.TAccountingStrategyDetail;
import com.ruoyi.chargingPile.api.vo.UpdateChargingPileStatusVo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.integration.api.model.*;
import com.ruoyi.integration.iotda.constant.SendTagConstant;
import com.ruoyi.integration.iotda.enums.ServiceIdMenu;
import com.ruoyi.integration.iotda.model.*;
import com.ruoyi.integration.iotda.utils.produce.IotMessageProduce;
import com.ruoyi.integration.iotda.utils.tools.CP56Time2aConverter;
import com.ruoyi.integration.iotda.utils.tools.MessageUtil;
import com.ruoyi.integration.iotda.utils.tools.StrategyUtil;
import com.ruoyi.integration.iotda.utils.tools.ChargingMessageUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;
 
/**
 * iotda消息监听
 */
@Slf4j
@RestController
@RequestMapping("/iot")
public class IotMessageListener {
 
    @Autowired
    private MessageUtil messageUtil;
    @Autowired
    private IotMessageProduce iotMessageProduce;
    @Resource
    private AccountingStrategyDetailClient accountingStrategyDetailClient;
    
    @Resource
    private RedisTemplate redisTemplate;
    
    @Resource
    private ChargingPileClient chargingPileClient;
    
    @Resource
    private ChargingMessageUtil chargingMessageUtil;
    
    
    
    
    /**
     * 设备消息监听
     * @param jsonObject
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/message")
    public AjaxResult<String> message(@RequestBody JSONObject jsonObject) throws IOException {
        log.info("接收到消息中转:{}",jsonObject);
        JSONObject body = jsonObject.getJSONObject("notify_data").getJSONObject("body");
        JSONObject content = null;
        try {
            content = body.getJSONObject("content");
        }catch (Exception e){
            String content1 = body.getString("content");
            String substring = content1.substring(content1.length() - 1);
            if(!"}".equals(substring)){
                content1 = content1.substring(0, content1.length() - 1) + "}";
            }
            log.info("content1:"+content1);
            content = JSON.parseObject(content1);
        }
        JSONObject header = jsonObject.getJSONObject("notify_data").getJSONObject("header");
        String deviceId = header.getString("device_id"); // 设备id
        String nodeId = header.getString("node_id"); // 设备编号
        String productId = header.getString("product_id"); // 产品id
        String service_id = content.getString("service_id");
        log.info("服务id:{}",service_id);
        ChargingMessage chargingMessage = new ChargingMessage();
        chargingMessage.setServiceId(service_id);
        // 设备消息下发
        String result;
        switch (service_id){
            case SendTagConstant.ONLINE:
                long ONLINE = System.currentTimeMillis();
                
                OnlineMessage onlineMessage = JSON.parseObject(content.toJSONString(),OnlineMessage.class);
                chargingMessage.setOnlineMessage(onlineMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
                // 响应硬件
                // 业务处理 登录认证应答
                OnlineReply onlineReply = new OnlineReply();
                onlineReply.setCharging_pile_code(onlineMessage.getCharging_pile_code());
                onlineReply.setOnline_result(0);
                result = iotMessageProduce.sendMessage(onlineReply.getCharging_pile_code(), ServiceIdMenu.ONLINE_REPLY.getKey(), messageUtil.onlineReply(onlineReply));
                log.info("充电桩登录认证-返回结果:{}",result);
                // 响应硬件 对时设置应答
                TimingSettingReply timingSettingReplyOnline = new TimingSettingReply();
                timingSettingReplyOnline.setCharging_pile_code(onlineMessage.getCharging_pile_code());
                timingSettingReplyOnline.setCurrent_time(CP56Time2aConverter.convertToCP56Time2a(new Date()));
                iotMessageProduce.sendMessage(timingSettingReplyOnline.getCharging_pile_code(), ServiceIdMenu.TIMING_SETTING_REPLY.getKey(),messageUtil.timingSettingReply(timingSettingReplyOnline));
    
                log.warn("ONLINE消息处理:{} 毫秒", System.currentTimeMillis() - ONLINE);
                break;
            case SendTagConstant.PING:
                long PING = System.currentTimeMillis();
                
                PingMessage pingMessage = JSON.parseObject(content.toJSONString(),PingMessage.class);
                //存储缓存中,5分钟有效
                redisTemplate.opsForValue().set("ping:" + pingMessage.getCharging_pile_code() + pingMessage.getCharging_gun_code(), pingMessage, 5, TimeUnit.MINUTES);
                
                // 响应硬件
                Pong pong = new Pong();
                pong.setCharging_pile_code(pingMessage.getCharging_pile_code());
                pong.setCharging_gun_code(pingMessage.getCharging_gun_code());
                pong.setCharging_gun_status(0);
                iotMessageProduce.sendMessage(pong.getCharging_pile_code(), ServiceIdMenu.PONG.getKey(), messageUtil.pong(pong));
    
    
                Long time = (Long) redisTemplate.opsForHash().get("charging_gun_online", (pingMessage.getCharging_pile_code() + pingMessage.getCharging_gun_code()));
                //小于1分钟才处理数据,防止频繁查询数据
                if(null != time && (System.currentTimeMillis() - time) < 60000){
                    log.warn("PING消息处理:{} 毫秒", System.currentTimeMillis() - PING);
                    break;
                }
                
                //检查修改设备状态
                UpdateChargingPileStatusVo vo1 = new UpdateChargingPileStatusVo();
                vo1.setGun_code(pingMessage.getCharging_gun_code());
                vo1.setPile_code(pingMessage.getCharging_pile_code());
                vo1.setStatus(pingMessage.getCharging_gun_status());
                chargingPileClient.updateChargingPileStatus(vo1);
                
                log.warn("PING消息处理:{} 毫秒", System.currentTimeMillis() - PING);
                break;
            case SendTagConstant.END_CHARGE:
                long END_CHARGE = System.currentTimeMillis();
                
                EndChargeMessage endChargeMessage = JSON.parseObject(content.toJSONString(),EndChargeMessage.class);
                chargingMessage.setEndChargeMessage(endChargeMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
    
                log.warn("END_CHARGE消息处理:{} 毫秒", System.currentTimeMillis() - END_CHARGE);
                break;
            case SendTagConstant.ERROR_MESSAGE:
                long ERROR_MESSAGE = System.currentTimeMillis();
                
                ErrorMessageMessage errorMessageMessage = JSON.parseObject(content.toJSONString(),ErrorMessageMessage.class);
                chargingMessage.setErrorMessageMessage(errorMessageMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
    
                log.warn("ERROR_MESSAGE消息处理:{} 毫秒", System.currentTimeMillis() - ERROR_MESSAGE);
                break;
            case SendTagConstant.BILLING_MODE_VERIFY:
                long BILLING_MODE_VERIFY = System.currentTimeMillis();
                
                BillingModeVerifyMessage billingModeVerifyMessage = JSON.parseObject(content.toJSONString(),BillingModeVerifyMessage.class);
                chargingMessage.setBillingModeVerifyMessage(billingModeVerifyMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
                // 响应硬件
                BillingModeVerifyReply billingModeVerifyReply = new BillingModeVerifyReply();
                if(billingModeVerifyMessage.getBilling_model_code().equals("0")){
                    // 首次
                    billingModeVerifyReply.setCharging_pile_code(billingModeVerifyMessage.getCharging_pile_code());
                    billingModeVerifyReply.setBilling_model_code("0");
                    billingModeVerifyReply.setBilling_model_result(1);
                }else {
                    // 查询桩使用的模版
                    CheckChargingStrategyDTO dto = new CheckChargingStrategyDTO();
                    dto.setCode(billingModeVerifyMessage.getCharging_pile_code());
                    dto.setStrategyDetailId(Integer.valueOf(billingModeVerifyMessage.getBilling_model_code()));
                    Boolean check = accountingStrategyDetailClient.checkChargingStrategy(dto).getData();
                    // 校验计费模版是否准确
                    billingModeVerifyReply.setCharging_pile_code(billingModeVerifyMessage.getCharging_pile_code());
                    billingModeVerifyReply.setBilling_model_code(billingModeVerifyMessage.getBilling_model_code());
                    if(check){
                        billingModeVerifyReply.setBilling_model_result(0);
                    }else {
                        billingModeVerifyReply.setBilling_model_result(1);
                    }
                }
                iotMessageProduce.sendMessage(billingModeVerifyReply.getCharging_pile_code(), ServiceIdMenu.BILLING_MODE_VERIFY_REPLY.getKey(),messageUtil.billingModeVerifyReply(billingModeVerifyReply));
    
                log.warn("ERROR_MESSAGE消息处理:{} 毫秒", System.currentTimeMillis() - BILLING_MODE_VERIFY);
                break;
            case SendTagConstant.ACQUISITION_BILLING_MODE:
                long ACQUISITION_BILLING_MODE = System.currentTimeMillis();
                
                AcquisitionBillingModeMessage acquisitionBillingModeMessage = JSON.parseObject(content.toJSONString(),AcquisitionBillingModeMessage.class);
                chargingMessage.setAcquisitionBillingModeMessage(acquisitionBillingModeMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
                // 响应硬件   计费模型请求应答  1=尖阶段,2=峰阶段,3=平阶段,4=谷阶段
                List<TAccountingStrategyDetail> accountingStrategyDetails = accountingStrategyDetailClient.getDetailListByCode(acquisitionBillingModeMessage.getCharging_pile_code()).getData();
                Map<Integer, TAccountingStrategyDetail> strategyPrice = StrategyUtil.getStrategyPrice(accountingStrategyDetails);
                // 价格设置
                AcquisitionBillingModeReply acquisitionBillingModeReply = new AcquisitionBillingModeReply();
                StrategyUtil.setStrategyPrice(strategyPrice,acquisitionBillingModeReply);
                // 时段设置
                StrategyUtil.setTime(accountingStrategyDetails,acquisitionBillingModeReply);
                TAccountingStrategyDetail accountingStrategyDetail = accountingStrategyDetailClient.getDetailByCode(acquisitionBillingModeMessage.getCharging_pile_code()).getData();
                acquisitionBillingModeReply.setBilling_model_code(accountingStrategyDetail.getId().toString());
                acquisitionBillingModeReply.setCharging_pile_code(acquisitionBillingModeMessage.getCharging_pile_code());
                acquisitionBillingModeReply.setLoss_ratio(BigDecimal.ZERO);
                iotMessageProduce.sendMessage(acquisitionBillingModeReply.getCharging_pile_code(), ServiceIdMenu.ACQUISITION_BILLING_MODE_REPLY.getKey(),messageUtil.acquisitionBillingModeReply(acquisitionBillingModeReply));
                
                log.warn("ACQUISITION_BILLING_MODE消息处理:{} 毫秒", System.currentTimeMillis() - ACQUISITION_BILLING_MODE);
                break;
            case SendTagConstant.UPLOAD_REAL_TIME_MONITORING_DATA:
                long UPLOAD_REAL_TIME_MONITORING_DATA = System.currentTimeMillis();
                
                log.info("充电实时数据上传");
                UploadRealTimeMonitoringDataMessage uploadRealTimeMonitoringDataMessage = JSON.parseObject(content.toJSONString(),UploadRealTimeMonitoringDataMessage.class);
                chargingMessage.setUploadRealTimeMonitoringDataMessage(uploadRealTimeMonitoringDataMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
    
                log.warn("UPLOAD_REAL_TIME_MONITORING_DATA消息处理:{} 毫秒", System.currentTimeMillis() - UPLOAD_REAL_TIME_MONITORING_DATA);
                break;
            case SendTagConstant.CHARGING_HANDSHAKE:
                long CHARGING_HANDSHAKE = System.currentTimeMillis();
                
                ChargingHandshakeMessage chargingHandshakeMessage = JSON.parseObject(content.toJSONString(),ChargingHandshakeMessage.class);
                chargingMessage.setChargingHandshakeMessage(chargingHandshakeMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
    
                log.warn("CHARGING_HANDSHAKE消息处理:{} 毫秒", System.currentTimeMillis() - CHARGING_HANDSHAKE);
                break;
            case SendTagConstant.PARAMETER_SETTING:
                long PARAMETER_SETTING = System.currentTimeMillis();
                
                ParameterSettingMessage parameterSettingMessage = JSON.parseObject(content.toJSONString(),ParameterSettingMessage.class);
                chargingMessage.setParameterSettingMessage(parameterSettingMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
                
                log.warn("PARAMETER_SETTING消息处理:{} 毫秒", System.currentTimeMillis() - PARAMETER_SETTING);
                break;
            case SendTagConstant.BMS_ABORT:
                long BMS_ABORT = System.currentTimeMillis();
    
                BmsAbortMessage bmsAbortMessage = JSON.parseObject(content.toJSONString(),BmsAbortMessage.class);
                chargingMessage.setBmsAbortMessage(bmsAbortMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
                
                log.warn("BMS_ABORT消息处理:{} 毫秒", System.currentTimeMillis() - BMS_ABORT);
                break;
            case SendTagConstant.MOTOR_ABORT:
                long MOTOR_ABORT = System.currentTimeMillis();
                
                MotorAbortMessage motorAbortMessage = JSON.parseObject(content.toJSONString(),MotorAbortMessage.class);
                chargingMessage.setMotorAbortMessage(motorAbortMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
    
                log.warn("MOTOR_ABORT消息处理:{} 毫秒", System.currentTimeMillis() - MOTOR_ABORT);
                break;
            case SendTagConstant.BMS_DEMAND_AND_CHARGER_EXPORTATION:
                long BMS_DEMAND_AND_CHARGER_EXPORTATION = System.currentTimeMillis();
                
                BmsDemandAndChargerExportationMessage bmsDemandAndChargerExportationMessage = JSON.parseObject(content.toJSONString(),BmsDemandAndChargerExportationMessage.class);
                chargingMessage.setBmsDemandAndChargerExportationMessage(bmsDemandAndChargerExportationMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
    
                log.warn("BMS_DEMAND_AND_CHARGER_EXPORTATION消息处理:{} 毫秒", System.currentTimeMillis() - BMS_DEMAND_AND_CHARGER_EXPORTATION);
                break;
            case SendTagConstant.BMS_INFORMATION:
                long BMS_INFORMATION = System.currentTimeMillis();
                
                BmsInformationMessage bmsInformationMessage = JSON.parseObject(content.toJSONString(),BmsInformationMessage.class);
                chargingMessage.setBmsInformationMessage(bmsInformationMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
    
                log.warn("BMS_INFORMATION消息处理:{} 毫秒", System.currentTimeMillis() - BMS_INFORMATION);
                break;
            case SendTagConstant.CHARGING_PILE_STARTS_CHARGING:
                long CHARGING_PILE_STARTS_CHARGING = System.currentTimeMillis();
                
                ChargingPileStartsChargingMessage chargingPileStartsChargingMessage = JSON.parseObject(content.toJSONString(),ChargingPileStartsChargingMessage.class);
                chargingMessage.setChargingPileStartsChargingMessage(chargingPileStartsChargingMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
                // 响应硬件
                PlatformConfirmationCharging platformConfirmationCharging = new PlatformConfirmationCharging();
                platformConfirmationCharging.setCharging_pile_code(chargingPileStartsChargingMessage.getCharging_pile_code());
                platformConfirmationCharging.setCharging_gun_code(chargingPileStartsChargingMessage.getCharging_gun_code());
                platformConfirmationCharging.setCard_number(chargingPileStartsChargingMessage.getAccount());
                platformConfirmationCharging.setAccount_balance(BigDecimal.ZERO);
                platformConfirmationCharging.setAuthentication(1);
                // TODO 若是失败,给出失败原因
                iotMessageProduce.sendMessage(platformConfirmationCharging.getCharging_pile_code(), ServiceIdMenu.PLATFORM_CONFIRMATION_CHARGING.getKey(),messageUtil.platformConfirmationCharging(platformConfirmationCharging));
    
                log.warn("CHARGING_PILE_STARTS_CHARGING消息处理:{} 毫秒", System.currentTimeMillis() - CHARGING_PILE_STARTS_CHARGING);
                break;
            case SendTagConstant.PLATFORM_START_CHARGING_REPLY:
                long PLATFORM_START_CHARGING_REPLY = System.currentTimeMillis();
                
                PlatformStartChargingReplyMessage platformStartChargingReplyMessage = JSON.parseObject(content.toJSONString(),PlatformStartChargingReplyMessage.class);
                chargingMessage.setPlatformStartChargingReplyMessage(platformStartChargingReplyMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
    
                log.warn("PLATFORM_START_CHARGING_REPLY消息处理:{} 毫秒", System.currentTimeMillis() - PLATFORM_START_CHARGING_REPLY);
                break;
            case SendTagConstant.PLATFORM_STOP_CHARGING_REPLY:
                long PLATFORM_STOP_CHARGING_REPLY = System.currentTimeMillis();
                
                PlatformStopChargingReplyMessage platformStopChargingReplyMessage = JSON.parseObject(content.toJSONString(),PlatformStopChargingReplyMessage.class);
                chargingMessage.setPlatformStopChargingReplyMessage(platformStopChargingReplyMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
    
                log.warn("PLATFORM_STOP_CHARGING_REPLY消息处理:{} 毫秒", System.currentTimeMillis() - PLATFORM_STOP_CHARGING_REPLY);
                break;
            case SendTagConstant.TRANSACTION_RECORD:
                long TRANSACTION_RECORD = System.currentTimeMillis();
                
                TransactionRecordMessage transactionRecordMessage = JSON.parseObject(content.toJSONString(),TransactionRecordMessage.class);
                transactionRecordMessage.setResult(content.toJSONString());
                chargingMessage.setTransactionRecordMessage(transactionRecordMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
                // 响应硬件
                ConfirmTransactionRecord confirmTransactionRecord = new ConfirmTransactionRecord();
                confirmTransactionRecord.setTransaction_serial_number(transactionRecordMessage.getTransaction_serial_number());
                confirmTransactionRecord.setConfirm_result(0);
                iotMessageProduce.sendMessage(transactionRecordMessage.getCharging_pile_code(), ServiceIdMenu.CONFIRM_TRANSACTION_RECORD.getKey(),messageUtil.confirmTransactionRecord(confirmTransactionRecord));
    
                log.warn("TRANSACTION_RECORD消息处理:{} 毫秒", System.currentTimeMillis() - TRANSACTION_RECORD);
                break;
            case SendTagConstant.UPDATE_BALANCE_REPLY:
                long UPDATE_BALANCE_REPLY = System.currentTimeMillis();
                
                UpdateBalanceReplyMessage updateBalanceReplyMessage = JSON.parseObject(content.toJSONString(),UpdateBalanceReplyMessage.class);
                chargingMessage.setUpdateBalanceReplyMessage(updateBalanceReplyMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
    
                log.warn("UPDATE_BALANCE_REPLY消息处理:{} 毫秒", System.currentTimeMillis() - UPDATE_BALANCE_REPLY);
                break;
            case SendTagConstant.SYNCHRONIZE_OFFLINE_CARD_REPLY:
                long SYNCHRONIZE_OFFLINE_CARD_REPLY = System.currentTimeMillis();
                
                SynchronizeOfflineCardReplyMessage synchronizeOfflineCardReplyMessage = JSON.parseObject(content.toJSONString(),SynchronizeOfflineCardReplyMessage.class);
                chargingMessage.setSynchronizeOfflineCardReplyMessage(synchronizeOfflineCardReplyMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
    
                log.warn("SYNCHRONIZE_OFFLINE_CARD_REPLY消息处理:{} 毫秒", System.currentTimeMillis() - SYNCHRONIZE_OFFLINE_CARD_REPLY);
                break;
            case SendTagConstant.CLEAR_OFFLINE_CARD_REPLY:
                long CLEAR_OFFLINE_CARD_REPLY = System.currentTimeMillis();
                
                ClearOfflineCardReplyMessage clearOfflineCardReplyMessage = JSON.parseObject(content.toJSONString(),ClearOfflineCardReplyMessage.class);
                chargingMessage.setClearOfflineCardReplyMessage(clearOfflineCardReplyMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
                
                log.warn("CLEAR_OFFLINE_CARD_REPLY消息处理:{} 毫秒", System.currentTimeMillis() - CLEAR_OFFLINE_CARD_REPLY);
                break;
            case SendTagConstant.WORKING_PARAMETER_SETTING_REPLY:
                long WORKING_PARAMETER_SETTING_REPLY = System.currentTimeMillis();
                
                WorkingParameterSettingReplyMessage workingParameterSettingReplyMessage = JSON.parseObject(content.toJSONString(),WorkingParameterSettingReplyMessage.class);
                chargingMessage.setWorkingParameterSettingReplyMessage(workingParameterSettingReplyMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
    
                log.warn("WORKING_PARAMETER_SETTING_REPLY消息处理:{} 毫秒", System.currentTimeMillis() - WORKING_PARAMETER_SETTING_REPLY);
                break;
            case SendTagConstant.TIMING_SETTING:
                long TIMING_SETTING = System.currentTimeMillis();
                
                TimingSettingMessage timingSettingMessage = JSON.parseObject(content.toJSONString(),TimingSettingMessage.class);
                chargingMessage.setTimingSettingMessage(timingSettingMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
                // 响应硬件 对时设置应答
                TimingSettingReply timingSettingReply = new TimingSettingReply();
                timingSettingReply.setCharging_pile_code(timingSettingMessage.getCharging_pile_code());
                timingSettingReply.setCurrent_time(CP56Time2aConverter.convertToCP56Time2a(new Date()));
                iotMessageProduce.sendMessage(timingSettingReply.getCharging_pile_code(), ServiceIdMenu.TIMING_SETTING_REPLY.getKey(),messageUtil.timingSettingReply(timingSettingReply));
    
                log.warn("TIMING_SETTING消息处理:{} 毫秒", System.currentTimeMillis() - TIMING_SETTING);
                break;
            case SendTagConstant.SETUP_BILLING_MODEL_REPLY:
                long SETUP_BILLING_MODEL_REPLY = System.currentTimeMillis();
                
                SetupBillingModelReplyMessage setupBillingModelReplyMessage = JSON.parseObject(content.toJSONString(),SetupBillingModelReplyMessage.class);
                chargingMessage.setSetupBillingModelReplyMessage(setupBillingModelReplyMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
    
                log.warn("SETUP_BILLING_MODEL_REPLY消息处理:{} 毫秒", System.currentTimeMillis() - SETUP_BILLING_MODEL_REPLY);
                break;
            case SendTagConstant.GROUND_LOCK_REAL_TIME_DATA:
                long GROUND_LOCK_REAL_TIME_DATA = System.currentTimeMillis();
                
                GroundLockRealTimeDataMessage groundLockRealTimeDataMessage = JSON.parseObject(content.toJSONString(),GroundLockRealTimeDataMessage.class);
                chargingMessage.setGroundLockRealTimeDataMessage(groundLockRealTimeDataMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
    
                log.warn("GROUND_LOCK_REAL_TIME_DATA消息处理:{} 毫秒", System.currentTimeMillis() - GROUND_LOCK_REAL_TIME_DATA);
                break;
            case SendTagConstant.CHARGING_PILE_RETURNS_GROUND_LOCK_DATA:
                long CHARGING_PILE_RETURNS_GROUND_LOCK_DATA = System.currentTimeMillis();
                
                ChargingPileReturnsGroundLockDataMessage chargingPileReturnsGroundLockDataMessage = JSON.parseObject(content.toJSONString(),ChargingPileReturnsGroundLockDataMessage.class);
                chargingMessage.setChargingPileReturnsGroundLockDataMessage(chargingPileReturnsGroundLockDataMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
    
                log.warn("CHARGING_PILE_RETURNS_GROUND_LOCK_DATA消息处理:{} 毫秒", System.currentTimeMillis() - CHARGING_PILE_RETURNS_GROUND_LOCK_DATA);
                break;
            case SendTagConstant.PLATFORM_RESTART_REPLY:
                long PLATFORM_RESTART_REPLY = System.currentTimeMillis();
                
                PlatformRestartReplyMessage platformRestartReplyMessage = JSON.parseObject(content.toJSONString(),PlatformRestartReplyMessage.class);
                chargingMessage.setPlatformRestartReplyMessage(platformRestartReplyMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
    
                log.warn("PLATFORM_RESTART_REPLY消息处理:{} 毫秒", System.currentTimeMillis() - PLATFORM_RESTART_REPLY);
                break;
            case SendTagConstant.QR_CODE_DELIVERY_REPLY:
                long QR_CODE_DELIVERY_REPLY = System.currentTimeMillis();
                
                QrCodeDeliveryReplyMessage qrCodeDeliveryReplyMessage = JSON.parseObject(content.toJSONString(),QrCodeDeliveryReplyMessage.class);
                chargingMessage.setQrCodeDeliveryReplyMessage(qrCodeDeliveryReplyMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
    
                log.warn("QR_CODE_DELIVERY_REPLY消息处理:{} 毫秒", System.currentTimeMillis() - QR_CODE_DELIVERY_REPLY);
                break;
            case SendTagConstant.SECURITY_DETECTION:
                long SECURITY_DETECTION = System.currentTimeMillis();
                
                SecurityDetectionMessage securityDetectionMessage = JSON.parseObject(content.toJSONString(),SecurityDetectionMessage.class);
                chargingMessage.setSecurityDetectionMessage(securityDetectionMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
    
                log.warn("SECURITY_DETECTION消息处理:{} 毫秒", System.currentTimeMillis() - SECURITY_DETECTION);
                break;
            default:
                long other = System.currentTimeMillis();
                
                PlatformRemoteUpdateReplyMessage platformRemoteUpdateReplyMessage = JSON.parseObject(content.toJSONString(),PlatformRemoteUpdateReplyMessage.class);
                chargingMessage.setPlatformRemoteUpdateReplyMessage(platformRemoteUpdateReplyMessage);
                chargingMessageUtil.handleMessage(chargingMessage);
    
                log.warn("other消息处理:{} 毫秒", System.currentTimeMillis() - other);
                break;
        }
        return AjaxResult.success();
    }
 
 
    @ApiOperation(value = "运营平台远程控制启机",tags = {"硬件接口"})
    @GetMapping(value = "/platformStartCharging")
    public R platformStartCharging(String orderNum,String deviceId,Long orderId,BigDecimal money) {
        // 运营平台远程控制启机
        PlatformStartCharging platformStartCharging = new PlatformStartCharging();
        platformStartCharging.setTransaction_serial_number(orderNum);
        platformStartCharging.setCharging_pile_code(deviceId);
        platformStartCharging.setCharging_gun_code("1");
        platformStartCharging.setCard_number(orderId.toString());
        platformStartCharging.setAccount_balance(money);
        String message = iotMessageProduce.sendMessage(platformStartCharging.getCharging_pile_code(), ServiceIdMenu.PLATFORM_START_CHARGING.getKey(), messageUtil.platformStartCharging(platformStartCharging));
        return R.ok(message);
    }
 
    @ApiOperation(value = "运营平台远程停机",tags = {"硬件接口"})
    @GetMapping(value = "/platformStopCharging")
    public R platformStopCharging(String deviceId) {
        // 运营平台远程停机
        PlatformStopCharging platformStopCharging = new PlatformStopCharging();
        platformStopCharging.setCharging_pile_code(deviceId);
        platformStopCharging.setCharging_gun_code("1");
        String message = iotMessageProduce.sendMessage(platformStopCharging.getCharging_pile_code(), ServiceIdMenu.PLATFORM_STOP_CHARGING.getKey(),messageUtil.platformStopCharging(platformStopCharging));
        return R.ok(message);
    }
 
    @ApiOperation(value = "修改余额",tags = {"硬件接口"})
    @GetMapping(value = "/updateBalance")
    public R updateBalance(String deviceId,Long orderId) {
        // 修改余额
        UpdateBalance updateBalance = new UpdateBalance();
        updateBalance.setCharging_pile_code(deviceId);
        updateBalance.setCharging_gun_code("1");
        updateBalance.setPhysical_card_number(orderId.toString());
        updateBalance.setAccount_balance(new BigDecimal(20));
        String message = iotMessageProduce.sendMessage(updateBalance.getCharging_pile_code(), ServiceIdMenu.UPDATE_BALANCE.getKey(),messageUtil.updateBalance(updateBalance));
        return R.ok(message);
    }
 
    @ApiOperation(value = "充电桩工作参数设置",tags = {"硬件接口"})
    @GetMapping(value = "/workingParameterSetting")
    public R workingParameterSetting(String deviceId) {
        // 充电桩工作参数设置
        WorkingParameterSetting workingParameterSetting = new WorkingParameterSetting();
        workingParameterSetting.setCharging_pile_code(deviceId);
        workingParameterSetting.setAllow_work(0);
        workingParameterSetting.setMaximum_output_power(220);
        String message = iotMessageProduce.sendMessage(workingParameterSetting.getCharging_pile_code(), ServiceIdMenu.WORKING_PARAMETER_SETTING.getKey(),messageUtil.workingParameterSetting(workingParameterSetting));
        return R.ok(message);
    }
 
    @ApiOperation(value = "对时设置",tags = {"硬件接口"})
    @GetMapping(value = "/timingSetting")
    public R timingSetting(String deviceId) {
        // 对时设置
        TimingSetting timingSetting = new TimingSetting();
        timingSetting.setCharging_pile_code(deviceId);
        timingSetting.setCurrent_time(CP56Time2aConverter.convertToCP56Time2a(new Date()));
        String message = iotMessageProduce.sendMessage(timingSetting.getCharging_pile_code(), ServiceIdMenu.TIMING_SETTING.getKey(),messageUtil.timingSetting(timingSetting));
        return R.ok(message);
    }
 
    @ApiOperation(value = "计费模型设置",tags = {"硬件接口"})
    @PostMapping(value = "/setupBillingModel")
    public R setupBillingModel(@RequestParam("deviceId") String deviceId, @RequestParam("json") String json) {
        SetupBillingModel setupBillingModel = new SetupBillingModel();
        List<TAccountingStrategyDetail> accountingStrategyDetails = JSON.parseArray(json, TAccountingStrategyDetail.class);
        // 计费模型
        Map<Integer, TAccountingStrategyDetail> strategyPrice = StrategyUtil.getStrategyPrice(accountingStrategyDetails);
        // 价格设置
        AcquisitionBillingModeReply acquisitionBillingModeReply = new AcquisitionBillingModeReply();
        StrategyUtil.setStrategyPrice(strategyPrice,acquisitionBillingModeReply);
        // 时段设置
        StrategyUtil.setTime(accountingStrategyDetails,acquisitionBillingModeReply);
        acquisitionBillingModeReply.setBilling_model_code(accountingStrategyDetails.get(0).getAccountingStrategyId().toString());
        acquisitionBillingModeReply.setCharging_pile_code(deviceId);
        acquisitionBillingModeReply.setLoss_ratio(BigDecimal.ZERO);
        BeanUtils.copyProperties(acquisitionBillingModeReply,setupBillingModel);
        System.err.println(setupBillingModel);
        String message = iotMessageProduce.sendMessage(setupBillingModel.getCharging_pile_code(), ServiceIdMenu.SETUP_BILLING_MODEL.getKey(),messageUtil.setupBillingModel(setupBillingModel));
        return R.ok(message);
    }
 
 
 
    @ApiOperation(value = "远程重启",tags = {"硬件接口"})
    @PostMapping(value = "/platformRestart")
    public R platformRestart(String deviceId) {
        // 远程重启
        PlatformRestart platformRestart = new PlatformRestart();
        platformRestart.setCharging_pile_code(deviceId);
        platformRestart.setOperate(2);
        String message = iotMessageProduce.sendMessage(platformRestart.getCharging_pile_code(), ServiceIdMenu.PLATFORM_RESTART.getKey(),messageUtil.platformRestart(platformRestart));
        return R.ok(message);
    }
 
    @ApiOperation(value = "测试",tags = {"硬件接口"})
    @GetMapping(value = "/test")
    public R test(String code) {
        String orderNum = "2024092646526785213546";
        String deviceId = "3401231001200202";
        Long orderId = 2024065544L;
        BigDecimal money = new BigDecimal(50);
        PlatformStartCharging platformStartCharging = new PlatformStartCharging();
        platformStartCharging.setTransaction_serial_number(orderNum);
        platformStartCharging.setCharging_pile_code(deviceId);
        platformStartCharging.setCharging_gun_code(code);
        platformStartCharging.setCard_number(orderId.toString());
        platformStartCharging.setAccount_balance(money);
        String message = iotMessageProduce.sendMessage(platformStartCharging.getCharging_pile_code(), ServiceIdMenu.PLATFORM_START_CHARGING.getKey(), messageUtil.platformStartCharging(platformStartCharging));
        System.err.println(platformStartCharging);
        return R.ok(message);
    }
 
    @ApiOperation(value = "测试",tags = {"硬件接口"})
    @GetMapping(value = "/stop")
    public R test1(String code) {
        PlatformStopCharging platformStopCharging = new PlatformStopCharging();
        platformStopCharging.setCharging_pile_code("3401231001200202");
        platformStopCharging.setCharging_gun_code(code);
        String message = iotMessageProduce.sendMessage(platformStopCharging.getCharging_pile_code(), ServiceIdMenu.PLATFORM_STOP_CHARGING.getKey(),messageUtil.platformStopCharging(platformStopCharging));
        return R.ok(message);
    }
    @ApiOperation(value = "计费模板下发",tags = {"硬件接口"})
    @GetMapping(value = "/accountingStrategy")
    public R accountingStrategy() {
        String code = "3401231001200202";
        String deviceId = "3401231001200202";
        List<TAccountingStrategyDetail> accountingStrategyDetails = accountingStrategyDetailClient.getDetailListByCode(code).getData();
        Map<Integer, TAccountingStrategyDetail> strategyPrice = StrategyUtil.getStrategyPrice(accountingStrategyDetails);
        // 价格设置
        AcquisitionBillingModeReply acquisitionBillingModeReply = new AcquisitionBillingModeReply();
        StrategyUtil.setStrategyPrice(strategyPrice,acquisitionBillingModeReply);
        // 时段设置
        StrategyUtil.setTime(accountingStrategyDetails,acquisitionBillingModeReply);
        TAccountingStrategyDetail accountingStrategyDetail = accountingStrategyDetailClient.getDetailByCode(code).getData();
        acquisitionBillingModeReply.setBilling_model_code(accountingStrategyDetail.getId().toString());
        acquisitionBillingModeReply.setCharging_pile_code(code);
        acquisitionBillingModeReply.setLoss_ratio(BigDecimal.ZERO);
        System.err.println(acquisitionBillingModeReply);
        iotMessageProduce.sendMessage(deviceId, ServiceIdMenu.ACQUISITION_BILLING_MODE_REPLY.getKey(),messageUtil.acquisitionBillingModeReply(acquisitionBillingModeReply));
        return R.ok();
    }
    
    
  
}