wrap.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. package main
  2. /*
  3. //linux
  4. //#cgo CFLAGS:-I./include
  5. //#cgo LDFLAGS:-L./dll -limos_mw_sdk -lmw_sdk_bp -lmw_sdk_ipc -lmw_sdk_player -lmw_sdk_rm -lcurl -lm -Wl,-rpath=./dll
  6. //arm linux
  7. #cgo CFLAGS:-I./include
  8. #cgo LDFLAGS:-L./lib -limos_mw_sdk -lmw_sdk_bp -lmw_sdk_ipc -lmw_sdk_player -lmw_sdk_rm -lm -Wl,--allow-multiple-definition -L/usr/staging_dir/target-arm_cortex-a7+neon_uClibc-0.9.33.2_eabi/usr/lib/libiconv-stub/lib -liconv
  9. #include "imos_sdk_def.h"
  10. #include "imos_sdk_pub.h"
  11. #include "imos_sdk_func.h"
  12. #include <stdlib.h>
  13. #include <string.h>
  14. void STDCALL* CgoImosMwStatusReportCallback(IN CHAR *pcUserID,IN ULONG ulReportType,IN VOID *pParam);
  15. void STDCALL* CgoImosMwTmsMultiuserPicUpload(IN IMOS_MW_MULTI_UNIVIEW_PROTOCOL_HEADER_S *pstUniviewData,IN ULONG ulStreamHandle);
  16. */
  17. import "C"
  18. import (
  19. "errors"
  20. "fmt"
  21. "math/rand"
  22. "time"
  23. "unsafe"
  24. "github.com/sirupsen/logrus"
  25. "lc/common/protocol"
  26. "lc/common/util"
  27. )
  28. //export CgoImosMwStatusReportCallback
  29. func CgoImosMwStatusReportCallback(pcUserID *C.char, ulReportType C.ulong, pParam unsafe.Pointer) unsafe.Pointer {
  30. userid := C.GoString(pcUserID)
  31. switch ulReportType {
  32. case C.IMOS_MW_STATUS_KEEPALIVE: //用户保活失败,无需调用退出接口
  33. logrus.Warnf("用户保活失败,userid:%v,data:%v", userid, *(*C.ulong)(pParam))
  34. GetITSDeviceMgr().NotifyLogout(userid)
  35. case C.IMOS_MW_STATUS_UPDATE: //升级状态
  36. //logrus.Infof("升级状态,userid:%s,data:%d", userid, *(*C.ulong)(pParam))
  37. case C.IMOS_MW_STATUS_PLAYER_MEDIA_PROCESS: //播放状态,不处理
  38. }
  39. return nil
  40. }
  41. //export CgoImosMwTmsMultiuserPicUpload
  42. func CgoImosMwTmsMultiuserPicUpload(pstUniviewData *C.IMOS_MW_MULTI_UNIVIEW_PROTOCOL_HEADER_S, ulStreamHandle C.ULONG) unsafe.Pointer {
  43. if pstUniviewData == nil {
  44. return nil
  45. }
  46. PassTime := C.GoStringN(&pstUniviewData.szPassTime[0], MinLength(C.int(C.strlen(&pstUniviewData.szPassTime[0])), C.IMOS_MW_UNIVIEW_MAX_TIME_LEN))
  47. if len(PassTime) >= 14 {
  48. PassTime = PassTime[0:14]
  49. }
  50. t, err := util.MlParseTimeX("20060102150405", PassTime)
  51. if err != nil {
  52. return nil
  53. }
  54. v := tagVehicleInfo{
  55. VehiclePlate: C.GoStringN(&pstUniviewData.szCarPlate[0], MinLength(C.int(C.strlen(&pstUniviewData.szCarPlate[0])), C.IMOS_MW_CAR_PLATE_MAX_LEN)),
  56. VehicleType: int8(pstUniviewData.lVehicleType),
  57. VehicleColor: byte(pstUniviewData.cVehicleColor),
  58. VehicleSpeed: int(pstUniviewData.lVehicleSpeed),
  59. PassTime: t,
  60. PlaceName: C.GoStringN(&pstUniviewData.szPlaceName[0], MinLength(C.int(C.strlen(&pstUniviewData.szPlaceName[0])), C.IMOS_MW_PLACE_NAME_MAX_LEN)),
  61. VehicleDirection: int8(pstUniviewData.lDirection),
  62. LaneID: int8(pstUniviewData.lLaneID),
  63. LaneType: int8(pstUniviewData.lLaneType),
  64. CamID: C.GoStringN(&pstUniviewData.szCamID[0], MinLength(C.int(C.strlen(&pstUniviewData.szCamID[0])), C.IMOS_MW_DEV_ID_MAX_LEN)),
  65. TollgateID: C.GoStringN(&pstUniviewData.szTollgateID[0], MinLength(C.int(C.strlen(&pstUniviewData.szTollgateID[0])), C.IMOS_MW_DEV_ID_MAX_LEN)),
  66. IdentifyStatus: int8(pstUniviewData.lIdentifyStatus),
  67. StreamHandle: int(ulStreamHandle),
  68. }
  69. logrus.Debugln(v)
  70. GetITSDeviceMgr().PushData(&v)
  71. return nil
  72. }
  73. var ErrNotLogin = errors.New("未登陆")
  74. func ResultToError(res int) error {
  75. return errors.New(fmt.Sprintf("错误码:%d", res))
  76. }
  77. func MinLength(A, B C.int) C.int {
  78. if A > B {
  79. return B
  80. }
  81. return A
  82. }
  83. type ITSDevice struct {
  84. CamID string //配置文件,相机编号
  85. TollgateID string //卡口编号:产生该信息的卡口代码
  86. User string //配置文件,登陆用户名
  87. Pass string //配置文件,登陆用户密码
  88. IP string //配置文件,摄像机IP
  89. Port uint16 //配置文件,摄像机端口
  90. UserID string //login后返回
  91. Handle uint64 //抓拍
  92. Static *protocol.VehicleStatic //数据统计
  93. Reconn int //
  94. }
  95. func NewITSDevice(tollgateid, ID, User, Pass, IP string, Port uint16) *ITSDevice {
  96. obj := ITSDevice{
  97. CamID: ID,
  98. TollgateID: tollgateid,
  99. User: User,
  100. Pass: Pass,
  101. IP: IP,
  102. Port: Port,
  103. UserID: "",
  104. Handle: 0,
  105. Static: protocol.NewVehicleStatic(),
  106. }
  107. return &obj
  108. }
  109. func (o *ITSDevice) Login() error {
  110. cUserID := (*C.char)(C.malloc(C.IMOS_MW_RES_CODE_LEN))
  111. user := C.CString(o.User)
  112. pass := C.CString(o.Pass)
  113. cip := C.CString(o.IP)
  114. defer C.free(unsafe.Pointer(cUserID))
  115. defer C.free(unsafe.Pointer(user))
  116. defer C.free(unsafe.Pointer(pass))
  117. defer C.free(unsafe.Pointer(cip))
  118. res := C.IMOS_MW_Login(user, pass, cip, C.ushort(o.Port), cUserID)
  119. if res == C.ERR_COMMON_SUCCEED {
  120. o.UserID = C.GoString((*C.char)(unsafe.Pointer(cUserID)))
  121. return nil
  122. }
  123. return ResultToError(int(res))
  124. }
  125. func (o *ITSDevice) Logout() error {
  126. if o.UserID == "" {
  127. return ErrNotLogin
  128. }
  129. o.StopMediaStream()
  130. usrid := C.CString(o.UserID)
  131. defer C.free(unsafe.Pointer(usrid))
  132. res := C.IMOS_MW_Logout(usrid)
  133. if res == C.ERR_COMMON_SUCCEED {
  134. //退出置空userid
  135. o.UserID = ""
  136. return nil
  137. }
  138. return ResultToError(int(res))
  139. }
  140. func (o *ITSDevice) GetDeviceStatus() (*tagMwBasicDeviceInfo, error) {
  141. if o.UserID == "" {
  142. return nil, ErrNotLogin
  143. }
  144. usrid := C.CString(o.UserID)
  145. defer C.free(unsafe.Pointer(usrid))
  146. var stBasicInfo C.IMOS_MW_BASIC_DEVICE_INFO_S
  147. res := C.IMOS_MW_GetDeviceStatus(usrid, 0, C.IMOS_MW_STATUS_BASIC_INFO, (unsafe.Pointer)(&stBasicInfo))
  148. if res == C.ERR_COMMON_SUCCEED {
  149. var bdi tagMwBasicDeviceInfo
  150. bdi.Manufacturer = C.GoStringN(&stBasicInfo.szManufacturer[0], MinLength(C.int(C.strlen(&stBasicInfo.szManufacturer[0])), C.IMOS_MW_VERSION_LEN)) //厂商
  151. bdi.DeviceModel = C.GoStringN(&stBasicInfo.szDeviceModel[0], MinLength(C.int(C.strlen(&stBasicInfo.szDeviceModel[0])), C.IMOS_MW_VERSION_LEN)) //设备类型
  152. bdi.SerialNumber = C.GoStringN(&stBasicInfo.szSerialNumber[0], MinLength(C.int(C.strlen(&stBasicInfo.szSerialNumber[0])), C.IMOS_MW_VERSION_LEN)) //设备序列号
  153. bdi.DeviceMAC = C.GoStringN(&stBasicInfo.szDeviceMAC[0], MinLength(C.int(C.strlen(&stBasicInfo.szDeviceMAC[0])), C.IMOS_MW_VERSION_LEN)) //设备MAC地址
  154. bdi.FirmwareVersion = C.GoStringN(&stBasicInfo.szFirmwareVersion[0], MinLength(C.int(C.strlen(&stBasicInfo.szFirmwareVersion[0])), C.IMOS_MW_VERSION_LEN)) //软件版本, program版本
  155. bdi.HardwareID = C.GoStringN(&stBasicInfo.szHardwareID[0], MinLength(C.int(C.strlen(&stBasicInfo.szHardwareID[0])), C.IMOS_MW_VERSION_LEN)) //硬件标识
  156. bdi.PCBVersion = C.GoStringN(&stBasicInfo.szPCBVersion[0], MinLength(C.int(C.strlen(&stBasicInfo.szPCBVersion[0])), C.IMOS_MW_VERSION_LEN)) //PCB版本
  157. bdi.UbootVersion = C.GoStringN(&stBasicInfo.szUbootVersion[0], MinLength(C.int(C.strlen(&stBasicInfo.szUbootVersion[0])), C.IMOS_MW_VERSION_LEN)) //UBOOT引导版本
  158. return &bdi, nil
  159. }
  160. return nil, ResultToError(int(res))
  161. }
  162. func (o *ITSDevice) Reboot() error {
  163. if o.UserID == "" {
  164. return ErrNotLogin
  165. }
  166. o.StopMediaStream()
  167. usrid := C.CString(o.UserID)
  168. defer C.free(unsafe.Pointer(usrid))
  169. res := C.IMOS_MW_Reboot(usrid)
  170. if res == C.ERR_COMMON_SUCCEED {
  171. return nil
  172. }
  173. return ResultToError(int(res))
  174. }
  175. func (o *ITSDevice) SetPicStreamDataCallback() error {
  176. if o.UserID == "" {
  177. return ErrNotLogin
  178. }
  179. usrid := C.CString(o.UserID)
  180. ip := C.CString("")
  181. defer C.free(unsafe.Pointer(usrid))
  182. defer C.free(unsafe.Pointer(ip))
  183. var ulCaptureStreamHandle C.ulong
  184. res := C.IMOS_MW_SetPicStreamDataCallback(usrid, 0, ip, C.IMOS_MW_TMS_MULTIUSER_PIC_UPLOAD_PF(C.CgoImosMwTmsMultiuserPicUpload), &ulCaptureStreamHandle)
  185. if res == C.ERR_COMMON_SUCCEED {
  186. o.Handle = uint64(ulCaptureStreamHandle)
  187. return nil
  188. }
  189. return ResultToError(int(res))
  190. }
  191. func (o *ITSDevice) StopMediaStream() error {
  192. if o.UserID == "" {
  193. return ErrNotLogin
  194. }
  195. usrid := C.CString(o.UserID)
  196. defer C.free(unsafe.Pointer(usrid))
  197. res := C.IMOS_MW_StopMediaStream(usrid, C.ulong(o.Handle))
  198. if res == C.ERR_COMMON_SUCCEED {
  199. o.Handle = 0
  200. return nil
  201. }
  202. return ResultToError(int(res))
  203. }
  204. func (o *ITSDevice) ToStatic(data *tagVehicleInfo) {
  205. o.Static.AddDirection(data.VehicleDirection)
  206. VehiclePlate := []rune(data.VehiclePlate)
  207. if len(VehiclePlate) > 2 {
  208. Province := string(VehiclePlate[0:1])
  209. ProvinceCity := string(VehiclePlate[0:2])
  210. o.Static.AddProvince(Province)
  211. o.Static.AddProvinceCity(ProvinceCity)
  212. } else {
  213. o.Static.AddProvince(itsConfig.DefProvince)
  214. o.Static.AddProvinceCity(itsConfig.DefCity)
  215. }
  216. pvs := &protocol.VehicleSpeed{
  217. Plate: data.VehiclePlate,
  218. Time: protocol.BJTime(data.PassTime),
  219. Type: data.VehicleType,
  220. Speed: data.VehicleSpeed,
  221. Direction: uint(data.VehicleDirection),
  222. }
  223. o.Static.AddVehicleSpeed(pvs)
  224. o.Static.AddVehicleType(data.VehicleType)
  225. }
  226. func (o *ITSDevice) SendStatic(t time.Time) {
  227. if o.Static == nil {
  228. return
  229. }
  230. logrus.Debugf("时间段:%s卡口%s检测到的车辆数%d", t.Format("2006-01-02 15:04:05"), o.CamID, len(o.Static.SliceVehicleSpeed))
  231. o.Static.StaticTime = t
  232. o.Static.TollgateID = o.TollgateID
  233. var obj protocol.Pack_VehicleStatic
  234. if str, err := obj.EnCode(o.CamID, appConfig.GID, GetNextUint64(), o.Static); err == nil {
  235. topic := GetTopic(protocol.DT_ITS, o.CamID, protocol.TP_ITS_VEHICLESTATIC)
  236. if buf, err := util.GzipEncode([]byte(str)); err == nil {
  237. GetMQTTMgr().Publish(topic, string(buf), 0, ToCloud)
  238. }
  239. }
  240. o.Static.Reset()
  241. }
  242. // GetSDKVersion 全局函数
  243. func GetSDKVersion() (string, error) {
  244. cDataver := (*C.uchar)(C.malloc(C.size_t(C.IMOS_MW_SDK_CLIENT_VERSION_LEN + 1)))
  245. defer C.free(unsafe.Pointer(cDataver))
  246. res := C.IMOS_MW_GetSDKVersion(cDataver)
  247. if res == C.ERR_COMMON_SUCCEED {
  248. return C.GoString((*C.char)(unsafe.Pointer(cDataver))), nil
  249. }
  250. return "", ResultToError(int(res))
  251. }
  252. func SetLog(dir string) error {
  253. cdir := C.CString(dir)
  254. defer C.free(unsafe.Pointer(cdir))
  255. res := C.IMOS_MW_SetLog(C.IMOS_SDK_LOG_DEBUG, cdir, 10*1024)
  256. if res == C.ERR_COMMON_SUCCEED {
  257. return nil
  258. }
  259. return ResultToError(int(res))
  260. }
  261. func init() {
  262. res := C.IMOS_MW_Initiate()
  263. if res != C.ERR_COMMON_SUCCEED {
  264. msg := fmt.Sprintf("调用IMOS_MW_Initiate发生错误:%v", res)
  265. panic(msg)
  266. }
  267. SetLog(util.GetPath(3))
  268. C.IMOS_MW_SetStatusCallback(C.IMOS_MW_STATUS_REPORT_CALLBACK_PF(C.CgoImosMwStatusReportCallback))
  269. }
  270. func uninit() {
  271. C.IMOS_MW_Cleanup()
  272. }
  273. func Test() {
  274. for {
  275. var x C.IMOS_MW_MULTI_UNIVIEW_PROTOCOL_HEADER_S
  276. plate := C.CString("湘A88888")
  277. C.memcpy(unsafe.Pointer(&x.szCarPlate[0]), unsafe.Pointer(plate), C.size_t(C.strlen(plate)))
  278. C.free(unsafe.Pointer(plate))
  279. x.lVehicleType = 1
  280. x.cVehicleColor = C.char('A')
  281. x.lVehicleSpeed = C.long(RandInt(1, 120))
  282. //经过时间
  283. t := C.CString(util.MlNow().Format("20060102150405"))
  284. C.memcpy(unsafe.Pointer(&x.szPassTime[0]), unsafe.Pointer(t), C.size_t(C.strlen(t)))
  285. C.free(unsafe.Pointer(t))
  286. //位置
  287. l := C.CString(time.Now().Format("某路口"))
  288. C.memcpy(unsafe.Pointer(&x.szPlaceName[0]), unsafe.Pointer(l), C.size_t(C.strlen(l)))
  289. C.free(unsafe.Pointer(l))
  290. x.lDirection = 1
  291. x.lLaneID = 1
  292. x.lLaneType = 0
  293. s := C.CString("SXT000000001")
  294. C.memcpy(unsafe.Pointer(&x.szCamID[0]), unsafe.Pointer(s), C.size_t(C.strlen(s)))
  295. C.free(unsafe.Pointer(s))
  296. k := C.CString("KK0000000001")
  297. C.memcpy(unsafe.Pointer(&x.szTollgateID[0]), unsafe.Pointer(k), C.size_t(C.strlen(k)))
  298. C.free(unsafe.Pointer(k))
  299. x.lIdentifyStatus = 0
  300. CgoImosMwTmsMultiuserPicUpload(&x, 11)
  301. time.Sleep(time.Duration(RandInt(400, 10000)) * time.Millisecond)
  302. pcUserID := C.CString("1122222333YYY3")
  303. dat := C.ulong(2222)
  304. CgoImosMwStatusReportCallback(pcUserID, C.ulong(1), unsafe.Pointer(&dat))
  305. C.free(unsafe.Pointer(pcUserID))
  306. }
  307. }
  308. // RandInt 随机整数
  309. func RandInt(min, max int) int {
  310. if min >= max || min == 0 || max == 0 {
  311. return max
  312. }
  313. return rand.Intn(max-min) + min
  314. }
  315. /*
  316. char --> C.char --> byte
  317. signed char --> C.schar --> int8
  318. unsigned char --> C.uchar --> uint8
  319. short int --> C.short --> int16
  320. short unsigned int --> C.ushort --> uint16
  321. int --> C.int --> int
  322. unsigned int --> C.uint --> uint32
  323. long int --> C.long --> int32 or int64
  324. long unsigned int --> C.ulong --> uint32 or uint64
  325. long long int --> C.longlong --> int64
  326. long long unsigned int --> C.ulonglong --> uint64
  327. float --> C.float --> float32
  328. double --> C.double --> float64
  329. wchar_t --> C.wchar_t -->
  330. void * -> unsafe.Pointer
  331. */