This repository has been archived by the owner on Jun 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
124 lines (102 loc) · 3.62 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const signalR = require('@microsoft/signalr');
const five = require('johnny-five');
const board = new five.Board();
const Oled = require('oled-js');
const font = require('oled-font-5x7');
const fs = require('fs'); // for logging
const util = require('util'); // for logging
const PIN_PIEZO = 11;
const OLED_OPTS = {
width: 128,
height: 32,
address: 0x3C // see https://github.com/noopkat/oled-js how to determine the address
};
const melodyB5 = [
['G3', 1 / 4],
[null, 1 / 8],
['G3', 1 / 4],
[null, 1 / 8],
['G3', 1 / 4],
[null, 1 / 8],
['D#3', 2],
[null, 2],
['F3', 1 / 4],
[null, 1 / 8],
['F3', 1 / 4],
[null, 1 / 8],
['F3', 1 / 4],
[null, 1 / 8],
['D3', 4],
[null, 1 / 8]
];
const CONTRACT_ADDRESS = 'KT1HbQepzV1nVGg8QVznG7z4RcHseD5kwqBn'; // Hic et Nunc Marketplace
// parameters to adapt
const OBJKT_ID = '181212'; // put your OBJKT ID here
function playMelody(piezo, melody, bpm) {
piezo.play({
song: melodyB5,
tempo: bpm
});
}
board.on('ready', () => {
// useful for debugging, uncomment to log
// function log () {
// logFile.write(util.format.apply(null, arguments) + '\n');
// // logStdout.write(util.format.apply(null, arguments) + '\n');
// }
// const logFile = fs.createWriteStream('logs/log.txt', { flags: 'a' });
// const logStdout = process.stdout;
const piezo = new five.Piezo(PIN_PIEZO);
const servo = new five.Servo(10);
const oled = new Oled(board, five, OLED_OPTS);
const connection = new signalR.HubConnectionBuilder()
.withUrl('https://api.tzkt.io/v1/events')
.build();
// https://api.tzkt.io/#section/SubscribeToOperations
async function init() {
await connection.start();
await connection.invoke('SubscribeToOperations', {
address: CONTRACT_ADDRESS,
types: 'transaction'
});
};
// auto-reconnect
connection.onclose(init);
connection.on('operations', (msg) => {
if (msg.type === 0) {
console.log(`subscription to contract ${CONTRACT_ADDRESS} confirmed, listening for OBJKT ${OBJKT_ID}`);
} else if (msg.type === 1) {
const data = msg.data;
// logging the incoming message, uncomment to log
// log(data);
for (let entry of data) {
if (entry.type === 'transaction' && entry.status === 'applied'
&& entry.parameter.entrypoint === 'collect'
&& entry.diffs[0].content.value.objkt_id == OBJKT_ID // note that === doesn't work
) {
const consoleMsg = `gm! Congratulations! OBJKT ${OBJKT_ID} got collected by ${entry.sender.address}`;
console.log(consoleMsg);
// shorter message for 128x32 display
const displayMsg = `gm! OBJKT ${OBJKT_ID}/ ${entry.sender.address}`;
playMelody(piezo, melodyB5, 100);
setTimeout(() => {
oled.clearDisplay();
oled.update();
oled.writeString(font, 1, displayMsg, 1, true, 2);
oled.update();
}, 5000);
setTimeout(() => {
servo.home();
servo.sweep();
}, 8000)
setTimeout(() => {
servo.stop();
}, 16000);
}
}
} else if (msg.type === 2) {
console.log(`Chain reorganisation`)
}
});
init();
});