xhou
4/26/2017 - 10:11 PM

Manipulate locks

Manipulate locks

# To print all plate_number, qr_code_uri

def print_bike_code_dict(file_name)
File.open("/tmp/new_12000.csv", "w") do |file|
BikePlate.all.each do |p|
    p.bike_codes.each do |c|
      file << p.plate_number << "," << c.qr_code_uri.to_s << "," 
      file << c.position << "\n"
    end
  end
  file.close
end
print_bike_code_dict("/tmp/bike_code_dict.csv", "w")

# create 
bike_lock = **begin **BikeLock.create!(
  plate_number: plate_number,  
  imei: imei,  
  iccid: iccid,  
  sim_sid: iccid_sid_hash[iccid],  # you can find the sim_sid from sims.cvs
  serial_number: serial_number, 
  status: :init,
)



BikePlate.create_unique

#good
filename = "/tmp/working.csv"
File.open(filename, "w") do |file|
    BikeLock.where("reported_at is not null and status in ('installed', 'bike_check_passed', 'table_check_passed', 'bike_checking') and lock_state='locked'").each do |l|
      file << l.status << "," << l.lock_state << "," << l.plate_number << "," << l.imei << "," << l.iccid << "," << l.sim_sid << "," << l.reported_at
      file << "\n"
    end
    file.close
end

filename = "/tmp/not_working_locks.csv"
File.open(filename, "w") do |file|
    BikeLock.where("reported_at is not null and status not in ('installed', 'bike_check_passed', 'table_check_passed', 'bike_checking')").each do |l|
      file << l.status << "," << l.lock_state << "," << l.plate_number << "," << l.imei << "," << l.iccid << "," << l.sim_sid << "," << l.reported_at
      file << "\n"
    end
    file.close
end



filename = "/tmp/all.csv"
File.open(filename, "w") do |file|
  BikeLock.where(status: %w(table_check_passed bike_checking bike_check_passed installed)).order(reported_at: :desc).each do |l|
    file << l.status << "," << l.lock_state << "," << l.plate_number << "," << l.imei << "," << l.iccid << "," << l.sim_sid << "," << l.reported_at
    file << "\n"
  end
  file.close
end

b = BikeLock.find_by(plate_number: '284826'); (ATCommands::ATCommander.new(b)).send(unlock_cmd, :sms)

filename = "/tmp/all_alive_locks.csv"
File.open(filename, "w") do |file|
  BikeLock.where("reported_at is not null").order(reported_at: :desc).each do |l|
    file << l.status << "," << l.lock_state << "," << l.plate_number << "," 
    file << l.imei << "," << l.iccid << "," << l.sim_sid << "," 
    file << l.reported_at << ","  << l.lock_state_updated_at 
    file << "\n"
  end
  file.close
end  
BikeLock.where("reported_at is not null").order(reported_at: :desc).each do |l|
  file << l.status << "," << l.lock_state << "," << l.plate_number << "," 
  file << l.imei << "," << l.iccid << "," << l.sim_sid << "," 
  file << l.reported_at << ","  << l.lock_state_updated_at 
  file << "\n"
end
file.close
Roo::Spreadsheet.open('/home/ec2-user/Lock Inventory.xlsx').sheet(0).each_row_streaming do |row|
  next if row[1].value.to_i < 1 || row[2].value.to_i < 1
  lock = BikeLock.find_by(plate_number: row[1].value.to_i)
  plate = BikePlate.find_by(plate_number: row[2].value.to_i)
  next if lock.nil? || plate.nil?
  lock.plate_number = plate.plate_number
  lock.save!
end

Roo::Spreadsheet.open('/home/ec2-user/Lock Inventory.xlsx').sheet(0).each_row_streaming do |row|
  next if row[1].value.to_i < 1 || row[2].value.to_i < 1
  lock1 = BikeLock.find_by(plate_number: row[1].value.to_i)
  lock2 = BikeLock.find_by(plate_number: row[2].value.to_i)
  pp lock1
  pp lock2
end

  
def deploy_lock(plate_number)
  bike_lock = BikeLock.find_by!(plate_number: plate_number.to_s)
  raise "Can't find lock for plate number #{plate_number}" if bike_lock.nil?
  unless bike_lock.may_install?
    # Cancell ongoing 
    bike_lock.reset! if bike_lock.may_reset?
    # change bike lock status to bike_check_passed, so that we can install the lock.
    bike_lock.update!(status: :bike_check_passed)
  end
  # install the bike to create the bike_body
  bike_lock.install!
  # deploy the bike_body, so that it creates a Bike instance for riders.
  bike_lock.bike_body.deploy!
end
deploy_lock('000000')
def change_plate_number(source, target)
  source_plate = BikePlate.find_by(plate_number: source)
  target_plate = BikePlate.find_by(plate_number: target)
  puts "No such plate for #{source}" if source_plate.nil?
  puts "No such plate for #{target}" if target_plate.nil?
  puts "Exist a bike associated with target}" if target_plate.bike.present?
  bike = source_plate.bike
  if bike.present?
    target_plate.update!(bike_id: bike.id)
    source_plate.update!(bike_id: nil)
    puts "Changed Bike \##{bike.id}"
  else
    puts "No bike associated with source number"
  end
  lock = BikeLock.find_by(plate_number: source)
  if lock.present?
    lock.update!(plate_number: target)
    puts "Changed BikeLock \##{lock.id}"
  else
    puts "No lock associated with source number"
  end
end