NotificationEvent.java
3.45 KB
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
/*
* Copyright (C) 2015 The Telink Bluetooth Light Project
*
*/
package com.telink.bluetooth.event;
import android.app.Application;
import com.telink.bluetooth.light.NotificationInfo;
import com.telink.bluetooth.light.NotificationParser;
import com.telink.bluetooth.light.Opcode;
import java.util.HashMap;
import java.util.Map;
/**
* 通知事件,比如设备的状态/分组信息发生变化等
*/
public class NotificationEvent extends DataEvent<NotificationInfo> {
/**
* 设备的状态变化事件
*/
public static final String ONLINE_STATUS = "com.telink.bluetooth.light.EVENT_ONLINE_STATUS";
/**
* 分组事件
*/
public static final String GET_GROUP = "com.telink.bluetooth.light.EVENT_GET_GROUP";
/**
* 闹铃事件
*/
public static final String GET_ALARM = "com.telink.bluetooth.light.EVENT_GET_ALARM";
/**
* 场景事件
*/
public static final String GET_SCENE = "com.telink.bluetooth.light.EVENT_GET_SCENE";
/**
* 时间同步事件
*/
public static final String GET_TIME = "com.telink.bluetooth.light.EVENT_GET_TIME";
private static final Map<Byte, String> EVENT_MAPPING = new HashMap<>();
static {
register(Opcode.BLE_GATT_OP_CTRL_DC, ONLINE_STATUS);
register(Opcode.BLE_GATT_OP_CTRL_D4, GET_GROUP);
register(Opcode.BLE_GATT_OP_CTRL_E7, GET_ALARM);
register(Opcode.BLE_GATT_OP_CTRL_E9, GET_TIME);
register(Opcode.BLE_GATT_OP_CTRL_C1, GET_SCENE);
}
/**
* 操作码
*/
protected int opcode;
/**
* 源地址,即设备/组地址
*/
protected int src;
public NotificationEvent(Object sender, String type, NotificationInfo args) {
super(sender, type, args);
this.opcode = args.opcode;
this.src = args.src;
}
/**
* 注册事件类型
*
* @param opcode
* @param eventType
* @return
*/
public static boolean register(byte opcode, String eventType) {
opcode = (byte) (opcode & 0xFF);
synchronized (NotificationEvent.class) {
if (EVENT_MAPPING.containsKey(opcode))
return false;
EVENT_MAPPING.put(opcode, eventType);
return true;
}
}
/**
* 注册事件类型
*
* @param opcode
* @param eventType
* @return
* @see NotificationEvent#register(byte, String)
*/
public static boolean register(Opcode opcode, String eventType) {
return register(opcode.getValue(), eventType);
}
/**
* 获取事件类型
*
* @param opcode 操作码
* @return
*/
public static String getEventType(byte opcode) {
opcode = (byte) (opcode & 0xFF);
synchronized (NotificationEvent.class) {
if (EVENT_MAPPING.containsKey(opcode))
return EVENT_MAPPING.get(opcode);
}
return null;
}
public static String getEventType(Opcode opcode) {
return getEventType(opcode.getValue());
}
public static NotificationEvent newInstance(Application sender, String type, NotificationInfo args) {
return new NotificationEvent(sender, type, args);
}
public Object parse() {
NotificationParser parser = NotificationParser.get(this.opcode);
return parser == null ? null : parser.parse(this.args);
}
}