Logic Machine Forum
BIM ioT - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Gateway (https://forum.logicmachine.net/forumdisplay.php?fid=10)
+--- Thread: BIM ioT (/showthread.php?tid=3347)

Pages: 1 2


BIM ioT - gdimaria - 06.05.2021

Hi, I have to connect LM to Bim ioT ACCA Software.  


Communication takes place via JSON strings such as the following:

READING MESSAGE:

{
"payload":{
"Type":
"Item":{
"Name":
"State":
"Type":
"Editable":
"Link":
},
"TimeStamp":
},
}



WRITING MESSAGE

{
“Action”:
“Controller”:
“ItemName”:
“ItemState”:
}




Hon can arrange that with LM?


Thank you so much


Peppe


RE: BIM ioT - admin - 06.05.2021

JSON is just a data format but what is used as a transport, HTTP or something else?


RE: BIM ioT - gdimaria - 06.05.2021

from the notes provided: "The connection to usBIM.IoT takes place via certified Web Sockets. The certificate is provided by
Acca and the flow of incoming and outgoing data is regularized by the formalism shown below "


RE: BIM ioT - gdimaria - 07.05.2021

(06.05.2021, 15:08)admin Wrote: JSON is just a data format but what is used as a transport, HTTP or something else?

I thought of translating the notes provided by our client to better understand the problem.
I hope you can help me.

Peppe

P.S. This BIM standard, as you certainly know, is expanding a lot and will soon become, at least in Italy, mandatory for any building project. So I think it is important to understand how to interface LM.


RE: BIM ioT - admin - 07.05.2021

You can use this library to create a websocket client script on LM: https://forum.logicmachine.net/showthread.php?tid=1294&pid=7823#pid7823


RE: BIM ioT - gdimaria - 10.05.2021

(07.05.2021, 07:59)admin Wrote: You can use this library to create a websocket client script on LM: https://forum.logicmachine.net/showthread.php?tid=1294&pid=7823#pid7823



they send me the attached certificate keys (temporary) for the ws connections.... Can you explain me how I could use them on LM?

the address of the server to connect is 


wss://test-hub.usbim.com


RE: BIM ioT - admin - 11.05.2021

1. Install 2021 RC1 firmware.
2. Create a user library named websocket using the attached source.
3. Upload certificates via FTP using ftp login, rename files with long names to cert.pem and key.pem accordingly.
4. Create a resident script with 0 sleep time.
Code:
if not client then
  require('json')
  ws = require('websocket')
  url = 'wss://test-hub.usbim.com'

  client, err = ws.client('sync', 1)

  opts = {
    protocol = 'tlsv13',
    key = '/home/ftp/key.pem',
    password = [[*q&'wrKQsx+#bj8pbEV=%7Gpr"S+sxV2ngr`N}SjCC`X#rtt!wY6PL`d2'3!^x}u]],
    certificate = '/home/ftp/cert.pem',
    cafile = '/home/ftp/ca-chain.cert.pem',
  }

  res, err = client:connect(url, opts)
  if not res then
    log('connection failed', err)
    client:sock_close()
    client = nil
  end
end

if client then
  frame, opcode, clean, code, reason = client:receive()
  log(frame, opcode, clean, code, reason)
  if frame then
    data = json.pcdecode(frame)
    log(data)
  elseif reason ~= 'timeout' then
    log('connection dropped', reason)
    client:sock_close()
    client = nil
  end
end

This example is only a starting point, it should be extended with additional send/receive logic.


RE: BIM ioT - gdimaria - 11.05.2021

(11.05.2021, 07:08)admin Wrote: 1. Install 2021 RC1 firmware.
2. Create a user library named websocket using the attached source.
3. Upload certificates via FTP using ftp login, rename files with long names to cert.pem and key.pem accordingly.
4. Create a resident script with 0 sleep time.
Code:
if not client then
  require('json')
  ws = require('websocket')
  url = 'wss://test-hub.usbim.com'

  client, err = ws.client('sync', 1)

  opts = {
    protocol = 'tlsv13',
    key = '/home/ftp/key.pem',
    password = [[*q&'wrKQsx+#bj8pbEV=%7Gpr"S+sxV2ngr`N}SjCC`X#rtt!wY6PL`d2'3!^x}u]],
    certificate = '/home/ftp/cert.pem',
    cafile = '/home/ftp/ca-chain.cert.pem',
  }

  res, err = client:connect(url, opts)
  if not res then
    log('connection failed', err)
    client:sock_close()
    client = nil
  end
end

if client then
  frame, opcode, clean, code, reason = client:receive()
  log(frame, opcode, clean, code, reason)
  if frame then
    data = json.pcdecode(frame)
    log(data)
  elseif reason ~= 'timeout' then
    log('connection dropped', reason)
    client:sock_close()
    client = nil
  end
end

This example is only a starting point, it should be extended with additional send/receive logic.

Thank you very much.... now I am getting that log.... I guess something is still wrong...


RE: BIM ioT - admin - 11.05.2021

This means that the script is connected but there are no messages to be read from the websocket connection.
Can you describe what data do you want to send/receive via this connection?


RE: BIM ioT - gdimaria - 11.05.2021

Sure, I already send you a pdf with the notes, by the way, they are JSON strings like the following:

READ MESSAGGE

{
"payload": {
"Type": "NotificationPackage"
"Item": {
"Name": "Temperatura-SudOvest"
"State": “28.5”
"Type": “NUMBER:FLOAT”
"Editable": 0,
"Link": ""
},
"TimeStamp": “2020-09-22T15:27:59.140Z”
}
}


WRITE MESSAGE


{
“Action”:“Post”
“Controller”:“Items”
“ItemName”:“Light-Box”
“ItemState”: “OFF”
}


TO REQUEST THE STATE OF A LAMP

{
“Action”:“GET”
“Controller”:“Items”
“ItemName”: “Light-Box”
}

TO REQUEST THE LIST OF OBJECTS

{
“Action”:“GET”
“Controller”: “Items”
}


RE: BIM ioT - admin - 11.05.2021

I know about the protocol but the question was about your own use case. What kind of devices are going to control like this? Or do you want to read some sensor values?


RE: BIM ioT - gdimaria - 11.05.2021

(11.05.2021, 09:42)admin Wrote: I know about the protocol but the question was about your own use case. What kind of devices are going to control like this? Or do you want to read some sensor values?

I have to let them to control the ETS objects in LM... so I have to send sensor values, object states and let them to command i.e. lamps, termostate setpoint, etc.


RE: BIM ioT - admin - 13.05.2021

1. Create a user library named bim-iot using the attached source.
2. Make sure that the updated websocket library and keys are in place.
3. Create a resident script with sleep time = 0:
Code:
if not wsclient then
  require('json')
  require('user.bim-iot')

  url = 'wss://test-hub.usbim.com'
  wsclient = require('user.websocket').client('sync', 1)

  opts = {
    protocol = 'tlsv13',
    key = '/home/ftp/key.pem',
    password = [[*q&'wrKQsx+#bj8pbEV=%7Gpr"S+sxV2ngr`N}SjCC`X#rtt!wY6PL`d2'3!^x}u]],
    certificate = '/home/ftp/cert.pem',
    cafile = '/home/ftp/ca-chain.cert.pem',
  }

  res, err = wsclient:connect(url, opts)
  if res then
    log('WS connection OK')
  else
    log('WS connection failed', err)
    wsclient:sock_close()
    wsclient = nil
  end

  if not lb then
    lb = require('localbus').new()

    lb:sethandler('groupwrite', eventhandler)
    lb:sethandler('groupresponse', eventhandler)

    lbfd = lb:getfd()
  end

  wsfd = wsclient.sock:getfd()

  init()
  sendall()
end

if wsclient then
  local res, lbstat, wsstat = socket.xselect(10, lbfd, wsfd)

  if res > 0 then
    if lbstat then
      lb:step()
    end

    if wsstat then
      local msg, opcode, clean, code, reason = wsclient:receive()
      log(msg, opcode, clean, code, reason)

      if msg then
        onwsmessage(msg)
      elseif reason ~= 'timeout' then
        log('WS connection dropped', reason)
        wsclient:sock_close()
        wsclient = nil
      end
    end
  end
else
  -- handle incoming object messages before trying to reconnect WS
  lb:loop(10)
end
Since I don't have the tools to test some parts might not work.


RE: BIM ioT - gdimaria - 13.05.2021

The connection is ok, but they still can't get nothing. I guess you already understood, but just to be clear:
they should receive any object event when it occurs and to be able to write on the objects.
Can I send you the backup of my LM in private? Could be useful?

Thanks

Peppe


RE: BIM ioT - admin - 13.05.2021

The script is sending the object values when the connection is established. Do you have any tool to read/write data from the IoT gateway side?


RE: BIM ioT - gdimaria - 13.05.2021

they checked the logs and the data they are receiving but it seems to be not in the correct format due to an error that is present in the documentation we sent you.
We receive the following JSON:

{
"payload": {
"Type": "NotificationPackage",
"Item": {
"Link": "",
"State": "Thu 13-05-2021",
"Type": "STRING",
"Name": "daymonthyear",
"Editable": 2
},
"TimeStamp": "2021-05-13T09: 00: 01.288Z"
}
}

But the correct format is:
{
"Type": "NotificationPackage",
"Item": {
"Link": "",
"State": "Thu 13-05-2021",
"Type": "STRING",
"Name": "daymonthyear",
"Editable": 2
},
"TimeStamp": "2021-05-13T09: 00: 01.288Z"
}

In practice, the payload node is not necessary.


RE: BIM ioT - admin - 13.05.2021

Modify the sendobjvalue function in bim-iot library and do a full script restart via disable/enable:
Code:
function sendobjvalue(obj)
  local data = json.encode({
    Type = 'NotificationPackage',
    Item = {
      Name = obj.name,
      State = encodevalue(obj.value, obj.dtype),
      Type = obj.dtype,
      Editable = 2, -- allow read/write
      Link = ''
    },
    TimeStamp = timestamp()
  })

  wsclient:send(data)
end



RE: BIM ioT - gdimaria - 13.05.2021

(13.05.2021, 09:53)admin Wrote: Modify the sendobjvalue function in bim-iot library and do a full script restart via disable/enable:
Code:
function sendobjvalue(obj)
  local data = json.encode({
    Type = 'NotificationPackage',
    Item = {
      Name = obj.name,
      State = encodevalue(obj.value, obj.dtype),
      Type = obj.dtype,
      Editable = 2, -- allow read/write
      Link = ''
    },
    TimeStamp = timestamp()
  })

  wsclient:send(data)
end

I already done it...  Wink
Everything seems to works fine at moment... 
now they said me that they will try to send write messages in the next days...  hopefully everything will be fine.

Thank you so much for your priceless support!

Peppe


RE: BIM ioT - gdimaria - 28.05.2021

(13.05.2021, 09:53)admin Wrote: Modify the sendobjvalue function in bim-iot library and do a full script restart via disable/enable:
Code:
function sendobjvalue(obj)
  local data = json.encode({
    Type = 'NotificationPackage',
    Item = {
      Name = obj.name,
      State = encodevalue(obj.value, obj.dtype),
      Type = obj.dtype,
      Editable = 2, -- allow read/write
      Link = ''
    },
    TimeStamp = timestamp()
  })

  wsclient:send(data)
end

HI,

They finally  tested the ws connection sending commands from their side: it doesn't work. 
They try to on/off a led or to set temperature on a termostate, but nothing happens.
They json strings they are sending are like: 

    {
  “Action”:“Post”
  “Controller”:“Items”
  “ItemName”:“Led 1”
  “ItemState”:“ON”
}

or
    {
  “Action”:“Post”
  “Controller”:“Items”
  “ItemName”:“Temperatura”
  “ItemState”:“22.0”
}

But no success.

Another problem is that they are receiving the status of ALL objects, ALL in read / write mode.

instead we must be able to choose which objects to transmit and specify whether to read or write or both.

I am grateful for your help.

BR 

Peppe


RE: BIM ioT - admin - 28.05.2021

Use the attached updated library and see if receiving values works.
You must tag the objects that you want to be accessible via this connection. Use either bim-read, bim-write or bim-read-write tag to define the access level. Only one tag can be used for any object.