var kue = require('kue');
var jobs = kue.createQueue();
var rest = require('restler');
var redis = require('redis');
var redisClient = redis.createClient();
var SmlParser = require('./libs/sml_parser');

jobs.process('sml', function(job, done) {
  var smlParser = new SmlParser(job.data.sml);
  redisClient.mget(['host', 'token'], function(err, reply) {
    rest.post(reply[0],{
      accessToken: reply[1] + "/api/v1/readings",
      data: {
        timestamp: Date(job.created_at),
        meter_serialnumber: smlParser.meterSerialnumber,
        energy_a_milliwattHour: smlParser.energyAMilliwattHour,
        energy_b_milliwattHour: smlParser.energyBMilliwattHour,
        power_milliwatt: smlParser.powerMilliwatt
      },
    }).on('success', function(data, response) {
      done();
    }).on('fail', function(data, response) {
      console.log("Fail:", data )
    }).on('error', function(err, response) {
      console.log('Error:', err);
    });
  });
});

@@ -286,16 +286,17 @@
 a.sml);%0A
+%0A
   redisC
@@ -322,16 +322,27 @@
  'token'
+, 'meterId'
 %5D, funct
@@ -363,33 +363,84 @@
 ) %7B%0A
-    rest.post(reply%5B0%5D,%7B%0A
+%0A    if(reply%5B2%5D == null)%7B%0A      rest.post(reply%5B0%5D + %22/api/v1/meters%22, %7B%0A  
     
@@ -466,31 +466,12 @@
 y%5B1%5D
- + %22/api/v1/readings%22
 ,%0A
+  
     
@@ -492,98 +492,651 @@
     
-timestamp: Date(job.created_at),%0A        meter_serialnumber: smlParser.meterSerialnumber,%0A
+  manufacturer_name: smlParser.manufacturerName,%0A          manufacturer_product_name: smlParser.productName,%0A          manufacturer_product_serialnumber: smlParser.meterSerialnumber%0A        %7D,%0A      %7D).on('success', function(data, response) %7B%0A        console.log('success:', data);%0A      %7D).on('fail', function(data, response) %7B%0A        console.log(%22Fail:%22, data )%0A      %7D).on('error', function(err, response) %7B%0A        console.log('Error:', err);%0A      %7D);%0A%0A    %7D else %7B%0A      rest.post(reply%5B0%5D + %22/api/v1/readings%22, %7B%0A        accessToken: reply%5B1%5D,%0A        data: %7B%0A          timestamp: Date(job.created_at),%0A          meter_id: reply%5B2%5D,%0A  
     
@@ -1187,32 +1187,34 @@
 AMilliwattHour,%0A
+  
         energy_b
@@ -1253,32 +1253,34 @@
 BMilliwattHour,%0A
+  
         power_mi
@@ -1315,27 +1315,31 @@
 iwatt%0A      
+  
 %7D,%0A
+  
     %7D).on('s
@@ -1370,24 +1370,65 @@
 response) %7B%0A
+        console.log('success:', data);%0A  
       done()
@@ -1429,16 +1429,18 @@
 done();%0A
+  
     %7D).o
@@ -1474,32 +1474,34 @@
 sponse) %7B%0A      
+  
 console.log(%22Fai
@@ -1508,24 +1508,26 @@
 l:%22, data )%0A
+  
     %7D).on('e
@@ -1561,24 +1561,26 @@
 se) %7B%0A      
+  
 console.log(
@@ -1603,18 +1603,27 @@
     
+  
 %7D);%0A
+    %7D%0A
   %7D);%0A
+%0A
 %7D);%0A

create meterId