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