Created
June 27, 2025 06:21
-
-
Save ayamoTake/5b199c108214367ba93de8fa515ae168 to your computer and use it in GitHub Desktop.
Isutribute code snipets
This file contains hidden or 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
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
</head> | |
<script type="module" src="server.js"></script> | |
<body> | |
<h3 id="deviceTitle">Sitting Watcher</h3> | |
<!-- <input type="button" onclick="sayHello()" value="Hello"></input> --> | |
<div id="messageDiv" ></div> | |
<div style="display: flex;"> | |
<input | |
style="flex: 1;" | |
type="button" onclick="test_act" value="TEST_ACT"/> | |
<input | |
style="flex: 1;" | |
type="button" onclick="test_act_stop" value="TEST_ACT_STOP"/> | |
</div> | |
<div style="display: flex;"> | |
<div style="flex: 1;"> | |
log: | |
<div id="sittingLog" ></div> | |
</div> | |
<div style="flex: 1;"> | |
<table> | |
<tr> | |
<td>over-sitting time(s):</td> | |
<td><span id="overSiggingThresholdGuide"></span></td> | |
<td> | |
<input id="overSittingThresholdInput" type="range" value="10" min="0" max="60" step="1" | |
onchange="set_over_sitting_threshold(event)" | |
oninput="show_over_sitting_threshold(event)" /> | |
</td> | |
</tr> | |
<tr> | |
<td>weight threshold:</td> | |
<td><span id="thresholdGuide"></span></td> | |
<td> | |
<input id="thresholdInput" type="range" value="10" min="0" max="511" step="5" | |
onchange="set_threshold(event)" | |
oninput="show_threshold(event)" /> | |
</td> | |
</tr> | |
</table> | |
</div> | |
</div> | |
</body> | |
</html> |
This file contains hidden or 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 sleep = msec => new Promise(resolve => setTimeout(resolve, msec)); | |
import nodeWebSocketLib from "websocket"; // https://www.npmjs.com/package/websocket | |
import {RelayServer} from "../RelayServer.js"; | |
import { requestI2CAccess } from "../node_modules/node-web-i2c/index.js"; | |
import ADS1X15 from "@chirimen/ads1x15"; | |
var channel; | |
var ads1115; | |
var state = { | |
threshold: 100, | |
weight: 0, | |
}; | |
async function init_all() { | |
const i2cAccess = await requestI2CAccess(); | |
const port = i2cAccess.ports.get(1); | |
ads1115 = new ADS1X15(port, 0x48); | |
// If you uses ADS1115, you have to select "true", otherwise select "false". | |
await ads1115.init(true, 7); // High Gain | |
console.log("init complete"); | |
await connect(); | |
cast_threshold(); | |
} | |
async function connect(){ | |
// webSocketリレーの初期化 | |
var relay = RelayServer("chirimentest", "chirimenSocket" , nodeWebSocketLib, "https://chirimen.org"); | |
channel = await relay.subscribe("isutribute_measure"); | |
console.log("web socketリレーサービスに接続しました"); | |
channel.onmessage = receiver; | |
} | |
function receiver(msg) { | |
let data = msg.data; | |
if (data.type == "set_threshold") { | |
change_threshold(data) | |
} else if (data.type == "request_cast_threshold") { | |
cast_threshold(); | |
} | |
} | |
function change_threshold(data){ | |
state.threshold = parseFloat(data.threshold); | |
console.log("new threshold: " + state.threshold ); | |
} | |
function cast_threshold() { | |
channel.send({ | |
"type": "cast_threshold", | |
"threshold": state.threshold, | |
}); | |
} | |
function judge(state) { | |
return state.weight >= state.threshold; | |
} | |
async function main() { | |
var firstTime = true; | |
const base = 22; | |
for (var i = 0; ;i++) { | |
var difA = await ads1115.read("0,1"); // p0-p1 differential mode | |
state.weight = difA - base; | |
console.log(state); | |
if (judge(state)) { | |
channel.send({ | |
"type": "sitting_signal", | |
"sit_time": new Date().toISOString(), | |
}); | |
console.log("sent"); | |
} | |
await sleep(1000); | |
} | |
} | |
await init_all(); | |
await main(); |
This file contains hidden or 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
import {RelayServer} from "https://chirimen.org/remote-connection/js/beta/RelayServer.js"; | |
const sleep = msec => new Promise(resolve => setTimeout(resolve, msec)); | |
window.show_threshold = show_threshold; | |
window.set_threshold = set_threshold_handler; | |
window.show_over_sitting_threshold = show_over_sitting_threshold; | |
window.set_over_sitting_threshold = set_over_sitting_threshold; | |
window.test_act = test_act; | |
window.test_act_stop = test_act_stop; | |
let data = { | |
"threshold": 0, | |
"type": "", | |
}; | |
let sitting_info_list = []; | |
var channel_measure; | |
var channel_act; | |
onload = async function(){ | |
// webSocketリレーの初期化 | |
var relay = RelayServer("chirimentest", "chirimenSocket" ); | |
channel_measure = await relay.subscribe("isutribute_measure"); | |
channel_act = await relay.subscribe("isutribute_act"); | |
messageDiv.innerText="web socketリレーサービスに接続しました"; | |
channel_measure.onmessage = receiver; | |
channel_measure.send({type: "request_cast_threshold"}); | |
overSiggingThresholdGuide.innerText = over_sitting_threshold / 1000; | |
overSittingThresholdInput.value = over_sitting_threshold / 1000; | |
} | |
function receiver(msg) { // メッセージを受信したときに起動する関数 | |
let data = msg.data; | |
if (data.type == "sitting_signal") { | |
get_sitting_signal(data); | |
} else if (data.type == "cast_threshold") { | |
cast_threshold_handler(data); | |
} | |
} | |
function get_new_sitting_info(sit_d) { | |
return { | |
begin: sit_d, | |
end: sit_d, | |
duration: 0, | |
}; | |
} | |
let over_sitting_threshold = 5000; | |
function is_over_sat(info) { | |
return info.end - info.begin >= over_sitting_threshold; | |
} | |
function display_sitting_log() { | |
const to_html = (item) => { | |
const over_p = is_over_sat(item); | |
const style = "color: " + (over_p ? "red": "green") + ";"; | |
return`<div style="${style}">${item.begin.toLocaleTimeString()} ~ ${item.end.toLocaleTimeString()}</div>` | |
}; | |
sittingLog.innerHTML = sitting_info_list | |
.toReversed() | |
.map(to_html) | |
.join(""); | |
} | |
function get_sitting_signal(data){ | |
const sit_d = new Date(data.sit_time); | |
if (sitting_info_list.length == 0) { | |
sitting_info_list.push(get_new_sitting_info(sit_d)); | |
} else { | |
const last_info = sitting_info_list.at(-1); | |
const continue_time = 2000; | |
if (Math.abs(last_info.end - sit_d) > continue_time) { | |
sitting_info_list.push(get_new_sitting_info(sit_d)); | |
} else { | |
sitting_info_list.at(-1).end = sit_d; | |
} | |
if (is_over_sat(sitting_info_list)) { | |
channel_act.send({ type: "over_sitting_signal" }) | |
} | |
} | |
display_sitting_log(); | |
} | |
function cast_threshold_handler(data) { | |
let v = data.threshold; | |
thresholdGuide.innerText = v; | |
thresholdInput.value = v; | |
} | |
function set_threshold_handler(event) { | |
set_threshold(event.target.value); | |
} | |
function set_threshold(value) { | |
console.log("threshold: " + value); | |
channel_measure.send({ | |
type: "set_threshold", | |
threshold: value, | |
}); | |
} | |
function show_threshold(event) { | |
thresholdGuide.innerText = event.target.value; | |
} | |
function set_over_sitting_threshold(event) { | |
over_sitting_threshold = event.target.value * 1000; | |
display_sitting_log(); | |
} | |
function show_over_sitting_threshold(event) { | |
overSiggingThresholdGuide.innerText = event.target.value; | |
} | |
function test_act() { | |
channel_act({ type: "over_sitting_signal" }); | |
} | |
function test_act_stop() { | |
channel_act({ type: "after_over_sitting_signal" }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment