public void Connect(string serverAddress, int serverPort) { hsClient = ScsServiceClientBuilder.CreateClient <IHSApplication>( new ScsTcpEndPoint(serverAddress, serverPort), this); hsClient.Connect(); hs = hsClient.ServiceProxy; Program.HsClient = hs; // make sure we're connected successfully double apiVersion = hs.APIVersion; callbackClient = ScsServiceClientBuilder.CreateClient <IAppCallbackAPI>(new ScsTcpEndPoint(serverAddress, serverPort), this); callbackClient.Connect(); callbacks = callbackClient.ServiceProxy; apiVersion = callbacks.APIVersion; hs.Connect(this.Name, this.InstanceFriendlyName()); }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Connects to Homeseer at the specified server address and port. /// This is called by the console wrapper. /// </summary> /// <param name="serverAddress">The server address.</param> /// <param name="serverPort">The server port.</param> /// <exception cref="Exception"> Error connecting homeseer SCS client </exception> /// <exception cref="Exception"> Error connecting callback SCS client </exception> /// <exception cref="Exception"> Error connecting homeseer to our plugin </exception> public virtual void Connect(string serverAddress, int serverPort) { // Create our main connection to the homeseer TCP communication framework // part 1 - hs object Proxy try { hsClient = ScsServiceClientBuilder.CreateClient <IHSApplication>(new ScsTcpEndPoint(serverAddress, serverPort), this); hsClient.Connect(); hs = hsClient.ServiceProxy; double APIVersion = hs.APIVersion; // just to make sure our connection is valid } catch (Exception ex) { throw new Exception("Error connecting homeseer SCS client: " + ex.Message, ex); } // part 2 - callback object Proxy try { callbackClient = ScsServiceClientBuilder.CreateClient <IAppCallbackAPI>(new ScsTcpEndPoint(serverAddress, serverPort), this); callbackClient.Connect(); callback = callbackClient.ServiceProxy; double APIVersion = callback.APIVersion; // just to make sure our connection is valid } catch (Exception ex) { throw new Exception("Error connecting callback SCS client: " + ex.Message, ex); } // Establish the reverse connection from homeseer back to our plugin try { hs.Connect(IFACE_NAME, INSTANCE_NAME); } catch (Exception ex) { throw new Exception("Error connecting homeseer to our plugin: " + ex.Message, ex); } }
private static void connectionJunction() { // create the user object that is the real plugin, accessed from the pluginAPI wrapper Util.callback = callback; Util.hs = host; plugin.OurInstanceFriendlyName = Util.Instance; // connect to HS so it can register a callback to us host.Connect(Util.IFACE_NAME, Util.Instance); Console.WriteLine("Connected, waiting to be initialized..."); do { System.Threading.Thread.Sleep(10); } while (client.CommunicationState == HSCF.Communication.Scs.Communication.CommunicationStates.Connected & !HSPI.bShutDown & !HSPI.timedReset); if (!HSPI.bShutDown) { connectionJunction(); // Attempt to reconnect, the disconnection was not intentional } else { Console.WriteLine("Connection lost, exiting"); // disconnect from server for good here client.Disconnect(); clientCallback.Disconnect(); wait(2); if (HSPI.timedReset) { reset = true; } else { reset = false; } } }
public static void Main(string[] args) { string serverIp = "127.0.0.1"; string serverCmd = null; foreach (string serverCmd_loopVariable in args) { serverCmd = serverCmd_loopVariable; string[] ch = new string[1]; ch[0] = "="; string[] parts = serverCmd.Split(ch, StringSplitOptions.None); switch (parts[0].ToLower()) { case "server": serverIp = parts[1]; break; case "instance": try { Util.Instance = parts[1]; } catch (Exception) { Util.Instance = ""; } break; } } Console.WriteLine("Plugin: " + Util.IFACE_NAME + " Instance: " + Util.Instance + " starting..."); Console.WriteLine("Connecting to server at " + serverIp + "..."); client = ScsServiceClientBuilder.CreateClient <IHSApplication>(new ScsTcpEndPoint(serverIp, 10400), plugin); clientCallback = ScsServiceClientBuilder.CreateClient <IAppCallbackAPI>(new ScsTcpEndPoint(serverIp, 10400), plugin); int Attempts = 1; TryAgain: try { client.Connect(); clientCallback.Connect(); double APIVersion = 0; try { host = client.ServiceProxy; APIVersion = host.APIVersion; // will cause an error if not really connected Console.WriteLine("Host API Version: " + APIVersion.ToString()); } catch (Exception ex) { Console.WriteLine("Error getting API version from host object: " + ex.Message + "->" + ex.StackTrace); //Return } try { callback = clientCallback.ServiceProxy; APIVersion = callback.APIVersion; // will cause an error if not really connected } catch (Exception ex) { Console.WriteLine("Error getting API version from callback object: " + ex.Message + "->" + ex.StackTrace); return; } } catch (Exception ex) { Console.WriteLine("Cannot connect attempt " + Attempts.ToString() + ": " + ex.Message); if (ex.Message.ToLower().Contains("timeout occurred.")) { Attempts += 1; if (Attempts < 6) { goto TryAgain; } } if (client != null) { client.Dispose(); client = null; } if (clientCallback != null) { clientCallback.Dispose(); clientCallback = null; } wait(4); return; } try { // create the user object that is the real plugin, accessed from the pluginAPI wrapper Util.callback = callback; Util.hs = host; plugin.OurInstanceFriendlyName = Util.Instance; // connect to HS so it can register a callback to us host.Connect(Util.IFACE_NAME, Util.Instance); Console.WriteLine("Connected, waiting to be initialized..."); do { System.Threading.Thread.Sleep(10); } while (client.CommunicationState == HSCF.Communication.Scs.Communication.CommunicationStates.Connected & !HSPI.bShutDown); Console.WriteLine("Connection lost, exiting"); // disconnect from server for good here client.Disconnect(); clientCallback.Disconnect(); wait(2); System.Environment.Exit(0); } catch (Exception ex) { Console.WriteLine("Cannot connect(2): " + ex.Message); wait(2); System.Environment.Exit(0); return; } }
public static void AddInstance(string InstanceName) { if (AllInstances.ContainsKey(InstanceName)) { return; } else { HSPI plugIn = new HSPI(); string sIp = "127.0.0.1"; // plugIn.OurInstanceFriendlyName = InstanceName; HSCF.Communication.ScsServices.Client.IScsServiceClient <IHSApplication> client = ScsServiceClientBuilder.CreateClient <IHSApplication>(new ScsTcpEndPoint(sIp, 10400), plugIn); HSCF.Communication.ScsServices.Client.IScsServiceClient <IAppCallbackAPI> clientCallback = ScsServiceClientBuilder.CreateClient <IAppCallbackAPI>(new ScsTcpEndPoint(sIp, 10400), plugIn); HomeSeerAPI.IHSApplication host = null; IAppCallbackAPI callback; int Attempts = 1; TryAgain: try { client.Connect(); clientCallback.Connect(); double APIVersion = 0; try { host = client.ServiceProxy; APIVersion = host.APIVersion; // will cause an error if not really connected Console.WriteLine("Host API Version: " + APIVersion.ToString()); } catch (Exception ex) { Console.WriteLine("Error getting API version from host object: " + ex.Message + "->" + ex.StackTrace); //Return } try { callback = clientCallback.ServiceProxy; APIVersion = callback.APIVersion; // will cause an error if not really connected } catch (Exception ex) { Console.WriteLine("Error getting API version from callback object: " + ex.Message + "->" + ex.StackTrace); return; } } catch (Exception ex) { Console.WriteLine("Cannot connect attempt " + Attempts.ToString() + ": " + ex.Message); if (ex.Message.ToLower().Contains("timeout occurred.")) { Attempts += 1; if (Attempts < 6) { goto TryAgain; } } if (client != null) { client.Dispose(); client = null; } if (clientCallback != null) { clientCallback.Dispose(); clientCallback = null; } wait(4); return; } try { AllInstances[InstanceName] = new InstanceHolder(plugIn, client, callback, host, InstanceName); host.Connect(Util.IFACE_NAME, InstanceName); Console.WriteLine("Connected, waiting to be initialized..."); // plugIn.OurInstanceFriendlyName = InstanceName; // create the user object that is the real plugIn, accessed from the plugInAPI wrapper // AllInstances[InstanceFriendlyName].callback = callback; // Util.hs = host; // plugIn.OurInstanceFriendlyName = Util.Instance; // connect to HS so it can register a callback to us // host.Connect(Util.IFACE_NAME, Util.Instance); int Count = 0; do { System.Threading.Thread.Sleep(1000); /* Count = (Count + 1) % (60*5);//save every 5 minutes, not every minute * if (Count == 0) * { * foreach (var Instance in AllInstances) * { * Instance.Value.hspi.Log("Saving devices for Instance "+Instance.Key, 0); * Instance.Value.host.SaveEventsDevices(); //THis function doesn't save plugin created devices, so not necessary to call * } * }*/ } while (client.CommunicationState == HSCF.Communication.Scs.Communication.CommunicationStates.Connected & !HSPI.bShutDown); Console.WriteLine("Connection lost, exiting"); // disconnect from server for good here // // clientCallback.Disconnect(); //wait(2); if (AllInstances.Count == 0) { client.Disconnect(); wait(2); System.Environment.Exit(0); } } catch (Exception ex) { Console.WriteLine("Cannot connect(2): " + ex.Message); wait(2); System.Environment.Exit(0); return; } } }