Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
IoT Platform
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IoT
IoT Platform
Merge requests
!20
Resolve "Data ingress for context information for tiki-wiki"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Resolve "Data ingress for context information for tiki-wiki"
39-data-ingress-for-context-information-for-tiki-wiki
into
staging
Overview
0
Commits
23
Changes
5
Merged
Rodrigo Gonçalves
requested to merge
39-data-ingress-for-context-information-for-tiki-wiki
into
staging
6 months ago
Overview
0
Commits
23
Changes
5
Expand
Related to
#39
Edited
6 months ago
by
Rodrigo Gonçalves
0
0
Merge request reports
Viewing commit
3056b8c3
Show latest version
5 files
+
746
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
5
Search (e.g. *.vue) (Ctrl+P)
3056b8c3
Sample clientes and usage for SmartDataContext in PHP and Python
· 3056b8c3
Rodrigo Goncalves
authored
6 months ago
sample-clients/php/client.php
0 → 100644
+
275
−
0
Options
<?php
namespace
SmartDataContextAPI
;
class
SmartDataContextAPIClient
{
private
string
$url
;
private
string
$certFile
;
private
string
$keyFile
;
private
bool
$verifyCertificate
;
public
function
__construct
(
string
$certFile
,
string
$keyFile
,
string
$url
=
"https://iot.lisha.ufsc.br/"
,
bool
$verifyCertificate
=
true
)
{
$this
->
certFile
=
$certFile
;
$this
->
keyFile
=
$keyFile
;
$this
->
url
=
rtrim
(
$url
,
'/'
)
.
'/'
;
$this
->
verifyCertificate
=
$verifyCertificate
;
}
private
function
doJsonPost
(
array
$json_data
,
string
$return_attribute
=
''
,
string
$endpoint
=
"api/v1_1/context.php"
)
{
$headers
=
[
'Content-Type: application/json'
];
$url
=
$this
->
url
.
$endpoint
;
$response
=
$this
->
makeRequest
(
$url
,
$headers
,
json_encode
(
$json_data
));
$response_json
=
json_decode
(
$response
,
true
);
if
(
isset
(
$response_json
[
'errors'
]))
{
throw
new
Exception
(
"Error processing request: "
.
json_encode
(
$response_json
[
'errors'
]));
}
if
(
$return_attribute
)
{
return
$response_json
[
'result'
][
$return_attribute
];
}
return
$response_json
[
'result'
];
}
private
function
makeRequest
(
string
$url
,
array
$headers
,
$data
=
null
)
{
$ch
=
curl_init
(
$url
);
curl_setopt
(
$ch
,
CURLOPT_RETURNTRANSFER
,
true
);
curl_setopt
(
$ch
,
CURLOPT_SSLCERT
,
$this
->
certFile
);
curl_setopt
(
$ch
,
CURLOPT_SSLKEY
,
$this
->
keyFile
);
curl_setopt
(
$ch
,
CURLOPT_HTTPHEADER
,
$headers
);
curl_setopt
(
$ch
,
CURLOPT_POST
,
true
);
if
(
!
$this
->
verifyCertificate
)
{
curl_setopt
(
$ch
,
CURLOPT_SSL_VERIFYHOST
,
false
);
curl_setopt
(
$ch
,
CURLOPT_SSL_VERIFYPEER
,
false
);
}
if
(
$data
)
{
curl_setopt
(
$ch
,
CURLOPT_POSTFIELDS
,
$data
);
}
$response
=
curl_exec
(
$ch
);
if
(
curl_errno
(
$ch
))
{
throw
new
Exception
(
'Curl error: '
.
curl_error
(
$ch
));
}
curl_close
(
$ch
);
return
$response
;
}
public
function
createSmartDataContext
(
$content
,
$features
,
$t0
=
-
1
,
$t1
=
-
1
,
$smartDataSources
=
[],
$smartDataUnits
=
[])
{
$json_request
=
[
"command"
=>
"/create"
,
"request"
=>
[
"content"
=>
$content
,
"features"
=>
$features
,
"t0"
=>
$t0
,
"t1"
=>
$t1
,
"smartDataUnits"
=>
$smartDataUnits
,
"smartDataSources"
=>
$smartDataSources
]
];
return
$this
->
doJsonPost
(
$json_request
,
'smartDataContextId'
);
}
public
function
associateSmartDataContext
(
$smartDataContextIds
,
$smartDataUnits
=
[],
$smartDataSources
=
[])
{
if
(
empty
(
$smartDataSources
)
&&
empty
(
$smartDataUnits
))
{
throw
new
Exception
(
"At least one smartDataSource or smartDataUnit must be informed"
);
}
$json_request
=
[
"command"
=>
"/associate"
,
"request"
=>
[
"smartDataContextIds"
=>
$smartDataContextIds
,
"smartDataUnits"
=>
$smartDataUnits
,
"smartDataSources"
=>
$smartDataSources
]
];
$result
=
$this
->
doJsonPost
(
$json_request
);
return
count
(
$result
)
===
1
?
$result
[
0
]
:
$result
;
}
public
function
unassociateSmartDataContext
(
$smartDataContextIds
,
$smartDataUnits
=
[],
$smartDataSources
=
[])
{
if
(
empty
(
$smartDataSources
)
&&
empty
(
$smartDataUnits
))
{
throw
new
Exception
(
"At least one smartDataSource or smartDataUnit must be informed"
);
}
$json_request
=
[
"command"
=>
"/unassociate"
,
"request"
=>
[
"smartDataContextIds"
=>
$smartDataContextIds
,
"smartDataUnits"
=>
$smartDataUnits
,
"smartDataSources"
=>
$smartDataSources
]
];
$result
=
$this
->
doJsonPost
(
$json_request
);
return
count
(
$result
)
===
1
?
$result
[
0
]
:
$result
;
}
public
function
getSmartDataContext
(
$smartDataContextId
)
{
$json_request
=
[
"command"
=>
"/get"
,
"request"
=>
[
"smartDataContextId"
=>
$smartDataContextId
]
];
return
$this
->
doJsonPost
(
$json_request
);
}
public
function
findSmartDataContext
(
$smartDataUnits
=
[],
$smartDataSources
=
[],
$t0
=
null
,
$t1
=
null
)
{
if
(
empty
(
$smartDataSources
)
&&
empty
(
$smartDataUnits
))
{
throw
new
Exception
(
"At least one smartDataSource or smartDataUnit must be informed"
);
}
$json_request
=
[
"command"
=>
"/contexts"
,
"request"
=>
[
"smartDataUnits"
=>
$smartDataUnits
,
"smartDataSources"
=>
$smartDataSources
]
];
if
(
$t0
)
{
$json_request
[
'request'
][
't0'
]
=
$t0
;
}
if
(
$t1
)
{
$json_request
[
'request'
][
't1'
]
=
$t1
;
}
return
$this
->
doJsonPost
(
$json_request
);
}
public
function
querySmartDataContext
(
$query
)
{
$json_request
=
[
"command"
=>
"/query"
,
"request"
=>
$query
];
return
$this
->
doJsonPost
(
$json_request
);
}
public
function
updateSmartDataContext
(
$id
,
$content
=
null
,
$features
=
null
,
$t0
=
null
,
$t1
=
null
,
$smartDataSources
=
null
,
$smartDataUnits
=
null
)
{
$json_request
=
[
"command"
=>
"/update"
,
"request"
=>
[
"smartDataContextId"
=>
$id
]
];
if
(
$content
)
{
$json_request
[
'request'
][
'content'
]
=
$content
;
}
if
(
$features
)
{
$json_request
[
'request'
][
'features'
]
=
$features
;
}
if
(
$t0
)
{
$json_request
[
'request'
][
't0'
]
=
$t0
;
}
if
(
$t1
)
{
$json_request
[
'request'
][
't1'
]
=
$t1
;
}
if
(
$smartDataSources
)
{
$json_request
[
'request'
][
'smartDataSources'
]
=
$smartDataSources
;
}
if
(
$smartDataUnits
)
{
$json_request
[
'request'
][
'smartDataUnits'
]
=
$smartDataUnits
;
}
return
$this
->
doJsonPost
(
$json_request
);
}
public
function
addUnstructuredDataFromFile
(
$smartDataContextId
,
$filePath
,
$fileName
=
null
,
$mimeType
=
null
)
{
if
(
!
$fileName
)
{
$fileName
=
basename
(
$filePath
);
}
if
(
!
$mimeType
)
{
$mimeType
=
mime_content_type
(
$filePath
);
}
$data
=
file_get_contents
(
$filePath
);
return
$this
->
addUnstructuredData
(
$smartDataContextId
,
$fileName
,
$mimeType
,
$data
);
}
public
function
addUnstructuredData
(
$smartDataContextId
,
$fileName
,
$mimeType
,
$data
)
{
$headers
=
[
"Content-Type:
$mimeType
"
,
"Filename:
$fileName
"
];
$url
=
$this
->
url
.
"api/v1_1/context.php?action=add-unstructured&smartDataContextId=
$smartDataContextId
"
;
$response
=
$this
->
makeRequest
(
$url
,
$headers
,
$data
);
$response_json
=
json_decode
(
$response
,
true
);
if
(
isset
(
$response_json
[
'errors'
]))
{
throw
new
Exception
(
"Error processing request: "
.
json_encode
(
$response_json
[
'errors'
]));
}
return
$response_json
[
'result'
][
'objectId'
];
}
public
function
getUnstructuredData
(
$smartDataContextId
,
$objectId
)
{
$url
=
$this
->
url
.
"api/v1_1/context.php"
;
$json_request
=
[
"command"
=>
"/unstructured/get"
,
"request"
=>
[
"smartDataContextId"
=>
$smartDataContextId
,
"objectId"
=>
$objectId
]
];
return
$this
->
makeRequest
(
$url
,
[],
json_encode
(
$json_request
));
}
public
function
saveUnstructuredDataToFile
(
$smartDataContextId
,
$objectId
,
$filePath
)
{
$data
=
$this
->
getUnstructuredData
(
$smartDataContextId
,
$objectId
);
file_put_contents
(
$filePath
,
$data
);
}
public
function
removeUnstructuredData
(
$smartDataContextId
,
$objectId
)
{
$json_request
=
[
"command"
=>
"/unstructured/remove"
,
"request"
=>
[
"smartDataContextId"
=>
$smartDataContextId
,
"objectId"
=>
$objectId
]
];
return
$this
->
doJsonPost
(
$json_request
);
}
}
?>
Loading