@ -6,13 +6,20 @@ import cn.iocoder.yudao.module.camera.dal.dataobject.street.StreetDO;
import cn.iocoder.yudao.module.camera.dal.entity.ScanData ;
import cn.iocoder.yudao.module.camera.service.resources.URLResourcesService ;
import cn.iocoder.yudao.module.camera.service.scan.ScanService ;
import cn.iocoder.yudao.module.logistics.dal.dataobject.checkconf.CheckConfDO ;
import cn.iocoder.yudao.module.system.service.dict.DictDataService ;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper ;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper ;
import com.google.gson.* ;
import jakarta.annotation.Resource ;
import lombok.extern.slf4j.Slf4j ;
import org.springframework.http.HttpEntity ;
import org.springframework.http.HttpHeaders ;
import org.springframework.http.HttpMethod ;
import okhttp3.* ;
import org.springframework.http.ResponseEntity ;
import org.springframework.stereotype.Service ;
import org.springframework.web.client.RestTemplate ;
import java.io.IOException ;
import java.time.LocalDate ;
@ -25,63 +32,66 @@ import java.util.Objects;
@Slf4j
public class YoloServiceImpl implements ScanService {
private static final MediaType JSON = MediaType . get ( "application/json; charset=utf-8" ) ;
@Resource
private OkHttpClient client ;
@Resource
private RestTemplate restTemplate ;
private final Gson gson ;
public YoloServiceImpl ( ) {
// 配置 Gson 支持 Java 8 时间类型
DateTimeFormatter dateFormatter = DateTimeFormatter . ofPattern ( "yyyy-MM-dd" ) ;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter . ofPattern ( "yyyy-MM-dd HH:mm:ss" ) ;
this . gson = new GsonBuilder ( )
. registerTypeAdapter ( LocalDate . class , ( JsonSerializer < LocalDate > ) ( date , type , jsonSerializationContext ) - >
new JsonPrimitive ( date . format ( dateFormatter ) ) )
. registerTypeAdapter ( LocalDate . class , ( JsonDeserializer < LocalDate > ) ( json , type , jsonDeserializationContext ) - >
LocalDate . parse ( json . getAsString ( ) , dateFormatter ) )
. registerTypeAdapter ( LocalDateTime . class , ( JsonSerializer < LocalDateTime > ) ( dateTime , type , jsonSerializationContext ) - >
new JsonPrimitive ( dateTime . format ( dateTimeFormatter ) ) )
. registerTypeAdapter ( LocalDateTime . class , ( JsonDeserializer < LocalDateTime > ) ( json , type , jsonDeserializationContext ) - >
LocalDateTime . parse ( json . getAsString ( ) , dateTimeFormatter ) )
. registerTypeAdapter ( LocalDate . class , ( JsonSerializer < LocalDate > ) ( date , type , jsonSerializationContext ) - >
new JsonPrimitive ( date . format ( dateFormatter ) ) )
. registerTypeAdapter ( LocalDate . class , ( JsonDeserializer < LocalDate > ) ( json , type , jsonDeserializationContext ) - >
LocalDate . parse ( json . getAsString ( ) , dateFormatter ) )
. registerTypeAdapter ( LocalDateTime . class , ( JsonSerializer < LocalDateTime > ) ( dateTime , type , jsonSerializationContext ) - >
new JsonPrimitive ( dateTime . format ( dateTimeFormatter ) ) )
. registerTypeAdapter ( LocalDateTime . class , ( JsonDeserializer < LocalDateTime > ) ( json , type , jsonDeserializationContext ) - >
LocalDateTime . parse ( json . getAsString ( ) , dateTimeFormatter ) )
. create ( ) ;
}
@Resource
private DictDataService dictDataService ;
@Override
public ScanData scan ( StreetDO streetDO , StockDO stockDO ) {
public ScanData scan ( StreetDO streetDO , StockDO stockDO , CheckConfDO checkConfDO ) {
return null ;
}
/ * *
* 识 别 是 否 存 在
*
* @param streetDO
* @param stockDO
* @return
* /
public ScanData scanExist ( StreetDO streetDO , StockDO stockDO ) {
public ScanData scanExist ( StreetDO streetDO , StockDO stockDO , CheckConfDO checkConfDO ) {
ScanData scanData = new ScanData ( ) ;
String urlPath = "http://" + streetDO . getPlcIp ( ) + ":" + dictDataService . parseDictData ( "Hik3DApi" , "yoloApiPort" ) . getValue ( ) ;
String picPath = "http://" + streetDO . getPlcIp ( ) + ":" + dictDataService . parseDictData ( "Hik3DApi" , "picPath" ) . getValue ( ) ;
String urlPath = "http://" + streetDO . getPlcIp ( ) + ":" + dictDataService . parseDictData ( "Hik3DApi" , "yoloApiPort" ) . getValue ( ) ;
String picPath = "http://" + streetDO . getPlcIp ( ) + ":" + dictDataService . parseDictData ( "Hik3DApi" , "picPath" ) . getValue ( ) ;
try {
scanData . setCode ( "有货" ) ;
Pojo pojo = new Pojo ( ) ;
pojo . setStreetId ( streetDO . getPlcId ( ) ) ;
pojo . setDirection ( stockDO . getDirection ( ) ) ;
pojo = setHttp ( pojo , urlPath + "/category/exist" ) ;
pojo = setHttp ( pojo , urlPath + "/category/exist" ) ;
log . info ( "lx3d拍照结果: " + pojo . toString ( ) ) ;
log . info ( "lx3d拍照结果: " + pojo . toString ( ) ) ;
if ( pojo . getResult ( ) ! = null & & pojo . getResult ( ) ) {
} else {
scanData . setCode ( "无货" ) ;
log . info ( "taskId: " + stockDO . getTaskId ( ) + " lx3d拍照结果: " + pojo . toString ( ) ) ;
}
if ( pojo . getResult ( ) ! = null & & pojo . getResult ( ) ) {
} else {
scanData . setCode ( "无货" ) ;
log . info ( "taskId: " + stockDO . getTaskId ( ) + " lx3d拍照结果: " + pojo . toString ( ) ) ;
}
// if (hikPythonEntity.getColors() != null && hikPythonEntity.getColors().size() > 0) {
// for (String color:hikPythonEntity.getColors()){
// if (color.startsWith(".")) {
@ -93,7 +103,7 @@ public class YoloServiceImpl implements ScanService {
// }
return scanData ;
} catch ( IOException e ) {
log . error ( "识别异常" , e ) ;
log . error ( "识别异常" , e ) ;
System . out . println ( "识别异常" ) ;
e . printStackTrace ( ) ;
throw new RuntimeException ( e ) ;
@ -106,29 +116,30 @@ public class YoloServiceImpl implements ScanService {
/ * *
* 识 别 是 否 存 在
*
* @param streetDO
* @param stockDO
* @return
* /
public ScanData scanClassify ( StreetDO streetDO , StockDO stockDO ) {
public ScanData scanClassify ( StreetDO streetDO , StockDO stockDO , CheckConfDO checkConfDO ) {
ScanData scanData = new ScanData ( ) ;
String urlPath = "http://" + streetDO . getPlcIp ( ) + ":" + dictDataService . parseDictData ( "Hik3DApi" , "yoloApiPort" ) . getValue ( ) ;
String picPath = "http://" + streetDO . getPlcIp ( ) + ":" + dictDataService . parseDictData ( "Hik3DApi" , "picPath" ) . getValue ( ) ;
String urlPath = "http://" + streetDO . getPlcIp ( ) + ":" + dictDataService . parseDictData ( "Hik3DApi" , "yoloApiPort" ) . getValue ( ) ;
String picPath = "http://" + streetDO . getPlcIp ( ) + ":" + dictDataService . parseDictData ( "Hik3DApi" , "picPath" ) . getValue ( ) ;
try {
// scanData.setCode("有货");
Pojo pojo = new Pojo ( ) ;
pojo . setStreetId ( streetDO . getPlcId ( ) ) ;
pojo . setDirection ( stockDO . getDirection ( ) ) ;
pojo = setHttp ( pojo , urlPath + "/yolo/classify" ) ;
pojo = setHttp ( pojo , urlPath + "/yolo/classify" ) ;
log . info ( "lx3d拍照结果: " + pojo . toString ( ) ) ;
log . info ( "lx3d拍照结果: " + pojo . toString ( ) ) ;
scanData . setCode ( pojo . getType ( ) ) ;
urlResourcesService . save ( URLResourcesDo . builder ( ) . url ( picPath + pojo . getPath ( ) ) . uuid ( stockDO . getCheckPic ( ) ) . type ( "1" ) . little ( "有无拍照" ) . build ( ) ) ;
urlResourcesService . save ( URLResourcesDo . builder ( ) . url ( picPath + pojo . getPath ( ) ) . uuid ( stockDO . getCheckPic ( ) ) . type ( "1" ) . little ( "有无拍照" ) . build ( ) ) ;
return scanData ;
} catch ( IOException e ) {
log . error ( "识别异常" , e ) ;
log . error ( "识别异常" , e ) ;
System . out . println ( "识别异常" ) ;
e . printStackTrace ( ) ;
throw new RuntimeException ( e ) ;
@ -138,27 +149,71 @@ public class YoloServiceImpl implements ScanService {
/ * *
* 识 别 是 否 存 在
*
* @param streetDO
* @param stockDO
* @return
* /
public ScanData scanDetect ( StreetDO streetDO , StockDO stockDO ) {
public ScanData scanDetect ( StreetDO streetDO , StockDO stockDO , CheckConfDO checkConfDO ) {
ScanData scanData = new ScanData ( ) ;
String urlPath = "http://" + streetDO . getPlcIp ( ) + ":" + dictDataService . parseDictData ( "Hik3DApi" , "yoloApiPort" ) . getValue ( ) ;
String picPath = "http://" + streetDO . getPlcIp ( ) + ":" + dictDataService . parseDictData ( "Hik3DApi" , "picPath" ) . getValue ( ) ;
try {
String saveApiPath = dictDataService . parseDictData ( "base_conf" , "data_api_path" ) . getValue ( ) ;
String savePath = dictDataService . parseDictData ( "camera_conf" , "media_path" ) . getValue ( ) ;
String urlPath = "http://127.0.0.1:8097" ;
try {
// scanData.setCode("有货");
Pojo pojo = new Pojo ( ) ;
pojo . setStreetId ( streetDO . getPlcId ( ) ) ;
pojo . setDirection ( stockDO . getDirection ( ) ) ;
pojo = setHttp ( pojo , urlPath + "/yolo/detect" ) ;
Pojo pojo = new Pojo ( ) ;
URLResourcesDo urlResourcesDo = urlResourcesService . getOne (
new QueryWrapper < URLResourcesDo > ( )
. eq ( "uuid" , stockDO . getCheckPic ( ) )
. eq ( "type" , 1 )
. eq ( "little" , "球机拍照" ) . last ( "limit 1" ) ) ;
pojo . setPic ( urlResourcesDo . getUrl ( ) . replace ( saveApiPath , savePath ) ) ;
HttpHeaders headers = new HttpHeaders ( ) ;
headers . setContentType ( org . springframework . http . MediaType . APPLICATION_JSON ) ;
HttpEntity < Pojo > entity = new HttpEntity < > ( pojo , headers ) ;
ResponseEntity < Pojo > response = restTemplate . exchange (
urlPath + "/yolo/detect" , // 替换为你的 API 地址
HttpMethod . POST ,
entity ,
Pojo . class
) ;
log . info ( "盘点结果:" + pojo . toString ( ) ) ;
if ( response . getBody ( ) ! = null ) {
scanData . setCode ( response . getBody ( ) . getCode ( ) ) ;
// 后端加.jpg 先写死就行
urlResourcesService . save ( URLResourcesDo . builder ( ) . url ( pojo . getPic ( ) . replace ( savePath , saveApiPath ) + ".jpg" ) . uuid ( stockDO . getCheckPic ( ) ) . type ( "1" ) . little ( "识别拍照" ) . build ( ) ) ;
}
return scanData ;
} catch ( Exception e ) {
log . error ( "识别异常" , e ) ;
System . out . println ( "识别异常" ) ;
e . printStackTrace ( ) ;
throw new RuntimeException ( e ) ;
}
// return null;
}
log . info ( "lx3d拍照结果: " + pojo . toString ( ) ) ;
scanData . setCode ( pojo . getType ( ) ) ;
urlResourcesService . save ( URLResourcesDo . builder ( ) . url ( picPath + pojo . getPath ( ) ) . uuid ( stockDO . getCheckPic ( ) ) . type ( "1" ) . little ( "有无拍照" ) . build ( ) ) ;
return scanData ;
public ScanData scanPicDetect ( StreetDO streetDO , StockDO stockDO , CheckConfDO checkConfDO ) {
ScanData scanData = new ScanData ( ) ;
String urlPath = "http://" + streetDO . getPlcIp ( ) + ":" + dictDataService . parseDictData ( "Hik3DApi" , "yoloApiPort" ) . getValue ( ) ;
String picPath = "http://" + streetDO . getPlcIp ( ) + ":" + dictDataService . parseDictData ( "Hik3DApi" , "picPath" ) . getValue ( ) ;
try {
// scanData.setCode("有货");
Pojo pojo = new Pojo ( ) ;
pojo = setHttp ( pojo , urlPath + "/yolo/detect" ) ;
log . info ( "lx3d拍照结果: " + pojo . toString ( ) ) ;
scanData . setCode ( pojo . getType ( ) ) ;
urlResourcesService . save ( URLResourcesDo . builder ( ) . url ( picPath + pojo . getPath ( ) ) . uuid ( stockDO . getCheckPic ( ) ) . type ( "1" ) . little ( "有无拍照" ) . build ( ) ) ;
return scanData ;
} catch ( IOException e ) {
log . error ( "识别异常" , e ) ;
log . error ( "识别异常" , e ) ;
System . out . println ( "识别异常" ) ;
e . printStackTrace ( ) ;
throw new RuntimeException ( e ) ;
@ -166,28 +221,30 @@ public class YoloServiceImpl implements ScanService {
// return null;
}
/ * *
* 识 别 是 否 存 在
*
* @param streetDO
* @param stockDO
* @return
* /
public ScanData scanningLxApiYolo ( StreetDO streetDO , StockDO stockDO ) {
ScanData scanData = new ScanData ( ) ;
String urlPath = "http://127.0.0.1:8097/judge/scan" ;
String urlPath = "http://127.0.0.1:8097/judge/scan" ;
try {
// scanData.setCode("有货");
Pojo pojo = new Pojo ( ) ;
pojo . setStreetId ( streetDO . getPlcId ( ) ) ;
pojo . setDirection ( stockDO . getDirection ( ) ) ;
pojo = setHttp ( pojo , urlPath + "/yolo/detect" ) ;
pojo = setHttp ( pojo , urlPath + "/yolo/detect" ) ;
log . info ( "lx3d拍照结果: " + pojo . toString ( ) ) ;
log . info ( "lx3d拍照结果: " + pojo . toString ( ) ) ;
scanData . setCode ( pojo . getType ( ) ) ;
urlResourcesService . save ( URLResourcesDo . builder ( ) . url ( pojo . getPath ( ) ) . uuid ( stockDO . getCheckPic ( ) ) . type ( "1" ) . little ( "有无拍照" ) . build ( ) ) ;
urlResourcesService . save ( URLResourcesDo . builder ( ) . url ( pojo . getPath ( ) ) . uuid ( stockDO . getCheckPic ( ) ) . type ( "1" ) . little ( "有无拍照" ) . build ( ) ) ;
return scanData ;
} catch ( IOException e ) {
log . error ( "识别异常" , e ) ;
log . error ( "识别异常" , e ) ;
System . out . println ( "识别异常" ) ;
e . printStackTrace ( ) ;
throw new RuntimeException ( e ) ;
@ -196,18 +253,17 @@ public class YoloServiceImpl implements ScanService {
}
/ * *
* 识 别 是 否 存 在
*
* @param streetDO
* @param stockDO
* @return
* /
public ScanData scanCameraDetect ( StreetDO streetDO , StockDO stockDO ) {
ScanData scanData = new ScanData ( ) ;
String urlPath = "http://" + streetDO . getPlcIp ( ) + ":" + dictDataService . parseDictData ( "Hik3DApi" , "yoloApiPort" ) . getValue ( ) ;
String picPath = "http://" + streetDO . getPlcIp ( ) + ":" + dictDataService . parseDictData ( "Hik3DApi" , "picPath" ) . getValue ( ) ;
String urlPath = "http://" + streetDO . getPlcIp ( ) + ":" + dictDataService . parseDictData ( "Hik3DApi" , "yoloApiPort" ) . getValue ( ) ;
String picPath = "http://" + streetDO . getPlcIp ( ) + ":" + dictDataService . parseDictData ( "Hik3DApi" , "picPath" ) . getValue ( ) ;
String realPath = dictDataService . getDictDataList ( "camera_conf" ) . get ( "media_path" ) . getValue ( ) ;
@ -215,28 +271,30 @@ public class YoloServiceImpl implements ScanService {
try {
// scanData.setCode("有货");
List < URLResourcesDo > urls = urlResourcesService . list ( new LambdaQueryWrapper < URLResourcesDo > ( ) . eq ( URLResourcesDo : : getUuid , stockDO . getCheckPic ( ) ) ) ;
for ( URLResourcesDo url : urls ) {
for ( URLResourcesDo url : urls ) {
Pojo pojo = new Pojo ( ) ;
pojo . setStreetId ( streetDO . getPlcId ( ) ) ;
pojo . setPath ( realPath + url . getUrl ( ) . replace ( saveApiPath , "" ) ) ;
pojo . setPath ( realPath + url . getUrl ( ) . replace ( saveApiPath , "" ) ) ;
pojo . setDirection ( stockDO . getDirection ( ) ) ;
pojo = setHttp ( pojo , urlPath + "/yolo/detect" ) ;
log . info ( "lx3d拍照结果: " + pojo . toString ( ) ) ;
if ( pojo . getType ( ) ! = null & & ! pojo . getType ( ) . equals ( "" ) ) {
if ( pojo . getType ( ) ! = null & & ! pojo . getType ( ) . equals ( "" ) ) {
scanData . setCode ( pojo . getType ( ) ) ;
}
}
// urlResourcesService.save(URLResourcesDo.builder().url(picPath+pojo.getPath()).uuid( stockDO.getCheckPic()).type("1").little("有无拍照").build());
return scanData ;
} catch ( IOException e ) {
log . error ( "识别异常" , e ) ;
log . error ( "识别异常" , e ) ;
System . out . println ( "识别异常" ) ;
e . printStackTrace ( ) ;
throw new RuntimeException ( e ) ;
}
// return null;
}
private static final MediaType JSON = MediaType . get ( "application/json; charset=utf-8" ) ;
/ * *
* 调 用 / api / picComputeAll 接 口 , 批 量 处 理 所 有 设 备 图 片
* /
@ -244,6 +302,7 @@ public class YoloServiceImpl implements ScanService {
String json = gson . toJson ( pojo ) ;
RequestBody body = RequestBody . create ( json , JSON ) ;
Request request = new Request . Builder ( )
@ -256,10 +315,10 @@ public class YoloServiceImpl implements ScanService {
System . out . println ( "Unexpected code " + response ) ;
String responseBody = Objects . requireNonNull ( response . body ( ) ) . string ( ) ;
log . info ( "picComputeAll:{}" , responseBody ) ;
log . info ( "picComputeAll:{}" , responseBody ) ;
return gson . fromJson ( responseBody , Pojo . class ) ;
} catch ( Exception e ) {
log . error ( "Error occurred while calling " + baseUrl , e ) ;
} catch ( Exception e ) {
log . error ( "Error occurred while calling " + baseUrl , e ) ;
return null ;
}
}