Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
IoTAssignment
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
samuel.hobman
IoTAssignment
Commits
37d39081
Commit
37d39081
authored
Jan 04, 2021
by
samuel.hobman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new file
parents
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
123 additions
and
0 deletions
+123
-0
humidity.js
humidity.js
+123
-0
No files found.
humidity.js
0 → 100644
View file @
37d39081
const
Discord
=
require
(
"
Discord.js
"
);
const
config
=
require
(
'
./config.json
'
);
const
client
=
new
Discord
.
Client
;
const
fs
=
require
(
"
fs
"
);
const
MongoClient
=
require
(
'
mongodb
'
).
MongoClient
;
const
uri
=
"
mongodb+srv://Sam:assignment@cluster0.issnm.mongodb.net/enclosure?retryWrites=true&w=majority
"
;
const
mongoClient
=
new
MongoClient
(
uri
,
{
useNewUrlParser
:
true
});
const
currentTime
=
new
Date
();
var
intervalID
=
setInterval
(
checkData
,
60000
);
client
.
once
(
'
ready
'
,
()
=>
{
console
.
log
(
"
I'm logged on!
"
);
});
client
.
on
(
'
message
'
,
async
message
=>
{
if
(
!
message
.
content
.
startsWith
(
'
!
'
)
||
message
.
author
.
bot
)
return
const
command
=
message
.
content
.
toLowerCase
();
switch
(
command
)
{
case
"
!report
"
:
const
flag
=
true
;
checkData
(
flag
);
break
;
case
"
!store
"
:
fs
.
readFile
(
'
data.txt
'
,
'
utf-8
'
,
function
(
err
,
data
)
{
if
(
err
)
{
return
console
.
log
(
err
);
}
let
preWorked
=
data
.
trim
().
split
(
'
'
);
humidity
=
preWorked
[
0
];
temperature
=
preWorked
[
1
];
enclosureData
=
{
time
:
currentTime
,
temperature
:
parseInt
(
temperature
,
10
),
humidity
:
parseInt
(
humidity
,
10
)
}
insertIntoMongo
(
enclosureData
);
});
break
;
case
"
!stoptracking
"
:
clearInterval
(
intervalID
);
message
.
channel
.
send
(
"
Stopping tracking.
"
);
break
;
case
"
!starttracking
"
:
if
(
intervalID
==
0
)
{
message
.
channel
.
send
(
"
You're already tracking.
"
);
console
.
log
(
intervalID
);
}
else
{
message
.
channel
.
send
(
"
You're tracking!
"
);
startTrack
()
console
.
log
(
intervalID
);
}
break
;
}
});
async
function
checkData
(
flag
){
// if flagged, the code simply sends the current humidity and temperature to me.
const
user
=
await
client
.
users
.
fetch
(
"
226082168294735872
"
);
// Because of scope, this is the cleanest way to ensure it messages me directly.
fs
.
readFile
(
'
data.txt
'
,
'
utf-8
'
,
function
(
err
,
data
)
{
if
(
err
)
{
return
console
.
log
(
err
);
}
let
preWorked
=
data
.
trim
().
split
(
'
'
);
// takes the data and splits it into an array, seperated by a space.
humidity
=
parseInt
(
preWorked
[
0
],
10
);
temperature
=
parseInt
(
preWorked
[
1
],
10
);
if
(
flag
){
sendToUser
(
user
,
temperature
,
humidity
);
}
else
{
if
(
humidity
>=
80
)
{
alertHumidity
(
user
,
humidity
,
tooHumid
=
true
);
}
else
if
(
humidity
<=
40
)
{
alertHumidity
(
user
,
humidity
);
}
if
(
temperature
>=
30
)
{
alertTemperature
(
user
,
temperature
,
tooHot
=
true
);
}
else
if
(
temperature
<=
20
)
{
alertTemperature
(
user
,
temperature
);
}
}
});
}
function
sendToUser
(
person
,
temperature
,
humidity
){
person
.
send
(
"
Temperature of the enclosure is currently
"
+
temperature
+
"
C, humidity is:
"
+
humidity
+
"
%
"
);
}
function
alertHumidity
(
person
,
humidity
,
tooHumid
)
{
if
(
tooHumid
)
{
person
.
send
(
"
Sir, your enclosure is too humid. It is currently
"
+
humidity
+
"
%
"
);
}
else
{
person
.
send
(
"
Sir, your encloure is getting arid. It is currently
"
+
humidity
+
"
%
"
);
}
}
function
alertTemperature
(
person
,
temperature
,
tooHot
)
{
if
(
tooHot
)
{
person
.
send
(
"
Sir, your enclosure is too hot. It is currently
"
+
temperature
+
"
C
"
);
}
else
{
person
.
send
(
"
Sir, your enclosure is getting cold. It is currently
"
+
temperature
+
"
C
"
);
}
}
async
function
insertIntoMongo
(
enclosureData
){
const
user
=
await
client
.
users
.
fetch
(
"
226082168294735872
"
);
try
{
const
thisSession
=
await
mongoClient
.
connect
();
// returns a promise to connect
const
database
=
thisSession
.
db
(
"
enclosure
"
);
const
collection
=
database
.
collection
(
"
data
"
);
const
result
=
await
collection
.
insertOne
(
enclosureData
);
console
.
log
(
"
Record inserted successfully.
"
);
user
.
send
(
"
Stored the current status of your enclosure.
"
);
}
finally
{
await
mongoClient
.
close
();
}
function
stopTrack
(
intervalID
)
{
clearInterval
(
intervalID
);
console
.
log
(
intervalID
);
}
}
function
startTrack
()
{
intervalID
=
setInterval
(
checkData
,
60000
);
}
client
.
login
(
config
.
token
);
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment