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