phaserunner 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b1dcf2ac4e83fe77fc1eb60f4992253de50c9ace
4
- data.tar.gz: 91728742cf1a696bc2570ee28d0dde63b9f9e1da
3
+ metadata.gz: f1406fca710360c306fc0d61dceb3af3bcbe63a4
4
+ data.tar.gz: 49ca014e29aa8589963bf6e6b2f7bf8d0aeed6c2
5
5
  SHA512:
6
- metadata.gz: ca1bdc6f822340a8a0033411951b4fdcb7b3e1c5469749e671ac5461f97576936ec2c7667708eaa861c30e363586332205ac54995545e1f1a96b91c5152cf41d
7
- data.tar.gz: fc303b29ccee47bd3e1504925ced6935215f21b7d780106b7e2844b05e2acab4f088a93d01dbeb13f0c037b7e58585de8cffce571decff9ef424ca0ee648d08f
6
+ metadata.gz: 4cffb4f5821f8c2bc9142534cc4634faf964d4948200c4816e50276135bf2099e70fbd07b6d8c992312f0a7c72707ac33ad98acb9a367b10950754346d317046
7
+ data.tar.gz: 640c45d6fa70cd28e0f3fddedfd890347b78e77be25fd33a93fde48d7c61b3723fba4f011b97080b34d87a014c4f144b56083b842436461d6028dbc8bfffdc0b
@@ -9,15 +9,16 @@ module Phaserunner
9
9
  exit 130
10
10
  end
11
11
 
12
+ # Handle the CLI
12
13
  class Cli
13
14
  attr_reader :modbus
14
15
  attr_reader :dict
15
- attr_reader :loop
16
+ attr_reader :loop_count
16
17
  attr_reader :quiet
17
- attr_reader :phaserunnerOutFd
18
18
 
19
19
  include GLI::App
20
20
 
21
+ # Does all the CLI work
21
22
  def main
22
23
  program_desc 'Read values from the Grin PhaseRunner Controller primarily for logging'
23
24
 
@@ -49,8 +50,8 @@ module Phaserunner
49
50
 
50
51
  desc 'Loop the command n times'
51
52
  default_value :forever
52
- arg 'loop', :optional
53
- flag [:l, :loop]
53
+ arg 'loop_count', :optional
54
+ flag [:l, :loop_count]
54
55
 
55
56
  desc 'Do not output to stdout'
56
57
  switch [:q, :quiet]
@@ -68,7 +69,7 @@ module Phaserunner
68
69
  count = args[1].to_i
69
70
  node = dict[address]
70
71
  puts modbus.range_address_header(address, count).join(",") unless quiet
71
- (0..loop).each do |i|
72
+ (0..loop_count).each do |i|
72
73
  puts modbus.read_raw_range(address, count).join(",") unless quiet
73
74
  end
74
75
  end
@@ -78,18 +79,20 @@ module Phaserunner
78
79
  long_desc %q(Logs interesting Phaserunner registers to stdout and a CSV file. File name in the form: phaserunner.#{Time.now.strftime('%Y-%m-%d_%H-%M-%S')}.csv)
79
80
  command :log do |log|
80
81
  log.action do |global_options, options, args|
82
+ filename = "phaserunner.#{Time.now.strftime('%Y-%m-%d_%H-%M-%S')}.csv"
83
+ output_fd = File.open(filename, 'w')
81
84
  header = modbus.bulk_log_header
82
85
  data = modbus.bulk_log_data
83
86
 
84
87
  # Generate and output header line
85
88
  hdr = %Q(Timestamp,#{header.join(",")})
86
89
  puts hdr unless quiet
87
- phaserunnerOutFd.puts hdr
90
+ output_fd.puts hdr
88
91
 
89
- (0..loop).each do |i|
92
+ (0..loop_count).each do |i|
90
93
  str = %Q(#{Time.now.utc.round(10).iso8601(6)},#{data.join(",")})
91
94
  puts str unless quiet
92
- phaserunnerOutFd.puts str
95
+ output_fd.puts str
93
96
  sleep 0.2
94
97
  end
95
98
  end
@@ -103,14 +106,13 @@ module Phaserunner
103
106
  # on that command only
104
107
  @modbus = Modbus.new(global)
105
108
  @dict = @modbus.dict
106
- # Handle that loop can be :forever or an Integer
107
- if global[:loop] == :forever
108
- @loop = Float::INFINITY
109
- else
110
- @loop = global[:loop].to_i
111
- end
109
+ # Handle that loop_count can be :forever or an Integer
110
+ @loop_count = if global[:loop_count] == :forever
111
+ Float::INFINITY
112
+ else
113
+ global[:loop_count].to_i
114
+ end
112
115
  @quiet = global[:quiet]
113
- @phaserunnerOutFd = File.open("phaserunner.#{Time.now.strftime('%Y-%m-%d_%H-%M-%S')}.csv", 'w')
114
116
  end
115
117
 
116
118
  post do |global,command,options,args|
@@ -5,12 +5,37 @@ require 'asi_bod'
5
5
  module Phaserunner
6
6
  # Methods for communicating with the Modbus interface to the Phaserunner
7
7
  class Modbus
8
+
9
+ # Returns the path to the default BODm.json file
10
+ def self.default_file_path
11
+ AsiBod::Bod.default_file_path
12
+ end
13
+
14
+ DEFAULTS = {
15
+ tty: '/dev/ttyUSB0',
16
+ baudrate: 115200,
17
+ slave_id: 1,
18
+ dictionary_file: default_file_path,
19
+ loop_count: :forever,
20
+ quiet: false,
21
+ registers_start_address: 258,
22
+ registers_count: 12,
23
+ registers_misc: [277,334]
24
+ }
25
+
26
+ attr_reader :tty
27
+ attr_reader :baudrate
28
+ attr_reader :slave_id
29
+ attr_reader :dictionary_file
30
+ attr_reader :loop_count
31
+ attr_reader :quiet
32
+
8
33
  # The registers of interest for logging
9
34
  # First a range
10
- REGISTERS_START_ADDRESS = 258
11
- REGISTERS_COUNT = 12
35
+ attr_reader :registers_start_address
36
+ attr_reader :registers_count
12
37
  # Sparse Registers of interest
13
- REGISTERS_MISC =[277,334]
38
+ attr_reader :registers_misc
14
39
 
15
40
  # Contains the Grin Phaesrunner Modbus Dictionary
16
41
  # @params [Hash<Integer, Hash>] dict The Dictionary with a key for each register address
@@ -25,39 +50,32 @@ module Phaserunner
25
50
  # @option dict [Stirng] :type Further info on how to interpret the value
26
51
  attr_reader :dict
27
52
 
28
- attr_reader :registers_start_address
29
- attr_reader :registers_count
30
- attr_reader :registers_misc
31
-
32
- # Returns the path to the default BODm.json file
33
- def self.default_file_path
34
- AsiBod::Bod.default_file_path
35
- end
53
+ attr_reader :bod
36
54
 
37
55
  # New Modbus
38
56
  # Converts the opts hash into Class Instance Variables (attr_readers)
39
57
  # Reads the JSON Grin Phaserunner Modbus Dictionary into a Hash
40
58
  # @params opts [Hash] comes from the CLI
41
59
  def initialize(opts)
60
+ # Start with defaults and allow input args to override them
61
+ final_opts = DEFAULTS.merge opts
62
+
42
63
  # Converts each key of the opts hash from the CLI into individual class attr_readers.
43
64
  # So they are now available to the rest of this Class as instance variables.
44
65
  # The key of the hash becomes the name of the instance variable.
45
66
  # They are available to all the methods of this class
46
67
  # See https://stackoverflow.com/a/7527916/38841
47
- opts.each_pair do |name, value|
68
+ final_opts.each_pair do |name, value|
48
69
  self.class.send(:attr_accessor, name)
49
70
  instance_variable_set("@#{name}", value)
50
- @registers_start_address = REGISTERS_START_ADDRESS
51
- @registers_count = REGISTERS_COUNT
52
- @registers_misc = REGISTERS_MISC
53
71
  end
54
72
 
73
+ # A few other Instance Variables
55
74
  @bod = AsiBod::Bod.new(bod_file: dictionary_file)
56
75
  @dict = @bod.hash_data
57
76
  end
58
77
 
59
78
  def read_raw_range(start_address, count)
60
- #puts("Reading from #{dict[start_address][:name]} scale: #{dict[start_address][:scale]} units: #{dict[start_address][:units]}")
61
79
  cl = ::ModBus::RTUClient.new(tty, baudrate)
62
80
  cl.with_slave(slave_id) do |slave|
63
81
  slave.read_holding_registers(start_address, count)
@@ -88,7 +106,9 @@ module Phaserunner
88
106
  # @param count [Integer] Count of addresses in range. Optional, has a default
89
107
  # @param misc_addresses [Array<Integer>] List of misc individual addresses. Optional, has a default
90
108
  # @return [Array<Integer>] List of the register values in the order requested
91
- def bulk_log_data(start_address=registers_start_address, count=registers_count, misc_addresses=registers_misc)
109
+ def bulk_log_data(start_address = registers_start_address,
110
+ count = registers_count,
111
+ misc_addresses = registers_misc)
92
112
  read_raw_range(start_address, count) + read_addresses(misc_addresses)
93
113
  end
94
114
 
@@ -97,10 +117,11 @@ module Phaserunner
97
117
  # @param count [Integer] Count of addresses in range. Optional, has a default
98
118
  # @param misc_addresses [Array<Integer>] List of misc individual addresses. Optional, has a default
99
119
  # @return [Array<String>] Array of the headers
100
- def bulk_log_header(start_address=registers_start_address, count=registers_count, misc_addresses=registers_misc)
120
+ def bulk_log_header(start_address = registers_start_address,
121
+ count = registers_count,
122
+ misc_addresses = registers_misc)
101
123
  range_address_header(start_address, count) +
102
124
  bulk_addresses_header(misc_addresses)
103
125
  end
104
126
  end
105
127
  end
106
-
@@ -1,3 +1,3 @@
1
1
  module Phaserunner
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.3'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phaserunner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert J. Berger