Skip to content

Instantly share code, notes, and snippets.

@a-andreyev
Last active January 28, 2016 15:21
Show Gist options
  • Save a-andreyev/7313566387540e118b38 to your computer and use it in GitHub Desktop.
Save a-andreyev/7313566387540e118b38 to your computer and use it in GitHub Desktop.
semiot gateway test config for mercuty-201
import QtQuick 2.2
import ru.semiot.gateway 0.1
SemIoTDeviceConfig {
// NOTE: is that ok that system driver is only one per device driver?
driverName: "udp";
onDriverConnected: {
addDriverDataSource({"port":55555})
}
onNewDataPacketReceived: {
// FORMAT:
// "M201" (4B)
// imp_counter (2B)
// KWh_counter (4B)
// newline (1B)
// == 10 BYTES
// TODO: checksum?
// TODO: mac or some id
var tick = tickFromData(dataPacket.data)
var kWhtick = kWhTickFromData(dataPacket.data)
var realTick = tick+kWhtick*3200
var tick2Wh = tickCounter2Wh(realTick)
deviceName = hashName("mercury-201-"+"-"+driverName+"-"+dataPacket.senderHost+"-"+dataPacket.senderPort)
var descriptionMap = {
'\\${HOST}':dataPacket.senderHost,
'\\${PORT}':dataPacket.senderPort
};
var electricEnergyConsumptionMap = {
'\\${TIMESTAMP}':dataPacket.timeStamp,
'\\${DATETIME}':dataPacket.dateTime,
'\\${Tick}':realTick,
'\\${Tick2Wh}':tick2Wh
};
descriptionDesc = replaceAll(descriptionDescSrc, descriptionMap)
electricEnergyConsumptionDesc = replaceAll(electricEnergyConsumptionDescSrc, electricEnergyConsumptionMap)
// NOTE: is that ok that we decide here how to organize res pathes?
newDataReady("/"+deviceName+"/description",descriptionDesc)
newDataReady("/"+deviceName+"/electricEnergyConsumption",electricEnergyConsumptionDesc)
}
property string deviceName
property string descriptionDesc : ''
property string electricEnergyConsumptionDesc : ''
property string descriptionDescSrc : '
This is ESP8266-Arduino based SemIoT device prototype for the
Mercury-201 electric energy consumption counter.
TODO: super ontology description.
Device source: udp://${HOST}:${PORT}
';
property string electricEnergyConsumptionDescSrc : '
This is ESP8266-Arduino based SemIoT device prototype for the
Mercury-201 electric energy consumption counter.
TODO: super ontology description.
Tick value: ${Tick}.
Watt-hour value: ${Tick2Wh} W*h.
Timestamp: ${TIMESTAMP}.
Datetime: ${DATETIME}.
';
// utils: NOTE: should probably move to udp driver
function replaceAll(str,mapObj){
var re = new RegExp(Object.keys(mapObj).join("|"),"g");
return str.replace(re, function(matched){
return mapObj[matched.replace(/[<>*()?$]/g, "\\$&")]; // :(
});
}
function hashName(str) {
var hash = 5381, // NOTE: why 5381
i = str.length
while(i)
hash = (hash * 33) ^ str.charCodeAt(--i)
return (hash >>> 0).toString();
}
function tickFromData(dataPacketData) {
console.log("M201 data=",(dataPacketData[0]),dataPacketData[1],dataPacketData[2],dataPacketData[3],dataPacketData[4],dataPacketData[5],dataPacketData[6],dataPacketData[7],dataPacketData[8],dataPacketData[9],dataPacketData[10])
return (dataPacketData[4]<<8)+dataPacketData[5]
}
function kWhTickFromData(dataPacketData) {
return (dataPacketData[6]<<24)+(dataPacketData[7]<<16)+(dataPacketData[8]<<0)+dataPacketData[9]
}
function tickCounter2Wh(tick) {
return tick*0.3125
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment