Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- /*!
- * Plugin for Xiaomi devices using the feaa characteristic
- */
- var global_1 = require("../lib/global");
- var plugin_1 = require("./plugin");
- function parseUrlData(variables){
- var result = {};
- global_1.Global.log("ruuvi-weather >> variables: " + variables, "debug");
- if(variables.length===9){ //Zero-pad URL if needed // example: AIgbAMLNs
- variables += "...";
- }
- console.log("fragment = " + variables + "; length = " + variables.length);
- //var decoded = base91.decode(variables); console.log("91 : " + decoded);
- var decoded64 = Buffer(variables, 'base64'); console.log("64 : " + decoded64);
- global_1.Global.log("ruuvi-weather >> decoded64: " + decoded64, "debug");
- /*
- 0: uint8_t format; // (0x01 = realtime sensor readings in base91)
- 1: uint8_t humidity; // one lsb is 0.5%
- 2-3: uint16_t temperature; // Signed 8.8 fixed-point notation.
- 4-5: uint16_t pressure; // (-50kPa)
- 6-7: uint16_t time; // seconds (now from reset, later ma ybe from last movement)
- */
- result['humidity'] = Math.round(decoded64[1] * 0.5);
- var uTemp = (((decoded64[2] & 127) << 8) | decoded64[3]);
- var tempSign = (decoded64[2] >> 7) & 1;
- result['temperature'] = Math.round(tempSign === 0 ? uTemp/256.0 : -1 * uTemp/256.0);
- result['pressure'] = Math.round((((decoded64[4] << 8) + decoded64[5]) + 50000) / 100);
- if (variables[0] == 'B' ) {
- result['beaconID'] = variables.charCodeAt(8); // r = 114
- }
- global_1.Global.log("ruuvi-weather >> result: " + JSON.stringify(result), "debug");
- return result;
- };
- var plugin = {
- name: "Ruuvi-Weather",
- description: "RuuviTag",
- advertisedServices: ["feaa"],
- isHandling: function (p) {
- global_1.Global.log("ruuvi-weather >> inne drinne", "debug");
- return p.advertisement.serviceData.some(function (entry) { return entry.uuid === "feaa"; });
- },
- createContext: function (peripheral) {
- var data = plugin_1.getServiceData(peripheral, "feaa");
- if (data == null)
- return;
- var url = data.toString(); // e.g. http://ruu.vi/#BGgTAMTgC
- global_1.Global.log("ruuvi-weather >> url: " + url, "debug");
- var urlParameter = url.split("#")[1];
- return parseUrlData(urlParameter);
- },
- defineObjects: function (context) {
- global_1.Global.log("ruuvi-weather >> context: " + JSON.stringify(context), "debug");
- if (context == null)
- return;
- var deviceObject = {
- common: null,
- native: null,
- };
- // no channels
- var stateObjects = [
- {
- id: "temperature",
- common: {
- role: "value",
- name: "Temperature",
- type: "number",
- unit: "°C",
- read: true,
- write: false,
- },
- native: null,
- },
- {
- id: "humidity",
- common: {
- role: "value",
- name: "Relative Humidity",
- type: "number",
- unit: "%rF",
- read: true,
- write: false,
- },
- native: null,
- },
- {
- id: "pressure",
- common: {
- role: "value",
- name: "Air Pressure",
- type: "number",
- unit: "hPa",
- read: true,
- write: false,
- },
- native: null,
- },
- {
- id: "beaconID",
- common: {
- role: "value",
- name: "Beacon ID",
- type: "number",
- unit: "",
- read: true,
- write: false,
- },
- native: null,
- }
- ];
- var ret = {
- device: deviceObject,
- channels: null,
- states: stateObjects,
- };
- return ret;
- },
- getValues: function (context) {
- global_1.Global.log("ruuvi-weather >> context: " + JSON.stringify(context), "debug");
- return context;
- },
- };
- module.exports = plugin;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement