DIY Home Automation - Page 4 - AVS Forum | Home Theater Discussions And Reviews

Forum Jump: 
 5Likes
 
Thread Tools
Old 07-17-2016, 06:52 PM - Thread Starter
AVS Special Member
 
BassThatHz's Avatar
 
Join Date: Apr 2008
Location: Northern Okan range (NW Cascades region)
Posts: 7,073
Mentioned: 67 Post(s)
Tagged: 0 Thread(s)
Quoted: 1995 Post(s)
Liked: 1501
There the face plates fit and there is enough space for the 10awg wires.


Now I need 16 1/4" screws for the heat sinks.
Attached Thumbnails
Click image for larger version

Name:	20160717_173108.jpg
Views:	110
Size:	46.2 KB
ID:	1551449  
dashpuppy likes this.
BassThatHz is offline  
Sponsored Links
Advertisement
 
Old 07-21-2016, 12:11 AM - Thread Starter
AVS Special Member
 
BassThatHz's Avatar
 
Join Date: Apr 2008
Location: Northern Okan range (NW Cascades region)
Posts: 7,073
Mentioned: 67 Post(s)
Tagged: 0 Thread(s)
Quoted: 1995 Post(s)
Liked: 1501
I tried creating my very first andriod app, I followed the instructions, but it gave me 60 java runtime errors.

and this is why I prefer Microsoft C# (because it NEVER gives you API runtime errors!)
It is backwards compatible since version 1.0 of the API!!! and most certainly the last few versions. Unlike java!
I don't have time to bleed!!!


I don't know andriod studio and I don't know java, so I'm off to a great start!
Attached Thumbnails
Click image for larger version

Name:	34567890.jpg
Views:	94
Size:	236.5 KB
ID:	1557393  
dashpuppy likes this.

Last edited by BassThatHz; 07-21-2016 at 12:15 AM.
BassThatHz is offline  
Old 07-21-2016, 09:33 AM
Advanced Member
 
dashpuppy's Avatar
 
Join Date: Dec 2014
Posts: 675
Mentioned: 3 Post(s)
Tagged: 0 Thread(s)
Quoted: 371 Post(s)
Liked: 195
Quote:
Originally Posted by BassThatHz View Post
I tried creating my very first andriod app, I followed the instructions, but it gave me 60 java runtime errors.

and this is why I prefer Microsoft C# (because it NEVER gives you API runtime errors!)
It is backwards compatible since version 1.0 of the API!!! and most certainly the last few versions. Unlike java!
I don't have time to bleed!!!


I don't know andriod studio and I don't know java, so I'm off to a great start!
There is a reason why im a hardware guru @ work I HATE software !
dashpuppy is offline  
Old 07-21-2016, 03:26 PM - Thread Starter
AVS Special Member
 
BassThatHz's Avatar
 
Join Date: Apr 2008
Location: Northern Okan range (NW Cascades region)
Posts: 7,073
Mentioned: 67 Post(s)
Tagged: 0 Thread(s)
Quoted: 1995 Post(s)
Liked: 1501
Well, I'm gonna forge ahead without a working compiler.

Got the GUI done. Now I need to make the buttons do something:


Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="bassthathz.bassthathzrelayapp.MainActivity">

    <Button
        android:layout_marginTop="0dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Music Mode"
        android:onClick="MusicModeClick"
        />

    <Button
        android:layout_marginTop="50dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Movie Mode"
        android:onClick="MovieModeClick"
        />
    <Button
        android:layout_marginTop="100dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="All Off"
        android:onClick="AllOffClick"
        />
</RelativeLayout>
Attached Thumbnails
Click image for larger version

Name:	4657.jpg
Views:	68
Size:	72.7 KB
ID:	1558313  

Last edited by BassThatHz; 07-21-2016 at 03:30 PM.
BassThatHz is offline  
Old 07-21-2016, 04:41 PM - Thread Starter
AVS Special Member
 
BassThatHz's Avatar
 
Join Date: Apr 2008
Location: Northern Okan range (NW Cascades region)
Posts: 7,073
Mentioned: 67 Post(s)
Tagged: 0 Thread(s)
Quoted: 1995 Post(s)
Liked: 1501
Alright, this should do it, I think....?

That said, the C# code can't yet handle this, it will have to be updated to accept these URL parameters from the Android and process them... (it is nothing difficult.)

Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="bassthathz.bassthathzrelayapp">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
Code:
package bassthathz.bassthathzrelayapp;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    
@Override

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

    public void MusicModeClick(View view)
    {
       int response = SendNetworkComm("MusicModeClick");
        ResponseHandler(response);
    }

    public void MovieModeClick(View view)
    {
        int response = SendNetworkComm("MovieModeClick");
        ResponseHandler(response);
    }

    public void AllOffClick(View view)
    {
        int response = SendNetworkComm("AllOffClick");
        ResponseHandler(response);
    }

    public void ResponseHandler(int response)
    {
        //todo
    }

    public void ErrorHandler(String UserMessage)
    {
        //todo
    }

    public int SendNetworkComm(String paramValue)
    {
        int response = -1;
        try
        {
            ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isConnected()) {
                // do network comm
                URL url = new URL("http://192.0.0.1/?action=" + paramValue);
                HttpURLConnection conn = null;
                try
                {
                    conn = (HttpURLConnection) url.openConnection();
                    conn.setReadTimeout(10000);
                    conn.setConnectTimeout(10000);
                    conn.setRequestMethod("GET");
                    //conn.setRequestProperty("Content-Type", "text/plain");
                    //conn.setRequestProperty("charset", "utf-8");
                    conn.setDoInput(true);
                    conn.connect();
                    response = conn.getResponseCode();
                }
                catch(MalformedURLException error)
                {
                    ErrorHandler("URL is invalid.");
                }
                catch(SocketTimeoutException error)
                {
                    ErrorHandler("Server could not be reached.");
                }
                catch (IOException error)
                {
                    ErrorHandler("Network error.");
                }
                finally
                {
                    if(conn != null)
                    {
                        conn.disconnect();
                    }
                }
            }
            else
            {
                ErrorHandler("Network Unavailable.");
            }
        }
        catch (IOException e)
        {
            ErrorHandler("Network Unavailable.");
        }
        return response;
    }
}

Last edited by BassThatHz; 07-21-2016 at 04:45 PM.
BassThatHz is offline  
Old 07-21-2016, 06:29 PM
Advanced Member
 
dashpuppy's Avatar
 
Join Date: Dec 2014
Posts: 675
Mentioned: 3 Post(s)
Tagged: 0 Thread(s)
Quoted: 371 Post(s)
Liked: 195
Quote:
Originally Posted by BassThatHz View Post
Alright, this should do it, I think....?



That said, the C# code can't yet handle this, it will have to be updated to accept these URL parameters from the Android and process them... (it is nothing difficult.)



Code:
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="bassthathz.bassthathzrelayapp">



    <uses-permission android:name="android.permission.INTERNET" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />



    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:supportsRtl="true"

        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

</manifest>


Code:
package bassthathz.bassthathzrelayapp;



import android.content.Context;

import android.net.ConnectivityManager;

import android.net.NetworkInfo;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;



import java.io.BufferedOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.SocketTimeoutException;

import java.net.URL;



public class MainActivity extends AppCompatActivity {



    
@Override


    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }



    public void MusicModeClick(View view)

    {

       int response = SendNetworkComm("MusicModeClick");

        ResponseHandler(response);

    }



    public void MovieModeClick(View view)

    {

        int response = SendNetworkComm("MovieModeClick");

        ResponseHandler(response);

    }



    public void AllOffClick(View view)

    {

        int response = SendNetworkComm("AllOffClick");

        ResponseHandler(response);

    }



    public void ResponseHandler(int response)

    {

        //todo

    }



    public void ErrorHandler(String UserMessage)

    {

        //todo

    }



    public int SendNetworkComm(String paramValue)

    {

        int response = -1;

        try

        {

            ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

            if (networkInfo != null && networkInfo.isConnected()) {

                // do network comm

                URL url = new URL("http://192.0.0.1/?action=" + paramValue);

                HttpURLConnection conn = null;

                try

                {

                    conn = (HttpURLConnection) url.openConnection();

                    conn.setReadTimeout(10000);

                    conn.setConnectTimeout(10000);

                    conn.setRequestMethod("GET");

                    //conn.setRequestProperty("Content-Type", "text/plain");

                    //conn.setRequestProperty("charset", "utf-8");

                    conn.setDoInput(true);

                    conn.connect();

                    response = conn.getResponseCode();

                }

                catch(MalformedURLException error)

                {

                    ErrorHandler("URL is invalid.");

                }

                catch(SocketTimeoutException error)

                {

                    ErrorHandler("Server could not be reached.");

                }

                catch (IOException error)

                {

                    ErrorHandler("Network error.");

                }

                finally

                {

                    if(conn != null)

                    {

                        conn.disconnect();

                    }

                }

            }

            else

            {

                ErrorHandler("Network Unavailable.");

            }

        }

        catch (IOException e)

        {

            ErrorHandler("Network Unavailable.");

        }

        return response;

    }

}


Is the ip your using just a temp one . I don't think 192.0.0.1 is rout able.


Sent from my iPhone using Tapatalk
dashpuppy is offline  
Old 07-21-2016, 07:21 PM - Thread Starter
AVS Special Member
 
BassThatHz's Avatar
 
Join Date: Apr 2008
Location: Northern Okan range (NW Cascades region)
Posts: 7,073
Mentioned: 67 Post(s)
Tagged: 0 Thread(s)
Quoted: 1995 Post(s)
Liked: 1501
Quote:
Originally Posted by dashpuppy View Post
Is the ip your using just a temp one . I don't think 192.0.0.1 is rout able.
It's just a placeholder. Ideally it would be configurable, haven't got that far yet.
BassThatHz is offline  
Old 07-21-2016, 07:30 PM - Thread Starter
AVS Special Member
 
BassThatHz's Avatar
 
Join Date: Apr 2008
Location: Northern Okan range (NW Cascades region)
Posts: 7,073
Mentioned: 67 Post(s)
Tagged: 0 Thread(s)
Quoted: 1995 Post(s)
Liked: 1501
Ok here is the newest version of the C# Relay up on my Google Drive:
C# Source Code
Windows Binary

This now has support for the Android app (or any app that can apply this request parameters syntax):
Code:
  
public static string HttpRequestHandler(HttpListenerRequest request)
        {
            String ReturnValue = String.Empty;

            var x = new RelayConfig.RelayConfig();
            if (request.QueryString != null)
            {
                String action = request.QueryString["action"];
                if (!String.IsNullOrEmpty(action))
                {
                    switch (action)
                    {
                        case "MusicModeClick":
                            x.MusicMode();
                            break;
                        case "MovieModeClick":
                            x.MovieMode();
                            break;
                        case "AllOffClick":
                            x.AllOff();
                            break;
                    }
                }
            }
            else
            {
                using (var reader = new StreamReader(request.InputStream, request.ContentEncoding))
                {
                    string text = reader.ReadToEnd();
                    if (text.Contains("btnMusic"))
                    {
                        x.MusicMode();
                    }
                    else if (text.Contains("btnMovies"))
                    {
                        x.MovieMode();
                    }
                    else if (text.Contains("btnOff"))
                    {
                        x.AllOff();
                    }
                }
                ReturnValue = GetFileAsString();
            }
            
            return ReturnValue;
        }
Code:
 
private void HttpRequestHandler(IAsyncResult result)
        {
            HttpListenerContext Context = listener.EndGetContext(result);

            HttpListenerRequest Request = Context.Request;
            HttpListenerResponse Response = Context.Response;

            if (Request.RawUrl != "/" && Request.RawUrl != "/Default.html" && !Request.RawUrl.Contains("/?action="))
            {
                return;
            }

            string ResponseString  = String.Empty;

            Response.StatusCode = 200;
            Response.StatusDescription = "OK";
            Response.ContentType = "text/html";

            if (_HttpResponseHandler != null)
            {
                ResponseString = _HttpResponseHandler.Invoke(Request);
            }

            if (String.IsNullOrEmpty(ResponseString))
            {
                ResponseString = "<HTML><BODY>Default Content</BODY></HTML>";
            }

            byte[] Buffer = System.Text.Encoding.UTF8.GetBytes(ResponseString);
            Response.ContentLength64 = Buffer.Length;
            System.IO.Stream OutputStream = Response.OutputStream;
            OutputStream.Write(Buffer, 0, Buffer.Length);
            OutputStream.Close();
            Response.Close();
       }
but I haven't tested this new code just yet.

"Should" work, it was a pretty small/simple change.
(But never say never... )

Last edited by BassThatHz; 07-21-2016 at 08:15 PM.
BassThatHz is offline  
Sponsored Links
Advertisement
 
 
Thread Tools


Forum Jump: 

Posting Rules  
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off