Create a sensor with SensorDataUnitType - azure-digital-twins

I'm trying to create a new device with sensor(s).
This is the payload I'm sending to create the new device:
{"Name":"DeviceABC","HardwareId":"D4xxx425","SpaceId":"xxxx-xxx-xx-xx-xxx","Status":"Provisioned","CreateIoTHubDevice":false,"Properties":[{"Name":"VendorName","Value":"MyVendor"},{"Name":"VendorDeviceId","Value":"D4xxx19425"},{"Name":"VendorDeviceType","Value":"electricity"}],"Sensors":[{"pollRate":0,"id":null,"dataType":"Json","dataUnitType":"KilowattHourEnergy","deviceId":null,"portType":null,"port":"electricity","spaceId":null,"type":"Classic"}]}
In this case I want to use a built in type. KWh
{
"id": 186,
"category": "SensorDataUnitType",
"name": "KilowattHourEnergy",
"disabled": false,
"logicalOrder": 0,
"friendlyName": "kWh"
}
The SensorDataType is :
{
"id": 314,
"spaceId": "xxxx-xx-xxx-xx-xxxx",
"category": "SensorDataType",
"name": "Json",
"disabled": false,
"logicalOrder": 0
}
When I do this for other devices without specifying a dataUnitType in the sensor object, it works fine. But as soon as I include it i get this:
{
"error": {
"code": "400.600.000.000",
"message": "Invalid datatype/dataunittype combination used on sensor."
}}

There seems to be a naming convention between SensorDataType and SensorDataUnitType. The convention is that the SensorDataUnitType needs to end with the full SensorDataType. In your example you have:
"dataType":"Json",
"dataUnitType":"KilowattHourEnergy",
But according to the convention it should be
"dataType":"Json",
"dataUnitType":"KilowattHourEnergyJson",
Of course you can't just change KilowattHourEnergy because it's in a system ontology. So to get it working I had to create KilowattHourEnergyJson as a new type.
I guess the most complete answer would include that in your case the SensorDataType probably shouldn't be JSON but should be Energy.

Related

How To Convert "created_timestamp" Value To A Valid Date In Python

I'm currently working on a Twitter bot that automatically reply messages, I'm doing this by using tweepy (the official python twitter library)
I need to filter messages based on the created time as I don't want to reply same message twice. Now the problem is that the API endpoint returns created_timestamp as string representation of positive integers.
Below is an example of data returned as per the doc
{
"next_cursor": "AB345dkfC",
"events": [
{ "id": "110", "created_timestamp": "1639919665615", ... },
{ "id": "109", "created_timestamp": "1639865141987", ... },
{ "id": "108", "created_timestamp": "1639827437833", ... },
{ "id": "107", "created_timestamp": "1639825389806", ... },
{ "id": "106", "created_timestamp": "1639825389796", ... },
{ "id": "105", "created_timestamp": "1639825389768", ... },
...
]
}
My question is "How do I convert the created_timestamp to a valid date using python" ?.
You might play with timestamps on this resource
And in your case could use methods like:
timestamp = int('timestamp_string')
datetime.fromtimestamp(timestamp, tz=None)
date.fromtimestamp(timestamp)
From the datetime standard library. But integers after the first line are already well comparable if the task is to distinguish differences between the timestamps.

Twitter API 2.0 - Unable to fetch user.fields

I am using API version 2.0 and unable to fetch the user.fields results. All other parameters seem to be returning results correctly. I'm following this documentation.
url = "https://api.twitter.com/2/tweets/search/all"
query_params = {
"query": "APPL",
"max_results": "10",
"tweet.fields": "created_at,lang,text,author_id",
"user.fields": "name,username,created_at,location",
"expansions": "referenced_tweets.id.author_id",
}
response = requests.request("GET", url, headers=headers, params=query_params).json()
Sample result:
{
'author_id': '1251347502013521925',
'text': 'All conspiracy. But watch for bad news on Apple. Such a vulnerable stocktechnically for the biggest market cap # $2.1T ( Thanks Jay). This is the glue for the bulls. But, they stopped innovating when Steve died, built a fancy office and split the stock. $appl',
'lang': 'en',
'created_at': '2021-06-05T02:33:48.000Z',
'id': '1401004298738311168',
'referenced_tweets': [{
'type': 'retweeted',
'id': '1401004298738311168'
}]
}
As you can see, the following information is not returned: name, username, and location.
Any idea how to retrieve this info?
Your query does actually return the correct data. I tested this myself.
A full example response will be structured like this:
{
"data": [
{
"created_at": "2021-06-05T02:33:48.000Z",
"lang": "en",
"id": "1401004298738311168",
"text": "All conspiracy. But watch for bad news on Apple. Such a vulnerable stocktechnically for the biggest market cap # $2.1T ( Thanks Jay). This is the glue for the bulls. But, they stopped innovating when Steve died, built a fancy office and split the stock. $appl",
"author_id": "1251347502013521925",
"referenced_tweets": [
{
"type": "retweeted",
"id": "1401004298738311168"
}
]
}
],
"includes": {
"users": [
{
"name": "Gary Casper",
"id": "1251347502013521925",
"username": "Hisel1979",
"created_at": "2020-07-11T13:39:58.000Z"
}
]
}
}
The sample result you provided comes from within the data object. However, the expanded object data will be nested in the includes object (in your case name, username, and location). The corresponding user object can be referenced via the author_id field.

Submitting a transaction via RESTful API (how to handle transactionid and timestamp)

Problem:
I am unable to POST a transaction via the RESTful API generated by the composer-rest-server. I am receiving statusCode 422; the transaction instance is not valid. However, the same example works in the Playground.
Scenario:
I've set up a transaction called Offer in my .cto file which posts an offer to buy a house:
// Offer - Specifies an offer that a bidder places on a house listing with an associated price
transaction Offer {
o Double bidPrice
--> HouseListing listing
--> Person bidder
}
The composer-rest-server has generated an API with the following JSON string to post a transaction of type Offer:
{
"$class": "org.acme.purchasing.Offer",
"bidPrice": 0,
"listing": "string",
"bidder": "string",
"transactionId": "string",
"timestamp": "2017-07-21T13:37:09.460Z"
}
I've since replaced this with a sample transaction using the following JSON code derived from the above example:
{
"$class": "org.acme.purchasing.Offer",
"bidPrice": 1000,
"listing": "001",
"bidder": "RJOHNSON",
"transactionId": "1b9aa63c-dfad-4aad-a610-dfc80f2796b2",
"timestamp": "2017-07-21T13:37:09.460Z"
}
The response returned is error code 422:
{
"error": {
"statusCode": 422,
"name": "ValidationError",
"message": "The `Offer` instance is not valid. Details: `transactionId` can't be set (value: \"1b9aa63c-dfad-4aad-a610-d...6b2\").",
"details": {
"context": "Offer",
"codes": {
"transactionId": [
"absence"
]
},
"messages": {
"transactionId": [
"can't be set"
]
}
},
"stack": "ValidationError: The `Offer` instance is not valid. Details: `transactionId` can't be set (value: \"1b9aa63c-dfad-4aad-a610-d...6b2\").\n at /usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/dao.js:355:12\n at ModelConstructor.<anonymous> (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/validations.js:566:11)\n at ModelConstructor.next (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/hooks.js:93:12)\n at ModelConstructor.<anonymous> (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/validations.js:563:23)\n at ModelConstructor.trigger (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/hooks.js:83:12)\n at ModelConstructor.Validatable.isValid (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/validations.js:529:8)\n at /usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/dao.js:351:9\n at doNotify (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/observer.js:155:49)\n at doNotify (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/observer.js:155:49)\n at doNotify (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/observer.js:155:49)\n at doNotify (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/observer.js:155:49)\n at Function.ObserverMixin._notifyBaseObservers (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/observer.js:178:5)\n at Function.ObserverMixin.notifyObserversOf (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/observer.js:153:8)\n at Function.ObserverMixin._notifyBaseObservers (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/observer.js:176:15)\n at Function.ObserverMixin.notifyObserversOf (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/observer.js:153:8)\n at Function.ObserverMixin._notifyBaseObservers (/usr/lib/node_modules/composer-rest-server/node_modules/loopback-datasource-juggler/lib/observer.js:176:15)"
}
}
Now the strange thing is that I've deployed the same BNA onto the Hyperledger Composer Playground and am able to execute transactions of type Offer successfully.
Note that in the Playground, "transactionId" and "timestamp" are not specified as the Playground appears to take care of these values. For example, this is what Playground proposes to me initially:
{
"$class": "org.acme.purchasing.Offer",
"bidPrice": 0,
"listing": "resource:org.acme.purchasing.HouseListing#id:7965",
"bidder": "resource:org.acme.purchasing.Person#id:4441"
}
Can anyone advise why it's saying the Offer instance is not valid? My first thought was that it's not liking the string I'm placing in "transactionId" but another Stack Overflow post points out that the transactionId is just an arbitrary UUIDv4 string which I've generated already.
Update #1: Failing even with default demo
In order to ensure by BNA is error-free, I've deployed the default carauction-demo (resembles my example closely) onto my local Hyperledger Fabric instance and deployed the composer-rest-server. I've also deployed the same BNA into the Playground. All assets and participants were created identical in both from the Explorer (local instance) and Playground. When it comes time to submit an Offer transaction:
{
"$class": "org.acme.vehicle.auction.Offer",
"bidPrice": 800,
"listing": "resource:org.acme.vehicle.auction.VehicleListing#L001",
"member": "resource:org.acme.vehicle.auction.Member#member3#acme.org"
}
This JSON was generated by the Playground and succeeds there. Copy/paste/executing into the Explorer yields a status 500 error.
{
"error": {
"statusCode": 500,
"name": "Error",
"message": "error trying invoke chaincode. Error: chaincode error (status: 500, message: Error: Object with ID 'string' in collection with ID 'Asset:org.acme.vehicle.auction.VehicleListing' does not exist)",
"stack": "Error: error trying invoke chaincode. Error: chaincode error (status: 500, message: Error: Object with ID 'string' in collection with ID 'Asset:org.acme.vehicle.auction.VehicleListing' does not exist)\n at _initializeChannel.then.then.then.then.catch (/usr/lib/node_modules/composer-rest-server/node_modules/composer-connector-hlfv1/lib/hlfconnection.js:806:34)"
}
}
I'm still at a lost as to what is wrong here.
After much experimentation and some searching, I've concluded that the problem was that NPM was installed using sudo (as root). I redid the installation as non-root and the problem has now been solved. Everything is working as expected.
Composer updates the transactionId itself as its generated, you cannot do this in your JSON and hence why you get the error. This is a Loopback -> Swagger conversion issue as it should not appear in a /POST REST operation - captured here https://github.com/hyperledger/composer/issues/663
So I have successfully executed Offer transactions (/POST). I think the '500' error is because there is a missing field ('string' type), or relationship in your Offer transaction via REST.
Using the example of the Car Auction network https://github.com/hyperledger/composer-sample-networks/blob/master/packages/carauction-network/models/auction.cto
Either of these Offer transactions using /POST were successful in Explorer:
{
"$class": "org.acme.vehicle.auction.Offer",
"bidPrice": 20,
"listing": "org.acme.vehicle.auction.VehicleListing#100",
"member": "org.acme.vehicle.auction.Member#a#b.com"
}
OR
{
"$class": "org.acme.vehicle.auction.Offer",
"bidPrice": 20,
"listing": "org.acme.vehicle.auction.VehicleListing#100",
"member": "org.acme.vehicle.auction.Member#a#b.com",
"timestamp": "2017-07-28T14:07:02.558Z"
}
responded with a 200 (SUCCESS) and a transactionId in Explorer:
{
"$class": "org.acme.vehicle.auction.Offer",
"bidPrice": 20,
"listing": "org.acme.vehicle.auction.VehicleListing#100",
"member": "org.acme.vehicle.auction.Member#a#b.com",
"transactionId": "e75b9934-1f08-4daf-90db-702bbe4b8fa1"
}
This is my VehicleListing asset JSON for #100 above
{
"$class": "org.acme.vehicle.auction.VehicleListing",
"listingId": "100",
"reservePrice": 50,
"description": "string",
"state": "FOR_SALE",
"offers": [
{
"$class": "org.acme.vehicle.auction.Offer",
"bidPrice": 50,
"listing": "100",
"member": "resource:org.acme.vehicle.auction.Member#a#b.com",
"transactionId": "string123",
"timestamp": "2017-07-28T14:07:02.825Z"
}
],
"vehicle": "resource:org.acme.vehicle.auction.Vehicle#123"
}
And I created asset for Vehicle 123 as well as Member a#b.com

How to observe the removal of a model from the Firebase Database?

I use Firebase Database, I have a list of cards, I need to monitor some cards if they are deleted. I tried to do this with ref.observe (.childRemoved), but this is not appropriate for my case, since some data can be deleted by the cardholder, and then this block will fire several times, I need to watch if the entire card was deleted. How can I do that?
My reference
let ref = FIRDatabase.database().reference().child(MainGateways.cards.rawValue).child(card.id)
My cards structure
"cards": {
"-Khv9rUVwErNHBzXcruO": {
"additionalInfo": {
"note": ""
},
"dateProperties": {
"day": "Flex",
"isFlexDate": true,
"isFlexTime": true,
"iso8601": "",
"time": ""
},
"interestedUsers": {
"NfM26A2YcPUFz8rfYa23Kr3mjCO2": {
"isApproved": false,
"isNew": true,
"isoDate": "2017-04-17T13:08:41+04:00",
"userID": "NfM26A2YcPUFz8rfYa23Kr3mjCO2",
"userItchableName": "Alexsander K."
}
},
"isNewPendingRequest": true,
"isPrivateStatus": false,
"isTest": false,
"ownerID": "1Cix149ThIOG1ULPVjyy0LyTxbe2",
"peopleProperties": {
"availableSeats": 1,
"numberOfPeople": 1,
"numberOfPeopleDescription": "1 person"
},
"title": "Genius Lounge and Sake Bar",
"userName": "Tim C.",
"version": 1
},
I also tried to make such a link, but then this block works if any card was removed from the list.
guard let ref = FIRDatabase.database().reference().child(MainGateways.cards.rawValue).child(card.id).parent else { return }
Look into Cloud Functions for Firebase. Using that, you can write JavaScript code that acts in response to database changes at the path you specify. This allows you to write logic independent of the client app to react to changes in the database.

Updating BLE Device Firmware in iOS

In my current iOS project, I need to upgrade the current firmware of the BLE device on availability of a newer version. And the server is providing me the data that sample format
{
"fileId": "687659c1-02be-48b9-97bf-37472bcbc9b6",
"length": 143184,
"createdDate": 1426010825894,
"version": "0.34",
"packets": [
{
"address": "00008000",
"data": "0041002091F3000035E100008DF300008DF300008DF300008DF30000000000000000000000000000000000008DF300008DF30000000000008DF30000ADAD00008DF30000C1E40000D1E40000E1E40000F1E4000001E5000011E5000021E5000031E5000041E5000051E5000061E5000071E5000081E5000091E50000A1E50000B1E500008DF300008DF300008DF300008DF300008DF300008DF300008DF300008DF300008DF300008DF300008DF300008DF300008DF300008DF300008DF300008DF300008DF300008DF300008DF300008DF300008DF300008DF300008DF30000ADDE00008DF300008DF300008DF30000C9AD000045E1000055E1000065E1000075E1000085E1000095E10000A5E10000B5E100008DF300008DF300008DF300008DF300008DF300008DF300008DF300008DF3000015F100008DF3000029F100008DF300008DF300008DF300008DF300008DF300007DDC00008DF300008DF300008DF300008DF300008DF300008DF300008DF300003DF1000051F100008DF3000008B50548054B1B1A062B00D808BD044B002BFBD09847F9E7743C0100773C01000000000008B50748074B1B1A9B1003EBD373591000D108BD044A002AFBD09047F9E700BF743C0100743C01000000000010B5064C237843B9FFF7D2FF044B13B10448AFF300800123237010BDD80E002000000000743C010008B5084B1BB10848"
},
{
"address": "00008200",
"data": "0849AFF300800848036813B1074B03B19847BDE80840FFF7C5BF00BF00000000743C0100DC0E0020743C01000000000070B50E46154678B914467119002CC4EB010106DD064B1868064B1B689847013CF3E725EAE57070BD4FF0FF3070BD00BF68300020201100200C4B1A6882B0920354BFD12240F2DD220192019AD2080192019A511E0191002AFAD140F0AA4240F02A401A60186002B0704700BF000C0F4010220023027201220360436043728272C27203734373704770B50C46154680E83000274B0846984723681E0746D42368244E13F0010F03D12368D8030FD507E0206820F00100B0472368D907FCD4F3E7206820F48030B0472368DA03FCD495E80C00287A697A0002134300F4F8501843AB7A8904002BEB7A01F4F80140EA010014BF80210021002B2B7B40EA01000CBF00210221002B40EA01000CBF002310231843B047206840F48030B0472368DB03FCD52046044B9847012070BD002070BD8DD400006982000051D7000038B590E830000C4B204698472B7B13B10423E361E3606B7B2068002B0CBF0023082340F001001843044B9847044B1B68DB07FBD538BD00BF8DD4000069820000000C0F4003689A689207FCD5024A5A6002F1AA425A607047010000557FB50E4B02A8984707238DF8103001238DF8133068460A490A4B02AA984748B172B6BFF35F8F084B00221A706846074B9847FEE707B05DF8"
}
],
"type": "application/octet-stream",
"name": "nextgen_0_34.hex"
}
I am aware of the method of writing data to a CBPeripheral by calling method
[peripheral writeValue:dataToWrite forCharacteristic:interestingCharacteristic
type:CBCharacteristicWriteWithResponse];
My questions are:
How can I use these addresses while creating a CBMutableCharacteristic?
I am getting about 120 packets. Is there a way that I can write data one by one so that I can show a progress to user?

Resources