Skip to content

Commit

Permalink
OLRMS TCP
Browse files Browse the repository at this point in the history
  • Loading branch information
Marvin Villegas committed Jul 30, 2020
1 parent 4fbdfb7 commit 84a33b8
Show file tree
Hide file tree
Showing 8 changed files with 1,061 additions and 32,444 deletions.
94 changes: 94 additions & 0 deletions api/Olrms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import axios from 'axios'
import md5 from 'md5'
import moment from 'moment'

const date = new Date()
const today = moment(date).add(8, 'hours').format('YYYY-MM-DD HH:mm:ss')

export default class Olrms {
constructor() {
this.authorization = md5('olrmsgps')
this.url = 'http://gps.olrms.com/gps/getdeviceinfo'
}
parseGprs({ data }) {
if (data) {
const gps = data.split(',')
const imei = gps[1] || ''
if (imei) {
const lat = gps[4] || ''
const lng = gps[5] || ''
const network = gps[16] || ''

const networkData = network.split('|')
const mcc = networkData[0] || ''
const mnc = networkData[1] || ''

return {
imei: imei,
lat: lat,
lng: lng,
mcc: mcc,
mnc: mnc,
}
}
}
return false
}
createGpsData({ data }) {
const gprs = this.parseGprs({ data })
if (gprs) {
return {
device_info: {
batt_level: '',
dt: [
{
date_time: today,
},
],
imei: gprs.imei,
laccid: [
{
lac: 0,
cid: 0,
},
],
latlng: [
{
lat: gprs.lat,
lng: gprs.lng,
},
],
mac_address: gprs.imei,
mcc: gprs.mcc,
mnc: gprs.mnc,
},
}
}

return false
}
async sendToApi({ data }) {
const payLoad = this.createGpsData({ data })
try {
if (payLoad) {
const response = await axios({
method: 'POST',
headers: {
Auth: this.authorization,
'Content-Type': 'application/json',
},
data: payLoad,
url: this.url,
})
if (response.status !== 200) {
console.error(`Status: ${response.status}`)
return false
}
console.log(response.data)
return true
}
} catch (error) {
console.error(error)
}
}
}
23 changes: 12 additions & 11 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
'use strict'

const net = require('net')
import net from 'net'
import Olrms from './api/Olrms.js'

const hostname = '127.0.0.1'
const port = 43552
const olrms = new Olrms()
const port = 43556

const app = net.createServer(function(socket) {
let remoteAddress = socket.remoteAddress
let remotePort = socket.remotePort
const app = net.createServer((socket) => {
const remoteAddress = socket.remoteAddress
const remotePort = socket.remotePort

console.log(`CONNECTED: ${remoteAddress}:${remotePort}`)
socket.setEncoding('utf8')

socket.on('data', function(data) {
console.log(`${data}`)
socket.on('data', (data) => {
olrms.sendToApi({ data })
})

socket.write(`Server Reply`)
socket.pipe(socket)
})

app.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`)
app.listen(port, () => {
console.log(`Server running at ${port}`)
})

module.exports = app
export default app
15 changes: 0 additions & 15 deletions app.yaml

This file was deleted.

3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Set options as a parameter, environment variable, or rc file.
require = require('esm')(module /*, options*/)
module.exports = require('./app.js')
Loading

0 comments on commit 84a33b8

Please sign in to comment.