111
hxb
2022-11-24 0a3e07f10937484145f33c7560607b4b2353cb81
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
package com.mm.android.deviceaddmodule.service;
 
import android.text.TextUtils;
 
import com.google.gson.Gson;
import com.mm.android.deviceaddmodule.LCDeviceEngine;
import com.mm.android.deviceaddmodule.mobilecommon.AppConsume.BusinessException;
import com.mm.android.deviceaddmodule.mobilecommon.businesstip.BusinessErrorCode;
import com.mm.android.deviceaddmodule.mobilecommon.entity.deviceadd.DeviceIntroductionInfo;
import com.mm.android.deviceaddmodule.openapi.data.DeviceLeadingInfoData;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
 
/**
 * 用于将服务返回信息保存成文件
 **/
public class FileSaveHelper {
    public static String INTRODUCTION_INFO_NAME="introduction.json";          //设备引导
 
    public static void saveToJsonInfo(String content, String fileName){
        String directory = getCachePath();
        FileSaveHelper.writeToFile(content, directory, fileName);
    }
 
 
    public static DeviceIntroductionInfo getIntroductionInfoCache(String model, String lan) throws BusinessException {
        // 读缓存
        String directory = getCachePath();
        String contentJson = getJsonFromFile(directory, model+"_"+lan+"_"+ FileSaveHelper.INTRODUCTION_INFO_NAME);
        if (TextUtils.isEmpty(contentJson)) {
            throw new BusinessException(BusinessErrorCode.BEC_COMMON_NULL_POINT);
        }
 
        DeviceLeadingInfoData.Response  response = null;
        try {
            Gson gson = new Gson();
            response = gson.fromJson(contentJson,
                    DeviceLeadingInfoData.Response.class);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(BusinessErrorCode.BEC_COMMON_NULL_POINT);
        }
 
        if (response == null || response.data == null
                || response.data.introductions == null) {
            throw new BusinessException(BusinessErrorCode.BEC_COMMON_NULL_POINT);
        }
 
        return DeviceAddEntityChangeHelper.parse2DeviceIntroductionInfo(response.data);
    }
 
    private static String getCachePath() {
        String userId = LCDeviceEngine.newInstance().userId;
        return LCDeviceEngine.newInstance().commonParam.getContext().getFilesDir() + File.separator + userId + File.separator;
    }
 
    private static void writeToFile(String content, String directory, String fileName) {
        // mobile SD card path +path
        BufferedWriter os = null;
        File file = new File(directory);
        try {
            if (!file.exists()) {
                if (!file.mkdirs()) {
                    return;
                }
            }
            file = new File(file, fileName);
            os = new BufferedWriter(new FileWriter(file), 1024);
            os.write(content);
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
    private static String getJsonFromFile(String dirctory, String fileName) {
        // mobile SD card path +path
        BufferedReader os = null;
        StringBuffer stringBuffer = new StringBuffer();
        File file = new File(dirctory, fileName);
        try {
            if (!file.exists()) {
                return "";
            }
            os = new BufferedReader(new FileReader(file));
            String line = "";
            while ((line = os.readLine()) != null) {
                stringBuffer.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return stringBuffer.toString().trim();
    }
}