@ -21,95 +21,94 @@ public class TelemetryDeviceInfoEnricher : ITelemetryActivityDataEnricher, ISing
{
{
private readonly ITelemetryActivityStorage _ telemetryActivityStorage ;
private readonly ITelemetryActivityStorage _ telemetryActivityStorage ;
private readonly ISoftwareInfoProvider _ softwareInfoProvider ;
private readonly ISoftwareInfoProvider _ softwareInfoProvider ;
private readonly Lazy < Guid > _d eviceId ;
private readonly Lazy < Guid ? > _d eviceId ;
public TelemetryDeviceInfoEnricher ( ITelemetryActivityStorage telemetryActivityStorage , ISoftwareInfoProvider softwareInfoProvider )
public TelemetryDeviceInfoEnricher ( ITelemetryActivityStorage telemetryActivityStorage , ISoftwareInfoProvider softwareInfoProvider )
{
{
_ telemetryActivityStorage = telemetryActivityStorage ;
_ telemetryActivityStorage = telemetryActivityStorage ;
_ softwareInfoProvider = softwareInfoProvider ;
_ softwareInfoProvider = softwareInfoProvider ;
_d eviceId = new Lazy < Guid > ( ( ) = >
_d eviceId = new Lazy < Guid ? > ( GetDeviceId ) ;
{
var deviceIdText = File . ReadAllText ( TelemetryPaths . ComputerId ) ;
return deviceIdText . To < Guid > ( ) ;
} ) ;
}
public async Task < bool > ShouldEnrichAsync ( ActivityData activity )
{
return await _ telemetryActivityStorage . ShouldAddDeviceInfoAsync ( ) ;
}
}
public async Task EnrichAsync ( ActivityData activity )
public async Task EnrichAsync ( ActivityData activity )
{
{
if ( _d eviceId . Value . HasValue )
activity [ ActivityPropertyNames . DeviceId ] = _d eviceId . Value ;
{
activity [ ActivityPropertyNames . DeviceId ] = _d eviceId . Value . Value ;
}
if ( ! await _ telemetryActivityStorage . ShouldAddDeviceInfoAsync ( ) )
if ( ! await _ telemetryActivityStorage . ShouldAddDeviceInfoAsync ( ) )
{
{
return ;
return ;
}
}
EnrichWithDeviceInfo ( activity ) ;
await EnrichWithSoftwareInfoAsync ( activity ) ;
await _ telemetryActivityStorage . MarkDeviceInfoAsAddedAsync ( ) ;
}
private void EnrichWithDeviceInfo ( ActivityData activity )
{
activity [ ActivityPropertyNames . DeviceType ] = GetDeviceType ( ) ;
activity [ ActivityPropertyNames . DeviceType ] = GetDeviceType ( ) ;
activity [ ActivityPropertyNames . DeviceLanguage ] = GetLanguage ( ) ;
activity [ ActivityPropertyNames . DeviceLanguage ] = GetLanguage ( ) ;
activity [ ActivityPropertyNames . OperatingSystem ] = GetOperatingSystem ( ) ;
activity [ ActivityPropertyNames . OperatingSystem ] = GetOperatingSystem ( ) ;
activity [ ActivityPropertyNames . CountryIsoCode ] = GetCountry ( ) ;
activity [ ActivityPropertyNames . CountryIsoCode ] = GetCountry ( ) ;
}
private async Task EnrichWithSoftwareInfoAsync ( ActivityData activity )
{
var softwareList = await _ softwareInfoProvider . GetSoftwareInfoAsync ( ) ;
var softwareList = await _ softwareInfoProvider . GetSoftwareInfoAsync ( ) ;
activity [ ActivityPropertyNames . InstalledSoftwares ] = softwareList ;
activity [ ActivityPropertyNames . InstalledSoftwares ] = softwareList ;
}
await _ telemetryActivityStorage . MarkDeviceInfoAsAddedAsync ( ) ;
private static Guid ? GetDeviceId ( )
{
if ( File . Exists ( TelemetryPaths . ComputerId ) )
{
var deviceIdText = File . ReadAllText ( TelemetryPaths . ComputerId ) ;
return deviceIdText . To < Guid > ( ) ;
}
return null ;
}
}
public OperationSystem GetOperatingSystem ( )
private stat ic OperationSystem GetOperatingSystem ( )
{
{
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
{
return OperationSystem . Windows ;
return OperationSystem . Windows ;
}
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
{
return OperationSystem . Linux ;
return OperationSystem . Linux ;
}
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
{
return OperationSystem . MacOS ;
return OperationSystem . MacOS ;
}
return OperationSystem . Unknown ;
return OperationSystem . Unknown ;
}
}
publ ic DeviceType GetDeviceType ( )
private stat ic DeviceType GetDeviceType ( )
{
{
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
{
return DetectDeviceTypeOnWindows ( ) ;
return DetectDeviceTypeOnWindows ( ) ;
}
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
{
return DetectDeviceTypeOnLinux ( ) ;
return DetectDeviceTypeOnLinux ( ) ;
}
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
{
return DetectDeviceTypeOnMacOS ( ) ;
return DetectDeviceTypeOnMacOS ( ) ;
}
if ( RuntimeInformation . OSDescription . ToLower ( ) . Contains ( "ios" ) )
if ( RuntimeInformation . OSDescription . ToLower ( ) . Contains ( "ios" ) )
{
return DeviceType . Laptop ;
return DeviceType . Laptop ;
}
return DeviceType . Unknown ;
return DeviceType . Unknown ;
}
}
publ ic string GetLanguage ( )
private stat ic string GetLanguage ( )
{
{
return CultureInfo . CurrentUICulture . Name ;
return CultureInfo . CurrentUICulture . Name ;
}
}
publ ic string GetCountry ( )
private stat ic string GetCountry ( )
{
{
var culture = CultureInfo . CurrentUICulture ;
var culture = CultureInfo . CurrentUICulture ;
if ( culture . IsNeutralCulture )
if ( culture . IsNeutralCulture )
@ -121,33 +120,40 @@ public class TelemetryDeviceInfoEnricher : ITelemetryActivityDataEnricher, ISing
return region . TwoLetterISORegionName ;
return region . TwoLetterISORegionName ;
}
}
private DeviceType DetectDeviceTypeOnWindows ( )
private static DeviceType DetectDeviceTypeOnWindows ( )
{
{
#if WINDOWS
try
try
{
{
using var searcher = new ManagementObjectSearcher ( "SELECT * FROM Win32_SystemEnclosure" ) ;
using var searcher = new ManagementObjectSearcher ( "SELECT * FROM Win32_SystemEnclosure" ) ;
foreach ( var obj in searcher . Get ( ) )
foreach ( var obj in searcher . Get ( ) )
{
{
var chassisTypes = obj [ "ChassisTypes" ] a s ushort [ ] ;
if ( obj [ "ChassisTypes" ] i s ushort [ ] chassisTypes & &
if ( chassisTypes ! = null & & chassisTypes . Any ( t = > t is 8 or 9 or 1 0 or 1 4 ) )
chassisTypes . Any ( t = > t is 8 or 9 or 1 0 or 1 4 ) )
{
{
return DeviceType . Laptop ;
return DeviceType . Laptop ;
}
}
}
}
}
}
catch { }
catch
{
// ignored
}
#endif
return DeviceType . Desktop ;
return DeviceType . Desktop ;
}
}
private DeviceType DetectDeviceTypeOnLinux ( )
private static DeviceType DetectDeviceTypeOnLinux ( )
{
{
try
try
{
{
var type = File . ReadAllText ( "/sys/class/dmi/id/chassis_type" ) ;
if ( File . Exists ( "/sys/class/dmi/id/chassis_type" ) )
if ( int . TryParse ( type . Trim ( ) , out var code ) & & code is 8 or 9 or 1 0 or 1 4 )
{
{
return DeviceType . Laptop ;
var type = File . ReadAllText ( "/sys/class/dmi/id/chassis_type" ) ;
if ( int . TryParse ( type . Trim ( ) , out var code ) & & code is 8 or 9 or 1 0 or 1 4 )
{
return DeviceType . Laptop ;
}
}
}
}
}
catch
catch
@ -158,11 +164,11 @@ public class TelemetryDeviceInfoEnricher : ITelemetryActivityDataEnricher, ISing
return DeviceType . Desktop ;
return DeviceType . Desktop ;
}
}
private DeviceType DetectDeviceTypeOnMacOS ( )
private static DeviceType DetectDeviceTypeOnMacOS ( )
{
{
try
try
{
{
var process = new Process
using var process = new Process
{
{
StartInfo = new ProcessStartInfo
StartInfo = new ProcessStartInfo
{
{
@ -179,14 +185,10 @@ public class TelemetryDeviceInfoEnricher : ITelemetryActivityDataEnricher, ISing
process . WaitForExit ( ) ;
process . WaitForExit ( ) ;
if ( output . Contains ( "MacBook" ) )
if ( output . Contains ( "MacBook" ) )
{
return DeviceType . Laptop ;
return DeviceType . Laptop ;
}
if ( output . Contains ( "iMac" ) | | output . Contains ( "Mac Pro" ) )
if ( output . Contains ( "iMac" ) | | output . Contains ( "Mac Pro" ) )
{
return DeviceType . Desktop ;
return DeviceType . Desktop ;
}
}
}
catch
catch
{
{
@ -195,6 +197,4 @@ public class TelemetryDeviceInfoEnricher : ITelemetryActivityDataEnricher, ISing
return DeviceType . Unknown ;
return DeviceType . Unknown ;
}
}
}
}