-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathobdsync.js
More file actions
83 lines (64 loc) · 1.83 KB
/
obdsync.js
File metadata and controls
83 lines (64 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Node.js Script to send OBD-II Messages to Cloud API from Raspberry Pi
* API is [YOUR DOMAIN]/log/:json
*
* Example RPM Value: { mode: '41', pid: '0C', name: 'rpm', value: 706 }
* Example Error Code (response mode 43): { mode: '43', name: 'requestdtc', value: { errors: [ 'P0444', '-', '-' ] } }
*
* Example payload to API:
* { obddata: { mode: '41', pid: '04', name: 'load_pct', value: 10.9375 },
* vin: 'JF1BJ673XPH968228',
* localdatetime: Sun Nov 10 2013 17:23:10 GMT+0000 (UTC),
* _id: 160 }
**/
var http = require('http');
var Db = require('tingodb')().Db,
assert = require('assert');
var logFileName = "./obdLog.db";
var db = new Db(logFileName, {});
var logger = db.collection("logger");
var cursor = logger.find({}).limit(1);
var argHost = process.argv[2];
var options = {
host:argHost,
path:"",
method:"GET",
port:"80"
};
setInterval(syncOneRecord,2000);
function syncOneRecord(){
//check to see if we're connected and can reach the remote API
require('dns').resolve(argHost, function(err) {
if (err){
// no connection
console.log("not connected");
}
else {
logger.find({}).count(function (err, count) {
console.log("Syncing... "+count+" records remain");
if(count == 0) return;
});
logger.findOne({}, function(err, item) {
assert.equal(null, err);
if(item==null) return;
console.log(item);
options.path = "/log/"+JSON.stringify(item);
http.get(options,function(resp) {
console.log(resp.statusCode);
//if(resp.statusCode == 200) {
logger.remove({'_id':item._id});
//}
var respstr = '';
resp.on('data',function(chunk){
respstr += chunk;
});
resp.on('end',function() {
console.log(respstr);
});
}).on("error", function(e){
console.log("Got error: " + e.message);
});
});
}
});
}