<commit_before>#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'rubygems'
require 'sinatra/rocketio/linda/client'
$stdout.sync = true

url =   ENV["LINDA_BASE"] || ARGV.shift || "http://localhost:5000"
spaces = (ENV["LINDA_SPACES"]||ENV["LINDA_SPACE"]||"test").split(/,/)
puts "connecting.. #{url}"
linda = Sinatra::RocketIO::Linda::Client.new url

tss = {}
spaces.each do |i|
  tss[i] = linda.tuplespace[i]
end
puts "spaces : #{tss.keys}"

lasts = Hash.new{|h,k| h[k] = Hash.new }

linda.io.on :connect do  ## RocketIO's "connect" event
  puts "connect!! <#{linda.io.session}> (#{linda.io.type})"
  tss.each do |name, ts|
    ts.watch ["sensor", "light"] do |tuple|
      next unless tuple.size == 3 and (tuple[2].to_s =~ /^[\d\.]+$/)
      puts "#{name} - #{tuple}"
      light = tuple[2].to_i
      if lasts[name] and lasts[name][:value]
        light_d = light - lasts[name][:value]
        msg = nil
        stat = nil
        if light_d > 20
          msg = "#{name}で電気が点きました"
          stat = "on"
        elsif light_d < -20
          msg = "#{name}で電気が消えました"
          stat = "off"
        end
        if msg
          puts msg
          if !lasts[name][:notify] or
              lasts[name][:notify] < (Time.now-5) or
              (lasts[name][:stat] and lasts[name][:stat] != stat)
            tss.values.each do |ts_|
              ts_.write ["skype", "send", "#{msg} - #{tuple}"]
              ts_.write ["twitter", "tweet", "#{msg} - #{tuple}"]
            end
            tss.each do |name_, ts_|
              next if name_ == name
              ts_.write ["say", msg]
            end
          end
          lasts[name][:stat] = stat
          lasts[name][:notify] = Time.now
        end
      end
      lasts[name][:value] = light
    end
  end
end

linda.io.on :disconnect do
  puts "RocketIO disconnected.."
end

linda.wait<commit_msg>refact<commit_after>#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'rubygems'
require 'sinatra/rocketio/linda/client'
$stdout.sync = true

url = ENV["LINDA_BASE"] || ARGV.shift || "http://localhost:5000"
spaces = (ENV["LINDA_SPACES"]||ENV["LINDA_SPACE"]||"test").split(/,/)
puts "connecting.. #{url}"
linda = Sinatra::RocketIO::Linda::Client.new url

tss = {}
spaces.each do |i|
  tss[i] = linda.tuplespace[i]
end
puts "spaces : #{tss.keys}"

lasts = Hash.new{|h,k| h[k] = Hash.new }

linda.io.on :connect do  ## RocketIO's "connect" event
  puts "connect!! <#{linda.io.session}> (#{linda.io.type})"
  tss.each do |name, ts|
    ts.watch ["sensor", "light"]{|tuple|
      next unless tuple.size == 3 and (tuple[2].to_s =~ /^[\d\.]+$/)
      puts "#{name} - #{tuple}"
      light = tuple[2].to_i
      unless lasts[name] and lasts[name][:value]
        lasts[name][:value] = light
        next
      end
      msg = nil
      stat = nil
      if 20 < light-lasts[name][:value]
        msg = "#{name}で電気が点きました"
        stat = :on
      elsif light - lasts[name][:value] < -20
        msg = "#{name}で電気が消えました"
        stat = :off
      end
      if [:on, :off].include? stat
        puts msg
        if lasts[name][:stat] != stat or
            lasts[name][:notify_at]+5 < Time.now
          tss.each do |name_, ts_|
            ts_.write ["skype", "send", "#{msg} - #{tuple}"]
            ts_.write ["twitter", "tweet", "#{msg} - #{tuple}"]
            ts_.write ["say", msg] if name_ != name
            ts_.write ["sensor", "light", stat] if name_ == name
          end
        end
        lasts[name] = {:stat => stat, :notify_at => Time.now}
      end
      lasts[name][:value] = light
    }
  end
end

linda.io.on :disconnect do
  puts "RocketIO disconnected.."
end

linda.wait
{'fraction_non_alphanumeric': 0.18381344307270234, 'fraction_numerical': 0.007133058984910837, 'mean_word_length': 2.258266309204647, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 8, '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': '2719390', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1352, 'n_words': 304}