1 package ai.suanzi.rtmpclient;
3 import android.widget.Toast;
5 import java.io.BufferedReader;
6 import java.io.BufferedWriter;
8 import java.io.FileReader;
9 import java.io.FileWriter;
10 import java.io.IOException;
13 import org.json.JSONException;
14 import org.json.JSONObject;
15 import org.apache.log4j.Logger;
17 public class UserInfo {
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");
27 private static UserInfo instance = null;
28 private UserInfo () {}
30 public static void setConfigPath(String fname){
32 File file = new File(configPath);
34 gLogger.error("Config file: " + configPath + " not exists! Create it");
37 }catch (IOException e){
38 gLogger.error("Create file error: " + e.getMessage());
44 public static UserInfo getConfig() {
45 if (instance != null) return instance;
47 File file = new File(configPath);
49 gLogger.error("getConfig - file configpath: " + configPath + " not exists");
50 return new UserInfo();
53 StringBuilder text = new StringBuilder();
55 BufferedReader br = new BufferedReader(new FileReader(file));
57 while((line = br.readLine()) != null){
61 }catch (IOException e){
65 String json = text.toString();
67 UserInfo info = new UserInfo();
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());
83 public boolean saveConfig() {
85 String jstring = toString();
87 File file = new File(configPath);
89 BufferedWriter bw = new BufferedWriter(new FileWriter(file));
92 } catch (IOException e){
93 gLogger.error("saveConfig - error: " + e.getMessage());
100 public void update(String server, String user, String macAddr, String cameraId) {
101 this.server = server;
103 this.macAddr = macAddr;
104 this.cameraId = cameraId;
107 public String toString () {
108 JSONObject obj = toJsonObj();
110 if (!obj.equals(null)) {
112 str = obj.toString(4);
113 }catch (JSONException e){
114 gLogger.error("toString - error: " + e.getMessage());
121 private JSONObject toJsonObj () {
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);
129 } catch (JSONException e) {
135 public String toUrl () {
136 //rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid
137 return server + "/" + user + "_" + macAddr + "_" + cameraId;
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;