DIY Home Automation - Page 4 - AVS Forum | Home Theater Discussions And Reviews
Baselworld is only a few weeks away. Getting the latest news is easy, Click Here for info on how to join the Watchuseek.com newsletter list. Follow our team for updates featuring event coverage, new product unveilings, watch industry news & more!



Forum Jump: 
 5Likes
Reply
Thread Tools
post #91 of 98 Unread 07-17-2016, 05:52 PM - Thread Starter
AVS Special Member
 
BassThatHz's Avatar
 
Join Date: Apr 2008
Location: Northern Okan range (NW Cascades region)
Posts: 6,589
Mentioned: 60 Post(s)
Tagged: 0 Thread(s)
Quoted: 1777 Post(s)
Liked: 1341
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:	71
Size:	46.2 KB
ID:	1551449  
dashpuppy likes this.
BassThatHz is online now  
Sponsored Links
Advertisement
 
post #92 of 98 Unread Yesterday, 11:11 PM - Thread Starter
AVS Special Member
 
BassThatHz's Avatar
 
Join Date: Apr 2008
Location: Northern Okan range (NW Cascades region)
Posts: 6,589
Mentioned: 60 Post(s)
Tagged: 0 Thread(s)
Quoted: 1777 Post(s)
Liked: 1341
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:	39
Size:	236.5 KB
ID:	1557393  
dashpuppy likes this.

Last edited by BassThatHz; Yesterday at 11:15 PM.
BassThatHz is online now  
post #93 of 98 Unread Today, 08:33 AM
Advanced Member
 
dashpuppy's Avatar
 
Join Date: Dec 2014
Location: Nanaimo B.C
Posts: 557
Mentioned: 3 Post(s)
Tagged: 0 Thread(s)
Quoted: 290 Post(s)
Liked: 152
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 !

Jason
dashpuppy is online now  
post #94 of 98 Unread Today, 02:26 PM - Thread Starter
AVS Special Member
 
BassThatHz's Avatar
 
Join Date: Apr 2008
Location: Northern Okan range (NW Cascades region)
Posts: 6,589
Mentioned: 60 Post(s)
Tagged: 0 Thread(s)
Quoted: 1777 Post(s)
Liked: 1341
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:	15
Size:	72.7 KB
ID:	1558313  

Last edited by BassThatHz; Today at 02:30 PM.
BassThatHz is online now  
post #95 of 98 Unread Today, 03:41 PM - Thread Starter
AVS Special Member
 
BassThatHz's Avatar
 
Join Date: Apr 2008
Location: Northern Okan range (NW Cascades region)
Posts: 6,589
Mentioned: 60 Post(s)
Tagged: 0 Thread(s)
Quoted: 1777 Post(s)
Liked: 1341
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; Today at 03:45 PM.
BassThatHz is online now  
post #96 of 98 Unread Today, 05:29 PM
Advanced Member
 
dashpuppy's Avatar
 
Join Date: Dec 2014
Location: Nanaimo B.C
Posts: 557
Mentioned: 3 Post(s)
Tagged: 0 Thread(s)
Quoted: 290 Post(s)
Liked: 152
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

Jason
dashpuppy is online now  
post #97 of 98 Unread Today, 06:21 PM - Thread Starter
AVS Special Member
 
BassThatHz's Avatar
 
Join Date: Apr 2008
Location: Northern Okan range (NW Cascades region)
Posts: 6,589
Mentioned: 60 Post(s)
Tagged: 0 Thread(s)
Quoted: 1777 Post(s)
Liked: 1341
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 online now  
post #98 of 98 Unread Today, 06:30 PM - Thread Starter
AVS Special Member
 
BassThatHz's Avatar
 
Join Date: Apr 2008
Location: Northern Okan range (NW Cascades region)
Posts: 6,589
Mentioned: 60 Post(s)
Tagged: 0 Thread(s)
Quoted: 1777 Post(s)
Liked: 1341
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:
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... )
BassThatHz is online now  
Sponsored Links
Advertisement
 
Reply DIY Speakers and Subs



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