Skip to content

Commit

Permalink
Integrated IoT
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Jun 12, 2021
1 parent 6d31b1c commit 422a488
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 12 deletions.
59 changes: 51 additions & 8 deletions manufacturer_client/src/views/asset_detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ const {
*/
const authorizableProperties = [
['weight', 'Certification Status'],
['location', 'Location']
['location', 'Location'],
['temperature', 'Temperature']
]

const _labelProperty = (label, value) => [
Expand Down Expand Up @@ -313,6 +314,39 @@ const ReportLocation = {
}
}

const ReportTemperature = (vnode) => {
let onsuccess = vnode.onsuccess || (() => null)
let xform = vnode.xform || ((x) => x)
console.log('VNODE ', vnode)
if (vnode.record.recordId === '879'){
fetch('http://blynk-cloud.com/qTr-WN8jLX1n2GnjTFScVlHh1x9d454j/get/v6')
.then(res =>{
if(res.ok){
console.log('SUCCESS!')
res.json()
.then(data => {
const temp = data[0]
console.log('Temperature: ', temp, ' with type: ', typeof(temp))
var t = parsing.floatifyValue(temp)
vnode.value = parsing.toInt(t)
console.log('State.update: ', vnode.value, ' with type: ', typeof(vnode.value))
_updateProperty(vnode.record, {
name: vnode.name,
[vnode.typeField]: xform(vnode.value),
dataType: vnode.type
}).then(() => {
vnode.value = ''
}).then(onsuccess)
})
}
else {
console.log('Not Successful :(')
}
})
.catch(error => console.log('ERROR!'))
}
}

const ReportValue = {
view: (vnode) => {
let onsuccess = vnode.attrs.onsuccess || (() => null)
Expand Down Expand Up @@ -491,13 +525,21 @@ const AssetDetail = {
? m(ReportLocation, { record, onsuccess: () => _loadData(record.recordId, vnode.state) })
: null)),

_row(
_labelProperty(
'Temperature',
_propLink(record, 'temperature', _formatTemp(getPropertyValue(record, 'temperature')))),
(isReporter(record, 'temperature', publicKey) && !record.final
? console.log('Unable to access')
: null)),
_row(
_labelProperty(
'Temperature',
_propLink(record, 'temperature', _formatTemp(getPropertyValue(record, 'temperature')))),
(isReporter(record, 'temperature', publicKey) && !record.final
? ReportTemperature({
name: 'temperature',
label: 'Temperature (°C)',
record,
typeField: 'intValue',
type: payloads.updateProperties.enum.INT,
xform: (x) => parsing.toInt(x),
onsuccess: () => _loadData(vnode.attrs.recordId, vnode.state)
})
: null)),

_row(
_labelProperty(
Expand Down Expand Up @@ -619,6 +661,7 @@ const _answerProposal = (record, publicKey, role, response) => {
}

const _updateProperty = (record, value) => {
console.log('record ', record, 'value', value)
let updatePayload = payloads.updateProperties({
recordId: record.recordId,
properties: [value]
Expand Down
46 changes: 42 additions & 4 deletions manufacturer_client/src/views/property_detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,15 @@ const typedWidget = state => {
}

const updateSubmitter = state => e => {
e.preventDefault()
const { name, dataType, recordId } = state.property

console.log('I am there!')
let value = null
if (state.update) {
value = state.update
} else {
value = state.tmp
}

const update = { name }
update.dataType = payloads.updateProperties.enum[dataType]
update[`${dataType.toLowerCase()}Value`] = value
Expand Down Expand Up @@ -129,6 +128,45 @@ const updateForm = state => {
])
}

//Button for fetching IoT device data
const iotForm = state => {
const { name, dataType, recordId } = state.property
if (recordId === '879'){
fetch('http://blynk-cloud.com/qTr-WN8jLX1n2GnjTFScVlHh1x9d454j/get/v6')
.then(res =>{
if(res.ok){
console.log('SUCCESS!')
res.json()
.then(data => {
const temp = data[0]
console.log('Temperature: ', temp, ' with type: ', typeof(temp))
var t = parsing.floatifyValue(temp)
state.update = parsing.toInt(t)
console.log('State.update: ', state.update, ' with type: ', typeof(state.update))
return state

// return m('form.my-5', {
// onsubmit: updateSubmitter(state)
// }, [
// m('.container',

// m('.col-md-2',
// m('button.btn.btn-primary', { type: 'submit' }, 'Fetch IoT')))
// ])
}).then((state)=>{
console.log('Update Submitter is below me...')
console.log('state', state)
updateSubmitter(state)
})
}
else {
console.log('Not Successful :(')
}
})
.catch(error => console.log('ERROR!'))
}
}

/**
* Displays updates to a property, and form for submitting new updates.
*/
Expand Down Expand Up @@ -169,7 +207,7 @@ const PropertyDetailPage = {
return [
layout.title(`${name} of ${record}`),
typedWidget(vnode.state),
isReporter ? updateForm(vnode.state) : null,
isReporter ? iotForm(vnode.state) : null,
m('.container',
layout.row([
m('h5.mr-auto', 'Update History'),
Expand Down

0 comments on commit 422a488

Please sign in to comment.