weathermatic 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/weathermatic.rb +919 -0
  3. metadata +51 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2e28492ac4f3228ff514d72a5daaefa3253a4dfb
4
+ data.tar.gz: 1a88898be55a76472491d622afa8c96c05d7e7d0
5
+ SHA512:
6
+ metadata.gz: 3f0524b0bc25ae9522167fe7f341446d23fd22cdf4134279676947770295c0156df2e1b1a1828cdad4f4f7f53e0149ca102683f05fe250d45846124c3e598bd5
7
+ data.tar.gz: 13d3691e6758ff967f094d16fc220289bd5966a07efdaa7e64a11bfb4161315a0fb0ec8b7ae86945525467b8d3971bf015542875df9be5c640ba4c2eb101bb10
@@ -0,0 +1,919 @@
1
+ class WeatherMatic
2
+ attr_reader :username, :password
3
+ require 'net/http'
4
+ require 'uri'
5
+ require 'openssl'
6
+ require 'json'
7
+ require 'base64'
8
+
9
+ # Usage: wm = WeatherMatic.new(:username => 'YOUR USERNAME', :password => 'YOUR PASSWORD')
10
+ # Automatically sets the base_uri to staging or production so that we don't have to change it by hand - should only be production for public release.
11
+
12
+ def initialize(args)
13
+ @username = args[:username]
14
+ @password = args[:password]
15
+ # @base_uri = "https://my.smartlinknetwork.com"
16
+ @base_uri = 'http://localhost:3000'
17
+ end
18
+
19
+ # All requests are essentially the same - the purpose of this note is really just to show the basic authorization header for reference
20
+ # Example Request:
21
+ # {
22
+ # "accept-encoding"=>["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
23
+ # "accept"=>["application/json"],
24
+ # "user-agent"=>["Ruby"],
25
+ # "content-type"=>["application/json"],
26
+ # "authorization"=>["Basic thisisnotarealbasicauthorizationstringbuthackitifyoumust=="]
27
+ # }
28
+
29
+ # Usage: wm.sites_list
30
+
31
+ # Returns a list of all sites that you have permission to administer. Permissions are set within the SmartLink UI
32
+ #
33
+ # Example Response:
34
+ # {
35
+ # "meta":{
36
+ # "access":true,
37
+ # "hmac":true,
38
+ # "success":true,
39
+ # "request":{
40
+ # "timestamp":"1407355110",
41
+ # "action":"index",
42
+ # "controller":"api/v2/sites",
43
+ # "site":{
44
+
45
+ # },
46
+ # "filters":{
47
+
48
+ # }
49
+ # },
50
+ # "message":null
51
+ # },
52
+ # "total":1,
53
+ # "total_pages":1,
54
+ # "page_num":1,
55
+ # "per_page":50,
56
+ # "result":{
57
+ # "sites":[
58
+ # {
59
+ # "address1":"3425 Black Canyon",
60
+ # "address2":"",
61
+ # "city":"Plano",
62
+ # "state":"TX",
63
+ # "contact":"",
64
+ # "name":"Test Site",
65
+ # "latitude":"33.084593",
66
+ # "longitude":"-96.759224",
67
+ # "id":223,
68
+ # "site_href":"/sites/223",
69
+ # "reports_href":"/sites/223/report",
70
+ # "create_controller_href":"/sites/223/new_controller",
71
+ # "can_edit":true,
72
+ # "can_read":true,
73
+ # "controllers":[
74
+ # {
75
+ # "name":"NS Test 1",
76
+ # "run_status":0,
77
+ # "id":754,
78
+ # "location":"Plano, TX",
79
+ # "activation_status":1,
80
+ # "controller_href":"/controls/754",
81
+ # "faults":[
82
+ # {
83
+ # "description":"Aircard Communications Error",
84
+ # "date":"2014-01-16T09:09:50Z",
85
+ # "id":6746
86
+ # }
87
+ # ],
88
+ # "alerts":[
89
+
90
+ # ]
91
+ # }
92
+ # ]
93
+ # }
94
+ # ]
95
+ # }
96
+ # }
97
+
98
+ def sites_list
99
+ uri = parse_uri(@base_uri+"/api/v2/sites?"+add_timestamp)
100
+ puts uri
101
+ @request = make_get(uri)
102
+ make_request(uri,@request)
103
+ end
104
+
105
+ # This turns controllers 684 and 750 'on' - you can get the controller ID out of the list_sites reply. You will only be able to run this command against controllers you own.
106
+ # Possible commands: remote_on, remote_off, rain_delay (requires :rain_delay => INTEGER param), clear_all_faults, stop_all
107
+ #
108
+ # Usage: wm.global_batch_command(:instruction_type => "2", :payload => {"action"=>"remote_on"}, :controllers => ["684","750"])
109
+ #
110
+ # Example Response
111
+ # {
112
+ # "meta":{
113
+ # "access":true,
114
+ # "hmac":true,
115
+ # "success":true,
116
+ # "message":null,
117
+ # "request":{
118
+ # "instruction_type":"2",
119
+ # "payload":{
120
+ # "action":"remote_on"
121
+ # },
122
+ # "controllers":[
123
+ # "699",
124
+ # "700"
125
+ # ],
126
+ # "timestamp":"1407510064",
127
+ # "action":"create",
128
+ # "controller":"api/v2/batches",
129
+ # "batch":{
130
+ # "instruction_type":"2",
131
+ # "payload":{
132
+ # "action":"remote_on"
133
+ # },
134
+ # "controllers":[
135
+ # "699",
136
+ # "700"
137
+ # ],
138
+ # "timestamp":"1407510064"
139
+ # }
140
+ # }
141
+ # },
142
+ # "result":{
143
+ # "user":{
144
+ # "id":205,
145
+ # "name":"Tyler Merritt",
146
+ # "email":"tyler.merritt@weathermatic.com"
147
+ # },
148
+ # "affected":[
149
+ # {
150
+ # "global_batch_id":47,
151
+ # "controller_id":699,
152
+ # "instruction_id":109,
153
+ # "action":null,
154
+ # "description":null
155
+ # },
156
+ # {
157
+ # "global_batch_id":47,
158
+ # "controller_id":700,
159
+ # "instruction_id":110,
160
+ # "action":null,
161
+ # "description":null
162
+ # }
163
+ # ],
164
+ # "instructions":[
165
+ # {
166
+ # "id":109,
167
+ # "type_common_name":"Command",
168
+ # "exception":null,
169
+ # "is_overnight":false,
170
+ # "status_id":1,
171
+ # "action":"Remote On",
172
+ # "user":"Tyler Merritt",
173
+ # "status":"Queued",
174
+ # "started_at":null,
175
+ # "ended_at":null,
176
+ # "delay_run_time":null,
177
+ # "controller":{
178
+ # "modem_version":"0.18",
179
+ # "created_at":"2013-02-15T18:08:29Z",
180
+ # "id":699,
181
+ # "name":"Super Roamer",
182
+ # "controller_href":"/controls/699"
183
+ # }
184
+ # },
185
+ # {
186
+ # "id":110,
187
+ # "type_common_name":"Command",
188
+ # "exception":null,
189
+ # "is_overnight":false,
190
+ # "status_id":1,
191
+ # "action":"Remote On",
192
+ # "user":"Tyler Merritt",
193
+ # "status":"Queued",
194
+ # "started_at":null,
195
+ # "ended_at":null,
196
+ # "delay_run_time":null,
197
+ # "controller":{
198
+ # "modem_version":"0.18",
199
+ # "created_at":"2013-02-15T22:19:19Z",
200
+ # "id":700,
201
+ # "name":"TMOB",
202
+ # "controller_href":"/controls/700"
203
+ # }
204
+ # }
205
+ # ]
206
+ # }
207
+ # }
208
+
209
+ def global_batch_command(params)
210
+ uri = parse_uri(@base_uri+"/api/v2/batches")
211
+ @request = make_post(uri)
212
+ if params[:payload]['action'] == "rain_delay"
213
+ params_hash = ({"instruction_type" => params[:instruction_type], "payload" => params[:payload], "controllers" => params[:controllers], "rain_delay" => params[:rain_delay], "timestamp" => Time.now.to_i.to_s}).to_json
214
+ else
215
+ params_hash = ({"instruction_type" => params[:instruction_type], "payload" => params[:payload], "controllers" => params[:controllers], "timestamp" => Time.now.to_i.to_s}).to_json
216
+ end
217
+ @request.body = params_hash
218
+ make_request(uri,@request)
219
+ end
220
+
221
+ # Gathers zone information about a single controller. Params are ":id => 'value'" where $value is a string
222
+
223
+ # Usage : wm.controller_zones(:id => '684')
224
+
225
+ # Example response:
226
+ # {
227
+ # "meta":{
228
+ # "access":true,
229
+ # "success":true,
230
+ # "hmac": "pass",
231
+ # "request":{
232
+ # "timestamp":"1407357310",
233
+ # "format":"json",
234
+ # "action":"index",
235
+ # "controller":"api/v2/zones",
236
+ # "id":"699",
237
+ # "zone":{
238
+
239
+ # }
240
+ # },
241
+ # "message":null
242
+ # },
243
+ # "result":{
244
+ # "zone":[
245
+ # {
246
+ # "id":38265,
247
+ # "number":1,
248
+ # "description":null,
249
+ # "sprinkler_type":2,
250
+ # "plant_type":3,
251
+ # "soil_type":1,
252
+ # "soil_slope":0,
253
+ # "adjustment":0,
254
+ # "created_at":"2012-01-03T22:18:28Z",
255
+ # "updated_at":"2012-01-05T21:51:09Z",
256
+ # "controller_id":699,
257
+ # "plant_type_definition_type":null,
258
+ # "sprinkler_type_definition_type":"index",
259
+ # "ignore_rain":false,
260
+ # "ignore_freeze":false,
261
+ # "ignore_sensor":false,
262
+ # "gpm":null,
263
+ # "valve_size":"0.0",
264
+ # "mv_enabled":null,
265
+ # "realtime_flow_enabled":false,
266
+ # "high_flow_limit":65535,
267
+ # "low_flow_limit":0,
268
+ # "current_seconds_ran":0,
269
+ # "running_average_reset_date":null,
270
+ # "current_average_flow":0,
271
+ # "ppg":null
272
+ # },
273
+ # {
274
+ # "id":38266,
275
+ # "number":2,
276
+ # "description":null,
277
+ # "sprinkler_type":2,
278
+ # "plant_type":3,
279
+ # "soil_type":1,
280
+ # "soil_slope":0,
281
+ # "adjustment":10,
282
+ # "created_at":"2012-01-03T22:18:28Z",
283
+ # "updated_at":"2012-01-05T21:51:09Z",
284
+ # "controller_id":699,
285
+ # "plant_type_definition_type":null,
286
+ # "sprinkler_type_definition_type":"index",
287
+ # "ignore_rain":false,
288
+ # "ignore_freeze":false,
289
+ # "ignore_sensor":false,
290
+ # "gpm":null,
291
+ # "valve_size":"0.0",
292
+ # "mv_enabled":null,
293
+ # "realtime_flow_enabled":false,
294
+ # "high_flow_limit":65535,
295
+ # "low_flow_limit":0,
296
+ # "current_seconds_ran":0,
297
+ # "running_average_reset_date":null,
298
+ # "current_average_flow":0,
299
+ # "ppg":null
300
+ # }
301
+ # ]
302
+ # }
303
+ # }
304
+
305
+ def controller_zones(params)
306
+ uri = parse_uri(@base_uri+"/api/v2/controllers/"+params[:id].to_s+"/zones?"+add_timestamp)
307
+ @request = make_get(uri)
308
+ make_request(uri,@request)
309
+ end
310
+
311
+ # Example -
312
+ # params[:id] = The ID of the controller you want to manually run as a STRING
313
+ # params[:run_action] = "start" <-- must be explicitly passed
314
+ # params[:program] = INTEGER. Valid options are 1, 2, 3, or 4 corresponding to the programs on the controller
315
+ # params[:zone] = INTEGER. 1-64. If you send a zone that does not exist, you will get an error
316
+ # params[:run_time] = INTEGER in minutes. 0-60 will run zones for $this number of minutes. 61+ will run in 5 minute increments. Example - passing 62 will run for 70 minutes (60 minutes in single-minute intervals, 2 in two 5-minute increments or 10. 60 + 10 = 70)
317
+ # params[:valve_zone] = INTEGER. 1-64. If you send a valve zone that does not exist, you will get an error
318
+
319
+ # Usage: wm.controller_manual_run(:run_action => "start", :zone => 1, :id => "699")
320
+
321
+ # Example Response:
322
+ # {
323
+ # "meta":{
324
+ # "access":true,
325
+ # "hmac":{
326
+ # "pass":true
327
+ # },
328
+ # "success":true,
329
+ # "request":{
330
+ # "run_action":"start",
331
+ # "program":null,
332
+ # "zone":1,
333
+ # "run_time":null,
334
+ # "valve_zone":null,
335
+ # "timestamp":"1407353317",
336
+ # "format":"json",
337
+ # "action":"manual_run",
338
+ # "controller":{
339
+
340
+ # },
341
+ # "id":"700"
342
+ # },
343
+ # "message":null
344
+ # },
345
+ # "result":{
346
+ # "instruction":{
347
+ # "id":16,
348
+ # "type_common_name":"Command",
349
+ # "exception":null,
350
+ # "is_overnight":false,
351
+ # "status_id":1,
352
+ # "action":"Manual Run ( minutes)",
353
+ # "user":"Tyler Merritt",
354
+ # "status":"Queued",
355
+ # "started_at":null,
356
+ # "ended_at":null,
357
+ # "delay_run_time":null,
358
+ # "controller":{
359
+ # "modem_version":"0.18",
360
+ # "created_at":"2013-02-15T22:19:19Z",
361
+ # "id":700,
362
+ # "name":"TMOB",
363
+ # "controller_href":"/controls/700"
364
+ # }
365
+ # }
366
+ # }
367
+ # }
368
+
369
+ def controller_manual_run(params)
370
+ uri = parse_uri(@base_uri+"/api/v2/controllers/"+params[:id].to_s+"/manual_run.json")
371
+ @request = make_post(uri)
372
+ @request.body = ({
373
+ "run_action" => params[:run_action],
374
+ "program" => params[:program],
375
+ "zone" => params[:zone],
376
+ "run_time" => params[:run_time],
377
+ "valve_zone" => params[:valve_zone],
378
+ "timestamp" => Time.now.to_i.to_s
379
+ }).to_json
380
+ make_request(uri,@request)
381
+ end
382
+
383
+ # Returns information about your user account
384
+
385
+ # Usage: wm.me
386
+
387
+ # Example Response:
388
+ # {
389
+ # "meta":{
390
+ # "access":true,
391
+ # "hmac":true,
392
+ # "success":true,
393
+ # "request":{
394
+ # "timestamp":"1407352539",
395
+ # "action":"me",
396
+ # "controller":"api/v2/users",
397
+ # "user":{
398
+
399
+ # }
400
+ # },
401
+ # "message":null
402
+ # },
403
+ # "result":{
404
+ # "user":{
405
+ # "id":205,
406
+ # "email":"tyler.merritt@weathermatic.com",
407
+ # "first_name":"Tyler",
408
+ # "last_name":"Merritt",
409
+ # "address_1":"3301 W Kingsley Rd.",
410
+ # "address_2":"",
411
+ # "city":"Garland",
412
+ # "company":"",
413
+ # "phone_1":null,
414
+ # "phone_2":null,
415
+ # "phone_3":null,
416
+ # "postal":"75041",
417
+ # "province":"Texas",
418
+ # "temp_units":"f"
419
+ # }
420
+ # }
421
+ # }
422
+
423
+ def me
424
+ uri = parse_uri(@base_uri+"/api/v2/users/me?"+add_timestamp)
425
+ @request = make_get(uri)
426
+ make_request(uri,@request)
427
+ end
428
+
429
+ # Example
430
+ # params[:range_start] should be passed as 'YYYY-MM-DD' as the start date (on and after) you would like a usage report from
431
+ # params[:range_end] should be passed as 'YYYY-MM-DD' as the end date (on and before) you would like the usage report from
432
+ # params[:interval] *optional* w = Weekly Intervals, m = Monthly Intervals, y = Yearly Intervals. SUM of the usage by Interval passed
433
+ # params[:controller_id] *optional* pass a specific controller ID or an array of controller IDs with the request to get controller usage instead of entire SITE usage
434
+ # params[:zone_number] *optional* pass a specific zone number with the request to get zone usage instead of entire SITE usage (DEPENDS ON A SINGLE CONTROLLER ALSO BEING PASSED)
435
+
436
+ # Usage: wm.site_usage(:site_id => 157, :controller_id => [794,750], :range_start => "2014-06-01", :range_end => "2014-07-25", :interval => 'w' )
437
+
438
+ # Example Response: (all units are in gallons for this site)
439
+ # {
440
+ # "meta":{
441
+ # "access":true,
442
+ # "hmac":true,
443
+ # "success":true,
444
+ # "request":{
445
+ # "range_start":"2014-06-01",
446
+ # "range_end":"2014-07-25",
447
+ # "interval":"w",
448
+ # "controller_id":[
449
+ # "794",
450
+ # "750"
451
+ # ],
452
+ # "zone_number":null,
453
+ # "timestamp":"1407506612",
454
+ # "action":"usage",
455
+ # "controller":"api/v2/reports_sites",
456
+ # "id":"157",
457
+ # "format":"json",
458
+ # "reports_site":{
459
+
460
+ # }
461
+ # },
462
+ # "message":null
463
+ # },
464
+ # "result":{
465
+ # "site":{
466
+ # "id":157,
467
+ # "name":"Engineering Dept",
468
+ # "controllers":[
469
+ # {
470
+ # "id":750,
471
+ # "name":"Andey's",
472
+ # "usage":[
473
+ # {
474
+ # "gallons":1512,
475
+ # "date":"2014-06-02"
476
+ # },
477
+ # {
478
+ # "gallons":1454,
479
+ # "date":"2014-06-09"
480
+ # },
481
+ # {
482
+ # "gallons":1240,
483
+ # "date":"2014-06-16"
484
+ # },
485
+ # {
486
+ # "gallons":1620,
487
+ # "date":"2014-06-23"
488
+ # },
489
+ # {
490
+ # "gallons":1582,
491
+ # "date":"2014-06-30"
492
+ # },
493
+ # {
494
+ # "gallons":1217,
495
+ # "date":"2014-07-07"
496
+ # },
497
+ # {
498
+ # "gallons":1357,
499
+ # "date":"2014-07-14"
500
+ # },
501
+ # {
502
+ # "gallons":454,
503
+ # "date":"2014-07-21"
504
+ # }
505
+ # ]
506
+ # },
507
+ # {
508
+ # "id":794,
509
+ # "name":"Plan-Id Test - 1 year basic",
510
+ # "usage":[
511
+ # {
512
+ # "gallons":8306,
513
+ # "date":"2014-06-02"
514
+ # },
515
+ # {
516
+ # "gallons":9015,
517
+ # "date":"2014-06-09"
518
+ # },
519
+ # {
520
+ # "gallons":8068,
521
+ # "date":"2014-06-16"
522
+ # },
523
+ # {
524
+ # "gallons":8564,
525
+ # "date":"2014-06-23"
526
+ # },
527
+ # {
528
+ # "gallons":8693,
529
+ # "date":"2014-06-30"
530
+ # },
531
+ # {
532
+ # "gallons":7871,
533
+ # "date":"2014-07-07"
534
+ # },
535
+ # {
536
+ # "gallons":5892,
537
+ # "date":"2014-07-14"
538
+ # },
539
+ # {
540
+ # "gallons":949,
541
+ # "date":"2014-07-21"
542
+ # }
543
+ # ]
544
+ # }
545
+ # ]
546
+ # }
547
+ # }
548
+ # }
549
+
550
+ def site_usage(params)
551
+ data = URI.encode_www_form({
552
+ "range_start" => params[:range_start],
553
+ "range_end" => params[:range_end],
554
+ "interval" => params[:interval],
555
+ "controller_id[]" => params[:controller_id],
556
+ "zone_number" => params[:zone_number],
557
+ "timestamp" => Time.now.to_i.to_s
558
+ })
559
+ uri = parse_uri(@base_uri+"/api/v2/reports/sites/"+params[:site_id].to_s+"/usage.json?" + data)
560
+ @request = make_get(uri)
561
+ make_request(uri,@request)
562
+ end
563
+
564
+ # Example
565
+ # params[:site_id] should be the integer value of the site for which you would like weather data
566
+ # params[:range_start] should be passed as 'YYYY-MM-DD' as the start date (on and after) you would like a usage report from
567
+ # params[:range_end] should be passed as 'YYYY-MM-DD' as the end date (on and before) you would like the usage report from
568
+ # params[:interval] *optional* w = Weekly Intervals, m = Monthly Intervals, y = Yearly Intervals. AVERAGE of the usage by Interval passed
569
+ # params[:controller_id] *optional* pass a specific controller ID or an array of controller IDs with the request to get controller usage instead of entire SITE usage
570
+
571
+ # Usage wm.site_weather(:site_id => 157, :controller_id => [699,700], :range_start => "2014-01-01", :range_end => "2014-03-30", :interval => 'w')
572
+
573
+ # {
574
+ # "meta":{
575
+ # "access":true,
576
+ # "hmac":true,
577
+ # "success":true,
578
+ # "message":nil,
579
+ # "request":{
580
+ # "range_start":"2014-01-01",
581
+ # "range_end":"2014-03-30",
582
+ # "interval":"w",
583
+ # "controller_id":[
584
+ # "699",
585
+ # "700"
586
+ # ],
587
+ # "timestamp":"1407274155",
588
+ # "action":"weather",
589
+ # "controller":"api/v2/reports_sites",
590
+ # "id":"157",
591
+ # "format":"json",
592
+ # "reports_site":{
593
+
594
+ # }
595
+ # }
596
+ # },
597
+ # "result":{
598
+ # "site":{
599
+ # "id":157,
600
+ # "name":"Engineering Dept",
601
+ # "controllers":[
602
+ # {
603
+ # "id":699,
604
+ # "name":"My Controller",
605
+ # "location":"Desk",
606
+ # "weather":[
607
+ # {
608
+ # "date":"2014-01-01",
609
+ # "high":0,
610
+ # "low":-2
611
+ # },
612
+ # {
613
+ # "date":"2014-01-05",
614
+ # "high":17,
615
+ # "low":-3
616
+ # },
617
+ # {
618
+ # "date":"2014-01-12",
619
+ # "high":20,
620
+ # "low":9
621
+ # }
622
+ # ]
623
+ # },
624
+ # {
625
+ # "id":700,
626
+ # "name":"Another Controller",
627
+ # "location":"Other Desk",
628
+ # "weather":[
629
+ # {
630
+ # "date":"2014-01-01",
631
+ # "high":19,
632
+ # "low":16
633
+ # },
634
+ # {
635
+ # "date":"2014-01-05",
636
+ # "high":22,
637
+ # "low":22
638
+ # },
639
+ # {
640
+ # "date":"2014-01-12",
641
+ # "high":22,
642
+ # "low":21
643
+ # }
644
+ # ]
645
+ # }
646
+ # ]
647
+ # }
648
+ # }
649
+ # }
650
+
651
+ def site_weather(params)
652
+ data = URI.encode_www_form({
653
+ "range_start" => params[:range_start],
654
+ "range_end" => params[:range_end],
655
+ "interval" => params[:interval],
656
+ "controller_id[]" => params[:controller_id],
657
+ "timestamp" => Time.now.to_i.to_s
658
+ })
659
+ uri = parse_uri(@base_uri+"/api/v2/reports/sites/"+params[:site_id].to_s+"/weather.json?"+data)
660
+ @request = make_get(uri)
661
+ make_request(uri,@request)
662
+ end
663
+
664
+ # Example
665
+ # params[:range_start] should be passed as 'YYYY-MM-DD' as the start date (on and after) you would like a usage report from
666
+ # params[:range_end] should be passed as 'YYYY-MM-DD' as the end date (on and before) you would like the usage report from
667
+ # params[:interval] *optional* w = Weekly Intervals, m = Monthly Intervals, y = Yearly Intervals. SUM of the usage by Interval passed
668
+ # params[:controller_id] *optional* pass a specific controller ID or an array of controller IDs with the request to get controller usage instead of entire SITE usage
669
+ # params[:zone_number] *optional* pass a specific zone number or an array of zone numbers with the request to get zone usage instead of entire SITE usage (DEPENDS ON SINGLE CONTROLLER ID ALSO BEING PASSED)
670
+
671
+ # Usage wm.site_events(:site_id => 157, :controller_id => [699,700], :zone_number => [1,2], :range_start => "2013-12-01", :range_end => "2013-12-30")
672
+ # This also works: wm.site_events(:site_id => 157, :controller_id => [699], :range_start => "2013-12-01", :range_end => "2013-12-05")
673
+
674
+ # {
675
+ # "meta":{
676
+ # "access":true,
677
+ # "hmac":true,
678
+ # "success":true,
679
+ # "request":{
680
+ # "range_start":"2013-12-01",
681
+ # "range_end":"2013-12-30",
682
+ # "interval":null,
683
+ # "controller_id":[
684
+ # "699",
685
+ # "700"
686
+ # ],
687
+ # "zone_number":[
688
+ # "1",
689
+ # "2"
690
+ # ],
691
+ # "timestamp":"1407505556",
692
+ # "action":"events",
693
+ # "controller":"api/v2/reports_sites",
694
+ # "id":"157",
695
+ # "format":"json",
696
+ # "reports_site":{
697
+
698
+ # }
699
+ # },
700
+ # "message":null
701
+ # },
702
+ # "result":{
703
+ # "site":{
704
+ # "id":157,
705
+ # "name":"Engineering Dept",
706
+ # "controllers":[
707
+ # {
708
+ # "id":699,
709
+ # "name":"Super Roamer",
710
+ # "zones":[
711
+ # {
712
+ # "zone_number":1,
713
+ # "name":null,
714
+ # "events":[
715
+ # {
716
+ # "id":5800,
717
+ # "date":"2013-12-18T15:38:13Z",
718
+ # "status":1,
719
+ # "created_at":"2013-12-18T15:38:13Z",
720
+ # "updated_at":"2014-08-08T13:45:08Z",
721
+ # "description":"Changed From None to Normal",
722
+ # "event_type":699,
723
+ # "is_fault":10
724
+ # },
725
+ # {
726
+ # "id":6014,
727
+ # "date":"2013-12-25T09:13:16Z",
728
+ # "status":1,
729
+ # "created_at":"2013-12-25T09:13:16Z",
730
+ # "updated_at":"2014-08-08T13:45:10Z",
731
+ # "description":"Changed From Freeze to Normal",
732
+ # "event_type":699,
733
+ # "is_fault":10
734
+ # }
735
+ # ]
736
+ # },
737
+ # {
738
+ # "zone_number":2,
739
+ # "name":null,
740
+ # "events":[
741
+ # {
742
+ # "id":5956,
743
+ # "date":"2013-12-23T09:25:14Z",
744
+ # "status":1,
745
+ # "created_at":"2013-12-23T09:25:14Z",
746
+ # "updated_at":"2014-08-08T13:45:09Z",
747
+ # "description":"Changed From Normal to Freeze",
748
+ # "event_type":699,
749
+ # "is_fault":10
750
+ # },
751
+ # {
752
+ # "id":6161,
753
+ # "date":"2013-12-30T11:13:09Z",
754
+ # "status":1,
755
+ # "created_at":"2013-12-30T11:13:09Z",
756
+ # "updated_at":"2014-08-08T13:45:11Z",
757
+ # "description":"Changed From Normal to Freeze",
758
+ # "event_type":699,
759
+ # "is_fault":10
760
+ # }
761
+ # ]
762
+ # }
763
+ # ]
764
+ # },
765
+ # {
766
+ # "id":700,
767
+ # "name":"TMOB",
768
+ # "zones":[
769
+ # {
770
+ # "zone_number":1,
771
+ # "name":null,
772
+ # "events":[
773
+ # {
774
+ # "id":1,
775
+ # "date":"2013-12-18T15:38:13Z",
776
+ # "status":0,
777
+ # "created_at":"2013-12-18T15:38:13Z",
778
+ # "updated_at":"2014-08-08T13:44:42Z",
779
+ # "description":"Changed Could not establish connection to Zone 4",
780
+ # "event_type":700,
781
+ # "is_fault":null
782
+ # },
783
+ # {
784
+ # "id":2,
785
+ # "date":"2013-12-18T16:38:13Z",
786
+ # "status":0,
787
+ # "created_at":"2013-12-18T16:38:13Z",
788
+ # "updated_at":"2014-08-08T13:44:42Z",
789
+ # "description":"Changed Could not establish connection to Zone 4",
790
+ # "event_type":700,
791
+ # "is_fault":null
792
+ # },
793
+ # {
794
+ # "id":3,
795
+ # "date":"2013-12-18T17:38:13Z",
796
+ # "status":0,
797
+ # "created_at":"2013-12-18T17:38:13Z",
798
+ # "updated_at":"2014-08-08T13:44:42Z",
799
+ # "description":"Changed Could not establish connection to Zone 4",
800
+ # "event_type":700,
801
+ # "is_fault":null
802
+ # }
803
+ # ]
804
+ # },
805
+ # {
806
+ # "zone_number":2,
807
+ # "name":null,
808
+ # "events":[
809
+ # {
810
+ # "id":4,
811
+ # "date":"2013-12-18T18:38:13Z",
812
+ # "status":1,
813
+ # "created_at":"2013-12-18T18:38:13Z",
814
+ # "updated_at":"2014-08-08T13:44:42Z",
815
+ # "description":"Changed Could not establish connection to Zone 4",
816
+ # "event_type":700,
817
+ # "is_fault":null
818
+ # },
819
+ # {
820
+ # "id":5,
821
+ # "date":"2013-12-18T19:38:13Z",
822
+ # "status":0,
823
+ # "created_at":"2013-12-18T19:38:13Z",
824
+ # "updated_at":"2014-08-08T13:44:42Z",
825
+ # "description":"Changed Could not establish connection to Zone 4",
826
+ # "event_type":700,
827
+ # "is_fault":null
828
+ # },
829
+ # {
830
+ # "id":6,
831
+ # "date":"2013-12-18T20:38:13Z",
832
+ # "status":1,
833
+ # "created_at":"2013-12-18T20:38:13Z",
834
+ # "updated_at":"2014-08-08T13:44:42Z",
835
+ # "description":"Changed Could not establish connection to Zone 4",
836
+ # "event_type":700,
837
+ # "is_fault":null
838
+ # }
839
+ # ]
840
+ # }
841
+ # ]
842
+ # }
843
+ # ]
844
+ # }
845
+ # }
846
+ # }
847
+
848
+ def site_events(params)
849
+ data = URI.encode_www_form({
850
+ "range_start" => params[:range_start],
851
+ "range_end" => params[:range_end],
852
+ "interval" => params[:interval],
853
+ "controller_id[]" => params[:controller_id],
854
+ "zone_number[]" => params[:zone_number],
855
+ "timestamp" => Time.now.to_i.to_s
856
+ })
857
+ uri = parse_uri(@base_uri+"/api/v2/reports/sites/"+params[:site_id].to_s+"/events.json?"+data)
858
+ @request = make_get(uri)
859
+ make_request(uri,@request)
860
+ end
861
+
862
+
863
+ private
864
+
865
+ # This is the generic request - it cannot be called directly
866
+ def make_request(uri,request)
867
+ @http = Net::HTTP.new(uri.host, uri.port)
868
+ # @http.use_ssl = true
869
+ # @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
870
+ @request = request
871
+ data = @request.method.eql?('GET') ? uri.query : @request.body
872
+ @request["Accept"] = "application/json"
873
+ @request["Content-Type"] = "application/json"
874
+ @request['x-api-hmac'] = generate_hmac_string(data)
875
+ @request["x-api-key"] = api_key
876
+ @request.basic_auth(username,password)
877
+ # puts "request headers are " + @request.to_hash.to_s
878
+ # puts "request body is " + @request.body.inspect
879
+ response = @http.request(@request)
880
+ return JSON.parse(response.body)
881
+ # puts JSON.parse(response.body)
882
+ end
883
+
884
+ def parse_uri(uri)
885
+ URI.parse(uri)
886
+ end
887
+
888
+ def generate_hmac_string(data)
889
+ key = secret_api_key
890
+ digest = OpenSSL::Digest.new('sha256')
891
+ hmac = OpenSSL::HMAC.digest(digest, key, data)
892
+ hmac = Base64.encode64(hmac).strip()
893
+ return hmac
894
+ end
895
+
896
+ def api_key
897
+ # ENV['SLN_API_KEY']
898
+ '998174b6f911b8e95db1c15d1ac59c4f' # This should be an environment variable for best security - this is not a production key
899
+ end
900
+
901
+ def secret_api_key
902
+ # ENV['SLN_API_SECRET_KEY']
903
+ # '245aa67451c534803dc93e7953ff40b6' # This should be an environment variable for best security - this is not a production key
904
+ '245aa67451c534803dc93e7953ffffff'
905
+ end
906
+
907
+ def make_get(uri)
908
+ Net::HTTP::Get.new(uri.request_uri)
909
+ end
910
+
911
+ def make_post(uri)
912
+ Net::HTTP::Post.new(uri.request_uri)
913
+ end
914
+
915
+ def add_timestamp
916
+ 'timestamp='+Time.now.to_i.to_s
917
+ end
918
+
919
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weathermatic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.7
5
+ platform: ruby
6
+ authors:
7
+ - Tyler Merritt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: SmartLink provides a web portal which connects SmartLine controllers
14
+ in the field to the cloud allowing basic operations like starting or stopping irrigation
15
+ for a specific zone on a specified controller. All controllers which belong to
16
+ a customer account can be managed via the web portal, iOS and Android applications,
17
+ or this API. The current version is v2 of the API. You must have a developer key
18
+ in order to use this version.
19
+ email:
20
+ - tyler.merritt@weathermatic.com
21
+ executables: []
22
+ extensions: []
23
+ extra_rdoc_files: []
24
+ files:
25
+ - lib/weathermatic.rb
26
+ homepage: http://www.smartlinknetwork.com/
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.2.2
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: An API wrapper in Ruby for interfacing with the SmartLink Network site at
50
+ my.smartlinknetwork.com
51
+ test_files: []