Advertisement
Guest User

ruuvi-weather

a guest
Mar 27th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  "use strict";
  2. /*!
  3.  * Plugin for Xiaomi devices using the feaa characteristic
  4.  */
  5. var global_1 = require("../lib/global");
  6. var plugin_1 = require("./plugin");
  7.  
  8.  
  9. function parseUrlData(variables){
  10.     var result = {};
  11.    
  12.     global_1.Global.log("ruuvi-weather >> variables: " + variables, "debug");
  13.     if(variables.length===9){ //Zero-pad URL if needed                         // example: AIgbAMLNs
  14.         variables += "...";
  15.     }
  16.     console.log("fragment = " + variables + "; length = " + variables.length);
  17.     //var decoded   = base91.decode(variables); console.log("91 : " + decoded);
  18.     var decoded64 = Buffer(variables, 'base64'); console.log("64 : " + decoded64);
  19.     global_1.Global.log("ruuvi-weather >> decoded64: " + decoded64, "debug");
  20.  
  21.     /*
  22.        0:   uint8_t     format;          // (0x01 = realtime sensor readings in base91)
  23.        1:   uint8_t     humidity;        // one lsb is 0.5%
  24.        2-3: uint16_t    temperature;     // Signed 8.8 fixed-point notation.
  25.        4-5: uint16_t    pressure;        // (-50kPa)
  26.        6-7: uint16_t    time;            // seconds (now from reset, later ma ybe from last movement)
  27.     */
  28.  
  29.     result['humidity'] = Math.round(decoded64[1] * 0.5);
  30.     var uTemp = (((decoded64[2] & 127) << 8) | decoded64[3]);
  31.     var tempSign = (decoded64[2] >> 7) & 1;
  32.     result['temperature'] = Math.round(tempSign === 0 ? uTemp/256.0 : -1 * uTemp/256.0);
  33.     result['pressure'] =  Math.round((((decoded64[4] << 8) + decoded64[5]) + 50000) / 100);
  34.     if (variables[0] == 'B' ) {            
  35.         result['beaconID'] = variables.charCodeAt(8); // r = 114
  36.     }
  37.      
  38.      global_1.Global.log("ruuvi-weather >> result: " + JSON.stringify(result), "debug");
  39.      return result;
  40. };
  41.  
  42. var plugin = {
  43.     name: "Ruuvi-Weather",
  44.     description: "RuuviTag",
  45.     advertisedServices: ["feaa"],
  46.     isHandling: function (p) {
  47.         global_1.Global.log("ruuvi-weather >> inne drinne", "debug");
  48.         return p.advertisement.serviceData.some(function (entry) { return entry.uuid === "feaa"; });
  49.     },
  50.     createContext: function (peripheral) {
  51.         var data = plugin_1.getServiceData(peripheral, "feaa");
  52.         if (data == null)
  53.             return;
  54.        
  55.         var url = data.toString(); // e.g. http://ruu.vi/#BGgTAMTgC
  56.         global_1.Global.log("ruuvi-weather >> url: " + url, "debug");
  57.        
  58.         var urlParameter = url.split("#")[1];
  59.        
  60.         return parseUrlData(urlParameter);
  61.     },
  62.     defineObjects: function (context) {
  63.         global_1.Global.log("ruuvi-weather >> context: " + JSON.stringify(context), "debug");
  64.        
  65.         if (context == null)
  66.             return;
  67.         var deviceObject = {
  68.             common: null,
  69.             native: null,
  70.         };
  71.         // no channels
  72.         var stateObjects = [
  73.                 {
  74.                     id: "temperature",
  75.                     common: {
  76.                         role: "value",
  77.                         name: "Temperature",
  78.                         type: "number",
  79.                         unit: "°C",
  80.                         read: true,
  81.                         write: false,
  82.                     },
  83.                     native: null,
  84.                 },
  85.                 {
  86.                     id: "humidity",
  87.                     common: {
  88.                         role: "value",
  89.                         name: "Relative Humidity",
  90.                         type: "number",
  91.                         unit: "%rF",
  92.                         read: true,
  93.                         write: false,
  94.                     },
  95.                     native: null,
  96.                 },
  97.                 {
  98.                     id: "pressure",
  99.                     common: {
  100.                         role: "value",
  101.                         name: "Air Pressure",
  102.                         type: "number",
  103.                         unit: "hPa",
  104.                         read: true,
  105.                         write: false,
  106.                     },
  107.                     native: null,
  108.                 },
  109.                 {
  110.                     id: "beaconID",
  111.                     common: {
  112.                         role: "value",
  113.                         name: "Beacon ID",
  114.                         type: "number",
  115.                         unit: "",
  116.                         read: true,
  117.                         write: false,
  118.                     },
  119.                     native: null,
  120.                 }
  121.                
  122.         ];
  123.         var ret = {
  124.             device: deviceObject,
  125.             channels: null,
  126.             states: stateObjects,
  127.         };
  128.  
  129.         return ret;
  130.     },
  131.     getValues: function (context) {
  132.         global_1.Global.log("ruuvi-weather >> context: " + JSON.stringify(context), "debug");
  133.         return context;
  134.     },
  135. };
  136. module.exports = plugin;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement