Last active
March 4, 2021 07:54
-
-
Save Utopiah/1ab2c1e1ee8b58c4abbd1ee0dde88d77 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const bearer = 'Bearer ...' // get it for your own gateway in Settings -> Developers > Create local auth | |
// or directly via http://gateway.local/oauth/authorize?response_type=code&client_id=local-token&scope=/things:readwrite&state=asdf | |
const myThing = 'zb-00001234' // get it by visiting http://gateway.local/things and opening a Thing | |
// UUID will be at the end of theURL | |
if (bearer.length + myThing.length < 25) throw 'you need to setup those values, see comments' | |
// utils | |
function loadAssetsFromURLs(URLs){ | |
var elements = [] | |
for (var url of URLs){ | |
var el = document.createElement("a-entity") | |
AFRAME.scenes[0].appendChild(el) | |
el.setAttribute("media-loader", { src: url, fitToBox: true, resolve: true }) | |
el.setAttribute("networked", { template: "#interactable-media" } ) | |
elements.push(el) | |
} | |
return elements | |
} | |
// setup room interactive elements | |
var lightSwitch = loadAssetsFromURLs(["https://poly.google.com/view/1xBl-8de6b0"])[0]; | |
lightSwitch.setAttribute("position", "-2.7 1.5 0"); | |
var lightBulb = loadAssetsFromURLs([ "https://uploads-prod.reticulum.io/files/e092e561-7e63-4338-a1d2-d346b782d90a.glb?token=e586011442cea429ae3f0891e65ead08" ])[0] | |
lightBulb.setAttribute("position", "100 100 100") | |
lightBulb.setAttribute("scale", "0 0 0"); | |
var lastSwitchChange = +Date.now(); | |
var switchStatusOn = false; | |
// behaviors | |
function newLightStatus(value){ | |
if (value) { | |
lightBulb.setAttribute("position", "-0.5 2.5 1"); | |
lightSwitch.setAttribute("rotation", "180 180 0"); | |
// actually arguable since it implies the gateway could update the physical switch... | |
switchStatusOn = true; | |
} else { | |
lightBulb.setAttribute("position", "100 100 100"); | |
lightSwitch.setAttribute("rotation", "0 180 0"); | |
switchStatusOn = false; | |
} | |
} | |
function turnLightOn( status ) { | |
fetch('https://gateway.local/things/'+myThing+'/properties/on', { | |
method: 'PUT', | |
headers: { | |
'Content-Type': 'application/json', | |
'Accept': 'application/json', | |
Authorization: bearer | |
}, | |
body: JSON.stringify({ on: status }) | |
}).then(res => { | |
return res.json(); | |
}).then(things => { | |
console.log(things); | |
}); | |
} | |
// interativity | |
var refreshLight = setInterval( _ => { fetch('https://gateway.local/things/'+myThing+'/properties/on', { | |
headers: { Accept: 'application/json', Authorization: bearer } | |
}).then(res => { | |
return res.json(); | |
}).then(lightOnProperty => { | |
newLightStatus( lightOnProperty.on ) | |
}); }, 1000 ) | |
var flipSwitchTrigger = setInterval( _ => { | |
if ( ( +Date.now() - lastSwitchChange > 5000 ) | |
&& | |
( lightSwitch.getAttribute("position").distanceTo( document.querySelector("#avatar-rig").getAttribute("position") ) < 2 ) ) | |
{ | |
console.log('flip the switch'); | |
lastSwitchChange = +Date.now(); | |
switchStatusOn = !switchStatusOn | |
turnLightOn( switchStatusOn ) | |
} | |
}, 1000 ) | |
var flipSwitchTriggerOther = setInterval( _ => { | |
var other = document.querySelectorAll("[networked-avatar][id^=naf]")[0] | |
if ( other && ( +Date.now() - lastSwitchChange > 5000 ) | |
&& | |
( lightSwitch.getAttribute("position").distanceTo( other.getAttribute("position") ) < 2 ) ) | |
{ | |
console.log('flip the switch'); | |
lastSwitchChange = +Date.now(); | |
switchStatusOn = !switchStatusOn | |
turnLightOn( switchStatusOn ) | |
} | |
}, 1000 ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
moving photons at light speed :) nice hack