8bb20b94067cac7fa075f86f4a096c09250945ce
[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("getConfig - error: " + e.getMessage());
63             e.printStackTrace();
64         }
65     }
66
67     public boolean saveConfig() {
68
69         String jstring = toString();
70
71         File file = new File(configPath);
72         try{
73             BufferedWriter bw = new BufferedWriter(new FileWriter(file));
74             bw.write(jstring);
75             bw.close();
76         } catch (IOException e){
77             gLogger.error("saveConfig - error: " + e.getMessage());
78             e.printStackTrace();
79             return false;
80         }
81         return true;
82     }
83
84 //    public void update(String server, String user, String macAddr, String cameraId) {
85 //        server = server;
86 //        user = user;
87 //        macAddr = macAddr;
88 //        cameraId = cameraId;
89 //    }
90
91     public String toString () {
92         JSONObject obj = toJsonObj();
93         String str = "";
94         if (!obj.equals(null)) {
95             try {
96                 str = obj.toString(4);
97             }catch (JSONException e){
98                 gLogger.error("toString - error: " + e.getMessage());
99                 e.printStackTrace();
100             }
101         }
102         return str;
103     }
104
105     private JSONObject toJsonObj () {
106         try {
107             JSONObject obj = new JSONObject();
108             obj.put("server", server);
109             obj.put("user", user);
110             obj.put("macAddr", macAddr);
111             obj.put("cameraId", cameraId);
112             return obj;
113         } catch (JSONException e) {
114             e.printStackTrace();
115             return null;
116         }
117     }
118
119     public static String toUrl () {
120         //rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid
121         //return server + "/" + user + "_" + macAddr + "_" + cameraId;
122         return server + "/" +  user + "_" + macAddr;
123
124
125     }
126
127 //    public String getValue(String key){
128 //        if(key.equals("server")) return server;
129 //        if(key.equals("user")) return user;
130 //        if(key.equals("macAddr")) return macAddr;
131 //        if(key.equals("cameraId")) return cameraId;
132 //        return "";
133 //    }
134
135 }