Rename the generated package
[rtmpclient.git] / app / src / main / java / ai / suanzi / rtmpclient / UserInfo.java
index 00e5e23..403e506 100644 (file)
@@ -1,38 +1,40 @@
 package ai.suanzi.rtmpclient;
 
-import android.content.Context;
-import android.util.Log;
-import android.widget.Toast;
-
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
-import java.io.InputStream;
+
 
 import org.json.JSONException;
 import org.json.JSONObject;
+import org.apache.log4j.Logger;
 
 public class UserInfo {
 
-    public String server = "";
-    public String user = "suanzi";
-    public String macAddr = "ac83f34ead90";
-    public String cameraId = "cameraId";
-    private static String configPath;
+    private static Logger gLogger = Logger.getLogger("UserInfo");
+
+    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;
-    }
 
-    public static UserInfo getConfig() {
-        if (instance != null) return instance;
-        File file = new File(configPath);
+        File file = new File (fname);
+        if(!file.exists()){
+            gLogger.debug(fname + " does not exist!.");
+            macAddr = addr;
+            return;
+        }
+
         StringBuilder text = new StringBuilder();
         try {
             BufferedReader br = new BufferedReader(new FileReader(file));
@@ -46,64 +48,89 @@ 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("readConfig - error: " + e.getMessage());
+            macAddr = addr;
             e.printStackTrace();
         }
-        instance = info;
-        return info;
     }
 
     public boolean saveConfig() {
 
         String jstring = toString();
 
-        Log.e("Config", "xxxxxxxxx "  + jstring);
-
         File file = new File(configPath);
         try{
             BufferedWriter bw = new BufferedWriter(new FileWriter(file));
             bw.write(jstring);
             bw.close();
         } catch (IOException e){
+            gLogger.error("saveConfig - error: " + e.getMessage());
             e.printStackTrace();
             return false;
         }
         return true;
     }
 
-    public void update(String server, String user, String macAddr, String cameraId) {
-        this.server = server;
-        this.user = user;
-        this.macAddr = macAddr;
-        this.cameraId = cameraId;
-    }
+//    public void update(String server, String user, String macAddr, String cameraId) {
+//        server = server;
+//        user = user;
+//        macAddr = macAddr;
+//        cameraId = cameraId;
+//    }
 
     public String toString () {
         JSONObject obj = toJsonObj();
-        if (obj.equals(null)) return "";
-        return obj.toString();
+        String str = "";
+        if (!obj.equals(null)) {
+            try {
+                str = obj.toString(4);
+            }catch (JSONException e){
+                gLogger.error("toString - error: " + e.getMessage());
+                e.printStackTrace();
+            }
+        }
+        return str;
     }
 
     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();
             return null;
         }
     }
+
+    public static String toUrl () {
+        //rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid
+        //return server + "/" + user + "_" + macAddr + "_" + cameraId;
+        return server + "/" +  user + "_" + macAddr;
+
+
+    }
+
+//    public String getValue(String key){
+//        if(key.equals("server")) return server;
+//        if(key.equals("user")) return user;
+//        if(key.equals("macAddr")) return macAddr;
+//        if(key.equals("cameraId")) return cameraId;
+//        return "";
+//    }
+
 }