wxr
2022-11-24 2af932533ef851bf983385244e9912976dbd4daa
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
package com.mm.android.deviceaddmodule;
 
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.text.TextUtils;
import com.company.NetSDK.CB_fSDKLogCallBack;
import com.company.NetSDK.DEVICE_NET_INFO_EX;
import com.lechange.opensdk.media.DeviceInitInfo;
import com.mm.android.deviceaddmodule.entity.DeviceNetInfo;
import com.mm.android.deviceaddmodule.mobilecommon.AppConsume.ProviderManager;
import com.mm.android.deviceaddmodule.mobilecommon.utils.LogUtil;
 
import org.json.JSONException;
import org.json.JSONObject;
 
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * 搜索局域网内设备信息的管理类
 */
 
public class SearchDeviceManager {
    private static final String TAG = "SearchDeviceService";
 
    private static volatile SearchDeviceManager sInstance;
    private volatile ConcurrentHashMap<String, DeviceNetInfo> mDeviceNetInfos = new ConcurrentHashMap<>();
    private SeachDeviceService.SearchDeviceBinder searchDevice;
    private ISearchDeviceListener mListener;
    boolean mIsConnected;               //service是否已连接
    volatile  boolean  mIsExist = false;
 
    private SearchDeviceManager() {
        mListener = new SearchDeviceImpl();
        mDeviceNetInfos = new ConcurrentHashMap<>();
 
    }
 
    public static SearchDeviceManager getInstance() {
        if (sInstance == null) {
            synchronized (SearchDeviceManager.class) {
                if (sInstance == null) {
                    sInstance = new SearchDeviceManager();
 
                }
            }
        }
        return sInstance;
    }
 
 
    public synchronized void connectService() {
 
        if(!mIsConnected){
            Intent intent = new Intent(ProviderManager.getAppProvider().getAppContext(), SeachDeviceService.class);
            mIsConnected = ProviderManager.getAppProvider().getAppContext().bindService(intent, mBinderPoolConnection, Context.BIND_AUTO_CREATE);
        }
        if(!mIsExist){
            mIsExist = true;
        }
    }
 
    private ServiceConnection mBinderPoolConnection = new ServiceConnection() {
 
        @Override
        public void onServiceDisconnected(ComponentName arg0) {
        }
 
        @Override
        public void onServiceConnected(ComponentName arg0, IBinder arg1) {
            LogUtil.debugLog(TAG, "onServiceConnected");
 
            searchDevice = (SeachDeviceService.SearchDeviceBinder) arg1;
 
            if (searchDevice != null) {
                try {
                    searchDevice.linkToDeath(mBinderPoolDeathRecipient, 0);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            registerListener(mListener);
            startSearch();
        }
    };
 
    private IBinder.DeathRecipient mBinderPoolDeathRecipient = new IBinder.DeathRecipient() {
 
        @Override
        public void binderDied() {
            LogUtil.debugLog(TAG, "binderDied");
            if (searchDevice != null) {
                searchDevice.unlinkToDeath(mBinderPoolDeathRecipient, 0);
                searchDevice = null;
            }
            connectService();
        }
    };
 
    public void registerListener(ISearchDeviceListener listener) {
        if (searchDevice != null) {
            searchDevice.registerListener(listener);
        }
    }
 
    public void unRegisterListener(ISearchDeviceListener listener) {
        if (searchDevice != null) {
            searchDevice.unRegisterListener(listener);
        }
    }
 
    public void stopSearch() {
        if (searchDevice != null) {
            searchDevice.stopSearchDevices();
            if(mIsConnected){
                ProviderManager.getAppProvider().getAppContext().unbindService(mBinderPoolConnection);
                mIsConnected = false;
            }
 
        }
    }
 
    public synchronized DeviceInitInfo getDeviceNetInfo(String snCode) {
        if (TextUtils.isEmpty(snCode))
            return null;
        if (mDeviceNetInfos != null&&mDeviceNetInfos.get(snCode)!=null&&mDeviceNetInfos.get(snCode).isValid()) {
            return mDeviceNetInfos.get(snCode).getDevNetInfoEx();
        }
        return null;
    }
 
    /**
     * 搜索
     */
    public synchronized void startSearch() {
        removeInvalidDevice();
        if (searchDevice != null) {
            connectService();
            searchDevice.startSearchDevices();
        }
    }
 
    public synchronized void clearDevice() {
        if (mDeviceNetInfos != null) {
            mDeviceNetInfos.clear();
            LogUtil.debugLog(TAG, "clear");
        }
    }
 
    /**
     * 列表中移除无效的设备信息
     */
    public synchronized void removeInvalidDevice() {
        if (mDeviceNetInfos != null) {
            LogUtil.debugLog(TAG, "removeInvalidDevice: " + mDeviceNetInfos);
            for (Map.Entry<String, DeviceNetInfo> entry : mDeviceNetInfos.entrySet()) {
                if (entry.getValue() != null) {
                    if (!entry.getValue().isValid()) {
                        // 移除无效的DeviceNetInfo
                        mDeviceNetInfos.remove(entry.getKey());
                        LogUtil.debugLog(TAG, "remove: " + entry.getKey());
                    } else {
                        // 将标志位重置
                        entry.getValue().setValid(false);
                    }
                }
            }
            LogUtil.debugLog(TAG, "removeInvalidDevice: " + mDeviceNetInfos);
        }
    }
 
    /**
     * 释放资源
     */
    public synchronized void checkSearchDeviceServiceDestory() {
        stopSearch();
        unRegisterListener(mListener);
        clearDevice();
        mIsExist = false;
    }
 
    public  synchronized boolean checkSearchDeviceServiceIsExist() {
        return mIsExist;
    }
 
    private class SearchDeviceImpl implements ISearchDeviceListener {
 
        @Override
        public void onDeviceSearched(String sncode, DeviceInitInfo info) {
            if (mDeviceNetInfos != null) {
                DeviceNetInfo deviceNetInfo=new DeviceNetInfo(info);
                mDeviceNetInfos.put(sncode, deviceNetInfo);
                LogUtil.debugLog(TAG, "onDeviceSearched: " + mDeviceNetInfos);
            }
        }
    }
 
    public interface ISearchDeviceListener {
        void onDeviceSearched(String sncode, DeviceInitInfo info);
    }
 
}