Improve LogUtil and mail
[rtmpclient.git] / app / src / main / java / ai / suanzi / rtmpclient / UserInfo.java
index 8009127..404562b 100644 (file)
@@ -19,40 +19,25 @@ import org.apache.log4j.Logger;
 
 public class UserInfo {
 
-    public String server = "";
-    public String user = "";
-    public String macAddr = "";
-    public String cameraId = "";
-    private static String configPath;
     private static Logger gLogger = Logger.getLogger("UserInfo");
 
-    private static String DEFAULT_SERVER = "rtmp://gpussh.suanzi.ai:1935/myapp";
-    private static String DEFAULT_USER = "yunzhi";
+    public static String  server = "rtmp://gpussh.suanzi.ai:1935/myapp";
+    public static String user = "yunzhi";
+    public static String macAddr = "";
+    public static String cameraId = "";
+    public static String configPath;
 
-    private static UserInfo instance = null;
     private UserInfo () {}
 
-    public static void setConfigPath(String fname){
+    public static void readConfig(String fname, String addr){
+        gLogger.debug("readConfig - fname " + fname + ", macaddr " + addr);
         configPath = fname;
-        File file = new File(configPath);
-        if(!file.exists()) {
-            gLogger.error("Config file: " + configPath + " not exists! Create it");
-            try {
-                file.createNewFile();
-            }catch (IOException e){
-                gLogger.error("Create file error: " + e.getMessage());
-                e.printStackTrace();
-            }
-        }
-    }
 
-    public static UserInfo getConfig() {
-        if (instance != null) return instance;
-
-        File file = new File(configPath);
-        if (!file.exists()){
-            gLogger.error("getConfig - file configpath: " + configPath + " not exists");
-            return new UserInfo();
+        File file = new File (fname);
+        if(!file.exists()){
+            gLogger.debug(fname + " does not exist!.");
+            macAddr = addr;
+            return;
         }
 
         StringBuilder text = new StringBuilder();
@@ -68,21 +53,20 @@ public class UserInfo {
         }
 
         String json = text.toString();
-
-        UserInfo info = new UserInfo();
-
         try {
             JSONObject jobj = new JSONObject(json);
-            info.server = jobj.getString("server");
-            info.user = jobj.getString("user");
-            info.macAddr = jobj.getString("macAddr");
-            info.cameraId = jobj.getString("cameraId");
+            server = jobj.getString("server");
+            user = jobj.getString("user");
+            macAddr = jobj.getString("macAddr");
+            if(!macAddr.equals(addr)){
+                gLogger.debug("Mac address changed from " + macAddr + " to " + addr);
+                macAddr = addr;
+            }
+            cameraId = jobj.getString("cameraId");
         } catch (JSONException e){
             gLogger.error("getConfig - error: " + e.getMessage());
             e.printStackTrace();
         }
-        instance = info;
-        return info;
     }
 
     public boolean saveConfig() {
@@ -103,10 +87,10 @@ public class UserInfo {
     }
 
     public void update(String server, String user, String macAddr, String cameraId) {
-        this.server = server;
-        this.user = user;
-        this.macAddr = macAddr;
-        this.cameraId = cameraId;
+        server = server;
+        user = user;
+        macAddr = macAddr;
+        cameraId = cameraId;
     }
 
     public String toString () {
@@ -126,10 +110,10 @@ public class UserInfo {
     private JSONObject toJsonObj () {
         try {
             JSONObject obj = new JSONObject();
-            obj.put("server", this.server);
-            obj.put("user", this.user);
-            obj.put("macAddr", this.macAddr);
-            obj.put("cameraId", this.cameraId);
+            obj.put("server", server);
+            obj.put("user", user);
+            obj.put("macAddr", macAddr);
+            obj.put("cameraId", cameraId);
             return obj;
         } catch (JSONException e) {
             e.printStackTrace();
@@ -137,10 +121,11 @@ public class UserInfo {
         }
     }
 
-    public String toUrl (String mac) {
+    public static String toUrl () {
         //rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid
         //return server + "/" + user + "_" + macAddr + "_" + cameraId;
-        return (server.equals("") ? DEFAULT_SERVER : server) + "/" + (user.equals("") ? DEFAULT_USER : user) + "_" + mac;
+        return server + "/" +  user + "_" + macAddr;
+
 
     }