Пример #1
0
        public Device(ILog log, Model.Configuration config, Model.TelemetryType telemetryType)
        {
            this.log           = log;
            this.config        = config;
            this.telemetryType = telemetryType;
            this.version       = Assembly.GetEntryAssembly().GetName().Version.ToString();

            // Setup App Insights
            if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY")))
            {
                insights             = true;
                this.telemetryClient = new TelemetryClient();
                this.telemetryClient.Context.Device.Id = Environment.GetEnvironmentVariable("DEVICE");
                this.telemetryClient.TrackEvent("IoTDeviceSimulator started");
                this.telemetryClient.GetMetric("SimulatorCount").TrackValue(1);
            }


            // Setup the Device Protocol
            switch (config.Protocol)
            {
            case TransportType.Mqtt:
                log.Info("Protocol: MQTT");
                break;

            case TransportType.Amqp:
                log.Info("Protocol: AMQP");
                break;

            default:
                log.Info("Protocol: MQTT");
                break;
            }

            // Determine if Symmetric Key or x509
            if (!string.IsNullOrEmpty(config.ConnectionString) && config.ConnectionString.Contains("509"))
            {
                is509 = true;
            }

            if (is509)
            {
                log.Info("Authentication: x509");
            }
            else
            {
                log.Info("Authentication: SymmetricKey");
            }

            // Determine if Down Stream Device
            if (!string.IsNullOrEmpty(config.ConnectionString) && !string.IsNullOrEmpty(config.EdgeHost))
            {
                config.ConnectionString = config.ConnectionString + ";GatewayHostName=" + config.EdgeHost;
                isLeaf = true;
                log.Info("Gateway: " + config.EdgeHost);
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            // Setup the logger
            var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());

            XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));

            // Setup the configuration File
            var config = new Model.Configuration();

            var directory = Directory.GetCurrentDirectory();

            if (directory.Contains("bin"))
            {
                // Visual Studio Path Hack
                directory = Directory.GetParent(directory).Parent.Parent.FullName;
            }

            var device_cert = Path.Combine(directory, "cert/device.pfx");

            if (File.Exists(device_cert))
            {
                config.CertPath = device_cert;
            }

            var root_ca = Path.Combine(directory, "cert/root-ca.pem");

            if (File.Exists(root_ca) && !String.IsNullOrEmpty(config.EdgeHost))
            {
                var store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
                try
                {
                    // The following line will work on Windows and on Linux but not on Mac OSX due to permissions of KeyChain
                    store.Open(OpenFlags.ReadWrite);
                    store.Add(new X509Certificate2(X509Certificate.CreateFromCertFile(root_ca)));
                    // store.Add(new X509Certificate2(root_ca, "password", X509KeyStorageFlags.Exportable));  // This would be the way to add on Mac
                }
                finally
                {
                    store.Close();
                }
            }

            var device = new Device(Log, config, Model.TelemetryType.CLIMATE);

            device.Start().GetAwaiter().GetResult();
        }