res.setStatus(HttpServletResponse.SC_OK);
res.setContentType("application/json");
// 二種方式都能存取上傳的圖檔
JSONObject json = new JSONObject();
// json.put("serving_url", servingUrl);
// json.put("blob_key", blobKey.getKeyString());
// json.put("direct_url", "/serve?blob_key=" + blobKey.getKeyString());
// 儲存在db的動作
DeviceUpload du = new DeviceUpload();
du.setDevID(devID);
du.setMySerial(mySerial);
du.setServing_url(servingUrl);
du.setBlob_key(blobKey.getKeyString());
du.setAddTime(CTCommon.getNowTime());
du_service.add(du);
log.info("新增一筆設備上傳記錄:" + JSON.toJSONString(du));
// 直接新增message info到db
// 並直接把message content發給相關的devices
JSONObject retJson = new JSONObject();
CarTracePushNotificationAPI ctpn_api = new CarTracePushNotificationAPI();
if (null != cr) {
String carID = cr.getEncodedKey();
String title = "設備上傳記錄";
String message = String.format("設備於%s上傳圖片", du.getAddTime());
JSONObject rowdata = new JSONObject();
rowdata.put("carID", carID);
rowdata.put("addTime", du.getAddTime());
JSONObject cpJson = new JSONObject();
cpJson.put("carID", carID);
cpJson.put("title", title);
cpJson.put("message", message);
cpJson.put("rowdata", rowdata);
retJson = ctpn_api.postCPPushNotification(cpJson);
String messageID = retJson.getString("messageID");
// JSONArray phoneArray = retJson.getJSONArray("phoneArray");
retJson = ctpn_api.postCPPushNotificationSend(carID, messageID);
} else if (null != pr) {
String phoneID = pr.getEncodedKey();
String title = "設備上傳記錄";
String message = String.format("設備於%s上傳圖片", du.getAddTime());
JSONObject rowdata = new JSONObject();
rowdata.put("phoneID", phoneID);
rowdata.put("addTime", du.getAddTime());
JSONObject cpJson = new JSONObject();
cpJson.put("phoneID", phoneID);
cpJson.put("title", title);
cpJson.put("message", message);
cpJson.put("rowdata", rowdata);
retJson = ctpn_api.postPCPushNotification(cpJson);
String messageID = retJson.getString("messageID");
// JSONArray phoneArray = retJson.getJSONArray("phoneArray");
retJson = ctpn_api.postPCPushNotificationSend(phoneID, messageID);