Created
July 6, 2016 17:27
-
-
Save germanviscuso/5f68063af429550b37ceb62e810d8eb2 to your computer and use it in GitHub Desktop.
Parking sensor to Kii snippet (node.js)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const raspi = require('raspi'); // Raspberry Pi GPIO library | |
const Serial = require('raspi-serial').Serial; // Raspberry Pi UART library | |
const thingNode = require('thing-node'); // Kii node.js library | |
const dissolve = require("dissolve"); // binary stream parser | |
const appId = '<REPLACE_WITH_KII_APP_ID>'; | |
const appKey = '<REPLACE_KII_APP_KEY>'; | |
const appSite = 'US'; | |
const vendorThingId = 'ParkingSensor_1'; | |
const thingPassword = '<CHOOSE_PASSWORD_AND_REPLACE_HERE>'; | |
const thingRegistrationFields = { | |
_vendorThingID: vendorThingId, | |
_password: thingPassword, | |
_thingType: 'Parking Sensor' | |
}; | |
const bucketName = 'parking_frames'; | |
var thingId; | |
var thingAccessToken; | |
var unknownFrameCount = 0; | |
var frameCount = 0; | |
// Initialize with Kii app info (get it at developer.kii.com) | |
thingNode.initialize(appId, appKey, appSite); | |
// Register Thing in cloud | |
console.log('Registering or loading thing...'); | |
thingNode.registerOrLoadThing(thingRegistrationFields, function (error, result) { | |
if (error) { | |
console.log(error); | |
process.exit(-1); | |
} else { | |
var thing = result; | |
thingId = thing.getThingID(); | |
console.log('Thing ID: ' + thingId); | |
thingAccessToken = thing.getAccessToken(); | |
parser.on('readable', function () { | |
var e; | |
console.log('Reading frame...'); | |
while (e = parser.read()) { | |
e.fi = frameCount++; | |
console.log('Parsed frame:'); | |
console.log(e); | |
// Creating Kii empty object... | |
var thingBucket = thing.bucketWithName(bucketName); | |
var obj = thingBucket.createObject(); | |
// Preparing Kii object... | |
obj._customInfo = e; | |
// Save Kii object to cloud | |
obj.save({ | |
success: function(theObject) { | |
console.log('Frame saved successfully! Object id: ' + theObject.getID()); | |
process.exit(0); | |
}, | |
failure: function(theObject, errorString) { | |
console.log('Error saving frame: ' + errorString); | |
} | |
}); | |
} | |
console.log('Frames: ' + frameCount + ' / Unknown frames: ' + unknownFrameCount); | |
}); | |
raspi.init(function() { | |
var options = {baudRate: 115200, dataBits: 8, parity: Serial.PARITY_NONE, stopBits: 1}; | |
var serial = new Serial(options); | |
serial.open(function() { | |
serial.on('data', function(data) { | |
parser.write(data); | |
}); | |
}); | |
}); | |
} | |
}); | |
var parser = dissolve().loop(function (end) { | |
this.uint8be('cid').tap(function () { | |
switch (this.vars.cid) { | |
case 0x01: // Command ID 0x01 – Sensor State | |
this.uint8be('hub').uint8be('typ').uint8be('pc').uint8be('rid').uint8be('sid'); | |
for (var i = 1; i <= 26; i++) { | |
this.uint8be('st' + i); | |
} | |
this.uint8be('ss').uint8be('mm').uint8be('hh').uint8be('crch').uint8be('crcl'); | |
break; | |
case 0x02: // Command ID 0x02 – RSSI | |
this.uint8be('hub').uint8be('typ').uint8be('pc').uint8be('rid').uint8be('rep'); | |
for (var i = 1; i <= 26; i++) { | |
this.uint8be('rssi' + i); | |
} | |
this.uint8be('dum').uint8be('bat').uint8be('ss').uint8be('mm').uint8be('hh').uint8be('crch').uint8be('crcl'); | |
break; | |
default: | |
unknownFrameCount++; | |
console.log('Unrecognized frame header: ' + this.vars.begin + ':' + this.vars.len + ':' + this.vars.seq + ':' + this.vars.cid); | |
} | |
}).tap(function () { | |
this.push(this.vars); | |
this.vars = {}; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment