1
wxr
2023-04-23 2cd55265ccff3b0a267d7953b2dd9e5dca437aa6
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
package com.videogo.remoteplayback.list.querylist;
 
import android.app.Activity;
 
import com.videogo.EzvizApplication;
import com.videogo.device.DeviceReportInfo;
import com.videogo.errorlayer.ErrorInfo;
import com.videogo.exception.BaseException;
import com.videogo.openapi.bean.EZDeviceRecordFile;
import com.videogo.openapi.bean.resp.CloudPartInfoFile;
import com.videogo.remoteplayback.list.RemoteListContant;
import com.videogo.remoteplayback.list.bean.CloudPartInfoFileEx;
import com.videogo.ui.common.HikAsyncTask;
import com.videogo.util.CollectionUtil;
import com.videogo.util.DateTimeUtil;
import com.videogo.util.LogUtil;
import com.videogo.util.Utils;
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
 
import ezviz.ezopensdk.R;
 
public class QueryDeviceRecordFilesAsyncTask extends HikAsyncTask<String, Void, Integer> {
 
    private int channelNo;
    private String deviceSerial;
    private Date queryDate;
    private List<CloudPartInfoFileEx> playBackListLocalItems = new ArrayList<CloudPartInfoFileEx>();
    private List<CloudPartInfoFile> convertCalendarFiles;
    private QueryPlayBackListTaskCallback playBackListTaskCallback = null;
    private volatile boolean abort = false;
    private boolean onlyHasLocal = false;
    private int localErrorCode = 0;
    private int queryMode = DeviceReportInfo.REPOERT_QUERY_RELATE_TF;
    private String queryDetail = "";
    private int cloudTotal;
    private final String MINUTE;
 
    public QueryDeviceRecordFilesAsyncTask(String deviceSerial, int channelNo,
                                           QueryPlayBackListTaskCallback playBackListTaskCallback) {
        MINUTE = ((Activity) playBackListTaskCallback).getString(R.string.local_play_hour);
        this.channelNo = channelNo;
        this.deviceSerial = deviceSerial;
        this.playBackListTaskCallback = playBackListTaskCallback;
    }
 
    public void setAbort(boolean abort) {
        this.abort = abort;
    }
 
    public void setQueryDate(Date queryDate) {
        this.queryDate = queryDate;
    }
 
    public void setOnlyHasLocal(boolean onlyHasLocal) {
        this.onlyHasLocal = onlyHasLocal;
    }
 
    @Override
    protected void onPreExecute() {
    }
 
    @Override
    protected Integer doInBackground(String... params) {
        cloudTotal = Integer.parseInt(params[0]);
        return searchLocalFile();
    }
 
    @Override
    protected void onPostExecute(Integer result) {
        if (!abort) {
            playBackListTaskCallback
                    .queryTaskOver(RemoteListContant.TYPE_LOCAL, queryMode, localErrorCode, queryDetail);
            if (result == RemoteListContant.QUERY_LOCAL_SUCCESSFUL) {
                playBackListTaskCallback.querySuccessFromDevice(playBackListLocalItems, cloudTotal, convertCalendarFiles);
            } else if (result == RemoteListContant.QUERY_NO_DATA) {
                if (onlyHasLocal) {
                    playBackListTaskCallback.queryOnlyLocalNoData();
                } else {
                    playBackListTaskCallback.queryLocalNoData();
                }
            } else if (result == RemoteListContant.QUERY_EXCEPTION) {
                if (onlyHasLocal) {
                    playBackListTaskCallback.queryException();
                } else {
                    playBackListTaskCallback.queryLocalException();
                }
            }
        }
    }
 
    private String calendar2String(Calendar cal) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateStr = sdf.format(cal.getTime());
        
        return dateStr;
    }
    private void convertEZDeviceRecordFile2CloudPartInfoFile(CloudPartInfoFile dst, EZDeviceRecordFile src, int pos) {
        dst.setStartTime(calendar2String(src.getStartTime()));
        dst.setEndTime(calendar2String(src.getStopTime()));
        dst.setPosition(pos);
    }
 
    public int searchLocalFile() {
        playBackListLocalItems.clear();
        Date beginDate = DateTimeUtil.beginDate(queryDate);
        Date endDate = DateTimeUtil.endDate(queryDate);
        Calendar startTime = Calendar.getInstance();
        Calendar endTime = Calendar.getInstance();
        startTime.setTime(queryDate);
        endTime.setTime(queryDate);
        
        startTime.set(Calendar.HOUR_OF_DAY, 0);
        startTime.set(Calendar.MINUTE, 0);
        startTime.set(Calendar.SECOND, 0);
        endTime.set(Calendar.HOUR_OF_DAY, 23);
        endTime.set(Calendar.MINUTE, 59);
        endTime.set(Calendar.SECOND, 59);
 
        List<EZDeviceRecordFile> tmpList = null;
        try {
            tmpList = EzvizApplication.getOpenSDK().searchRecordFileFromDevice(deviceSerial,channelNo,
                    startTime, endTime);
        } catch (BaseException e) {
            e.printStackTrace();
 
            ErrorInfo errorInfo = e.getObject();
            LogUtil.d("search file list failed. error ", errorInfo.toString());
        }
 
        convertCalendarFiles = new ArrayList<>();
        if (tmpList != null && tmpList.size() > 0) {
            for (int i = 0; i < tmpList.size(); i++) {
                EZDeviceRecordFile file = tmpList.get(i);
                CloudPartInfoFile cpif = new CloudPartInfoFile();
 
                convertEZDeviceRecordFile2CloudPartInfoFile(cpif, file, i);
                convertCalendarFiles.add(cpif);
            }
        }
 
        if (CollectionUtil.isNotEmpty(convertCalendarFiles)) {
            Collections.sort(convertCalendarFiles);
        }
        int length = convertCalendarFiles.size();
        int i = 0;
        while (i < length) {
            CloudPartInfoFileEx cloudPartInfoFileEx = new CloudPartInfoFileEx();
            CloudPartInfoFile dataOne = getCloudPartInfoFile(convertCalendarFiles.get(i), beginDate, endDate);
            dataOne.setPosition(cloudTotal + i);
            Calendar beginCalender = Utils.convert14Calender(dataOne.getStartTime());
 
            String hour = getHour(beginCalender.get(Calendar.HOUR_OF_DAY));
            cloudPartInfoFileEx.setHeadHour(hour);
            cloudPartInfoFileEx.setDataOne(dataOne);
            i++;
            if (i > length - 1) {
                playBackListLocalItems.add(cloudPartInfoFileEx);
                continue;
            }
            CloudPartInfoFile dataTwo = getCloudPartInfoFile(convertCalendarFiles.get(i), beginDate, endDate);
            if (hour.equals(getHour(Utils.convert14Calender(dataTwo.getStartTime()).get(Calendar.HOUR_OF_DAY)))) {
                dataTwo.setPosition(cloudTotal + i);
                cloudPartInfoFileEx.setDataTwo(dataTwo);
                i++;
                if (i > length - 1) {
                    playBackListLocalItems.add(cloudPartInfoFileEx);
                    continue;
                }
                CloudPartInfoFile dataThree = getCloudPartInfoFile(convertCalendarFiles.get(i), beginDate, endDate);
                if (hour.equals(getHour(Utils.convert14Calender(dataThree.getStartTime()).get(Calendar.HOUR_OF_DAY)))) {
                    dataThree.setPosition(cloudTotal + i);
                    cloudPartInfoFileEx.setDataThree(dataThree);
                    i++;
                }
            }
            playBackListLocalItems.add(cloudPartInfoFileEx);
        }
 
        if (CollectionUtil.isNotEmpty(playBackListLocalItems)) {
            return RemoteListContant.QUERY_LOCAL_SUCCESSFUL;
        }
        return RemoteListContant.QUERY_NO_DATA;
 
    }
 
    private CloudPartInfoFile getCloudPartInfoFile(CloudPartInfoFile cloudPartInfoFile, Date beginDate, Date endDate) {
        Calendar beginCalender = Utils.convert14Calender(cloudPartInfoFile.getStartTime());
//        if (beginCalender.getTimeInMillis() < beginDate.getTime()) {
//            beginCalender = Calendar.getInstance();
//            beginCalender.setTime(beginDate);
//        }
        Calendar endCalender = Utils.convert14Calender(cloudPartInfoFile.getEndTime());
        if (endCalender.getTimeInMillis() > endDate.getTime()) {
            endCalender = Calendar.getInstance();
            endCalender.setTime(endDate);
        }
        cloudPartInfoFile.setStartTime(new SimpleDateFormat("yyyyMMddHHmmss").format(beginCalender.getTime()));
        cloudPartInfoFile.setEndTime(new SimpleDateFormat("yyyyMMddHHmmss").format(endCalender.getTime()));
        return cloudPartInfoFile;
    }
 
    private String getHour(int hourOfDay) {
        return hourOfDay + MINUTE;
    }
 
}