pom.xml
@@ -62,6 +62,7 @@ <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> @@ -88,6 +89,20 @@ <version>4.1.39.Final</version> </dependency> <!--Gson--> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency> <!--å页--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.12</version> </dependency> </dependencies> <build> src/main/kotlin/com/flightfeather/obd/ObdApplication.kt
@@ -1,15 +1,23 @@ package com.flightfeather.obd import com.flightfeather.obd.socket.SocketServerClient import org.springframework.boot.ApplicationRunner import org.springframework.boot.autoconfigure.EnableAutoConfiguration import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration import org.springframework.boot.runApplication import org.springframework.context.annotation.Bean @SpringBootApplication class ObdApplication class ObdApplication{ @Bean fun runner() = ApplicationRunner{ SocketServerClient().startServer(9000) } } fun main(args: Array<String>) { SocketServerClient().startServer(9000) runApplication<ObdApplication>(*args) } src/main/kotlin/com/flightfeather/obd/ServletInitializer.kt
@@ -1,5 +1,7 @@ package com.flightfeather.obd import org.springframework.boot.autoconfigure.EnableAutoConfiguration import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration import org.springframework.boot.builder.SpringApplicationBuilder import org.springframework.boot.web.servlet.support.SpringBootServletInitializer src/main/kotlin/com/flightfeather/obd/lightshare/bean/BaseJson.kt
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,10 @@ package com.flightfeather.obd.lightshare.bean /** * åºç¡Jsonç»æï¼ææçæ°æ®ä»¥æ¤ä¸ºåºç±» * @author riku * Date: 2019/8/27 */ open class BaseJson{ val cmdCode: Int? = null } src/main/kotlin/com/flightfeather/obd/lightshare/bean/ObdDataVo.kt
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,47 @@ package com.flightfeather.obd.lightshare.bean import com.fasterxml.jackson.annotation.JsonIgnoreProperties import com.google.gson.annotations.SerializedName import java.util.* @JsonIgnoreProperties(ignoreUnknown = true) class ObdDataVo : BaseJson() { var id: Int? = null @SerializedName("vin") var obdVin: String? = null var obdTime: Date? = null var obdLng: Double? = null var obdLat: Double? = null @SerializedName("protocol") var obdProtocol: Int? = null var obdMil: Int? = null var obdIdCode: String? = null var obdVerificationCode: String? = null var obdFaultCodeNum: Int? = null var obdFaultCode: String? = null var obdSpeed: Int? = null var obdAirPressure: Double? = null var obdEngineTorque: Double? = null var obdFrictionTorque: Double? = null var obdEngineRpm: Int? = null var obdStartFuelFlow: Double? = null var obdScrUpstreamNo: Double? = null var obdScrDownstreamNo: Double? = null var obdRemainReactant: Double? = null var obdAirInput: Double? = null var obdScrInputTemp: Double? = null var obdScrOutputTemp: Double? = null var obdDpf: Double? = null var obdEngineCoolantTemp: Double? = null var obdFuelLevel: Double? = null var obdLocationStatus: Int? = null var obdTotalMileage: Double? = null var obdEngineTorqueMode: String? = null var obdAcceleratorPedal: Double? = null var obdTotalOilConsumption: Double? = null var obdUreaBoxTemp: Double? = null var obdUreaVolume: Int? = null var obdTotalUreaConsume: Double? = null var obdDpfTemp: Double? = null var obdFirmwareVersion: String? = null } src/main/kotlin/com/flightfeather/obd/lightshare/packgeinfo.kt
ÎļþÒÑɾ³ý src/main/kotlin/com/flightfeather/obd/lightshare/service/ObdDataService.kt
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,13 @@ package com.flightfeather.obd.lightshare.service import com.flightfeather.obd.lightshare.bean.ObdDataVo /** * @author riku * Date: 2019/8/27 */ interface ObdDataService { fun getDataByVinCode(vinCode: String, pageNum: Int?, pageSize: Int?): MutableList<ObdDataVo> } src/main/kotlin/com/flightfeather/obd/lightshare/service/impl/ObdDataServiceImpl.kt
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,19 @@ package com.flightfeather.obd.lightshare.service.impl import com.flightfeather.obd.lightshare.bean.ObdDataVo import com.flightfeather.obd.lightshare.service.ObdDataService import com.flightfeather.obd.repository.ObdDataRepository import org.springframework.stereotype.Service /** * @author riku * Date: 2019/8/27 */ @Service class ObdDataServiceImpl(val obdDataRepository: ObdDataRepository) : ObdDataService { override fun getDataByVinCode(vinCode: String, pageNum: Int?, pageSize: Int?): MutableList<ObdDataVo> { return obdDataRepository.getDataByVinCode(vinCode, pageNum, pageSize) } } src/main/kotlin/com/flightfeather/obd/lightshare/web/ObdDataController.kt
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,20 @@ package com.flightfeather.obd.lightshare.web import com.flightfeather.obd.lightshare.service.ObdDataService import org.springframework.web.bind.annotation.* /** * @author riku * Date: 2019/8/27 */ @RestController @RequestMapping("obd/data") class ObdDataController(val obdDataService: ObdDataService) { @GetMapping("/{id}") fun getById( @PathVariable("id") id: String, @RequestParam("page", required = false) pageNum: Int?, @RequestParam("per_page", required = false) pageSize: Int? ) = obdDataService.getDataByVinCode(id, pageNum, pageSize) } src/main/kotlin/com/flightfeather/obd/repository/ObdDataRepository.kt
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,26 @@ package com.flightfeather.obd.repository import com.flightfeather.obd.domain.entity.ObdData import com.flightfeather.obd.domain.mapper.ObdDataMapper import com.flightfeather.obd.lightshare.bean.ObdDataVo import org.springframework.beans.BeanUtils import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Component /** * obdæ°æ®ç¸å ³æ°æ®åºæä½æ¥å£ * @author riku * Date: 2019/8/27 */ interface ObdDataRepository { /** * åå¨obdæ°æ® */ fun saveObdData(data:ObdDataVo) /** * éè¿æ±½è½¦vinç è·åææ°æ°æ® */ fun getDataByVinCode(vinCode: String, pageNum: Int?, pageSize: Int?): MutableList<ObdDataVo> } src/main/kotlin/com/flightfeather/obd/repository/impl/ObdDataDaoImpl.kt
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,46 @@ package com.flightfeather.obd.repository.impl import com.flightfeather.obd.domain.entity.ObdData import com.flightfeather.obd.domain.mapper.ObdDataMapper import com.flightfeather.obd.lightshare.bean.ObdDataVo import com.flightfeather.obd.repository.ObdDataRepository import com.github.pagehelper.PageHelper import org.springframework.beans.BeanUtils import org.springframework.stereotype.Repository import tk.mybatis.mapper.entity.Example /** * obdæ°æ®ç¸å ³æ°æ®åºæä½å®ç° * @author riku * Date: 2019/8/27 */ @Repository class ObdDataDaoImpl(val obdDataMapper: ObdDataMapper) : ObdDataRepository { override fun saveObdData(data: ObdDataVo) { val obdData = ObdData() BeanUtils.copyProperties(data, obdData) obdDataMapper.insert(obdData) } override fun getDataByVinCode(vinCode: String, pageNum: Int?, pageSize: Int?): MutableList<ObdDataVo> { val example = Example(ObdData::class.java).apply { createCriteria().andEqualTo("obdVin", vinCode) orderBy("obdTime").desc() } //å页 val offset = (pageSize?.times(pageNum?.minus(1) ?: 0)) ?: 0 val a = PageHelper.offsetPage<ObdData>(offset, pageSize ?: 10) val result = obdDataMapper.selectByExample(example) val resultList = mutableListOf<ObdDataVo>() result.forEach { val vo = ObdDataVo() BeanUtils.copyProperties(it, vo) resultList.add(vo) } return resultList } } src/main/kotlin/com/flightfeather/obd/socket/DeviceSession.kt
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,26 @@ package com.flightfeather.obd.socket import io.netty.channel.ChannelHandlerContext import java.util.concurrent.ConcurrentHashMap /** * ç¨äºä¿åè¿æ¥ç设å¤å对åºçsessionéé * Date: 2019.8.27 * @author riku */ class DeviceSession { companion object{ private val deviceMap = ConcurrentHashMap<String, ChannelHandlerContext?>() fun saveDevice(deviceCode: String?, channel: ChannelHandlerContext?) { deviceCode?.let { deviceMap.put(deviceCode, channel) } } fun getDevice(deviceCode: String?): ChannelHandlerContext? { return if (deviceMap.contains(deviceCode)) deviceMap[deviceCode] else null } } } src/main/kotlin/com/flightfeather/obd/socket/MessageManager.kt
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,49 @@ package com.flightfeather.obd.socket import com.flightfeather.obd.lightshare.bean.BaseJson import com.flightfeather.obd.lightshare.bean.ObdDataVo import com.flightfeather.obd.repository.ObdDataRepository import com.google.gson.Gson import io.netty.channel.ChannelHandlerContext import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Component import javax.annotation.PostConstruct import javax.annotation.Resource /** * å¤çsocketæ¥æ¶çæ¶æ¯ * Date: 2019.8.27 * @author riku */ @Component class MessageManager{ companion object{ private lateinit var instance: MessageManager } @Autowired lateinit var obdDataRepository: ObdDataRepository @PostConstruct fun init() { instance = this instance.obdDataRepository = this.obdDataRepository } fun dealMsg(msg: String, ctx: ChannelHandlerContext?) { try { val baseJson = Gson().fromJson<BaseJson>(msg, BaseJson::class.java) when (baseJson.cmdCode) { 2001 -> { val data = Gson().fromJson(msg, ObdDataVo::class.java) DeviceSession.saveDevice(data.obdVin, ctx) instance.obdDataRepository.saveObdData(data) } } } catch (e: Throwable) { e.printStackTrace() } } } src/main/kotlin/com/flightfeather/obd/socket/ServerHandler.kt
@@ -1,8 +1,12 @@ package com.flightfeather.obd.socket import com.flightfeather.obd.domain.entity.ObdData import com.google.gson.Gson import io.netty.channel.ChannelHandler import io.netty.channel.ChannelHandlerContext import io.netty.channel.ChannelInboundHandlerAdapter import io.netty.util.AttributeKey class ServerHandler : ChannelInboundHandlerAdapter() { @@ -18,11 +22,16 @@ override fun channelRead(ctx: ChannelHandlerContext?, msg: Any?) { super.channelRead(ctx, msg) val attribute = ctx?.channel()?.attr(attributeKey)?.apply { if (get() == null) { // set() } if (msg is String) { MessageManager().dealMsg(msg, ctx) } // val attribute = ctx?.channel()?.attr(attributeKey)?.apply { // if (get() == null) { // set(data.obdVin) // } // } } override fun channelReadComplete(ctx: ChannelHandlerContext?) { @@ -34,6 +43,7 @@ } override fun exceptionCaught(ctx: ChannelHandlerContext?, cause: Throwable?) { super.exceptionCaught(ctx, cause) cause?.printStackTrace() ctx?.close() } } src/main/kotlin/com/flightfeather/obd/socket/SocketServerClient.kt
@@ -7,6 +7,7 @@ import io.netty.channel.socket.SocketChannel import io.netty.channel.socket.nio.NioServerSocketChannel import io.netty.channel.socket.nio.NioSocketChannel import io.netty.handler.codec.string.StringDecoder /** * socketé¿è¿æ¥æå¡ç«¯ @@ -15,16 +16,19 @@ */ class SocketServerClient { // val sessionMap = HashMap<String, IoSession> private val bossGroup = NioEventLoopGroup() private val workerGroup = NioEventLoopGroup() fun startServer(port: Int) { initialize()?.bind(port)?.sync() ?.channel()?.closeFuture()?.sync() } fun stopServer() { bossGroup.shutdownGracefully() workerGroup.shutdownGracefully() } private fun initialize(): ServerBootstrap? { val bossGroup = NioEventLoopGroup() val workerGroup = NioEventLoopGroup() try { return ServerBootstrap() @@ -32,16 +36,15 @@ .channel(NioServerSocketChannel::class.java) .childHandler(object : ChannelInitializer<NioSocketChannel>() { override fun initChannel(p0: NioSocketChannel?) { p0?.pipeline()?.addLast(ServerHandler()) p0?.pipeline() ?.addLast(StringDecoder()) ?.addLast(ServerHandler()) } }) .option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true) } catch (e: Throwable) { e.printStackTrace() } finally { bossGroup.shutdownGracefully() workerGroup.shutdownGracefully() } return null src/main/resources/application.yml
@@ -1 +1,24 @@ spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://47.100.191.150:3306/obd?serverTimezone=Asia/Shanghai&prepStmtCacheSize=517&cachePrepStmts=true&autoReconnect=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false username: obd password: obd2019 mybatis: type-aliases-package: com.flightfeather.obd.domain.entity mapper-locations: classpath*:mapper/*.xml ## éç¨ Mapper é ç½® #mappers å¤ä¸ªæ¥å£æ¶éå·éå¼ mapper: mappers: com.flightfeather.obd.domain.MyMapper not-empty: false identity: MYSQL pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql src/test/kotlin/com/flightfeather/obd/ObdApplicationTests.kt
@@ -1,7 +1,12 @@ package com.flightfeather.obd import com.flightfeather.obd.domain.mapper.ObdDataMapper import com.flightfeather.obd.lightshare.bean.BaseJson import com.flightfeather.obd.lightshare.bean.ObdDataVo import com.google.gson.Gson import org.junit.Test import org.junit.runner.RunWith import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.test.context.junit4.SpringRunner @@ -9,8 +14,25 @@ @SpringBootTest class ObdApplicationTests { @Autowired lateinit var obdDataMapper: ObdDataMapper @Test fun contextLoads() { } @Test fun foo1(): Unit { // val map = GsonJsonParser().parseMap("{\"vin\":\"vin001\",\"protocol\":2}") val map = Gson().fromJson("{\"vin\":\"vin001\",\"protocol\":2,\"cmdCode\":2001}", ObdDataVo::class.java) val res = obdDataMapper.selectAll() res.forEach { println(it.obdVin) println(it.obdTime) } println(map.obdVin) println(map.obdProtocol) println(map.cmdCode) } }