static void TurnEverythingOff(string serviceNamespace, string gatewayId) { ChannelFactory <IEnergyServiceGatewayOperationsChannel> netTcpRelayChannelFactory = null; IEnergyServiceGatewayOperationsChannel netTcpRelayChannel = null; try { Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, ServiceBusHelper.GetGatewayServicePath(gatewayId)); //For WS2207HttpRelayBinding // Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("http", serviceNamespace, ServiceBusHelper.GetGatewayServicePath(gatewayId)); netTcpRelayChannelFactory = new ChannelFactory <IEnergyServiceGatewayOperationsChannel>("RelayTcpEndpoint", new EndpointAddress(serviceUri)); netTcpRelayChannel = netTcpRelayChannelFactory.CreateChannel(); netTcpRelayChannel.Open(); Console.WriteLine("Connected to " + serviceUri.ToString()); Console.WriteLine("Light switch is:" + netTcpRelayChannel.GetLightingValue(gatewayId, "Lighting-1")); netTcpRelayChannel.SetLightingValue(gatewayId, "Lighting-1", 0); Console.WriteLine("Light switch turned OFF"); Console.WriteLine("Current Temperature:" + netTcpRelayChannel.GetCurrentTemp(gatewayId, "HVAC-1")); Console.WriteLine("Current Set Point:" + netTcpRelayChannel.GetHVACSetpoint(gatewayId, "HVAC-1")); Console.WriteLine("Current HVAC Mode:" + netTcpRelayChannel.GetHVACMode(gatewayId, "HVAC-1")); netTcpRelayChannel.SetHVACMode(gatewayId, "HVAC-1", 0); netTcpRelayChannel.SetHVACSetpoint(gatewayId, "HVAC-1", 78); Console.WriteLine("Set HVAC mode to OFF"); Console.WriteLine("Set everything to off on " + gatewayId); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if (netTcpRelayChannel != null && netTcpRelayChannelFactory != null) { netTcpRelayChannel.Close(); netTcpRelayChannelFactory.Close(); } } }
private void InitNetTcpRelayServer() { try { string issuerName = tsSolutionName.Text; string issuerKey = tsSolutionPassword.Text; string serviceNamespaceDomain = tsSolutionToConnect.Text; TransportClientEndpointBehavior behavior = new TransportClientEndpointBehavior(); behavior.TokenProvider = SharedSecretTokenProvider.CreateSharedSecretTokenProvider(issuerName, Encoding.ASCII.GetBytes(issuerKey)); Uri address = ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespaceDomain, ServiceBusHelper.GetGatewayServicePath(tsGatewayId.Text)); //For WS2207HttpRelayBinding // Uri address = ServiceBusEnvironment.CreateServiceUri("http", serviceNamespaceDomain, ServiceBusHelper.GetGatewayServicePath(tsGatewayId.Text)); serviceUri = address.ToString(); server = new ServiceHost(this, address); server.Description.Endpoints[0].Behaviors.Add(behavior); ServiceRegistrySettings settings = new ServiceRegistrySettings(); settings.DiscoveryMode = DiscoveryType.Public; settings.DisplayName = address.ToString(); foreach (ServiceEndpoint se in server.Description.Endpoints) { se.Behaviors.Add(settings); } server.Open(); AddLog("Gateway Server Running with ServiceUri:" + address.ToString()); AddLog("Service registered for public discovery."); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }