public InstanceHolder(HSPI HSPI, HSCF.Communication.ScsServices.Client.IScsServiceClient <IHSApplication> Client, HomeSeerAPI.IAppCallbackAPI ClientCallback, HomeSeerAPI.IHSApplication Host, string Name) { name = Name; hspi = HSPI; hspi.OurInstanceFriendlyName = name; ajaxName = name; if (name != "") { ajaxName = ":" + name; } client = Client; callback = ClientCallback; host = Host; Devices = GetDevices(this); modPage = new ModbusDevicePage("ModbusDevicePage", this); scrPage = new ScratchpadDevicePage("ScratchpadPage", this); modAjax = new MosbusAjaxReceivers(this); bacnetDevices = new BACnetDevices(BACnetDevices.BaseUrl, this); bacnetDataService = new BACnetDataService(BACnetDataService.BaseUrl, this); bacnetHomeSeerDevices = new BACnetHomeSeerDevices(BACnetHomeSeerDevices.BaseUrl, this); siidPage = new SIID_Page(Util.IFACE_NAME, this); //bacnetDevices = new BACnetDevices(BACnetDataService.BaseUrl + this.ajaxName.Replace(":", "_"), this); //bacnetDataService = new BACnetDataService(BACnetDataService.BaseUrl + this.ajaxName.Replace(":", "_"), this); //bacnetHomeSeerDevices = new BACnetHomeSeerDevices(BACnetHomeSeerDevices.BaseUrl + this.ajaxName.Replace(":", "_"), this); //CPU use is high, so try to minimize iterating through devices //Also minimize calls to and from the device's plugin extra data store. Keep parallel copy, maybe only update when change }
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; } } }