9d95894b43a522b9015ee966e621aec544032d98
[rtmpclient.git] / app / src / main / java / ai / suanzi / rtmpclient / UserInfo.java
1 package ai.suanzi.rtmpclient;
2
3 import android.widget.Toast;
4
5 import java.io.BufferedReader;
6 import java.io.BufferedWriter;
7 import java.io.File;
8 import java.io.FileReader;
9 import java.io.FileWriter;
10 import java.io.IOException;
11
12
13 import org.json.JSONException;
14 import org.json.JSONObject;
15 import org.apache.log4j.Logger;
16
17 public class UserInfo {
18
19     public String server = "";
20     public String user = "";
21     public String macAddr = "";
22     public String cameraId = "";
23     private static String configPath;
24     private static Logger gLogger = Logger.getLogger("UserInfo");
25
26
27     private static UserInfo instance = null;
28     private UserInfo () {}
29
30     public static void setConfigPath(String fname){
31         configPath = fname;
32         File file = new File(configPath);
33         if(!file.exists()) {
34             gLogger.error("Config file: " + configPath + " not exists! Create it");
35             try {
36                 file.createNewFile();
37             }catch (IOException e){
38                 gLogger.error("Create file error: " + e.getMessage());
39                 e.printStackTrace();
40             }
41         }
42     }
43
44     public static UserInfo getConfig() {
45         if (instance != null) return instance;
46
47         File file = new File(configPath);
48         if (!file.exists()){
49             gLogger.error("getConfig - file configpath: " + configPath + " not exists");
50             return new UserInfo();
51         }
52
53         StringBuilder text = new StringBuilder();
54         try {
55             BufferedReader br = new BufferedReader(new FileReader(file));
56             String line;
57             while((line = br.readLine()) != null){
58                 text.append(line);
59             }
60             br.close();
61         }catch (IOException e){
62             e.printStackTrace();
63         }
64
65         String json = text.toString();
66
67         UserInfo info = new UserInfo();
68
69         try {
70             JSONObject jobj = new JSONObject(json);
71             info.server = jobj.getString("server");
72             info.user = jobj.getString("user");
73             info.macAddr = jobj.getString("macAddr");
74             info.cameraId = jobj.getString("cameraId");
75         } catch (JSONException e){
76             gLogger.error("getConfig - error: " + e.getMessage());
77             e.printStackTrace();
78         }
79         instance = info;
80         return info;
81     }
82
83     public boolean saveConfig() {
84
85         String jstring = toString();
86
87         File file = new File(configPath);
88         try{
89             BufferedWriter bw = new BufferedWriter(new FileWriter(file));
90             bw.write(jstring);
91             bw.close();
92         } catch (IOException e){
93             gLogger.error("saveConfig - error: " + e.getMessage());
94             e.printStackTrace();
95             return false;
96         }
97         return true;
98     }
99
100     public void update(String server, String user, String macAddr, String cameraId) {
101         this.server = server;
102         this.user = user;
103         this.macAddr = macAddr;
104         this.cameraId = cameraId;
105     }
106
107     public String toString () {
108         JSONObject obj = toJsonObj();
109         String str = "";
110         if (!obj.equals(null)) {
111             try {
112                 str = obj.toString(4);
113             }catch (JSONException e){
114                 gLogger.error("toString - error: " + e.getMessage());
115                 e.printStackTrace();
116             }
117         }
118         return str;
119     }
120
121     private JSONObject toJsonObj () {
122         try {
123             JSONObject obj = new JSONObject();
124             obj.put("server", this.server);
125             obj.put("user", this.user);
126             obj.put("macAddr", this.macAddr);
127             obj.put("cameraId", this.cameraId);
128             return obj;
129         } catch (JSONException e) {
130             e.printStackTrace();
131             return null;
132         }
133     }
134
135     public String toUrl () {
136         //rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid
137         return server + "/" + user + "_" + macAddr + "_" + cameraId;
138     }
139
140     public String getValue(String key){
141         if(key.equals("server")) return server;
142         if(key.equals("user")) return user;
143         if(key.equals("macAddr")) return macAddr;
144         if(key.equals("cameraId")) return cameraId;
145         return "";
146     }
147 }