・曲を変える

プレイリストから、その時すれ違った人の聴いていた曲名をクリック。

するとTopの自分が今、聴いている曲が先ほどクリックしたものに変わります。
となりの♪をクリックすると、、、

こんな感じで、音楽ファイルの再生場所へ飛びます。
そんなこんなでアプリは完成!!!
あとはハードを完成させれば、シンポジウムを待つだけだ〜☆
ここにいるよ。が、「Mulius」を完成させるまでの軌跡。

#aとbがすれ違った時
def ab
@encounter = Encounter.new(params[:encounter])#新しいencounterを作ります
@user1 = User.find(1)#ユーザー1のID
@user2 = User.find(2)#ユーザー2のID
conditions = ["user_id=?", 1 ]
@playlist = Playlist.find(:first, :conditions => conditions, :order => "updated_at DESC" )
@playlistid = @playlist.id #いま作っているプレイリストを探し出し、関連づける
@encounter.user_id = @user2.id
@encounter.song_id = @user2.song_id#ユーザーbのその時の曲をそのままencounterのsong_idとする。
@encounter.message = @user2.message#ユーザーbのその時のメッセージをそのままencounterのmessageとする。
@encounter.playlist_id = @playlistid
#最終的に、いろいろな所から引っ張って来たデータを、encounterの中に入れる。
if @encounter.save
flash[:notice] = '新規登録完了'
redirect_to :action => 'index'
else
render :action => 'new'
end
end
def index
@search = params[:id]
conditions = ["user_id=?",@search]
@playlists = Playlist.find(:all, :conditions=> conditions )
end
C192.168.0.111:3000
GET /main HTTP/1.1<^M^J>
HOST: 192.168.0.111<^M^J><^M^J>myPort.write("C192.168.0.111:3000\n");//ここはCRのみ
myPort.write("GET /main HTTP/1.1\r\n");//こちらはCRLFを一回
myPort.write("HOST: 192.168.0.111\r\n\r\n");//CRLFを二回serialSend("c192.168.0.111:3000" + "\n");
serialSend("GET /main HTTP/1.1\r\n" + "\r\n");
serialSend("HOST: 192.168.0.111"+ "\r\n\r\n");Serial.print("c192.168.0.111:3000" + "\n");
Serial.print("GET /main HTTP/1.1\r\n" + "\r\n");
Serial.print("HOST: 192.168.0.111"+ "\r\n\r\n");$ dig www.sfc.keio.ac.jp
C133.27.4.221:80
GET /index.html HTTP/1.1<^M^J>
HOST: www.sfc.keio.ac.jp<^M^J><^M^J>
import processing.serial.*;
Serial myPort; // Serial object
int step = 0; // which step in the process you're on
char linefeed = 10; // ASCII linefeed character
void setup()
{
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil(linefeed);
}
void draw()
{
}
void serialEvent(Serial myPort) {
print(myPort.readString());
}
void keyReleased() {
switch (step) {
case 0:
myPort.write("C133.27.4.221:80\n");
step++;
break;
case 1:
myPort.write("GET /index.html HTTP/1.1\r\n");
myPort.write("HOST: www.sfc.keio.ac.jp\r\n\r\n");
step++;
break;
}
}C192.168.0.111:3000
GET /login HTTP/1.1<^M^J>
HOST: 192.168.0.111<^M^J><^M^J>myPort.write("C192.168.0.111:3000\n");
myPort.write("GET /encounter HTTP/1.1\r\n");
myPort.write("HOST: 192.168.0.111\r\n\r\n");var CR = String.fromCharCode(13);
var CRLF = String.fromCharCode(13, 10);
var c = "C192.168.0.111:3000";
var h = "HOST: 192.168.0.111";
var con = "Content-Type: application/x-wwwform-urlencoded";
var p1 = "POST /encounter/ab HTTP/1.1";
var p2 = "POST /encounter/ac HTTP/1.1";
var p3 = "POST /encounter/ad HTTP/1.1"; serialSend(c + CR);
serialSend(p1 + CRLF);
serialSend(h + CRLF);
serialSend(con + CRLF + CRLF);

//for A
serialInit(9600);
pinMode(0, true);
digitalWrite(0,true);
ain1 = analogRead(1);
radioInit(0xDDDD, 0, 11, 3);
var checkb= 1;
var checkc= 1;
var checkd= 1;
while(true) {
function onRadioReceive(seq,src,pan,data,rss) {
if (data == 'b' && checka == 1){
serialSend("a"+"\r\n");
checkb = 0;
checkc = 1;
checkd = 1;
}else if(data == 'c' && checkb == 1){
serialSend("c"+"\r\n");
checkb = 1;
checkc = 0;
checkd = 1;
}else if(data == 'd' && checkd == 1){
serialSend("d"+"\r\n");
checka = 1;
checkb = 1;
checkd = 0;
}else{
}
}
radioSend(0xFFFF, 'a');
}//for C
serialInit(9600);
pinMode(0, true);
digitalWrite(0,true);
ain1 = analogRead(1);
radioInit(0xDDDD, 0, 11, 3);
var checka= 1;
var checkb= 1;
var checkd= 1;
while(true) {
function onRadioReceive(seq,src,pan,data,rss) {
if (data == 'a' && checka == 1){
serialSend("a"+"\r\n");
checka = 0;
checkb = 1;
checkd = 1;
}else if(data == 'b' && checkb == 1){
serialSend("b"+"\r\n");
checka = 1;
checkb = 0;
checkd = 1;
}else if(data == 'd' && checkd == 1){
serialSend("d"+"\r\n");
checka = 1;
checkb = 1;
checkd = 0;
}else{
}
}
radioSend(0xFFFF, 'c');
}//for D
serialInit(9600);
pinMode(0, true);
digitalWrite(0,true);
ain1 = analogRead(1);
radioInit(0xDDDD, 0, 11, 3);
var checka= 1;
var checkb= 1;
var checkc= 1;
while(true) {
function onRadioReceive(seq,src,pan,data,rss) {
if (data == 'a' && checka == 1){
serialSend("a"+"\r\n");
checka = 0;
checkb = 1;
checkc = 1;
}else if(data == 'b' && checkb == 1){
serialSend("b"+"\r\n");
checka = 1;
checkb = 0;
checkc = 1;
}else if(data == 'c' && checkd == 1){
serialSend("c"+"\r\n");
checka = 1;
checkb = 1;
checkc = 0;
}else{
}
}
radioSend(0xFFFF, 'd');
}

rails -d mysql posttestruby script/generate model usert.column :name, :string
t.column :message, :textruby script/generate controller user index new show createdef index
@users = User.find(:all)
end
def create
@user = User.new(params[:user])#上のnewでの値を受け取っています。
if@user.save
redirect_to :action => 'index'
else
render :action => 'new'
end
end
def new
end
def destroy
@user = User.find(params[:id])
@user.destroy
flash[:notice] = '削除しました'
redirect_t<h1>Users#index</h1>
<div class ="notice"><%= h(flash[:notice])%></div>
<p><%= link_to('新規ユーザー作成', :action => 'new')%></p>
<% @users.each do |user| -%>
<li>
<%= link_to(h(user.name), :action => 'show', :id => user.id)%>
<%= link_to('削除', {:action => 'destroy', :id => user.id},
{:method => :post, :confirm => '本当に削除しますか?'})%>
</li>
<% end -%>
<h1>Users#new#新しいユーザーを作る</h1>
<% form_for :user, @user,
:url=>{:action => 'create'} do |form| %>
<p>名前: <%=form.text_field :name, :size => 16 %> </p>
<p>メッセージ:<br /><%=form.text_area :message, :colse =>40, :rows => 3%> </p>
<p>
<%= form.submit 'Create' %>
</p>
<% end %>
<p><%= link_to('一覧へ戻る', :action => 'index') %></p>
<h1>Users#new#新しいユーザーを作る</h1>
<form action="/user/create" method="post"><div style="margin:0;padding:
0"><input name="authenticity_token" type="hidden"
value="3459c27ed8923e8483d23bd0b4a8a870f248bc56" /></div>
<p>名前: <input id="user_name" name="user[name]" size="16"
type="text" /> </p>
<p>メッセージ:<br /><textarea cols="40" colse="40"
id="user_message" name="user[message]" rows="3"></textarea> </p>
<p>
<input id="user_submit" name="commit" type="submit" value="Create" />
</p>
</form>
<p><a href="/user">一覧へ戻る</a></p>
C192.168.0.111:3000
POST /user/create HTTP/1.1 ^M^J
HOST: 192.168.0.111:3000 ^M^J
user[name]=hoge&user[message]=hoge ^M^J
telnet 192.168.0.111 3000
POST /user/create HTTP/1.1
HOST: 192.168.0.111 3000
user[name]=hoge&user[message]=hoge
Processing UserController#index (for 192.168.0.111 at 2009-02-03 20:38:01) [GET]
SQL (0.1ms) SET NAMES 'utf8'
SQL (0.1ms) SET SQL_AUTO_IS_NULL=0
User Load (0.7ms) SELECT * FROM `users`
Rendering user/index
User Columns (3.0ms) SHOW FIELDS FROM `users`
Completed in 69ms (View: 63, DB: 1) | 200 OK [http://192.168.0.111/user]
POST /user/create HTTP/1.1 ^M^J
HOST: 192.168.0.111 ^M^J
Content-Type: application/x-wwwform-urlencodeduser[name]=hoge&user[message]=hoge
<% if @current_user -%>
<%= @current_user.user_name %> さんのアカウント |
<%= link_to 'ログアウト',
{ :controller => '/login', :action => 'logout' },
{ :method => 'post',
:confirm => '本当にログアウトしますか?' } -%>
<% end -%>
<p>ログイン名: <%=form.text_field :login_name, :size => 16 %></p>
<p>パスワード: <%=form.password_field :password, :size => 16%></p> attr_accessor :password, :password_confirmation
attr_protected :salt, :hashed_password
# パスワードが送られたとき
def password=(pw)
@password = pw
if pw and pw.size > 0
self.salt = rand(100000000)
self.hashed_password = User.hashed_password(pw, self.salt)
end
end

map.root :controller => "users"ruby script/generate model songclass CreateSongs < ActiveRecord::Migration
def self.up
create_table :songs do |t|
t.column :song_name, :string # 曲名
t.column :artist_name, :string # アーティスト
t.column :song_url, :string # URL
t.timestamps
end
end
def self.down
drop_table :songs
end
end# 開発環境のためのフィクスチャ: songs
one:
id: 1
song_name: test1
artist_name: さとう # アーティスト
song_url: http://google.co.jp # URL
created_at: <%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>
updated_at: <%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>
two:
id: 2
song_name: test2
artist_name: やまだ # アーティスト
song_url: http://google.co.jp # URL
created_at: <%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>
updated_at: <%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>
three:
id: 3
song_name: test3
artist_name: すずき # アーティスト
song_url: http://google.co.jp # URL
created_at: <%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>
updated_at: <%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>rake db:initializeclass User < ActiveRecord::Base
belongs_to :song
end
class Song < ActiveRecord::Base
has_many :users
end
t.column :listening_song, :stringt.column :song_id, :integer # 外部キー@user.listening_songと記述していた所を、@user.song.song_nameと変えるだけです!
$ ruby script/generate migration alter_usersclass AlterUsers < ActiveRecord::Migration
def self.up
add_column :users, :login_name, :string, # ログイン名
:null => false
add_column :users, :hashed_password, :string
# 暗号化されたパスワード
add_column :users, :salt, :string # パスワードソルト
end
def self.down
remove_column :users, :login_name
remove_column :users, :hashed_password
remove_column :users, :salt
end
endclass User < ActiveRecord::Base
belongs_to :song
attr_accessor :password, :password_confirmation
attr_protected :salt, :hashed_password
def self.authenticate(login_name, password)
user = find_by_login_name(login_name)
if user and user.hashed_password ==
hashed_password(password, user.salt)
user
else
nil
end
end
# ハッシュ値の生成
def self.hashed_password(password, salt)
Digest::SHA1.hexdigest(sprintf("%s%08d", password, salt))
end
end
login_name: user1(userごとに、ここは変える必要があります。)
hashed_password: <%= User.hashed_password('password', 12345678) %>
salt: 12345678rake db:migrate$ ruby script/generate controller login indexclass LoginController < ApplicationController
# ログイン
def login
login_name = params[:login_name]
password = params[:password]
user = User.authenticate(login_name, password)
if user
session[:user_id] = user.id
redirect_to :controller => 'users', :action => 'index'
flash[:notice] = "Logged in successfully"
else
session[:user_id] = nil
redirect_to :controller => 'login', :action => 'index'
flash[:warning] = 'ログイン失敗'
flash[:login_name] = login_name
end
end
# ログアウト
def logout
session[:user_id] = nil
redirect_to :controller => 'login', :action => 'index'
end
end
<% unless @current_user -%>
<div>
<h1>ログイン</h1>
<% form_tag '/login/login', :method => 'post' do %>
<%= hidden_field_tag 'from', request.request_uri %>
<table border="0">
<tr><td align="right">ユーザー名:</td>
<td><%= text_field_tag 'login_name', flash[:login_name],
:size => 16 %></td></tr>
<tr><td align="right">パスワード:</td>
<td><%= password_field_tag 'password', '',
:size => 16 %></td></tr>
<tr><td colspan="2" align="center">
<%= submit_tag 'ログイン' %>
</td></tr>
</table>
<% end %>
</div>
<% end -%>
<% if flash[:warning] -%>
<div id="warning">
<%= flash[:warning] %>
</div>
<% end -%>
class ApplicationController < ActionController::Base
helper :all
protect_from_forgery # :secret => 'b9df6040833a6d4befdb3d8b5fa52082'
before_filter :resume_session
private
# セッションの再開
def resume_session
return unless session[:user_id]
begin
@current_user = User.find(session[:user_id])
rescue ActiveRecord::RecordNotFound
session[:user_id] = nil
end
end
# ログインしていないユーザーをはじく
def block_non_users
unless @current_user
flash[:warning] = 'ログインが必要です。'
redirect_to :controller => 'login', :action => 'index'
return false
end
end
end
before_filter :block_non_users
class UsersController < ApplicationController
def index
@users = User.find(:all)
end
def show
@user = User.find(params[:id])
end
#indexとshowは前回と同じです。
def new
@user = User.new
end
#モデルクラス名.newで新しいレコード作成
def edit
@user = User.find(params[:id])
end
#showと同じ
#新規作成用フォーム
def create
@user = User.new(params[:user])#上のnewでの値を受け取っています。
if@user.save
flash[:notice] = '新しいユーザーが追加されました!'
redirect_to :action => 'show' , :id => @user #処理が終わったら新規作成したユーザーのshow/(id)へ
else
render :action => 'new'
end
end
#flashで、画面に処理表示の結果を出力することができます。
def update
@user = User.find(params[:id])
@user.attributes = params[:user]
@user.save
if@user.save
flash[:notice] = '情報が更新されました'
redirect_to :action => 'show' , :id => @user#処理が終わったら,更新したページ=show/(id)へ
else
render :action => 'edit'
end
end
#editの値をうけとってupdateしています。
def destroy
@user = User.find(params[:id])
@user.destroy
flash[:notice] = '削除しました'
redirect_to :action => 'index'#処理が終わったらindexへ
end
end
<h1>Test003: Users : indexページ</h1>
<div class ="notice"><%= h(flash[:notice])%></div>#Flashによる、処理結果の表示
<p><%= link_to('新規ユーザー作成', :action => 'new')%></p>
<% @users.each do |user| -%>
<li>
<%= link_to(h(user.user_name), :action => 'show', :id => user.id)%>
<%= link_to('編集', :action => 'edit', :id => user.id)%>
<%= link_to('削除', {:action => 'destroy', :id => user.id},
{:method => :post, :confirm => '本当に削除しますか?'})%>
</li>
<% end -%>

<h1>Members#edit#ユーザーデータを編集する</h1>
<% form_for :user, @user,
:url=>{:action => 'update', :id => @user} do |form| %>
<p>名前: <%=form.text_field :user_name, :size => 16 %> </p>
<p>曲: <%=form.text_field :listening_song, :size => 16 %> </p>
<p>メッセージ:<br /><%=form.text_area :message, :colse =>40, :rows => 3%> </p>
<p>
<%= form.submit '更新' %>
</p>
<% end %>
<p><%= link_to('一覧へ戻る', :action => 'index') %></p>

<h1>Users#new#新しいユーザーを作る</h1>
<% form_for :user, @user,
:url=>{:action => 'create'} do |form| %>
<p>名前: <%=form.text_field :user_name, :size => 16 %> </p>
<p>曲: <%=form.text_field :listening_song, :size => 16 %> </p>
<p>メッセージ:<br /><%=form.text_area :message, :colse =>40, :rows => 3%> </p>
<p>
<%= form.submit 'Create' %>
</p>
<% end %>
<p><%= link_to('一覧へ戻る', :action => 'index') %></p>

form_for
namespace :db do
desc "Create the development and test databases."
task :create do
require 'yaml'
path = File.dirname(__FILE__) + "/../../config/database.yml"
configurations = YAML.load_file(path)
%w(development test).each do |env|
config = configurations[env]
if config['adapter'] == 'mysql'
command = sprintf(%{mysql --user %s}, config['username'])
command += " --password=#{config['password']}" if config['password'] and !config['password'].empty?
command += " --host #{config['host']}" if config['host'] and !config['host'].empty?
command += " --port #{config['port']}" if config['port'] and !config['port'].empty?
sh command + %( --execute="drop database if exists #{config['database']}")
if config['encoding'] and !config['encoding'].empty?
sh command + %( --execute="create database #{config['database']} default character set #{config['encoding']}")
else
sh command + %( --execute="create database #{config['database']}")
end
elsif config['adapter'] == 'postgresql'
# 未実装
else
raise "Unsupported database adapter #{config['adapter']}"
end
end
end
desc "Create and initialize the development and test databases."
task :initialize => ["db:create", "db:migrate", "db:import:development", "db:test:prepare"]
end
rake db:initialize

$ rails -d mysql test002$ mysql -u rootmysql> create database test002_development;mysql> create database test002_test;mysql> exit$ cd test002$ ruby script/generate model user
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.column :user_name, :string, # 名前(姓)
:null => false
t.column :listening_song, :string # 聴いている曲
t.column :message, :text # 備考
t.column :created_at, :datetime # 登録日時
t.column :updated_at, :datetime # 更新日時
t.timestamps
end
end
def self.down
drop_table :users
end
end
:null => false$ rake db:migrate
(in /Users/suzukiyasuko/rails/test002)
== CreateUsers: migrating ====================================================
-- create_table(:users)
-> 0.0048s
== CreateUsers: migrated (0.0053s) ===========================================
mysql> use test002_development;mysql> desc users;
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_name | varchar(255) | NO | | NULL | |
| listening_song | varchar(255) | YES | | NULL | |
| message | text | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+----------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
# 開発環境のためのフィクスチャ: users
John:
id: 1
user_name: ジョンレノン
listening_song: 赤いスイートピー
message: 松田聖子イイ!!
created_at: <%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>
updated_at: <%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>
Yauko:
id: 2
user_name: 鈴木泰子
listening_song: 木綿のハンカチーフ
message: 古い曲ばっかりだな
created_at: <%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>
updated_at: <%= Time.now.strftime("%Y-%m-%d %H:%M:%S") %>
require 'active_record'
require 'active_record/fixtures'
namespace :db do
def load_data(env)
ActiveRecord::Base.establish_connection(env)
pattern = File.join(RAILS_ROOT, 'db', 'fixtures',
env.to_s, '*.{yml,csv}')
Dir.glob(pattern).each do |fixture_file|
table_name = File.basename(fixture_file, '.*')
Fixtures.create_fixtures('db/fixtures/' + env.to_s, table_name)
end
end
namespace :import do
desc "Load fixture data into the development database."
task :development => :environment do
load_data :development
end
desc "Load fixture data into the production database."
task :production => :environment do
load_data :production
end
end
end
$ rake db:import:development$ ruby script/generate controller users index show
class UsersController < ApplicationController
def index
@users = User.find(:all)
end
def show
@user = User.find(params[:id])
end
end
<% @users.each do |user| -%>
<li>
<%= link_to(h(user.user_name), :action => 'show', :id => user.id)%>
</li>
<% end -%>
<p>名前は:<%=@user.user_name%></p>
<p>聴いている曲は:<%=@user.listening_song %></p>
<p>メッセージ:<%=@user.message%></p>
<%= link_to '一覧表示に戻る', :action => 'index'%>
$ ruby script/server
equire 'socket'
client = TCPSocket.open('192.168.0.123', 10001)
while true do
client.write('a')
p client.gets.chomp
sleep(3)
client.write('b')
p client.gets.chomp
sleep(3)
end
serialInit(9600);をserialInit(4800);に変えると上手くいく場合もあるそうです。
require 'socket'
client = TCPSocket.open('192.168.0.123', 10001)
//ここで自分でWiPortに割り振ったIPアドレスとポート
//ここから下は無限ループ
while true do
//trueの間はずっと実行=Falseなんてここにはないので、無限になりますね。
client.write('a')
//クライアント=WiPortにaを送り続けます
p client.gets.chomp
//p = print
//client = 上で記述した“TCPSocket~・・・”のこと
//gets = 入力データを文字列オブジェクトとして返します
//chomp = 改行を取り除いた文字列を生成して返します。
sleep(1)
//上の出力が終わった後、停止する
client.write('b')
p client.gets.chomp
sleep(1)
//上の三行と同じ
end