18045010223
2025-07-07 0d3a683a0c97154b1f2e6657398664537e4e3e82
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
package org.yzh.web.controller;
 
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.yzh.commons.model.R;
import org.yzh.commons.util.BytesUtils;
import org.yzh.protocol.t808.T0001;
import org.yzh.protocol.t808.T8900;
import org.yzh.protocol.t808.T8900Lock.*;
import org.yzh.web.endpoint.MessageManager;
import reactor.core.publisher.Mono;
 
import java.util.Calendar;
import java.util.Date;
 
@RestController
@RequestMapping("device")
@RequiredArgsConstructor
public class JT8900LockController {
    private final MessageManager messageManager;
 
    /**
     * 控制蝶阀锁状态 2025-05-26 zwy
     * @param clientId 终端ID
     * @param lockId 锁ID
     * @param status 锁状态(1:开, 2:关)
     * @return 操作结果
     */
    @Operation(summary = "控制蝶阀锁状态")
    @PostMapping("/control")
    public Mono<R<T0001>> controlValveLock(
            @RequestParam String clientId,
            @RequestParam String lockId,
            @RequestParam int status) {
 
        // 构建蝶阀锁控制消息
        ValveLockProtocol valveLockProtocol=new ValveLockProtocol();
        valveLockProtocol.setLockId(BytesUtils.hex2bytes(lockId));
        valveLockProtocol.setStatus((byte)status);
        valveLockProtocol.calculateChecksum();
 
        T8900 request = new T8900();
        request.setClientId(clientId);
        request.setValveLockProtocol(valveLockProtocol);
 
        // 发送消息并获取响应
        return messageManager.requestR(request.build(), T0001.class);
    }
 
    @Operation(summary = "读取电子锁当前状态")
    @PostMapping("query-status")
    public Mono<R<T0001>> queryLockStatus(
            @RequestParam String clientId, // 终端手机号String
            @RequestParam String lockId // 蝶阀锁ID(如0xaaaa)
    ) {
        T8900State t8900State = new T8900State(lockId);
        T8900 t8900 = new T8900();
        t8900.setClientId(clientId);
        t8900.setT8900State(t8900State);
        // 发送消息并指定响应类为T8900Response
        //T8900Response response = messageManager.request(clientId, t8900, T8900Response.class).block();
 
        //return response.parseStatus(); // 解析响应结果
        return messageManager.requestR(t8900.build(), T0001.class);
 
//        messageManager.request(clientId, request, T8900Response.class)
//                .map(T8900Response::get)
    }
 
    // 触发设备号读取
    @Operation(summary = "读取设备号")
    @GetMapping("/read-device-id")
    public Mono<R<T0001>> readDeviceId(@RequestParam String clientId) {
        T8900ReadDeviceID readDeviceID = new T8900ReadDeviceID();
        readDeviceID.setChecksum(readDeviceID.calculateChecksum());
        T8900 t8900 = new T8900();
        t8900.setClientId(clientId);
        t8900.setReadDeviceID(readDeviceID);
 
        return messageManager.requestR(t8900.build(), T0001.class);
    }
 
    @Operation(summary = "设置设备号")
    @PostMapping("/set-device-Id")
    public Mono<R<T0001>> setDeviceId(
            @RequestParam String clientId,
            @RequestParam String lockId,
            @RequestParam String newlockId
    ){
        T8900SetDeviceID setDeviceID = new T8900SetDeviceID();
        setDeviceID.setLockId(BytesUtils.hex2bytes(lockId));
        setDeviceID.setNewlockId(BytesUtils.hex2bytes(newlockId));
        T8900 t8900 = new T8900();
        t8900.setClientId(clientId);
        t8900.setSetDeviceID(setDeviceID);
 
        return messageManager.requestR(t8900.build(), T0001.class);
    }
 
    // 触发读所有信息
    @Operation(summary = "读所有信息")
    @PostMapping("/read-device-all")
    public Mono<R<T0001>> readAllInformation(
            @RequestParam String clientId,
            @RequestParam String lockId
    ) {
        T8900ReadAllInformation readAllInformation = new T8900ReadAllInformation();
        readAllInformation.setLockId(BytesUtils.hex2bytes(lockId));
        Date currentDate = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        readAllInformation.setYear((byte) calendar.get(Calendar.YEAR));
        readAllInformation.setMonth((byte) calendar.get(Calendar.MONTH));
        readAllInformation.setDay((byte) calendar.get(Calendar.DAY_OF_MONTH));
        readAllInformation.setHour((byte) calendar.get(Calendar.HOUR_OF_DAY));
        readAllInformation.setMinute((byte) calendar.get(Calendar.MINUTE));
        readAllInformation.setSecond((byte) calendar.get(Calendar.SECOND));
        T8900 t8900 = new T8900();
        t8900.setClientId(clientId);
        t8900.setReadAllInformation(readAllInformation);
 
        return messageManager.requestR(t8900.build(), T0001.class);
    }
 
    @Operation(summary = "设置蝶阀锁离线开锁密码")
    @PostMapping("/setPassword")
    public Mono<R<T0001>> setPassword(
            @RequestParam String clientId,
            @RequestParam String lockId,
            @RequestParam String password
    ){
        T8900SetPassword setPassword = new T8900SetPassword();
        setPassword.setLockId(BytesUtils.hex2bytes(lockId));
        String[] pwd = password.split(",");
        if(pwd.length != 10){
            System.out.println("密码必须为10组,每组六位!");
            return null;
        }
        setPassword.setPassword1(BytesUtils.hex2bytes(pwd[0]));
        setPassword.setPassword2(BytesUtils.hex2bytes(pwd[1]));
        setPassword.setPassword3(BytesUtils.hex2bytes(pwd[2]));
        setPassword.setPassword4(BytesUtils.hex2bytes(pwd[3]));
        setPassword.setPassword5(BytesUtils.hex2bytes(pwd[4]));
        setPassword.setPassword6(BytesUtils.hex2bytes(pwd[5]));
        setPassword.setPassword7(BytesUtils.hex2bytes(pwd[6]));
        setPassword.setPassword8(BytesUtils.hex2bytes(pwd[7]));
        setPassword.setPassword9(BytesUtils.hex2bytes(pwd[8]));
        setPassword.setPassword10(BytesUtils.hex2bytes(pwd[9]));
 
        T8900 t8900 = new T8900();
        t8900.setClientId(clientId);
        t8900.setSetPassword(setPassword);
        return messageManager.requestR(t8900.build(), T0001.class);
    }
}