37 lines
3.5 KiB
Java
37 lines
3.5 KiB
Java
package cn.ucvl.zhaodisplay;
|
|
|
|
import org.json.*;
|
|
|
|
/** Entirely synthetic grants and machines. Test APK has no Internet permission. */
|
|
final class HomeFixture {
|
|
static volatile String mode="";static volatile int commands;static volatile JSONObject lastCommand;
|
|
static volatile boolean holdNextCatalog=false,held=false,release=false;
|
|
static JSONObject power(String id)throws Exception{
|
|
long now=System.currentTimeMillis()/1000;boolean offline=id.equals("demo-offline"),unknown=mode.equals("devices-unknown");
|
|
int op=mode.equals("devices-pending")?2:0;
|
|
JSONObject p=new JSONObject().put("run",unknown?JSONObject.NULL:offline?0:1).put("op",op).put("state",unknown?"unknown":offline?"offline":"online").put("checkedAt",now-(mode.equals("devices-stale")?90:0)).put("canStart",offline&&!unknown&&op==0).put("canShutdown",!offline&&!unknown&&op==0&&!mode.equals("devices-view-only")).put("managed",true);
|
|
p.put("last",op==0?JSONObject.NULL:new JSONObject().put("id","bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb").put("op",2).put("status","pending").put("result","等待设备确认").put("at",now));return p;
|
|
}
|
|
static JSONObject device(String id,String name,String family)throws Exception{return new JSONObject().put("id",id).put("source","managed").put("name",name).put("deviceType","computer").put("familyId",family).put("familyName",family.equals("demo-home")?"演示家庭":"演示工作室").put("siteName","演示书房").put("power",power(id));}
|
|
static JSONObject call(String token,JSONObject body)throws Exception{
|
|
if(token==null||token.isEmpty())throw new AssertionError("phone home requires the pairing bearer token");
|
|
if(mode.equals("devices-denied"))throw new DisplayApi.ApiError(403);
|
|
String action=body.getString("action");
|
|
if(action.equals("catalog")){
|
|
JSONArray families=new JSONArray().put(new JSONObject().put("id","demo-home").put("name","演示家庭").put("kind","family"));
|
|
JSONArray devices=new JSONArray();
|
|
if(!mode.equals("devices-empty")&&!token.equals("rebound-token")){families.put(new JSONObject().put("id","demo-office").put("name","演示工作室").put("kind","office"));devices.put(device("demo-online","演示笔记本","demo-home")).put(device("demo-offline","演示工作电脑","demo-office"));}
|
|
JSONObject result=new JSONObject().put("households",families).put("devices",devices).put("activeId","demo-home");
|
|
if(holdNextCatalog){holdNextCatalog=false;held=true;long until=System.currentTimeMillis()+5000;while(!release&&System.currentTimeMillis()<until)Thread.sleep(10);held=false;}
|
|
return result;
|
|
}
|
|
if(action.equals("status"))return new JSONObject().put("power",power(body.getString("deviceId")));
|
|
if(action.equals("command")){
|
|
if(!body.optBoolean("confirm")||!body.optString("requestId").matches("[0-9a-f]{32}"))throw new AssertionError("command must carry explicit confirm and a unique request id");
|
|
int op=body.optInt("op");if(op!=1&&op!=2)throw new AssertionError("only discrete power operations");
|
|
commands++;lastCommand=body;JSONObject p=power(body.getString("deviceId"));p.put("op",op).put("canStart",false).put("canShutdown",false).put("last",new JSONObject().put("id",body.getString("requestId")).put("op",op).put("status","pending").put("result","已提交演示请求").put("at",System.currentTimeMillis()/1000));return new JSONObject().put("power",p);
|
|
}
|
|
throw new AssertionError("unexpected home action "+action);
|
|
}
|
|
}
|