@ -2,7 +2,10 @@ package com.storeroom.modules.archives.controller;
import cn.hutool.http.HttpResource ;
import com.alibaba.fastjson.JSONObject ;
import com.google.common.base.Strings ;
import com.storeroom.annotaion.rest.AnonymousGetMapping ;
import com.storeroom.modules.archives.service.ArchivesTagService ;
import com.storeroom.modules.device.service.DeviceService ;
import com.storeroom.modules.device.service.DeviceSpecParamService ;
import com.storeroom.modules.device.service.dto.DeviceSpecParamDto ;
@ -14,21 +17,17 @@ import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse ;
import org.apache.http.client.HttpClient ;
import org.apache.http.client.methods.HttpGet ;
import org.apache.http.client.methods.HttpPost ;
import org.apache.http.entity.StringEntity ;
import org.apache.http.impl.client.CloseableHttpClient ;
import org.apache.http.impl.client.HttpClients ;
import org.apache.http.util.EntityUtils ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.data.domain.Pageable ;
import org.springframework.web.bind.annotation.GetMapping ;
import org.springframework.web.bind.annotation.RequestMapping ;
import org.springframework.web.bind.annotation.RestController ;
import javax.servlet.http.HttpServletRequest ;
import java.io.BufferedReader ;
import java.io.IOException ;
import java.io.InputStreamReader ;
import java.io.* ;
import java.net.InetAddress ;
import java.net.NetworkInterface ;
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
@ -43,14 +42,15 @@ public class RFIDController {
private final DeviceService deviceService ;
private final DeviceSpecParamService deviceSpecParamService ;
private final ArchivesTagService archivesTagService ;
@ApiOperation ( "读取epc和Tid号" )
@GetMapping ( "/ReadEpc" )
public ApiResponse < Object > ReadEpc ( String op , String sDevID ) throws IOException {
public ApiResponse < Object > ReadEpc ( String op , String sDevID ) throws IOException {
String result = "" ;
try {
HttpClient client = HttpClients . createDefault ( ) ;
String url = "http://" + ip + "/RFIDInterface.aspx?op=" + op + "&sDevID=" + sDevID ;
String url = "http://" + ip + "/RFIDInterface.aspx?op=" + op + "&sDevID=" + sDevID ;
HttpGet httpGet = new HttpGet ( url ) ;
httpGet . setHeader ( "Content-Type" , "application/x-www-form-urlencoded" ) ;
System . out . println ( "调用URL: " + httpGet . getURI ( ) ) ;
@ -65,7 +65,7 @@ public class RFIDController {
String line = null ;
StringBuffer responseSB = new StringBuffer ( ) ;
while ( ( line = reader . readLine ( ) ) ! = null ) {
if ( line . contains ( "<!" ) ) {
if ( line . contains ( "<!" ) ) {
break ;
}
responseSB . append ( line . trim ( ) ) ;
@ -74,7 +74,7 @@ public class RFIDController {
reader . close ( ) ;
httpClient . close ( ) ;
result = responseSB . toString ( ) ;
} catch ( Exception e ) {
} catch ( Exception e ) {
System . err . println ( e . toString ( ) ) ;
}
return ApiResponse . success ( result ) ;
@ -82,16 +82,16 @@ public class RFIDController {
@ApiOperation ( "写epc" )
@GetMapping ( "/WriteEPC" )
public ApiResponse < Object > WriteEPC ( String op , String sDevID , String EAS , String Type , String Code , String Tid ) throws IOException {
public ApiResponse < Object > WriteEPC ( String op , String sDevID , String EAS , String Type , String Code , String Tid ) throws IOException {
String result = "" ;
try {
HttpClient client = HttpClients . createDefault ( ) ;
String url = "http://" + ip + "/RFIDInterface.aspx?op=" + op
+ "&sDevID=" + sDevID
+ "&EAS=" + EAS
+ "&Type=" + Type
+ "&Code=" + Code
+ "&Tid=" + Tid ;
String url = "http://" + ip + "/RFIDInterface.aspx?op=" + op
+ "&sDevID=" + sDevID
+ "&EAS=" + EAS
+ "&Type=" + Type
+ "&Code=" + Code
+ "&Tid=" + Tid ;
HttpGet httpGet = new HttpGet ( url ) ;
httpGet . setHeader ( "Content-Type" , "application/x-www-form-urlencoded" ) ;
System . out . println ( "调用URL: " + httpGet . getURI ( ) ) ;
@ -106,7 +106,7 @@ public class RFIDController {
String line = null ;
StringBuffer responseSB = new StringBuffer ( ) ;
while ( ( line = reader . readLine ( ) ) ! = null ) {
if ( line . contains ( "<!" ) ) {
if ( line . contains ( "<!" ) ) {
break ;
}
responseSB . append ( line . trim ( ) ) ;
@ -115,7 +115,12 @@ public class RFIDController {
reader . close ( ) ;
httpClient . close ( ) ;
result = responseSB . toString ( ) ;
} catch ( Exception e ) {
if ( null ! = result ) {
JSONObject json = JSONObject . parseObject ( result ) ;
if ( "0" . equals ( json . get ( "code" ) + "" ) )
archivesTagService . updateEasByTid ( EAS , Tid ) ;
}
} catch ( Exception e ) {
System . err . println ( e . toString ( ) ) ;
}
return ApiResponse . success ( result ) ;
@ -123,20 +128,20 @@ public class RFIDController {
@ApiOperation ( "根据mac地址获取设备id" )
@GetMapping ( "/getDeviceIdByMac" )
public ApiResponse < Object > ReadEpc ( String mac ) {
public ApiResponse < Object > ReadEpc ( String mac ) {
DeviceSpecParamDto deviceSpecParamDto = deviceSpecParamService . findMacAddress ( mac ) ;
if ( null = = deviceSpecParamDto )
if ( null = = deviceSpecParamDto )
return ApiResponse . success ( null ) ;
return ApiResponse . success ( deviceService . findById ( deviceSpecParamDto . getDeviceInfoId ( ) ) ) ;
}
@ApiOperation ( "读写器状态检测" )
@GetMapping ( "/CheckStatus" )
public ApiResponse < Object > CheckStatus ( String op , String sDevID ) throws IOException {
public ApiResponse < Object > CheckStatus ( String op , String sDevID ) throws IOException {
String result = "" ;
try {
HttpClient client = HttpClients . createDefault ( ) ;
String url = "http://" + ip + "/RFIDInterface.aspx?op=" + op + "&sDevID=" + sDevID ;
String url = "http://" + ip + "/RFIDInterface.aspx?op=" + op + "&sDevID=" + sDevID ;
HttpGet httpGet = new HttpGet ( url ) ;
httpGet . setHeader ( "Content-Type" , "application/x-www-form-urlencoded" ) ;
System . out . println ( "调用URL: " + httpGet . getURI ( ) ) ;
@ -151,7 +156,7 @@ public class RFIDController {
String line = null ;
StringBuffer responseSB = new StringBuffer ( ) ;
while ( ( line = reader . readLine ( ) ) ! = null ) {
if ( line . contains ( "<!" ) ) {
if ( line . contains ( "<!" ) ) {
break ;
}
responseSB . append ( line . trim ( ) ) ;
@ -160,7 +165,7 @@ public class RFIDController {
reader . close ( ) ;
httpClient . close ( ) ;
result = responseSB . toString ( ) ;
} catch ( Exception e ) {
} catch ( Exception e ) {
System . err . println ( e . toString ( ) ) ;
}
return ApiResponse . success ( result ) ;
@ -168,42 +173,72 @@ public class RFIDController {
@ApiOperation ( "获取设备mac地址" )
@AnonymousGetMapping ( "/getDeviceMac" )
public ApiResponse < Object > getDeviceMac ( HttpServletRequest request ) {
String ip = request . getHeader ( "X-real-ip" ) ;
if ( ip = = null | | ip . length ( ) = = 0 | | "unknown" . equalsIgnoreCase ( ip ) ) {
ip = request . getHeader ( "x-forwarded-for" ) ;
public ApiResponse < Object > getDeviceMac ( HttpServletRequest request ) {
String Xip = request . getHeader ( "X-Real-IP" ) ;
String XFor = request . getHeader ( "X-Forwarded-For" ) ;
if ( ! Strings . isNullOrEmpty ( XFor ) & & ! "unKnown" . equalsIgnoreCase ( XFor ) ) {
/ / 多次反向代理后会有多个ip值 , 第一个ip才是真实ip
int index = XFor . indexOf ( "," ) ;
if ( ! "127.0.0.1" . equals ( XFor ) ) {
if ( index ! = - 1 ) {
return ApiResponse . success ( XFor . substring ( 0 , index ) ) ;
} else {
return ApiResponse . success ( XFor ) ;
}
}
XFor = null ;
}
if ( ip = = null | | ip . length ( ) = = 0 | | "unknown" . equalsIgnoreCase ( ip ) ) {
ip = request . getHeader ( "Proxy-Client-IP" ) ;
XFor = Xip ;
if ( ! Strings . isNullOrEmpty ( XFor ) & & ! "unKnown" . equalsIgnoreCase ( XFor ) ) {
/ / String mac = MacUtil . getMacInWindows ( XFor ) ;
return ApiResponse . success ( XFor ) ;
}
if ( ip = = null | | ip . length ( ) = = 0 | | "unknown" . equalsIgnoreCase ( ip ) ) {
ip = request . getHeader ( "WL-Proxy-Client-IP" ) ;
if ( Strings . nullToEmpty ( XFor ) . trim ( ) . isEmpty ( ) | | "unknown" . equalsIgnoreCase ( XFor ) ) {
XFor = request . getHeader ( "Proxy-Client-IP" ) ;
}
if ( ip = = null | | ip . length ( ) = = 0 | | "unknown" . equalsIgnoreCase ( ip ) ) {
ip = request . getHeader ( "HTTP_CLIENT_IP" ) ;
if ( Strings . nullToEmpty ( XFor ) . trim ( ) . isEmpty ( ) | | "unknown" . equalsIgnoreCase ( XFor ) ) {
XFor = request . getHeader ( "WL-Proxy-Client- IP" ) ;
}
if ( ip = = null | | ip . length ( ) = = 0 | | "unknown" . equalsIgnoreCase ( ip ) ) {
ip = request . getHeader ( "HTTP_X_FORWARDED_FOR " ) ;
if ( Strings . nullToEmpty ( XFor ) . trim ( ) . isEmpty ( ) | | "unknown" . equalsIgnoreCase ( XFor ) ) {
XFor = request . getHeader ( "HTTP_CLIENT_IP " ) ;
}
if ( ip = = null | | ip . length ( ) = = 0 | | "unknown" . equalsIgnoreCase ( ip ) ) {
ip = request . getRemoteAddr ( ) ;
if ( Strings . nullToEmpty ( XFor ) . trim ( ) . isEmpty ( ) | | "unknown" . equalsIgnoreCase ( XFor ) ) {
XFor = request . getHeader ( "HTTP_X_FORWARDED_FOR" ) ;
}
if ( Strings . nullToEmpty ( XFor ) . trim ( ) . isEmpty ( ) | | "unknown" . equalsIgnoreCase ( XFor ) ) {
XFor = request . getRemoteAddr ( ) ;
}
String macAddr = null ;
try {
Process process = Runtime . getRuntime ( ) . exec ( "nbtstat -a " + ip ) ;
BufferedReader br = new BufferedReader (
new InputStreamReader ( process . getInputStream ( ) ) ) ;
Pattern pattern = Pattern . compile ( "([A-F0-9]{2}-){5}[A-F0-9]{2}" ) ;
Matcher matcher ;
for ( String strLine = br . readLine ( ) ; strLine ! = null ;
strLine = br . readLine ( ) ) {
matcher = pattern . matcher ( strLine ) ;
if ( matcher . find ( ) ) {
macAddr = matcher . group ( ) ;
break ;
/ / Process process = Runtime . getRuntime ( ) . exec ( "nbtstat -a " + XFor ) ;
/ / BufferedReader br = new BufferedReader (
/ / new InputStreamReader ( process . getInputStream ( ) ) ) ;
/ / Pattern pattern = Pattern . compile ( "([A-F0-9]{2}-){5}[A-F0-9]{2}" ) ;
/ / Matcher matcher ;
/ / for ( String strLine = br . readLine ( ) ; strLine ! = null ;
/ / strLine = br . readLine ( ) ) {
/ / matcher = pattern . matcher ( strLine ) ;
/ / if ( matcher . find ( ) ) {
/ / macAddr = matcher . group ( ) ;
/ / break ;
/ / }
/ / }
String str = "" ;
Process process = Runtime . getRuntime ( ) . exec ( "nbtstat -a " + XFor ) ;
InputStreamReader ir = new InputStreamReader ( process . getInputStream ( ) , "gbk" ) ;
LineNumberReader input = new LineNumberReader ( ir ) ;
for ( int i = 0 ; i < 100 ; i + + ) {
str = input . readLine ( ) ;
if ( null ! = str ) {
if ( str . indexOf ( "MAC 地址" ) > 1 ) {
macAddr = str . substring ( str . indexOf ( "MAC 地址" ) + 9 ) ;
break ;
}
}
}
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}