From cf12bc45ccbb56e5026e3c2269f753b834a748ae Mon Sep 17 00:00:00 2001 From: riku <risaku@163.com> Date: 星期二, 27 八月 2019 15:30:50 +0800 Subject: [PATCH] 1.完成socket的obd数据获取及数据库存储 2.完成获取obd数据的相关接口1个 --- src/main/kotlin/com/flightfeather/obd/socket/DeviceSession.kt | 26 +++ src/main/kotlin/com/flightfeather/obd/repository/ObdDataRepository.kt | 26 +++ src/main/kotlin/com/flightfeather/obd/lightshare/bean/ObdDataVo.kt | 47 ++++++ pom.xml | 15 ++ src/main/kotlin/com/flightfeather/obd/ObdApplication.kt | 14 + src/main/kotlin/com/flightfeather/obd/repository/impl/ObdDataDaoImpl.kt | 46 ++++++ /dev/null | 2 src/main/kotlin/com/flightfeather/obd/ServletInitializer.kt | 2 src/main/kotlin/com/flightfeather/obd/lightshare/bean/BaseJson.kt | 10 + src/main/kotlin/com/flightfeather/obd/lightshare/service/impl/ObdDataServiceImpl.kt | 19 ++ src/test/kotlin/com/flightfeather/obd/ObdApplicationTests.kt | 22 +++ src/main/kotlin/com/flightfeather/obd/lightshare/web/ObdDataController.kt | 20 ++ src/main/kotlin/com/flightfeather/obd/socket/MessageManager.kt | 49 +++++++ src/main/kotlin/com/flightfeather/obd/socket/ServerHandler.kt | 20 ++ src/main/resources/application.yml | 23 +++ src/main/kotlin/com/flightfeather/obd/lightshare/service/ObdDataService.kt | 13 + src/main/kotlin/com/flightfeather/obd/socket/SocketServerClient.kt | 19 +- 17 files changed, 355 insertions(+), 18 deletions(-) diff --git a/pom.xml b/pom.xml index 7c168a5..905f446 100644 --- a/pom.xml +++ b/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> diff --git a/src/main/kotlin/com/flightfeather/obd/ObdApplication.kt b/src/main/kotlin/com/flightfeather/obd/ObdApplication.kt index 50ebef2..362df64 100644 --- a/src/main/kotlin/com/flightfeather/obd/ObdApplication.kt +++ b/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) } diff --git a/src/main/kotlin/com/flightfeather/obd/ServletInitializer.kt b/src/main/kotlin/com/flightfeather/obd/ServletInitializer.kt index 36cc6d5..cfb618c 100644 --- a/src/main/kotlin/com/flightfeather/obd/ServletInitializer.kt +++ b/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 diff --git a/src/main/kotlin/com/flightfeather/obd/lightshare/bean/BaseJson.kt b/src/main/kotlin/com/flightfeather/obd/lightshare/bean/BaseJson.kt new file mode 100644 index 0000000..fe88122 --- /dev/null +++ b/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 +} \ No newline at end of file diff --git a/src/main/kotlin/com/flightfeather/obd/lightshare/bean/ObdDataVo.kt b/src/main/kotlin/com/flightfeather/obd/lightshare/bean/ObdDataVo.kt new file mode 100644 index 0000000..bf68060 --- /dev/null +++ b/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 +} \ No newline at end of file diff --git a/src/main/kotlin/com/flightfeather/obd/lightshare/packgeinfo.kt b/src/main/kotlin/com/flightfeather/obd/lightshare/packgeinfo.kt deleted file mode 100644 index 05b7a70..0000000 --- a/src/main/kotlin/com/flightfeather/obd/lightshare/packgeinfo.kt +++ /dev/null @@ -1,2 +0,0 @@ -package com.flightfeather.obd.lightshare - diff --git a/src/main/kotlin/com/flightfeather/obd/lightshare/service/ObdDataService.kt b/src/main/kotlin/com/flightfeather/obd/lightshare/service/ObdDataService.kt new file mode 100644 index 0000000..41865b5 --- /dev/null +++ b/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> + +} \ No newline at end of file diff --git a/src/main/kotlin/com/flightfeather/obd/lightshare/service/impl/ObdDataServiceImpl.kt b/src/main/kotlin/com/flightfeather/obd/lightshare/service/impl/ObdDataServiceImpl.kt new file mode 100644 index 0000000..592810c --- /dev/null +++ b/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) + } + +} \ No newline at end of file diff --git a/src/main/kotlin/com/flightfeather/obd/lightshare/web/ObdDataController.kt b/src/main/kotlin/com/flightfeather/obd/lightshare/web/ObdDataController.kt new file mode 100644 index 0000000..17de186 --- /dev/null +++ b/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) +} \ No newline at end of file diff --git a/src/main/kotlin/com/flightfeather/obd/repository/ObdDataRepository.kt b/src/main/kotlin/com/flightfeather/obd/repository/ObdDataRepository.kt new file mode 100644 index 0000000..f600d4f --- /dev/null +++ b/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> +} \ No newline at end of file diff --git a/src/main/kotlin/com/flightfeather/obd/repository/impl/ObdDataDaoImpl.kt b/src/main/kotlin/com/flightfeather/obd/repository/impl/ObdDataDaoImpl.kt new file mode 100644 index 0000000..5d3885c --- /dev/null +++ b/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 + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/flightfeather/obd/socket/DeviceSession.kt b/src/main/kotlin/com/flightfeather/obd/socket/DeviceSession.kt new file mode 100644 index 0000000..8cd64a8 --- /dev/null +++ b/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 + +/** + * 鐢ㄤ簬淇濆瓨杩炴帴鐨勮澶囧強瀵瑰簲鐨剆ession閫氶亾 + * 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 + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/flightfeather/obd/socket/MessageManager.kt b/src/main/kotlin/com/flightfeather/obd/socket/MessageManager.kt new file mode 100644 index 0000000..1674ae2 --- /dev/null +++ b/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() + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/flightfeather/obd/socket/ServerHandler.kt b/src/main/kotlin/com/flightfeather/obd/socket/ServerHandler.kt index e1480ee..42a6c2a 100644 --- a/src/main/kotlin/com/flightfeather/obd/socket/ServerHandler.kt +++ b/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() } } \ No newline at end of file diff --git a/src/main/kotlin/com/flightfeather/obd/socket/SocketServerClient.kt b/src/main/kotlin/com/flightfeather/obd/socket/SocketServerClient.kt index bf89577..4c1f489 100644 --- a/src/main/kotlin/com/flightfeather/obd/socket/SocketServerClient.kt +++ b/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 diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 8b13789..2b6df51 100644 --- a/src/main/resources/application.yml +++ b/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 \ No newline at end of file diff --git a/src/test/kotlin/com/flightfeather/obd/ObdApplicationTests.kt b/src/test/kotlin/com/flightfeather/obd/ObdApplicationTests.kt index e896af4..93dea64 100644 --- a/src/test/kotlin/com/flightfeather/obd/ObdApplicationTests.kt +++ b/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) + } + } -- Gitblit v1.9.3