serialbar 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 00293fd411b0ce3c80444a09c28fa28795c017fd
4
+ data.tar.gz: b8f93a2ea2cb6501426436f846630a867541bc24
5
+ SHA512:
6
+ metadata.gz: a4304f21fad39ea00df12213924c91286dcf3151820df1cd1acf568cd279b5440e000b4258c56bb980073a22715ed368a03f81415ec4e5e4a0271db2c041d6cd
7
+ data.tar.gz: a000a0095d0738b287562fad4bc7ce0f57ce5c38593db4357569854f4feb61f84fd94f2f693de0fea36441b9e2a2ff891bd86086f5c089e0d3b93b29385e7b2d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ *~
2
+ serialbox.sublime-project
3
+ serialbox.sublime-workspace
4
+ mongoid.yml
5
+ spec/mongoid.yml
6
+ doc
7
+ dummy
8
+ *.gem
9
+ .yardoc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ #ruby '2.1.1'
5
+
6
+ #gem 'serialport'
7
+ #gem 'mongoid'
8
+ #gem 'json'
9
+ #gem 'awesome_print'
data/Gemfile.lock ADDED
@@ -0,0 +1,56 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ serialbar (0.0.1)
5
+ serialport (~> 1.3, >= 1.3.0)
6
+ thor (~> 0.19, >= 0.19.1)
7
+ timers (~> 3.0, >= 3.0.0)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activemodel (3.2.18)
13
+ activesupport (= 3.2.18)
14
+ builder (~> 3.0.0)
15
+ activesupport (3.2.18)
16
+ i18n (~> 0.6, >= 0.6.4)
17
+ multi_json (~> 1.0)
18
+ builder (3.0.4)
19
+ diff-lcs (1.2.5)
20
+ hitimes (1.2.1)
21
+ i18n (0.6.9)
22
+ mongoid (3.1.6)
23
+ activemodel (~> 3.2)
24
+ moped (~> 1.4)
25
+ origin (~> 1.0)
26
+ tzinfo (~> 0.3.29)
27
+ moped (1.5.2)
28
+ multi_json (1.10.1)
29
+ origin (1.1.0)
30
+ rake (0.9.6)
31
+ rspec (3.0.0)
32
+ rspec-core (~> 3.0.0)
33
+ rspec-expectations (~> 3.0.0)
34
+ rspec-mocks (~> 3.0.0)
35
+ rspec-core (3.0.2)
36
+ rspec-support (~> 3.0.0)
37
+ rspec-expectations (3.0.2)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.0.0)
40
+ rspec-mocks (3.0.2)
41
+ rspec-support (~> 3.0.0)
42
+ rspec-support (3.0.2)
43
+ serialport (1.3.0)
44
+ thor (0.19.1)
45
+ timers (3.0.0)
46
+ hitimes
47
+ tzinfo (0.3.39)
48
+
49
+ PLATFORMS
50
+ ruby
51
+
52
+ DEPENDENCIES
53
+ mongoid (~> 3.1, >= 3.1.6)
54
+ rake (~> 0)
55
+ rspec (~> 3.0, >= 3.0.0)
56
+ serialbar!
data/README.rdoc ADDED
@@ -0,0 +1,42 @@
1
+ = \Serialbox
2
+
3
+ * http://github.com/jonbramble/serialbar
4
+
5
+ == DESCRIPTION:
6
+
7
+ # Work in progress, not ready for use
8
+
9
+ Serialbar acts as an intermediary between serial port data and database based storage
10
+
11
+ This was part of a project to save data from scientific instruments in a mongodb database but was made to be extendable
12
+
13
+ == FEATURES:
14
+
15
+ Generators are available to create a bearbones program
16
+
17
+ You still need to know the instrument response and how to parse the serialport data and pass that to data objects
18
+
19
+ == TODO:
20
+
21
+ A server component will be added in the future for quick data visualisation
22
+ More adapters need to be written and will be based on Active Model
23
+ Configuration file for projects
24
+
25
+ == SYNOPSIS
26
+
27
+ #todo
28
+
29
+ == INSTALL
30
+
31
+ * gem install serialbar (not available just yet)
32
+
33
+
34
+ == LICENSE:
35
+
36
+ Copyright (C) 2014 Jonathan Bramble
37
+
38
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
39
+
40
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
data/bin/serialbar ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift File.expand_path('../../lib', __FILE__)
3
+
4
+ require 'serialbar/cli'
5
+
6
+ Serialbar::CLI.start
@@ -0,0 +1,36 @@
1
+ module Serialbar
2
+ module Adapter
3
+ module InstanceMethods
4
+ end
5
+
6
+ module ClassMethods
7
+
8
+ def foo
9
+ end
10
+
11
+ def configure_adapter(type,classname="DataFile")
12
+ base = const_get classname
13
+ load_adapter(base,type)
14
+ end
15
+
16
+ def load_adapter(base,type)
17
+ # load module based on type
18
+ case type
19
+ when :mongoid
20
+ require 'serialbar/adapters/mongoid'
21
+ mod = Mongoid::Document
22
+ when :filesystem
23
+ require 'serialbar/adapters/file_system'
24
+ end
25
+ base.send :include, mod
26
+ end
27
+
28
+ end
29
+
30
+ def self.included(base)
31
+ base.extend(ClassMethods)
32
+ base.send :include, InstanceMethods
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,9 @@
1
+ puts "filesystem adapter"
2
+
3
+ module Serialbox
4
+ module Adapters
5
+ module FileSystem
6
+
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ begin
2
+ require 'mongoid' unless defined?(::Mongoid)
3
+ rescue LoadError
4
+ puts "Mongoid is not installed. Please, do '$gem install mongoid', or add mongoid to your Gemfile"
5
+ end
6
+
7
+ #sets up the mongoid connection
8
+ Mongoid.load!("mongoid.yml", :production) # will need to be able to configure this from the main file
9
+
10
+ module Serialbox
11
+ module Adapters
12
+
13
+ end
14
+ end
File without changes
File without changes
@@ -0,0 +1,89 @@
1
+ require 'thor'
2
+ require 'serialbar/generators/boiler_plate'
3
+
4
+ module Serialbar
5
+
6
+ class CLI < Thor
7
+ include Thor::Actions
8
+
9
+ default_task :usage
10
+
11
+ def self.source_root
12
+ File.dirname(__FILE__)
13
+ end
14
+
15
+ desc "list_adapters", "list the currently available adapters"
16
+ def list_adapters
17
+ say "Currently available adapters"
18
+ say "\tmongoid"
19
+ end
20
+
21
+ option :adapter, aliases: "-d", desc: "set the adapter type", required: true
22
+ desc "new NAME", "create a new serialbar project"
23
+ def new(name)
24
+ say "creating a new serialbar project #{name}", :green
25
+ readme(name) # create readme file in project dir
26
+
27
+ case options[:adapter]
28
+ when "mongoid"
29
+ say 'Using mongoid adapter', :green
30
+ insert_mongoid(name)
31
+ insert_data_file(name,"mongoid")
32
+ else
33
+ say 'Adapter type unknown', :red
34
+ #invoke(:list_adapters,"")
35
+ end
36
+
37
+ insert_listener(name)
38
+ end
39
+
40
+ desc "help", "serialbar help"
41
+ def usage
42
+ puts <<-DES
43
+ Usage: Serialbox help [command]
44
+
45
+ Description: A simple app to create a database or file based storage adapter for serial port data
46
+
47
+ DES
48
+ help
49
+ end
50
+
51
+
52
+ no_commands do
53
+ def readme(name)
54
+ #create readme file and insert the contents
55
+ #str = "text to read"
56
+ #copy_file "generators/README", "#{name}/README"
57
+ #insert_into_file "#{name}/README", str
58
+ end
59
+
60
+ def insert_mongoid(name)
61
+ copy_file "generators/mongoid_example.yml", "#{name}/mongoid_example.yml"
62
+ #append specific instructions to readme
63
+ end
64
+
65
+ def insert_data_file(name,adapter)
66
+ create_file "#{name}/data_file.rb" do
67
+ Serialbar::BoilerPlate.data_file_class
68
+ end
69
+
70
+ str = Serialbar::BoilerPlate.config_adapter_string(adapter)
71
+ insert_into_file "#{name}/data_file.rb", str , :after => "include Serialbox::Adapters\n"
72
+
73
+ end
74
+
75
+ def insert_listener(name)
76
+ create_file "#{name}/listener.rb" do
77
+ Serialbar::BoilerPlate.listener_class
78
+ end
79
+ end
80
+
81
+ #when developed
82
+ def insert_file_system
83
+ #append instructions to readme
84
+ end
85
+ end
86
+
87
+
88
+ end
89
+ end
@@ -0,0 +1,7 @@
1
+ module Serialbar
2
+ module Exceptions
3
+ class NoParseMethodError < NoMethodError; end
4
+ class PortNotInitialized < StandardError; end
5
+ # can we put our customised messages in here?
6
+ end
7
+ end
@@ -0,0 +1 @@
1
+ Serialbox project
@@ -0,0 +1,37 @@
1
+ module Serialbox
2
+ module BoilerPlate
3
+
4
+ def self.listener_class
5
+ "require 'serialbox'\n"+
6
+ "require_relative 'data_file'\n\n"+
7
+ "class Listener\n"+
8
+ "\tinclude Serialbox::Listener\n"+
9
+ "\tdef parse(string)\n"+
10
+ "\t#TODO: implement your parser here\n"+
11
+ "\tend\n"+
12
+ "end\n"+
13
+ "\n\n"+
14
+ "ln = Listener.new\n"+
15
+ "ln.setup(serialport params)\n"+
16
+ "ln.run\n"
17
+ end
18
+
19
+ def self.data_file_class
20
+ "class DataFile\n"+
21
+ "\tinclude Serialbox::Adapter\n"+
22
+ "\tstore_in session: 'default'\n"+
23
+ "\t#TODO: add your data variables here\n"+
24
+ "\t#field :time\n"+
25
+ "\t#field :experiment\n"+
26
+ "end\n\n"+
27
+ "#Subclass DataFile for specific data points"
28
+ "class DataPoint < DataFile\n"+
29
+ "\t#field :temp\n"+
30
+ "end"
31
+ end
32
+
33
+ def self.config_adapter_string(adapter)
34
+ "\tconfigure_adapter :#{adapter.to_sym}\n"
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,24 @@
1
+ #mongoid v3.~
2
+ production:
3
+ sessions:
4
+ default:
5
+ # URI allows for easy replica-set connection setup.
6
+ # Use environment variables or a config file to keep your credentials safe.
7
+ database: name
8
+ username: user
9
+ password: password
10
+ hosts:
11
+ - localhost:27017
12
+ options:
13
+ # Default consistency is :eventual, which reads from secondary first. Strong forces reads to primary.
14
+ # You should use strong consistency if you need the freshest data available.
15
+ consistency: :strong
16
+ # max_retries specifies the number of times your driver will attempt an operation before giving up.
17
+ max_retries: 30
18
+ # retry_interval specifies the number of seconds to wait before retrying a single operation.
19
+ retry_interval: 1
20
+ # timeout is the time in seconds for the driver to wait for an operation.
21
+ # A value of 30 allows for plenty of time for DNS slowness/network blips.
22
+ # Note that if you have a long-running query (over 30 seconds), it will timeout.
23
+ # Use Queryable#no_timeout for long queries: http://mongoid.org/en/origin/docs/options.html.
24
+ timeout: 1
@@ -0,0 +1 @@
1
+ test text
@@ -0,0 +1,7 @@
1
+ module Serialbar
2
+ module Helpers
3
+ def time
4
+ return Time.now
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,124 @@
1
+ require 'serialport'
2
+ require 'timers'
3
+
4
+ module Serialbar
5
+ module Listener
6
+
7
+ # Setup the serial port
8
+ def setup(port, baud=9600, data_bits=8, stop_bits=1, parity=1)
9
+ @portname = port
10
+ @sp = SerialPort.new(@portname,baud,data_bits,stop_bits,parity)
11
+ @setup = true
12
+ end
13
+
14
+ # Direct access to the serialport serial port object
15
+ def serial_port
16
+ return @sp if port_initialized?
17
+ end
18
+
19
+ # Trigger listening on setup serial port
20
+ #
21
+ # simply reads each line from the port and passes it as string to implemented parse method
22
+ def run
23
+ #is the serial port setup?
24
+ puts "Listening on serial port #{@portname}"
25
+ if port_initialized?
26
+ @sp.flush_input
27
+ begin
28
+ while data = @sp.readline
29
+ parse(data)
30
+ end
31
+ rescue Interrupt
32
+ puts "Exiting"
33
+ end
34
+ end
35
+ end
36
+
37
+ # Poll device by sending string
38
+ #
39
+ # ==== Attributes
40
+ #
41
+ # * +send+ - string to send to the serial port
42
+ # * +lines+ - number of lines to read back
43
+ #
44
+ # ==== Examples
45
+ #
46
+ # data = listener.poll("#001\\n")
47
+ #
48
+ # read lines not implemented yet
49
+ def poll(send,lines=1)
50
+ if port_initialized?
51
+ begin
52
+ @sp.write(send)
53
+ data = @sp.readline
54
+ rescue Interrupt
55
+ puts "Exiting"
56
+ end
57
+ parse(data)
58
+ end
59
+ end
60
+
61
+ # Poll device with a timer
62
+ # could use clever missing_method stuff here
63
+ #
64
+ # data is passed as an argument to parse
65
+ #
66
+ # ==== Attributes
67
+ #
68
+ # * +send+ - string to send to the serial port
69
+ # * +lines+ - number of lines to read back
70
+ # * +n+ - number of seconds between each poll of the device
71
+ #
72
+ def poll_every_n_seconds(send,lines=1,n=1)
73
+ timer = Timers.new
74
+ every_seconds = timer.every(n) { parse(poll(send,lines)) }
75
+ loop { timers.wait }
76
+ end
77
+
78
+ # Poll device with a timer
79
+ # could use clever missing_method stuff here
80
+ #
81
+ # data is passed as an argument to parse
82
+ #
83
+ # ==== Attributes
84
+ #
85
+ # * +send+ - string to send to the serial port
86
+ # * +lines+ - number of lines to read back
87
+ # * +n+ - number of minutes between each poll of the device
88
+ #
89
+ def poll_every_n_minutes(send,lines=1,n=1)
90
+ timer = Timers.new
91
+ every_seconds = timer.every(60*n) { parse(poll(send,lines)) }
92
+ loop { timers.wait }
93
+ end
94
+
95
+ # Using method missing to check for the inclusion of the subclass implementation of parse
96
+ #
97
+ def method_missing(id, *args) #:nodoc:
98
+ if id.to_s.eql?("parse")
99
+ raise Serialbar::Exceptions::NoParseMethodError, "Parse method not implemented"
100
+ else
101
+ raise NoMethodError
102
+ end
103
+ end
104
+
105
+ def listener? #:nodoc: Testing method for class inclusion
106
+ return true #testing method
107
+ end
108
+
109
+ def parse_missing? #:nodoc: Testing method for presence of parse
110
+ parse("test string")
111
+ end
112
+
113
+ def port_initialized?
114
+ if @sp.nil?
115
+ raise Serialbar::Exceptions::PortNotInitialized, "Call setup on listener class to initialize serial port"
116
+ else
117
+ return true
118
+ end
119
+
120
+ end
121
+
122
+ end
123
+
124
+ end
@@ -0,0 +1,41 @@
1
+ # module DataPoint
2
+ #extend Mongoid::Document
3
+
4
+
5
+
6
+ # module ClassMethods
7
+
8
+
9
+ # end
10
+
11
+ # module InstanceMethods
12
+
13
+
14
+ # end
15
+
16
+ # def self.included(receiver)
17
+ # receiver.extend ClassMethods
18
+ # receiver.send :include, InstanceMethods
19
+ # end
20
+
21
+ # end
22
+
23
+
24
+ c_name = "#{adapter_name.to_s.capitalize}"
25
+ doc = Serialbox::Adapters.const_get(c_name).constants
26
+ adpstr = c_name+"::"+doc[0].to_s
27
+ @adapter = adpstr.constantize
28
+
29
+
30
+ #module DataPoint
31
+
32
+ #def adapter
33
+
34
+ #end
35
+ # include Mongoid::Document # what if we want alternative configurable data end points
36
+
37
+ # what other attributes might the general datapoint have
38
+ # unique id is included with mongoid
39
+ # field :time
40
+
41
+ #end
@@ -0,0 +1,3 @@
1
+ module Serialbar
2
+ VERSION = "0.0.1"
3
+ end
data/lib/serialbar.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'serialbar/version'
2
+ require 'serialbar/exceptions'
3
+ require 'serialbar/cli'
4
+ require 'serialbar/listener'
5
+ require 'serialbar/helpers'
6
+ require 'serialbar/adapter'
data/serialbar.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'serialbar/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "serialbar"
8
+ spec.version = Serialbar::VERSION
9
+ spec.date = '2014-06-20'
10
+ spec.summary = "An adapter gem between incomming serial port data and storage"
11
+ spec.description = "Parse and save incoming data to storage, default mongodb"
12
+ spec.authors = ["Jonathan Bramble"]
13
+ spec.email = ["jbramble82@hotmail.com"]
14
+ spec.homepage = 'https://github.com/jonbramble/serialbar/'
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency 'rake', '~> 0'
23
+ spec.add_development_dependency 'rspec', '~> 3.0', '>= 3.0.0'
24
+ spec.add_development_dependency "mongoid", "~> 3.1", '>= 3.1.6'
25
+
26
+ spec.add_runtime_dependency "serialport", '~> 1.3', '>= 1.3.0'
27
+ spec.add_runtime_dependency "thor", '~> 0.19', '>= 0.19.1'
28
+ spec.add_runtime_dependency "timers", '~> 3.0', '>= 3.0.0'
29
+ end
@@ -0,0 +1,9 @@
1
+ {
2
+ "folders":
3
+ [
4
+ {
5
+ "follow_symlinks": true,
6
+ "path": "."
7
+ }
8
+ ]
9
+ }