Files
ucvl-home-vision/android-display/tests/phone-preview/DisplayApi.java
T

19 lines
2.6 KiB
Java

package cn.ucvl.zhaodisplay;
import org.json.*;
/** Test-only transport. The fixture has no Internet permission and never calls production. */
final class DisplayApi {
static volatile boolean offline=false,expired=false;
static class ApiError extends Exception {final int status;ApiError(int status){this.status=status;}}
static JSONObject call(String path,String token,JSONObject body)throws Exception{
if(expired)throw new ApiError(401);if(offline)throw new ApiError(503);String action=body.optString("action");
if(path.equals("/api/voice")){
if(action.equals("history")){boolean old=body.optLong("before")>0;JSONArray items=new JSONArray();for(int i=0;i<3;i++)items.put(new JSONObject().put("id","chat-"+i).put("seq",i+1).put("mine",i%2==0).put("senderName",i%2==0?"示例成员的手机":"家庭显示终端").put("at",1792387200+i*60-(old?86400:0)).put("seconds",i+3.6).put("delivery",i==2?"queued":"played").put("archive",i==2?"pending":"cloud"));return new JSONObject().put("items",items).put("before",old?0:12);}
return new JSONObject("{\"enabled\":false,\"allowed\":[],\"candidates\":[{\"id\":\"phone:demo\",\"name\":\"示例家人的手机\"}],\"targets\":[{\"id\":\"display:demo\",\"archiveKey\":\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\"name\":\"家庭显示终端\",\"kind\":\"display\",\"online\":true}],\"conversations\":[],\"sent\":[],\"inbox\":[]}");
}
if(action.equals("update")||action.equals("stop"))return new JSONObject().put("ok",true);
if(action.equals("list")){JSONObject a=new JSONObject("{\"id\":\"a\",\"name\":\"家人甲(示例)\",\"status\":\"live\"}");a.put("position",new JSONObject().put("latitude",0).put("longitude",0).put("accuracy",25).put("at",System.currentTimeMillis()/1000-120));JSONObject b=new JSONObject("{\"id\":\"b\",\"name\":\"家人乙(示例)\",\"status\":\"scheduled\",\"scheduleReason\":\"sampling\"}");JSONObject c=new JSONObject().put("id","c").put("name","家人丙(示例)").put("position",new JSONObject().put("latitude",0).put("longitude",0).put("accuracy",10).put("at",System.currentTimeMillis()/1000));return new JSONObject().put("items",new JSONArray().put(b).put(a).put(c));}
if(action.equals("settings"))return new JSONObject("{\"receipt\":\"fixture\",\"intervalSeconds\":7200,\"sampleIntervalSeconds\":300,\"historyEnabled\":true,\"historyDefault\":true,\"viewerIds\":[],\"phoneIds\":[\"demo\"],\"displayIds\":[],\"viewers\":[],\"phones\":[{\"id\":\"demo\",\"name\":\"示例家人的手机\"}],\"displays\":[],\"history\":{\"pending\":0,\"archived\":2,\"deleting\":0}}");
throw new ApiError(403);
}
}