|
| 1 | +import ..protocol as protocol |
| 2 | + |
| 3 | +class Lora extends protocol.Data: |
| 4 | + static MT := 1004 |
| 5 | + static PAYLOAD := 2 |
| 6 | + static SPREAD_FACTOR := 4 |
| 7 | + static CODING_RATE := 5 |
| 8 | + static BANDWIDTH := 6 |
| 9 | + static CENTER_FREQUENCY := 7 |
| 10 | + static TX_POWER := 8 |
| 11 | + static PREAMBLE_LENGTH := 9 |
| 12 | + static RECEIVE_MS := 10 |
| 13 | + static SLEEP := 11 |
| 14 | + static STATE := 12 |
| 15 | + |
| 16 | + static setMsg --spreadFactor/int --codingRate/int --bandwidth/int --centerFrequency/int --txPower/int --preambleLength/int --sleep/int?=null -> protocol.Message: |
| 17 | + msg := protocol.Message MT |
| 18 | + msg.data.addDataUint8 SPREAD_FACTOR spreadFactor |
| 19 | + msg.data.addDataUint8 CODING_RATE codingRate |
| 20 | + msg.data.addDataUint8 BANDWIDTH bandwidth |
| 21 | + msg.data.addDataUint32 CENTER_FREQUENCY centerFrequency |
| 22 | + msg.data.addDataUint8 TX_POWER txPower |
| 23 | + msg.data.addDataUint8 PREAMBLE_LENGTH preambleLength |
| 24 | + if sleep: |
| 25 | + msg.data.addDataUint8 SLEEP sleep |
| 26 | + msg.header.data.addDataUint8 protocol.Header.TYPE_MESSAGE_METHOD protocol.Header.METHOD_SET |
| 27 | + return msg |
| 28 | + |
| 29 | + static getMsg -> protocol.Message: |
| 30 | + msg := protocol.Message MT |
| 31 | + msg.header.data.addDataUint8 protocol.Header.TYPE_MESSAGE_METHOD protocol.Header.METHOD_GET |
| 32 | + return msg |
| 33 | + |
| 34 | + static doMsg --payload/ByteArray --receiveMs/int -> protocol.Message: |
| 35 | + msg := protocol.Message MT |
| 36 | + msg.data.addData PAYLOAD payload |
| 37 | + msg.data.addDataUint32 RECEIVE_MS receiveMs |
| 38 | + msg.header.data.addDataUint8 protocol.Header.TYPE_MESSAGE_METHOD protocol.Header.METHOD_DO |
| 39 | + return msg |
| 40 | + |
| 41 | + static subscribeMsg -> protocol.Message: |
| 42 | + msg := protocol.Message MT |
| 43 | + msg.header.data.addDataUint8 protocol.Header.TYPE_MESSAGE_METHOD protocol.Header.METHOD_SUBSCRIBE |
| 44 | + return msg |
| 45 | + |
| 46 | + static unsubscribeMsg -> protocol.Message: |
| 47 | + msg := protocol.Message MT |
| 48 | + msg.header.data.addDataUint8 protocol.Header.TYPE_MESSAGE_METHOD protocol.Header.METHOD_UNSUBSCRIBE |
| 49 | + return msg |
| 50 | + |
| 51 | + constructor.fromData data/protocol.Data: |
| 52 | + super.fromData data |
| 53 | + |
| 54 | + payload -> ByteArray: |
| 55 | + return getData PAYLOAD |
| 56 | + |
| 57 | + spreadFactor -> int: |
| 58 | + return getDataUint8 SPREAD_FACTOR |
| 59 | + |
| 60 | + codingRate -> int: |
| 61 | + return getDataUint8 CODING_RATE |
| 62 | + |
| 63 | + bandwidth -> int: |
| 64 | + return getDataUint8 BANDWIDTH |
| 65 | + |
| 66 | + centerFrequency -> int: |
| 67 | + return getDataUint32 CENTER_FREQUENCY |
| 68 | + |
| 69 | + txPower -> int: |
| 70 | + return getDataUint8 TX_POWER |
| 71 | + |
| 72 | + preambleLength -> int: |
| 73 | + return getDataUint8 PREAMBLE_LENGTH |
| 74 | + |
| 75 | + receiveMs -> int: |
| 76 | + return getDataUint32 RECEIVE_MS |
| 77 | + |
| 78 | + sleep -> int: |
| 79 | + return getDataUint8 SLEEP |
| 80 | + |
| 81 | + state -> int: |
| 82 | + return getDataUint STATE |
| 83 | + |
| 84 | + stringify -> string: |
| 85 | + return { |
| 86 | + "Payload": payload, |
| 87 | + "Spread Factor": spreadFactor, |
| 88 | + "Coding Rate": codingRate, |
| 89 | + "Bandwidth": bandwidth, |
| 90 | + "Center Frequency": centerFrequency, |
| 91 | + "TX Power": txPower, |
| 92 | + "Preamble Length": preambleLength, |
| 93 | + "Receive Ms": receiveMs, |
| 94 | + "Sleep": sleep, |
| 95 | + "State": state, |
| 96 | + }.stringify |
0 commit comments