|
@@ -0,0 +1,202 @@
|
|
|
+package com.welampiot.utils;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.welampiot.configuration.ScreenConfig;
|
|
|
+import com.welampiot.dto.ScreenDTO;
|
|
|
+import com.welampiot.dto.SystemConfigDto;
|
|
|
+import com.welampiot.service.SystemConfigService;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.net.ConnectException;
|
|
|
+import java.net.Socket;
|
|
|
+import java.net.SocketTimeoutException;
|
|
|
+import java.sql.Timestamp;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+public class ScreenUtil {
|
|
|
+ /**
|
|
|
+ * 诺瓦显示屏获取token值
|
|
|
+ * @param systemConfigService
|
|
|
+ * @return token
|
|
|
+ */
|
|
|
+ public static String getTaiLongToken(SystemConfigService systemConfigService){
|
|
|
+ SystemConfigDto taiLongToken = systemConfigService.getOneBykey("TaiLongToken");
|
|
|
+ SystemConfigDto taiLongTokenExpireTime = systemConfigService.getOneBykey("TaiLongTokenExpireTime");
|
|
|
+
|
|
|
+ if (taiLongToken == null || taiLongTokenExpireTime == null || System.currentTimeMillis() > Timestamp.valueOf(taiLongTokenExpireTime.getValue()).getTime()){
|
|
|
+ String url = ScreenConfig.taiLongHost+"/v1/oauth/token";
|
|
|
+ String data = "{\"username\":\""+ScreenConfig.taiLongUsername+"\",\"password\":\""+ScreenConfig.taiLongPassword+"\"}";
|
|
|
+ HashMap<String, String> stringStringHashMap = new HashMap<>();
|
|
|
+ stringStringHashMap.put("username",ScreenConfig.taiLongUsername);
|
|
|
+ stringStringHashMap.put("Content-Type","application/json");
|
|
|
+ String s = WebUtils.requestPost(url, data,stringStringHashMap);
|
|
|
+ Map jsonObject = JSON.parseObject(s,Map.class);
|
|
|
+ if (jsonObject != null){
|
|
|
+ if (jsonObject.containsKey("status") && (int)jsonObject.get("status") == 0 && jsonObject.containsKey("data")){
|
|
|
+ JSONObject data2 = (JSONObject) jsonObject.get("data");
|
|
|
+ String token = (String) data2.get("token");
|
|
|
+ if (taiLongToken == null){
|
|
|
+ SystemConfigDto systemConfigDto = new SystemConfigDto();
|
|
|
+ systemConfigDto.setKey("TaiLongToken");
|
|
|
+ systemConfigDto.setValue(token);
|
|
|
+ systemConfigService.addByDto(systemConfigDto);
|
|
|
+ }else {
|
|
|
+ taiLongToken.setValue(token);
|
|
|
+ systemConfigService.updateByDto(taiLongToken);
|
|
|
+ }
|
|
|
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Date date = new Date(System.currentTimeMillis()+12*3600*1000);
|
|
|
+ String format = simpleDateFormat.format(date);
|
|
|
+ if (taiLongTokenExpireTime == null){
|
|
|
+ SystemConfigDto systemConfigDto = new SystemConfigDto();
|
|
|
+ systemConfigDto.setKey("TaiLongTokenExpireTime");
|
|
|
+ systemConfigDto.setValue(format);
|
|
|
+ systemConfigService.addByDto(systemConfigDto);
|
|
|
+ }else {
|
|
|
+ taiLongTokenExpireTime.setValue(format);
|
|
|
+ systemConfigService.updateByDto(taiLongTokenExpireTime);
|
|
|
+ }
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }else {
|
|
|
+ return taiLongToken.getValue();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 诺瓦显示发送节目信息
|
|
|
+ * @param systemConfigService
|
|
|
+ * @param url 发送地址
|
|
|
+ * @param data 发送内容
|
|
|
+ * @return 返回发送结果
|
|
|
+ */
|
|
|
+ public static String sendTaiLongPro(SystemConfigService systemConfigService,String url,String data){
|
|
|
+ String taiLongToken = getTaiLongToken(systemConfigService);
|
|
|
+ url = ScreenConfig.taiLongHost+url;
|
|
|
+ HashMap<String, String> stringStringHashMap = new HashMap<>();
|
|
|
+ stringStringHashMap.put("username",ScreenConfig.taiLongUsername);
|
|
|
+ stringStringHashMap.put("token",taiLongToken);
|
|
|
+ stringStringHashMap.put("Content-Type","application/json");
|
|
|
+ String s = WebUtils.requestPost(url, data,stringStringHashMap);
|
|
|
+ return s;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 熙迅屏幕发送指令信息
|
|
|
+ * @param msg 指令信息
|
|
|
+ * @return 指令返回信息
|
|
|
+ */
|
|
|
+ public static String sendXiXunInfo(String msg) {
|
|
|
+ try {
|
|
|
+ //创建客户端的Socket对象
|
|
|
+ Socket socket = new Socket(ScreenConfig.xiXunServerHost, ScreenConfig.xiXunServerPort);
|
|
|
+ socket.setSoTimeout(ScreenConfig.xiXunServerTimeout);
|
|
|
+
|
|
|
+ //获取输出流,写数据
|
|
|
+ OutputStream outputStream = socket.getOutputStream();
|
|
|
+ outputStream.write(msg.getBytes());
|
|
|
+
|
|
|
+ //接收服务器的反馈
|
|
|
+ InputStream inputStream = socket.getInputStream();
|
|
|
+ byte[] bys = new byte[1024];
|
|
|
+ int len = inputStream.read(bys);
|
|
|
+ String s = new String(bys, 0, len);
|
|
|
+ //释放资源
|
|
|
+ socket.close();
|
|
|
+ return s;
|
|
|
+ }
|
|
|
+ catch (ConnectException e4) {
|
|
|
+ }
|
|
|
+ catch (Exception e5) {
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 熙迅屏幕发送指令信息
|
|
|
+ * @param msg 指令信息
|
|
|
+ * @param timeout 指令超时时间
|
|
|
+ * @return 指令返回信息
|
|
|
+ */
|
|
|
+ public static String sendXiXunInfo(String msg,int timeout) {
|
|
|
+ try {
|
|
|
+ //创建客户端的Socket对象
|
|
|
+ Socket socket = new Socket(ScreenConfig.xiXunServerHost, ScreenConfig.xiXunServerPort);
|
|
|
+ socket.setSoTimeout(timeout);
|
|
|
+
|
|
|
+ //获取输出流,写数据
|
|
|
+ OutputStream outputStream = socket.getOutputStream();
|
|
|
+ outputStream.write(msg.getBytes());
|
|
|
+
|
|
|
+ //接收服务器的反馈
|
|
|
+ InputStream inputStream = socket.getInputStream();
|
|
|
+ byte[] bys = new byte[1024];
|
|
|
+ int len = inputStream.read(bys);
|
|
|
+ String s = new String(bys, 0, len);
|
|
|
+ //释放资源
|
|
|
+ socket.close();
|
|
|
+ return s;
|
|
|
+ }
|
|
|
+ catch (ConnectException e4) {
|
|
|
+ }
|
|
|
+ catch (Exception e5) {
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 熙迅屏幕本地缓存网页信息
|
|
|
+ * @param num 屏幕序列号
|
|
|
+ * @param url 缓存网页地址
|
|
|
+ * @param screenId 屏幕id
|
|
|
+ * @return 缓存后的网页地址
|
|
|
+ */
|
|
|
+ public static String xiXunUploadWebInfo(String num,String url,int screenId) {
|
|
|
+ String newUrl = "/weather/weather_"+System.currentTimeMillis()+".html";
|
|
|
+ String cmd = "{\"type\":\"downloadFileToLocal\",\"url\":\""+url+"\",\"path\":\""+newUrl+"\",\"_id\":\"save_web_"+screenId+"\"}";
|
|
|
+ sendXiXunInfo("{\"cardId\":\""+num+"\",\"cmd\":"+cmd+"}");
|
|
|
+ cmd = "{\"type\":\"getLocalFileLength\",\"path\":\""+newUrl+"\",\"_id\":\"get_save_web_"+screenId+"\"}";
|
|
|
+ String s = WebUtils.requestGet(url);
|
|
|
+ File file = new File("../upload/test.html");
|
|
|
+ try{
|
|
|
+ if (!file.exists()) file.createNewFile();
|
|
|
+ BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
|
|
|
+ new FileOutputStream("../upload/test.html"));
|
|
|
+ bufferedOutputStream.write(s.getBytes());
|
|
|
+ }catch (Exception e){
|
|
|
+ }
|
|
|
+ file = new File("../upload/test.html");
|
|
|
+ long size = file.length();
|
|
|
+ String s1 = sendXiXunInfo("{\"cardId\":\"" + num + "\",\"cmd\":" + cmd + "}");
|
|
|
+ if (s1.length() != 0){
|
|
|
+ JSONObject jsonObject = JSON.parseObject(s1);
|
|
|
+ long length = (long)jsonObject.get("length");
|
|
|
+ int times = 1;
|
|
|
+ while (length != size && times < 3){
|
|
|
+ times ++;
|
|
|
+ s1 = sendXiXunInfo("{\"cardId\":\"" + num + "\",\"cmd\":" + cmd + "}");
|
|
|
+ if (s1.length() == 0) break;
|
|
|
+ jsonObject = JSON.parseObject(s1);
|
|
|
+ length = (long)jsonObject.get("length");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (length == size) return "file:///data/data/com.xixun.xy.conn/files/local"+newUrl;
|
|
|
+ }
|
|
|
+ return url;
|
|
|
+ }
|
|
|
+
|
|
|
+// public static void screenshot(ScreenDTO dto){
|
|
|
+// if (dto.getDevType() == 0){ // 熙迅
|
|
|
+// String cmd = "{\"cardId\":\""+dto.getNum()+"\",\"cmd\":{\"type\": \"callCardService\",\"fn\": \"screenshot\",\"arg1\": 100,\"arg2\": 50,\"_id\":\"get_screen_image_"+dto.getNum()+"\"}}";
|
|
|
+// sendXiXunInfo(cmd,0);
|
|
|
+// } else if (dto.getDevType() == 1) {
|
|
|
+// sendTaiLongPro();
|
|
|
+// }
|
|
|
+// }
|
|
|
+}
|