-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.js
100 lines (78 loc) · 3.2 KB
/
publish.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
//imp MAM stuff
const Mam = require('../../Downloads/IOTAProject/mam.client.js-master/mam.client.js-master/lib/mam.client.js')
const { asciiToTrytes, trytesToAscii } = require('@iota/converter')
const mode = 'restricted'
const secretKey = 'VERYSECRETKEY'
const provider = 'https://nodes.devnet.iota.org'
//import express from 'express';
//var express = require('express')
//var app = express()
//var bodyParser = require("body-parser")
//const moment = require('moment');
//const mamExplorerLink = `https://mam-explorer.firebaseapp.com/?provider=${encodeURIComponent(provider)}&mode=${mode}&key=${secretKey.padEnd(81, '9')}&root=`
let numberofSteps;
let kilometersWalked;
let timestamp;
//const TIMEINTERVAL = 30;
// Initialise MAM State
let mamState = Mam.init(provider)
// Set channel mode
mamState = Mam.changeMode(mamState, mode, secretKey)
// Publish to tangle
const publish = async packet => {
//console.log(packet.kilometersWalked);
//console.log(packet.numberofSteps);
//console.log(packet.message);
//console.log(packet.timestamp);
// Create MAM Payload - STRING OF TRYTES
const trytes = asciiToTrytes(JSON.stringify(packet))
const message = Mam.create(mamState, trytes)
// Save new mamState
mamState = message.state
// Attach the payload
//await Mam.attach(message.payload, message.address, 3, 9)
await Mam.attach(message.payload, message.address)
console.log('Published', packet, '\n');
console.log (message.root);
return message.root
}
const generateJSON = function() {
numberOfSteps= Math.floor((Math.random()*89)+10);
kilometersWalked= Math.floor(990/9);
timestamp= (new Date()).toLocaleString();
const json = {"Number of Steps": numberOfSteps, "kilometers walked" : kilometersWalked, "dateTime": timestamp};
return json;
}
const publishAll = async () => {
const json = generateJSON()
const root = await publish(json)
//numberOfSteps: Math.floor((Math.random()*89)+10),
//kilometersWalked: Math.floor(990/9),
//timestamp: (new Date()).toLocaleString()
return root
}
// Callback used to pass data out of the fetch
//const logData = data => console.log('Fetched and parsed', JSON.parse(trytesToAscii(data)), '\n')
//let x = 0
//while(x!=3){
publishAll()
//setInterval(publishAll, TIMEINTERVAL*100);
//x++
//}
//.then(async root => {
// Output asyncronously using "logData" callback function
//await Mam.fetch(root, mode, secretKey, logData)
// Output syncronously once fetch is completed
//const result = await Mam.fetch(root, mode, secretKey)
//result.messages.forEach(message => console.log('Fetched and parsed', JSON.parse(trytesToAscii(message)), '\n'))
//console.log(`Verify with MAM Explorer:\n${mamExplorerLink}${root}\n`);
//})
//Set view engine to ejs
//app.set("view engine", "ejs")
//Tell Express where we keep our index.ejs
//app.set("views", __dirname + "/views")
//Use body-parser
//app.use(bodyParser.urlencoded({ extended: false }))
//Instead of sending Hello World, we render index.ejs
//app.get("/", (req, res) => { res.render("index", {kilometers: kilometersWalked, time: timestamp, steps: numberOfSteps }) })
//app.listen(8080, () => { console.log("Server online on http://localhost:8080"); });