Announcement

Collapse
No announcement yet.

Can anyone share their project files for a basic TCP server plugin?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can anyone share their project files for a basic TCP server plugin?

    Hi guys,
    There are a few plugin examples about but i can not find anything which covers TCP comms.

    I have adapted and re-published serial solution that was shared with me...so hopefully that helps others who need to create a serial plugin....see my thread on this for the plugin files. https://forums.homeseer.com/showthread.php?t=186797

    I have grabbed some code from around the web and have a working TCP solution in VB.NET but before I translate it into HS, I thought I'd ask if someone has already done it and would like to share?

    Would love to be able to leverage others code to get a kickstart on getting my TCPl devices connected.

  • #2
    Anyone have a TCP plugin they will share?

    Comment


    • #3
      Bumpety bump.

      Comment


      • #4
        I wish I could help but with the exception of a few sample plugins, there really isn't much "community" plugin development here. Likely because of the ability to sell plugins.

        I've been trying to get into plugin dev too, and it's been somewhat of an up hill battle (some of it self imposed time limitation).

        Sent from my XT1585 using Tapatalk
        MeiAutomtion Home

        Comment


        • #5
          Ooo, this looks interesting!!!


          Sent from my iPhone using Tapatalk

          Comment


          • #6
            Yeah. Very short sighted model in terms of the longevity of the product and community imho.

            There are a few stallwarts who do help alot, (THANKYOU), but given the people who sell the software are no longer actively supporting it (see bug list), I may be flogging a dead horse.

            Shame, as I have already paid and invested significant time to (begin to) understand the code, I am here to stay...Guess I will just have to work out how to leverage the serial plugin as a base and apply one of the many example vb.net tcp scripts

            Homeseer:
            Currently Active Users: 269 (20 members and 249 guests)

            That's alot of guests!
            Last edited by davros; February 15th, 2017, 06:16 AM.

            Comment


            • #7
              Originally posted by davros View Post
              Yeah. Very short sighted model in terms of the longevity of the product and community imho.

              There are a few stallwarts who do help alot, (THANKYOU), but given the people who sell the software are no longer actively supporting it (see bug list), I may be flogging a dead horse.

              Shame, as I have already paid and invested significant time to (begin to) understand the code, I am here to stay...Guess I will just have to work out how to leverage the serial plugin as a base and apply one of the many example vb.net tcp scripts

              Homeseer:
              Currently Active Users: 269 (20 members and 249 guests)

              That's alot of guests!
              I actually think the community is great and shares a lot over all.... When it comes to scripts and helping each other out. I also think that HomeSeer itself contributes a good amount to the community (more than my last HA system: Vera). I just don't see a lot of sharing or open sourcing of plugins. There are a few Homeseer plugins that they made open source because they decided to no longer develop them, but I can't remember where they are. Maybe those will help. I'll see if I can find them. That said, most plugin developers are generally more than ready to help out, they're just not gonna give you the source to code they may be using in plugin they charge for.

              @Moskus and a few others have released "better" sample plugins, but I don't think they cover the specific part you are looking for.
              MeiAutomtion Home

              Comment


              • #8
                Thanks for that sirmeili.

                It is rather surprising that there are not standard templates for the most basic of all communications mechanisms, TCP and Serial.

                I get that the specific handling would be different for each device, bu the basic TCP and serial is standard and so anyone should be happy to share that - as all the complex stuff is in the rest of the logic ie. creating devices and controls, setup screens and parsing returned data.

                Do appreciate your post and help.

                Comment


                • #9
                  Originally posted by davros View Post
                  Thanks for that sirmeili.

                  It is rather surprising that there are not standard templates for the most basic of all communications mechanisms, TCP and Serial.

                  I get that the specific handling would be different for each device, bu the basic TCP and serial is standard and so anyone should be happy to share that - as all the complex stuff is in the rest of the logic ie. creating devices and controls, setup screens and parsing returned data.

                  Do appreciate your post and help.
                  No problem. And here is the post about them open sourcing some of their plugins. I'm not sure if they are still out there, but maybe they can help you out

                  https://forums.homeseer.com/showthre...=173036&page=3
                  MeiAutomtion Home

                  Comment


                  • #10
                    There has never been much in the way of open sourcing or github type collaborations with HS plugins. There are one or two around (including some of HS's plugins that sit on Github but I took a look at the WMP plugin and it would not help you) but IMHO because of the effort and time involved (I don't class them as trivial to write) in developing them then they are guarded relatively closely by the developers. As there is zero to stop someone just having the code, using it and then earning money off it selling it back through HomeSeer.
                    My Plugins:

                    Pushover 3P | DoorBird 3P | Current Cost 3P | Velleman K8055 3P | LAMetric 3P | Garadget 3P | Hive 3P |
                    Yeelight 3P | Nanoleaf 3P

                    Comment


                    • #11
                      Originally posted by mrhappy View Post
                      There has never been much in the way of open sourcing or github type collaborations with HS plugins. There are one or two around (including some of HS's plugins that sit on Github but I took a look at the WMP plugin and it would not help you) but IMHO because of the effort and time involved (I don't class them as trivial to write) in developing them then they are guarded relatively closely by the developers. As there is zero to stop someone just having the code, using it and then earning money off it selling it back through HomeSeer.
                      Thank you for saying it better than me LOL.

                      Also, yes, plugins seem not very trivial to write. I've been able to muddle through some, but it seems overly complicated to me. That could just be because I come from a different style of development though (more web/web service based).
                      MeiAutomtion Home

                      Comment


                      • #12
                        Yep, but it's still a BS excuse not to help others out. There is no reason why someone could not publish a sample TCP solution. It's not the secret sauce. The specific device logic is where the special stuff happens. Not the base level comms.

                        Anyways, after a few hours of grunt work, I have come across some code that looks like it will do the trick...on youtube of all places https://www.youtube.com/watch?v=3hfXdJj9f9s

                        It has both client and server and server is threaded and allows for multiple clients to connect as well as relaying data to all clients.

                        I have added it to my serial comm's plugin (previously been published here on the forums) and it's working well. Yes, loads to tweak and adjust for config, error handling and restarts, events etc...but hey, it's a start!

                        So, it looks like this in my serial plugin...
                        Code:
                        Imports System.Net
                        Imports System.Net.Sockets
                        .....
                            Dim TCPserver As TcpListener
                            Dim listOfClients As New List(Of TcpClient)
                        ......
                        
                        
                            Private Sub StartTCPServer()
                                Try
                                    Dim TCP_ServerIP As String = "192.168.0.20"
                                    Dim TCPPort As Integer = 5656
                        
                                    TCPserver = New TcpListener(IPAddress.Parse(TCP_ServerIP), TCPPort)
                                    TCPserver.Start()
                        
                                    Threading.ThreadPool.QueueUserWorkItem(AddressOf NewTCPClient)
                        
                                Catch ex As Exception
                                    hs.WriteLog(IFACE_NAME, ex.Message)
                                End Try
                            End Sub
                        
                            Friend Sub NewTCPClient(state As Object)
                                Dim client As TcpClient = TCPserver.AcceptTcpClient()
                                Console.WriteLine("TCP Server : NewTCPClient module entry")
                        
                                Try
                                    ' There can be multiple clients, but we only allow one connection per IP address.
                                    ' If the client is already in the list of clients then remove it as it must have dropped off and we're now using a different port to connect.
                                    Dim i = 1
                                    Dim clientList As New List(Of TcpClient)
                                    If listOfClients IsNot Nothing Then
                                        For Each c As TcpClient In listOfClients
                                            Console.WriteLine("TCP Server : clients : " & i & ". " & c.Client.RemoteEndPoint.ToString())
                                            i = i + 1
                                            If DirectCast(c.Client.RemoteEndPoint, System.Net.IPEndPoint).Address.ToString() = DirectCast(client.Client.RemoteEndPoint, System.Net.IPEndPoint).Address.ToString() Then
                                                Console.WriteLine("TCP Server : Removing client : " & i & ". " & client.Client.RemoteEndPoint.ToString())
                                            Else
                                                clientList.Add(c)
                                            End If
                                        Next
                                        listOfClients = clientList
                                    End If
                        
                        
                                    ' Add the client to the list of clients
                                    listOfClients.Add(client)
                        
                                    Threading.ThreadPool.QueueUserWorkItem(AddressOf NewTCPClient)
                                    Console.WriteLine("TCP Server : connected " & client.Client.RemoteEndPoint.ToString())
                        
                                    Dim counter As Integer = 0
                        
                                    While counter < 10000
                                        Dim ns As NetworkStream = client.GetStream()
                                        Dim toReceive(100000) As Byte
                                        counter = counter + 1
                        
                                        Dim length As Integer = ns.Read(toReceive, 0, toReceive.Length)
                        
                                        Dim TCPReceivedText As String = Encoding.ASCII.GetString(toReceive, 0, length)
                        
                                        ' If we got a message (>0 characters) then do something with it
                                        If TCPReceivedText.Length > 0 Then
                                            counter = 0
                                            Console.WriteLine("TCP Server : Received   : " & TCPReceivedText.Replace(vbCr, "").Replace(vbLf, ""))
                                            WriteToSerialPort(TCPReceivedText.Replace(vbCr, "").Replace(vbLf, "")) ' Remove CR and LF)
                                        End If
                                    End While
                                    ' Remove the client that had the exception from the list of clients
                                    If listOfClients.Contains(client) Then
                                        listOfClients.Remove(client)
                                    End If
                                    hs.WriteLog(IFACE_NAME, "TCP Server : Disconnected : " & DirectCast(client.Client.RemoteEndPoint, System.Net.IPEndPoint).Address.ToString() & " due to too many zero length frames")
                                    Console.WriteLine("TCP Server : Disconnected      : " & DirectCast(client.Client.RemoteEndPoint, System.Net.IPEndPoint).Address.ToString() & " due to too many zero length frames")
                        
                                Catch ex As Exception
                                    ' Remove the client that had the exception from the list of clients
                                    If listOfClients.Contains(client) Then
                                        listOfClients.Remove(client)
                                    End If
                                    hs.WriteLog(IFACE_NAME, "TCP Server : Exception : Disconnected : " & DirectCast(client.Client.RemoteEndPoint, System.Net.IPEndPoint).Address.ToString() & " : " & ex.Message)
                                    Console.WriteLine("TCP Server : Exception : : Disconnected : " & DirectCast(client.Client.RemoteEndPoint, System.Net.IPEndPoint).Address.ToString() & ex.Message)
                                End Try
                            End Sub
                        Here is an example of the full code that will compile and run the server in VB.net

                        Code:
                        Imports System.Net
                        Imports System.Net.Sockets
                        Imports System.Text
                        
                        Module MainModule
                        
                            Dim TCPserver As TcpListener
                            Dim listOfClients As New List(Of TcpClient)
                        
                        
                            Sub Main()
                        
                                Try
                        
                                    Dim TCP_ServerIP As String = "192.168.0.20"
                                    Dim TCPPort As Integer = 5656
                        
                                    TCPserver = New TcpListener(IPAddress.Parse(TCP_ServerIP), TCPPort)
                                    TCPserver.Start()
                        
                                    Threading.ThreadPool.QueueUserWorkItem(AddressOf NewTCPClient)
                        
                                Catch ex As Exception
                                    Console.WriteLine(ex)
                                End Try
                            End Sub
                        
                            Private Sub NewTCPClient(state As Object)
                                Dim client As TcpClient = TCPserver.AcceptTcpClient()
                        
                                Try
                                    ' Add the client to the list of clients
                                    listOfClients.Add(client)
                                    Threading.ThreadPool.QueueUserWorkItem(AddressOf NewTCPClient)
                        
                                    While True
                                        Dim ns As NetworkStream = client.GetStream()
                                        Dim toReceive(100000) As Byte
                        
                                        Dim length As Integer = ns.Read(toReceive, 0, toReceive.Length)
                        
                                        Dim TCPReceivedText As String = Encoding.ASCII.GetString(toReceive, 0, length)
                        
                                        ' If we got a message (>0 characters) then do something with it
                                        If TCPReceivedText.Length > 0 Then
                                            ' Write it out to the console
                                            Console.WriteLine(TCPReceivedText)
                                            ' Relay the message to any connected clients.
                                            ' This is just a demo, you are unlikely to want to do this in this case.
                                            For Each c As TcpClient In listOfClients
                                                If c IsNot client Then
                                                    Dim nns As NetworkStream = c.GetStream()
                                                    nns.Write(Encoding.ASCII.GetBytes(TCPReceivedText), 0, TCPReceivedText.Length)
                                                End If
                                            Next
                                            '' Send message back to the client that sent the data
                                            Dim toSend() As Byte = Encoding.ASCII.GetBytes("Message Received...")
                                            ns.Write(toSend, 0, toSend.Length)
                                        End If
                                    End While
                        
                                Catch ex As Exception
                                    ' Remove the client that had the exception from the list of clients
                                    If listOfClients.Contains(client) Then
                                        listOfClients.Remove(client)
                                    End If
                                    hs.WriteLog(IFACE_NAME, ex.Message)
                                End Try
                            End Sub
                        End Module
                        Last edited by davros; May 22nd, 2017, 09:43 PM.

                        Comment

                        Working...
                        X