Tools.scala 3.87 KB
Newer Older
杜发飞 committed
1 2 3 4 5
package com.hikcreate.data.util

import scala.collection.mutable
import scala.collection.JavaConverters._
import com.alibaba.fastjson.{JSON, JSONArray, JSONObject}
1  
dufafei committed
6
import com.hikcreate.data.client.IgniteClient
杜发飞 committed
7 8
import com.hikcreate.data.common.Logging
import com.hikcreate.data.constant.Const
1  
dufafei committed
9 10
import com.hikcreate.ignite.domain.basic.{AlarmTypeInfo, EnterpriseInfo, VehicleInfo}
import org.apache.ignite.cache.query.SqlQuery
杜发飞 committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
import scalaj.http.Http

object Tools extends Logging{

  def addLocation(json:JSONObject): Array[JSONObject] = {
    val gnss = json.getJSONArray("gnssList")
    json.remove("gnssList")
    val map = mutable.HashMap.empty[String,Any]
    json.keySet().asScala.foreach(key=>map.put(key,json.get(key)))
    (0 until gnss.size()).map{ index=>
      val addJson = gnss.getJSONObject(index)
      addJson.putAll(map.asJava)
      addJson
    }.toArray
  }

  def getAddressAndLocationCode(lon:Double,lat:Double):(String,String) = {
    val json = new JSONObject()
    val arr = new JSONArray()
    val lonAndLat = new JSONObject()
    lonAndLat.put("longitude",lon)
    lonAndLat.put("latitude",lat)
    arr.add(lonAndLat)
    json.put("locations",arr)
杜发飞 committed
35 36 37 38 39 40
    val response = Http(Const.areaCodeAndAddressUrl)
      .postData(json.toJSONString)
      .header("content-type","application/json")
      //.charset("ISO-8859-1")
      .timeout(connTimeoutMs = 8000, readTimeoutMs = 8000)
      .asString
杜发飞 committed
41 42 43 44 45 46 47 48 49 50 51
    if(response.code == 200){
      val body = JSON.parseObject(response.body)
      val item = body.getJSONObject("result").getJSONArray("regeoItems").getJSONObject(0)
      val address = item.getString("formattedAddress")
      val locationCode =  item.getJSONObject("addressComponent").getString("adcode")
      (address,locationCode)
    }else{
      throw new RuntimeException("http请求城市区编码出错")
    }
  }

1  
dufafei committed
52
  //根据经纬度获取地区编码
杜发飞 committed
53
  def getLocationCode(lon:Double,lat:Double): (String,String,String) = {
杜发飞 committed
54 55 56 57 58 59 60
    val locationCode = getAddressAndLocationCode(lon,lat)._2
    val provinceCode = locationCode.substring(0,2)
    val cityCode = locationCode.substring(2,4)
    val areaCode = locationCode.substring(4,6)
    (provinceCode,cityCode,areaCode)
  }

杜发飞 committed
61
  def getInfoContentJsonobj(infoStr:String):JSONObject = {
杜发飞 committed
62 63 64 65
    val jsonStr=("{\""+infoStr.replace(":=","\":\"").replace(";","\",\"")+"\"}").replace(",\"\"}","}")
    val jSONObject = JSON.parseObject(jsonStr)
    jSONObject
  }
1  
dufafei committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

  //根据企业编码得到企业信息
  def getEnterpriseInfo(enterpriseCode:String): Option[EnterpriseInfo] = {
    val vehicleInfoSql = new SqlQuery[Long,EnterpriseInfo](classOf[EnterpriseInfo],"enterpriseCode = ?")
    val vehicleInfos = IgniteClient.basicEnterpriseInfo.query(vehicleInfoSql.setArgs(enterpriseCode)).getAll
    if(vehicleInfos.size() == 1 ){
      val vehicleInfo = vehicleInfos.get(0).getValue
      Some(vehicleInfo)
    }else{
      None
    }
  }

  //根据车牌号和车牌号码得到车辆信息
  def getVehicleInfo(plateNum:String,plateColor:String): Option[VehicleInfo] = {
    val vehicleInfoSql = new SqlQuery[Long,VehicleInfo](classOf[VehicleInfo],"PLATENUM = ? AND PLATECOLOR= ? ")
    val vehicleInfos = IgniteClient.basicVehicleInfo.query(vehicleInfoSql.setArgs(plateNum,plateColor)).getAll
    if(vehicleInfos.size() == 1 ){
      val vehicleInfo = vehicleInfos.get(0).getValue
      Some(vehicleInfo)
    }else{
      None
    }
  }

  //根据报警类型和事件类型 得到 平台报警等级code 和 平台报警类型名称
  def getAlarmInfo(warnType:String,eventType:String): Option[AlarmTypeInfo] = {
    val alarmInfoSql = new SqlQuery[Long,AlarmTypeInfo](classOf[AlarmTypeInfo],"warningBigType = ? AND warningSmallType = ? ")
    val alarmInfos = IgniteClient.basicAlarmTypeInfo.query(alarmInfoSql.setArgs(warnType,eventType)).getAll
    if(alarmInfos.size() == 1 ){
      val alarmInfo = alarmInfos.get(0).getValue
      Some(alarmInfo)
    }else{
      None
    }
  }
杜发飞 committed
102
}