<commit_before>#!/usr/bin/env ruby

require 'serialport'
require 'ted/recorder'
require 'influxdb'
require 'rest_client'

username = 'ted'
password = '<PASSWORD>'
database = 'energy'
api_key = ARGV[1]
meter_id = ARGV[2]

sp = SerialPort.new ARGV[0], 1200
influxdb = InfluxDB::Client.new database, :username => username, :password => password

plotwatt_messages = []

while true do
  begin
    byte = sp.read(1)
  end until ["aa"] == byte.unpack('H*')

  bytes = "#{byte}#{sp.read(10)}"

  message = Ted::Recorder::Ted1000Message.new(bytes)
  if message.verify?

    puts "received and verified #{message.inspect}"
    begin
      influxdb.write_point("power", {:value => message.power})
      influxdb.write_point("voltage", {:value => message.voltage})
      puts "Wrote power:#{message.power} and voltage:#{message.voltage} to influxdb"
    rescue InfluxDB::Error => e
      puts e
    end

    kw_power = message.power / 1000.0
    plotwatt_messages << "#{meter_id},#{kw_power.round(2)},#{Time.new.to_i}"
    if plotwatt_messages.size >= 60
      puts plotwatt_messages.join(',')
      begin
        response = RestClient.post "http://#{api_key}:@plotwatt.com/api/v2/push_readings", plotwatt_messages.join(',')
        plotwatt_messages = [] if response.code == 200
      rescue RestClient::ServerBrokeConnection
      end
    end
  end

end<commit_msg>rescue everything. don't judge me.<commit_after>#!/usr/bin/env ruby

require 'serialport'
require 'ted/recorder'
require 'influxdb'
require 'rest_client'

username = 'ted'
password = '<PASSWORD>'
database = 'energy'
api_key = ARGV[1]
meter_id = ARGV[2]

sp = SerialPort.new ARGV[0], 1200
influxdb = InfluxDB::Client.new database, :username => username, :password => password

plotwatt_messages = []

while true do
  begin
    byte = sp.read(1)
  end until ["aa"] == byte.unpack('H*')

  bytes = "#{byte}#{sp.read(10)}"

  message = Ted::Recorder::Ted1000Message.new(bytes)
  if message.verify?

    puts "received and verified #{message.inspect}"
    begin
      influxdb.write_point("power", {:value => message.power})
      influxdb.write_point("voltage", {:value => message.voltage})
      puts "Wrote power:#{message.power} and voltage:#{message.voltage} to influxdb"
    rescue InfluxDB::Error => e
      puts e
    end

    kw_power = message.power / 1000.0
    plotwatt_messages << "#{meter_id},#{kw_power.round(2)},#{Time.new.to_i}"
    if plotwatt_messages.size >= 60
      puts plotwatt_messages.join(',')
      begin
        response = RestClient.post "http://#{api_key}:@plotwatt.com/api/v2/push_readings", plotwatt_messages.join(',')
        plotwatt_messages = [] if response.code == 200
      rescue 
        puts e
      end
    end
  end

end
{'fraction_non_alphanumeric': 0.14917127071823205, 'fraction_numerical': 0.019152854511970532, 'mean_word_length': 3.707105719237435, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6406854', 'n_tokens_mistral': 991, 'n_tokens_neox': 935, 'n_words': 200}