├── .gitignore ├── AspNetMonsters.Blazor.Sensors.sln ├── AspNetMonsters.Blazor.Sensors ├── AmbientLightSensor.cs ├── AmbientLightSensorService.cs ├── AspNetMonsters.Blazor.Sensors.csproj ├── content │ ├── Sensors.js │ └── Sensors.js.map ├── package-lock.json └── package.json ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # MSTest test Results 33 | [Tt]est[Rr]esult*/ 34 | [Bb]uild[Ll]og.* 35 | 36 | # NUNIT 37 | *.VisualState.xml 38 | TestResult.xml 39 | 40 | # Build Results of an ATL Project 41 | [Dd]ebugPS/ 42 | [Rr]eleasePS/ 43 | dlldata.c 44 | 45 | # .NET Core 46 | project.lock.json 47 | project.fragment.lock.json 48 | artifacts/ 49 | **/Properties/launchSettings.json 50 | 51 | *_i.c 52 | *_p.c 53 | *_i.h 54 | *.ilk 55 | *.meta 56 | *.obj 57 | *.pch 58 | *.pdb 59 | *.pgc 60 | *.pgd 61 | *.rsp 62 | *.sbr 63 | *.tlb 64 | *.tli 65 | *.tlh 66 | *.tmp 67 | *.tmp_proj 68 | *.log 69 | *.vspscc 70 | *.vssscc 71 | .builds 72 | *.pidb 73 | *.svclog 74 | *.scc 75 | 76 | # Chutzpah Test files 77 | _Chutzpah* 78 | 79 | # Visual C++ cache files 80 | ipch/ 81 | *.aps 82 | *.ncb 83 | *.opendb 84 | *.opensdf 85 | *.sdf 86 | *.cachefile 87 | *.VC.db 88 | *.VC.VC.opendb 89 | 90 | # Visual Studio profiler 91 | *.psess 92 | *.vsp 93 | *.vspx 94 | *.sap 95 | 96 | # TFS 2012 Local Workspace 97 | $tf/ 98 | 99 | # Guidance Automation Toolkit 100 | *.gpState 101 | 102 | # ReSharper is a .NET coding add-in 103 | _ReSharper*/ 104 | *.[Rr]e[Ss]harper 105 | *.DotSettings.user 106 | 107 | # JustCode is a .NET coding add-in 108 | .JustCode 109 | 110 | # TeamCity is a build add-in 111 | _TeamCity* 112 | 113 | # DotCover is a Code Coverage Tool 114 | *.dotCover 115 | 116 | # Visual Studio code coverage results 117 | *.coverage 118 | *.coveragexml 119 | 120 | # NCrunch 121 | _NCrunch_* 122 | .*crunch*.local.xml 123 | nCrunchTemp_* 124 | 125 | # MightyMoose 126 | *.mm.* 127 | AutoTest.Net/ 128 | 129 | # Web workbench (sass) 130 | .sass-cache/ 131 | 132 | # Installshield output folder 133 | [Ee]xpress/ 134 | 135 | # DocProject is a documentation generator add-in 136 | DocProject/buildhelp/ 137 | DocProject/Help/*.HxT 138 | DocProject/Help/*.HxC 139 | DocProject/Help/*.hhc 140 | DocProject/Help/*.hhk 141 | DocProject/Help/*.hhp 142 | DocProject/Help/Html2 143 | DocProject/Help/html 144 | 145 | # Click-Once directory 146 | publish/ 147 | 148 | # Publish Web Output 149 | *.[Pp]ublish.xml 150 | *.azurePubxml 151 | # TODO: Comment the next line if you want to checkin your web deploy settings 152 | # but database connection strings (with potential passwords) will be unencrypted 153 | *.pubxml 154 | *.publishproj 155 | 156 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 157 | # checkin your Azure Web App publish settings, but sensitive information contained 158 | # in these scripts will be unencrypted 159 | PublishScripts/ 160 | 161 | # NuGet Packages 162 | *.nupkg 163 | # The packages folder can be ignored because of Package Restore 164 | **/packages/* 165 | # except build/, which is used as an MSBuild target. 166 | !**/packages/build/ 167 | # Uncomment if necessary however generally it will be regenerated when needed 168 | #!**/packages/repositories.config 169 | # NuGet v3's project.json files produces more ignorable files 170 | *.nuget.props 171 | *.nuget.targets 172 | 173 | # Microsoft Azure Build Output 174 | csx/ 175 | *.build.csdef 176 | 177 | # Microsoft Azure Emulator 178 | ecf/ 179 | rcf/ 180 | 181 | # Windows Store app package directories and files 182 | AppPackages/ 183 | BundleArtifacts/ 184 | Package.StoreAssociation.xml 185 | _pkginfo.txt 186 | 187 | # Visual Studio cache files 188 | # files ending in .cache can be ignored 189 | *.[Cc]ache 190 | # but keep track of directories ending in .cache 191 | !*.[Cc]ache/ 192 | 193 | # Others 194 | ClientBin/ 195 | ~$* 196 | *~ 197 | *.dbmdl 198 | *.dbproj.schemaview 199 | *.jfm 200 | *.pfx 201 | *.publishsettings 202 | orleans.codegen.cs 203 | 204 | # Since there are multiple workflows, uncomment next line to ignore bower_components 205 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 206 | #bower_components/ 207 | 208 | # RIA/Silverlight projects 209 | Generated_Code/ 210 | 211 | # Backup & report files from converting an old project file 212 | # to a newer Visual Studio version. Backup files are not needed, 213 | # because we have git ;-) 214 | _UpgradeReport_Files/ 215 | Backup*/ 216 | UpgradeLog*.XML 217 | UpgradeLog*.htm 218 | 219 | # SQL Server files 220 | *.mdf 221 | *.ldf 222 | *.ndf 223 | 224 | # Business Intelligence projects 225 | *.rdl.data 226 | *.bim.layout 227 | *.bim_*.settings 228 | 229 | # Microsoft Fakes 230 | FakesAssemblies/ 231 | 232 | # GhostDoc plugin setting file 233 | *.GhostDoc.xml 234 | 235 | # Node.js Tools for Visual Studio 236 | .ntvs_analysis.dat 237 | node_modules/ 238 | 239 | # Typescript v1 declaration files 240 | typings/ 241 | 242 | # Visual Studio 6 build log 243 | *.plg 244 | 245 | # Visual Studio 6 workspace options file 246 | *.opt 247 | 248 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 249 | *.vbw 250 | 251 | # Visual Studio LightSwitch build output 252 | **/*.HTMLClient/GeneratedArtifacts 253 | **/*.DesktopClient/GeneratedArtifacts 254 | **/*.DesktopClient/ModelManifest.xml 255 | **/*.Server/GeneratedArtifacts 256 | **/*.Server/ModelManifest.xml 257 | _Pvt_Extensions 258 | 259 | # Paket dependency manager 260 | .paket/paket.exe 261 | paket-files/ 262 | 263 | # FAKE - F# Make 264 | .fake/ 265 | 266 | # JetBrains Rider 267 | .idea/ 268 | *.sln.iml 269 | 270 | # CodeRush 271 | .cr/ 272 | 273 | # Python Tools for Visual Studio (PTVS) 274 | __pycache__/ 275 | *.pyc 276 | 277 | # Cake - Uncomment if you are using it 278 | # tools/** 279 | # !tools/packages.config 280 | 281 | # Telerik's JustMock configuration file 282 | *.jmconfig 283 | 284 | # BizTalk build output 285 | *.btp.cs 286 | *.btm.cs 287 | *.odx.cs 288 | *.xsd.cs 289 | -------------------------------------------------------------------------------- /AspNetMonsters.Blazor.Sensors.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27428.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetMonsters.Blazor.Sensors", "AspNetMonsters.Blazor.Sensors\AspNetMonsters.Blazor.Sensors.csproj", "{E0DEC7CC-B1B4-4249-BD4A-6C45563FAD43}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {E0DEC7CC-B1B4-4249-BD4A-6C45563FAD43}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {E0DEC7CC-B1B4-4249-BD4A-6C45563FAD43}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {E0DEC7CC-B1B4-4249-BD4A-6C45563FAD43}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {E0DEC7CC-B1B4-4249-BD4A-6C45563FAD43}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {356205F6-DA25-420F-8C7A-A4D9C8973C6B} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /AspNetMonsters.Blazor.Sensors/AmbientLightSensor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspNetMonsters.Blazor.Sensors 4 | { 5 | public class AmbientLightSensor 6 | { 7 | private readonly Guid id; 8 | public AmbientLightSensor(Guid id) 9 | { 10 | this.id = id; 11 | } 12 | public Guid Id { get; } 13 | 14 | public bool Activated { get; set; } 15 | public bool HasReading { get; set; } 16 | public decimal Illuminance { get; set; } 17 | 18 | public Action OnReading { get; set; } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /AspNetMonsters.Blazor.Sensors/AmbientLightSensorService.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.JSInterop; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace AspNetMonsters.Blazor.Sensors 9 | { 10 | public class AmbientLightSensorService 11 | { 12 | static IDictionary _activeSensors = new Dictionary(); 13 | public async Task StartReading(Action onReading) 14 | { 15 | var requestId = Guid.NewGuid(); 16 | AmbientLightSensor sensor = new AmbientLightSensor(requestId); 17 | sensor.OnReading = onReading; 18 | _activeSensors.Add(requestId, sensor); 19 | await JSRuntime.Current.InvokeAsync( 20 | "AspNetMonsters_Blazor_Sensors.startAmbientLightSensor", 21 | new DotNetObjectRef(this), 22 | requestId); 23 | return sensor; 24 | } 25 | 26 | public async Task StopReading(AmbientLightSensor sensor) 27 | { 28 | await JSRuntime.Current.InvokeAsync( 29 | "AspNetMonsters_Blazor_Sensors.stopAmbientLightSensor", 30 | sensor.Id); 31 | } 32 | 33 | [JSInvokable] 34 | public void ReceiveResponse( 35 | string id, 36 | string hasReading, 37 | string activated, 38 | string illuminance) 39 | { 40 | AmbientLightSensor sensor; 41 | var idVal = Guid.Parse(id); 42 | sensor = _activeSensors.First(x => x.Key == idVal).Value; 43 | sensor.HasReading = Convert.ToBoolean(hasReading); 44 | sensor.Activated = Convert.ToBoolean(activated); 45 | sensor.Illuminance = Convert.ToDecimal(illuminance); 46 | sensor.OnReading(); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /AspNetMonsters.Blazor.Sensors/AspNetMonsters.Blazor.Sensors.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netstandard2.0 5 | Library 6 | true 7 | false 8 | 7.3 9 | 10 | https://github.com/AspNetMonsters/Blazor.Sensors 11 | https://github.com/AspNetMonsters/Blazor.Sensors/blob/master/LICENSE 12 | Simon Timms, David Paquette, James Chambers 13 | Blazor interop for browser Sensor API 14 | https://github.com/AspNetMonsters/Blazor.Sensors 15 | Blazor Sensors AmbientLightSensor 16 | true 17 | AspNetMonsters.Blazor.Sensors 18 | AspNetMonsters.Blazor.Sensors 19 | 0.6.0.1010 20 | 21 | ${DefaultItemExcludes};node_modules\** 22 | 23 | Git 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /AspNetMonsters.Blazor.Sensors/content/Sensors.js: -------------------------------------------------------------------------------- 1 | var AspNetMonsters_Blazor_Sensors = 2 | function () { 3 | 4 | "use strict"; 5 | var sensors = {}; 6 | 7 | 8 | function dispatchResponse(dotnetHelper, id, hasReading, activated, illuminance) { 9 | dotnetHelper.invokeMethodAsync('ReceiveResponse', id, hasReading.toString(), activated.toString(), illuminance.toString()) 10 | .then(r => console.log(r)); 11 | } 12 | 13 | return { 14 | startAmbientLightSensor: function (dotnetHelper, requestId) { 15 | if ('AmbientLightSensor' in window) { 16 | var sensor = new AmbientLightSensor(); 17 | sensors[requestId] = sensor; 18 | sensor.onreading = function () { 19 | dispatchResponse(dotnetHelper, requestId, sensor.hasReading, sensor.activated, sensor.illuminance); 20 | }; 21 | sensor.onerror = function (event) { 22 | console.log(event.error.name, event.error.message); 23 | }; 24 | sensor.start(); 25 | } 26 | else { 27 | return "No ambient light sensor available"; 28 | } 29 | }, 30 | stopAmbientLightSensor: function (requestId) { 31 | if ('AmbientLightSensor' in window) { 32 | if (sensors[requestId]) { 33 | sensors[requestId].stop(); 34 | delete sensors[requestId]; 35 | } 36 | } 37 | } 38 | }; 39 | }(); 40 | -------------------------------------------------------------------------------- /AspNetMonsters.Blazor.Sensors/content/Sensors.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"Sensors.js","sourceRoot":"","sources":["../src/Sensors.ts"],"names":[],"mappings":";AAEA,IAAI,gBAAgB,GAA2D,MAAM,CAAC,gBAAgB,CAAC;AACvG,IAAI,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC;AAC/B,IAAI,kBAAuB,CAAC;AAC5B,IAAI,OAAO,GAAW,EAAE,CAAC;AAEzB,gBAAgB,CAAC,+CAA+C,EAAE,UAAC,SAAS;IACxE,EAAE,CAAC,CAAC,oBAAoB,IAAI,MAAM,CAAC,CAAC,CAAC;QACjC,IAAI,MAAM,GAAG,IAAI,kBAAkB,EAAE,CAAC;QACtC,OAAO,CAAC,SAAS,CAAC,GAAG,MAAM,CAAC;QAC5B,MAAM,CAAC,SAAS,GAAG;YACf,gBAAgB,CAAC,SAAS,EAAE,MAAM,CAAC,UAAU,EACzC,MAAM,CAAC,SAAS,EAChB,MAAM,CAAC,WAAW,CAAC,CAAC;QAC5B,CAAC,CAAC;QACF,MAAM,CAAC,OAAO,GAAG,UAAU,KAAK;YAC5B,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QACvD,CAAC,CAAC;QACF,MAAM,CAAC,KAAK,EAAE,CAAC;IACnB,CAAC;IACD,IAAI,CAAC,CAAC;QACF,MAAM,CAAC,mCAAmC,CAAC;IAC/C,CAAC;AACL,CAAC,CAAC,CAAC;AAEH,gBAAgB,CAAC,8CAA8C,EAAE,UAAC,SAAS;IACvE,EAAE,CAAC,CAAC,oBAAoB,IAAI,MAAM,CAAC,CAAC,CAAC;QACjC,EAAE,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;YACrB,OAAO,CAAC,SAAS,CAAC,CAAC,IAAI,EAAE,CAAC;YAC1B,OAAO,OAAO,CAAC,SAAS,CAAC,CAAC;QAC9B,CAAC;IACL,CAAC;AACL,CAAC,CAAC,CAAC;AAGH,IAAI,YAAY,GAAG,+BAA+B,CAAC;AACnD,IAAI,SAAS,GAAG,+BAA+B,CAAC;AAChD,IAAI,IAAI,GAAG,2BAA2B,CAAC;AACvC,0BAA0B,EAAU,EAAE,UAAU,EAAE,SAAS,EAAE,WAAW;IACpE,IAAI,qBAAqB,GAAG,QAAQ,CAAC,UAAU,CAC3C,YAAY,EACZ,SAAS,EACT,IAAI,EACJ,iBAAiB,CACpB,CAAC;IAGF,QAAQ,CAAC,UAAU,CAAC,qBAAqB,EAAE,IAAI,EAAE;QAC7C,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC;QAC3B,QAAQ,CAAC,cAAc,CAAC,UAAU,CAAC,QAAQ,EAAE,CAAC;QAC9C,QAAQ,CAAC,cAAc,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC;QAC7C,QAAQ,CAAC,cAAc,CAAC,WAAW,CAAC,QAAQ,EAAE,CAAC;KAClD,CAAC,CAAC;AACP,CAAC"} -------------------------------------------------------------------------------- /AspNetMonsters.Blazor.Sensors/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aspnetmonsters-blazorsensors", 3 | "version": "0.3.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "@types/emscripten": { 8 | "version": "0.0.31", 9 | "resolved": "https://registry.npmjs.org/@types/emscripten/-/emscripten-0.0.31.tgz", 10 | "integrity": "sha1-FggX0TJOi3BJYE05rEfYXu7t1Zc=", 11 | "dev": true, 12 | "requires": { 13 | "@types/webassembly-js-api": "0.0.1" 14 | } 15 | }, 16 | "@types/webassembly-js-api": { 17 | "version": "0.0.1", 18 | "resolved": "https://registry.npmjs.org/@types/webassembly-js-api/-/webassembly-js-api-0.0.1.tgz", 19 | "integrity": "sha1-YtULIBB319TMEJuxytoi/f1FI/s=", 20 | "dev": true 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /AspNetMonsters.Blazor.Sensors/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aspnetmonsters-blazorsensors", 3 | "version": "0.3.0", 4 | "description": "", 5 | "devDependencies": { 6 | "@types/emscripten": "0.0.31" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 ASP.NET Monsters 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AspNetMonsters.Blazor.Sensors 2 | This package provides Blazor applications with access to the browser sensor apis. Currently only the [AmbientLightSensor api](https://developer.mozilla.org/en-US/docs/Web/API/AmbientLightSensor) is supported. 3 | 4 | ## Usage 5 | 1) In your Blazor app, add the `AspNetMonsters.Blazor.Sensors` [NuGet package](https://www.nuget.org/packages/AspNetMonsters.Blazor.Sensors/) 6 | 7 | ``` 8 | Install-Package AspNetMonsters.Blazor.Sensors -IncludePrerelease 9 | ``` 10 | 11 | 1) In your Blazor app's `Startup.cs` `ConfigureServices` method, register the `AmbientLightSensorService`. 12 | 13 | ``` 14 | services.AddSingleton(); 15 | ``` 16 | 17 | 1) Now you can inject the `AmbientLightSensorService` into any Blazor page and use it like this: 18 | 19 | ``` 20 | @using AspNetMonsters.Blazor.Sensors 21 | @inject AmbientLightSensorService sensorService 22 |

Let there be light!

23 | 24 |

@sensor?.Illuminance

25 | 26 | 27 | 28 | 29 | 30 | 31 | @functions 32 | { 33 | AmbientLightSensor sensor; 34 | 35 | protected override async Task OnInitAsync() 36 | { 37 | await Start(); 38 | } 39 | 40 | private async Task Start() 41 | { 42 | sensor = await sensorService.StartReading(() => 43 | { 44 | StateHasChanged(); 45 | }); 46 | } 47 | 48 | private async Task Stop() 49 | { 50 | if (sensor != null) 51 | { 52 | await sensorService.StopReading(sensor); 53 | sensor = null; 54 | } 55 | } 56 | } 57 | ``` 58 | --------------------------------------------------------------------------------