1 package ai.suanzi.rtmpclient;
3 import android.content.Context;
4 import android.net.wifi.WifiInfo;
5 import android.net.wifi.WifiManager;
6 import android.widget.Toast;
8 import java.io.BufferedReader;
9 import java.io.BufferedWriter;
11 import java.io.FileReader;
12 import java.io.FileWriter;
13 import java.io.IOException;
16 import org.json.JSONException;
17 import org.json.JSONObject;
18 import org.apache.log4j.Logger;
20 public class UserInfo {
22 public String server = "";
23 public String user = "";
24 public String macAddr = "";
25 public String cameraId = "";
26 private static String configPath;
27 private static Logger gLogger = Logger.getLogger("UserInfo");
29 private static String DEFAULT_SERVER = "rtmp://gpussh.suanzi.ai:1935/myapp";
30 private static String DEFAULT_USER = "yunzhi";
32 private static UserInfo instance = null;
33 private UserInfo () {}
35 public static void setConfigPath(String fname){
37 File file = new File(configPath);
39 gLogger.error("Config file: " + configPath + " not exists! Create it");
42 }catch (IOException e){
43 gLogger.error("Create file error: " + e.getMessage());
49 public static UserInfo getConfig() {
50 if (instance != null) return instance;
52 File file = new File(configPath);
54 gLogger.error("getConfig - file configpath: " + configPath + " not exists");
55 return new UserInfo();
58 StringBuilder text = new StringBuilder();
60 BufferedReader br = new BufferedReader(new FileReader(file));
62 while((line = br.readLine()) != null){
66 }catch (IOException e){
70 String json = text.toString();
72 UserInfo info = new UserInfo();
75 JSONObject jobj = new JSONObject(json);
76 info.server = jobj.getString("server");
77 info.user = jobj.getString("user");
78 info.macAddr = jobj.getString("macAddr");
79 info.cameraId = jobj.getString("cameraId");
80 } catch (JSONException e){
81 gLogger.error("getConfig - error: " + e.getMessage());
88 public boolean saveConfig() {
90 String jstring = toString();
92 File file = new File(configPath);
94 BufferedWriter bw = new BufferedWriter(new FileWriter(file));
97 } catch (IOException e){
98 gLogger.error("saveConfig - error: " + e.getMessage());
105 public void update(String server, String user, String macAddr, String cameraId) {
106 this.server = server;
108 this.macAddr = macAddr;
109 this.cameraId = cameraId;
112 public String toString () {
113 JSONObject obj = toJsonObj();
115 if (!obj.equals(null)) {
117 str = obj.toString(4);
118 }catch (JSONException e){
119 gLogger.error("toString - error: " + e.getMessage());
126 private JSONObject toJsonObj () {
128 JSONObject obj = new JSONObject();
129 obj.put("server", this.server);
130 obj.put("user", this.user);
131 obj.put("macAddr", this.macAddr);
132 obj.put("cameraId", this.cameraId);
134 } catch (JSONException e) {
140 public String toUrl (String mac) {
141 //rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid
142 //return server + "/" + user + "_" + macAddr + "_" + cameraId;
143 return (server.equals("") ? DEFAULT_SERVER : server) + "/" + (user.equals("") ? DEFAULT_USER : user) + "_" + mac;
147 public String getValue(String key){
148 if(key.equals("server")) return server;
149 if(key.equals("user")) return user;
150 if(key.equals("macAddr")) return macAddr;
151 if(key.equals("cameraId")) return cameraId;