Skip to content

Instantly share code, notes, and snippets.

@Utopiah
Last active March 4, 2021 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Utopiah/1ab2c1e1ee8b58c4abbd1ee0dde88d77 to your computer and use it in GitHub Desktop.
Save Utopiah/1ab2c1e1ee8b58c4abbd1ee0dde88d77 to your computer and use it in GitHub Desktop.
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 )
@rzr
Copy link

rzr commented Nov 4, 2020

moving photons at light speed :) nice hack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment