Hackster is hosting Hackster Holidays, Ep. 5: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 5 on Friday!
Somatat Na Takuatung
Created October 30, 2017

Smart Event

Smart Event is the digital signage that hang in order on the wall in the funeral.

121
Smart Event

Things used in this project

Story

Read more

Schematics

Hardware Schematic

Code

Smart Event Monitor

Java
Use for initial and get data from Firebase
package com.example.smart_event_monitor;

import android.app.DownloadManager;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;

import java.util.UUID;

public class MainActivity extends AppCompatActivity {

    private static final String UUID_KEY = "_UUID";
    private static final String PREFS_NAME = "MyPrefs";

    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Post to initial data to Firebase when device power on
        initMonitor();

        // Set path to node on Firebase
        DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("monitors").child(getDeviceId());

        // Set view for display data from Firebase
        FirebaseRecyclerAdapter<Monitor, MonitorViewHolder> adapter = new FirebaseRecyclerAdapter<Monitor, MonitorViewHolder>(
                Monitor.class,
                R.layout.individual,
                MonitorViewHolder.class,
                myRef
        ) {
            @Override
            protected void populateViewHolder (MonitorViewHolder viewHolder, Monitor model, int position) {
                viewHolder.setDescription(model.getDescription());
                viewHolder.setImage(model.getImage());
                viewHolder.setTitle(model.getTitle());
            }
        };

        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }

    private void initMonitor() {
        DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
        DatabaseReference mMonitorRef = mRootRef.child("monitors");
        mMonitorRef.child(getDeviceId()).child(getDeviceId()).child("title").setValue("TEST");
        mMonitorRef.child(getDeviceId()).child(getDeviceId()).child("description").setValue("TEST");
        mMonitorRef.child(getDeviceId()).child(getDeviceId()).child("image").setValue("https://firebasestorage.googleapis.com/v0/b/testfirebase1-410d9.appspot.com/o/templates%2Fwreath_1.jpg?alt=media&token=efcd6e75-d1fb-4248-b793-a7729876619d");
    }

    private String getDeviceId() {
        SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
        // Generate UUID
        if(!prefs.contains(UUID_KEY)) {
            prefs.edit().putString(UUID_KEY, UUID.randomUUID().toString()).apply();
        }
        return prefs.getString(UUID_KEY, UUID.randomUUID().toString());
    }

    public static class MonitorViewHolder extends RecyclerView.ViewHolder {
        TextView text_description;
        ImageView imageview;
        TextView text_title;
        // Display Image & Text on monitor
        public MonitorViewHolder(View itemView) {
            super(itemView);
            text_description = (TextView) itemView.findViewById(R.id.description);
            imageview = (ImageView) itemView.findViewById(R.id.imageview);
            text_title = (TextView) itemView.findViewById(R.id.title);
        }

        public void setTitle(String title) {
            text_title.setText(title);
        }

        public void setDescription(String description) {
            text_description.setText(description);
        }

        public void setImage(String image) {
            Picasso.with(imageview.getContext()).load(image).into(imageview);
        }
    }
}

Credits

Somatat Na Takuatung

Somatat Na Takuatung

1 project • 0 followers
UI/UX designer

Comments