SHARE
TWEET

Untitled

a guest May 19th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. exports.protocol = "GPS103";
  2. exports.model_name = "TK103";
  3. exports.compatible_hardware = ["TK103/supplier"];
  4.  
  5. const Util = require('./util');
  6. const util = new Util();
  7. const parser = require('./parser');
  8.  
  9. var adapter = function (device) {
  10.   if (!(this instanceof adapter)) return new adapter(device);
  11.  
  12.   this.device = device;
  13.  
  14.   /*******************************************
  15.    *   PARSE THE INCOMING STRING FROM THE DECIVE
  16.    *   You must return an object with a least: device_id, cmd and action.
  17.    *   Also you can add other parts.
  18.    *
  19.    *   It will send to other functions has msg_parts.
  20.    *
  21.    *   return device_id: The device_id (imei)
  22.    *   return cmd: command from the device. if action is other it send cmd to try in that function
  23.    *   return action: login_request, ping, alarm, other.
  24.    *
  25.    *  Example of return:
  26.    *  {
  27.    *      device_id: xxxxxxxxxxxxxxx,
  28.    *      cmd:    AB103,
  29.    *      action: login_request,
  30.    *      ...
  31.    *  }
  32.    ******************************************/
  33.   this.parse_data = function (data) {
  34.     let dataArray = parser.dataProcess(data);
  35.  
  36.     let msg_parts = {
  37.       device_id: dataArray['imei'],
  38.       cmd: dataArray['cmd'],
  39.       rawData: dataArray['rawData']
  40.     }
  41.     switch (msg_parts.cmd) {
  42.       case "login_request":
  43.         msg_parts.action = "login_request";
  44.         break;
  45.       case "ping":
  46.         msg_parts.action = "ping";
  47.         break;
  48.       case "alarm":
  49.         msg_parts.action = "alarm";
  50.         break;
  51.       default:
  52.         msg_parts.action = "other";
  53.     }
  54.     return msg_parts;
  55.   }
  56.  
  57.   /**
  58.    * Authorize the device to send data when is authorized by user.
  59.    * Is called when you call device.login_authorized(true).
  60.    */
  61.   this.authorize = function () {
  62.     this.send_comand(null, "LOAD");
  63.   }
  64.  
  65.   /** Required if your action is other
  66.    *  Run other functions when action in parse_data return other.
  67.    */
  68.   this.run_other = function (cmd, msg_parts) {
  69.     if (cmd === 'heartbeat'){
  70.       this.send_comand(null, "ON");
  71.     }
  72.   }
  73.  
  74.   /** Required if your action isn't request_login and you aren't still call this.login_authorized(true) in server callback;
  75.    *  If action isn't login_request and the device are not login.
  76.    */
  77.   this.request_login_to_device = function () {
  78.     //@TODO: Implement this.   
  79.   }
  80.  
  81.   /** Required if your action is alarm
  82.    * Is call when action is alarm.
  83.    *
  84.    * return {code: xxxx,
  85.    *         ...
  86.    *         }
  87.    *
  88.    * It is emit has alarmData.
  89.    *
  90.    * You can listening subscribing to device
  91.    * Ex: device.on('alarm', code, alarmData, msgParts)
  92.    */
  93.   this.receive_alarm = function (msg_parts) {}
  94.  
  95.   /** Required if your action is ping
  96.    * Is call when action is ping. It method get the coordinates.
  97.    * return gpsdata.
  98.    *
  99.    * Ex: device.on('ping', gpsdata, msg_parts)
  100.    */
  101.   this.get_ping_data = function (msg_parts) {
  102.     let gpsData = parser.getGPSData(msg_parts['rawData']);
  103.     return gpsData;
  104.   }
  105.  
  106.   /**
  107.    * Send commands to device.
  108.    *
  109.    * You can listening subscribing to device
  110.    * Ex: device.on('send_data', gpsdata, msg_parts)
  111.    */
  112.   this.send_comand = function (cmd, data) {
  113.     var msg = data;
  114.     this.device.send(msg);
  115.   }
  116.  
  117.   /**
  118.    * Format data to send device
  119.    */
  120.   this.format_data = function (params) {
  121.     /* FORMAT THE DATA TO BE SENT */
  122.   }
  123.  
  124.   /**
  125.    * Is needed if you call device.set_refresh_time.
  126.    * Send data to device
  127.    *
  128.    * NOTE: The method recive interval and duration but because js hasn't types and is not validate the types
  129.    * of interval or duration I will use one of this to pass the imei.
  130.    */
  131.   this.set_refresh_time = function (interval, imei) {
  132.     if(util.isEmpty || !util.isInt(interval)){
  133.       throw new Error("Interval can't be empty and has to be a integer");
  134.     }
  135.     if(util.isEmpty || !util.isInt(imei)){
  136.       throw new Error("Imei can't be empty and has to be a integer");
  137.     }
  138.     let msg = `**,imei:${imei},C,${interval}s`
  139.   }
  140.  
  141.   /**
  142.    * Optional
  143.    * Change the status of device from loged to false.
  144.    * Is called when you call device.logout();
  145.    *
  146.    * return string with data formated
  147.    */
  148.   this.logout = function () {}
  149. }
  150. module.exports.adapter = adapter;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top