habdsl 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +32 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +423 -0
- data/Rakefile +12 -0
- data/doc/tempareture_items.png +0 -0
- data/example/example.json +5 -0
- data/example/example.xlsx +0 -0
- data/example/example_dsl.rb +12 -0
- data/example/example_dslparser.rb +25 -0
- data/example/example_jsonparser.rb +18 -0
- data/example/example_sheetparser.rb +18 -0
- data/lib/habdsl/base_parser.rb +65 -0
- data/lib/habdsl/dsl_parser.rb +43 -0
- data/lib/habdsl/json_parser.rb +19 -0
- data/lib/habdsl/model/dsl.rb +29 -0
- data/lib/habdsl/model/equipment.rb +64 -0
- data/lib/habdsl/model/location.rb +65 -0
- data/lib/habdsl/model/point.rb +57 -0
- data/lib/habdsl/result_parser.rb +13 -0
- data/lib/habdsl/sheet_parser.rb +28 -0
- data/lib/habdsl/version.rb +5 -0
- data/lib/habdsl.rb +38 -0
- data/output/.keep +0 -0
- data/sig/habdsl.rbs +4 -0
- metadata +100 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 39341817368937700b7cabf1d0e5770c836ac18134c318eaefcc0d955b2805fa
|
|
4
|
+
data.tar.gz: bc867e6cb1bd2a63d90a54b092deef24e66a29d6faa7e6e1b0cc7862db66d32d
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 418da45b2da36110be735b467f27557d4f974dd205f4ba4652752b17829e1fda6333b5fc7b7b6d0d24d37b34407e79fd134abf2240edf01e19652127b67bbc4d
|
|
7
|
+
data.tar.gz: 0e9d6e110faabd54db923bbd65ace69c7bdcd8e0acda5751962d3a60bd6bf0fbe363aa010d3b34fe12372ae44d330f5c5d0fe3447b17bf131be9f00f8542e872
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
AllCops:
|
|
2
|
+
TargetRubyVersion: 3.3
|
|
3
|
+
NewCops: enable
|
|
4
|
+
|
|
5
|
+
Layout/LineLength:
|
|
6
|
+
Max: 120
|
|
7
|
+
|
|
8
|
+
Layout/MultilineMethodCallIndentation:
|
|
9
|
+
EnforcedStyle: indented_relative_to_receiver
|
|
10
|
+
|
|
11
|
+
Layout/SpaceInsideBlockBraces:
|
|
12
|
+
SpaceBeforeBlockParameters: false
|
|
13
|
+
|
|
14
|
+
Metrics/AbcSize:
|
|
15
|
+
Max: 25
|
|
16
|
+
|
|
17
|
+
Metrics/ParameterLists:
|
|
18
|
+
CountKeywordArgs: false
|
|
19
|
+
|
|
20
|
+
Style/Alias:
|
|
21
|
+
Enabled: true
|
|
22
|
+
EnforcedStyle: prefer_alias_method
|
|
23
|
+
|
|
24
|
+
Style/FormatStringToken:
|
|
25
|
+
Enabled: false
|
|
26
|
+
|
|
27
|
+
Style/ParallelAssignment:
|
|
28
|
+
Enabled: false
|
|
29
|
+
|
|
30
|
+
Style/StringLiterals:
|
|
31
|
+
Enabled: true
|
|
32
|
+
EnforcedStyle: double_quotes
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
|
10
|
+
identity and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
|
26
|
+
community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
|
31
|
+
any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
|
35
|
+
without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official email address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
[INSERT CONTACT METHOD].
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
|
86
|
+
actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
|
93
|
+
ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
|
113
|
+
community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.1, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 SenoOh
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
# Habdsl
|
|
2
|
+
Habdsl implements a mechanism to generate openHAB Items configuration DSL from:
|
|
3
|
+
+ Ruby internal DSL syntax.
|
|
4
|
+
+ Ruby internal DSL syntax and JSON data.
|
|
5
|
+
+ Ruby internal DSL syntax and Spreadsheet data.
|
|
6
|
+
|
|
7
|
+
It enables developers to define openHAB Items using familiar Ruby syntax, improving readability and maintainability.
|
|
8
|
+
|
|
9
|
+
For example:
|
|
10
|
+
```bash
|
|
11
|
+
$ vim habdsl.rb
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
```ruby
|
|
15
|
+
require 'habdsl'
|
|
16
|
+
|
|
17
|
+
input_code = <<~RUBY
|
|
18
|
+
location(name: "house", label: "House", icon: "house") do
|
|
19
|
+
location(name: "livingroom", label: "Living Room", icon: "bedroom") do
|
|
20
|
+
equipment(name: "sensor", label: "Sensor", icon: "siren") do
|
|
21
|
+
point(name: "temperature", label: "Temperature", type: "Number",
|
|
22
|
+
icon: "temperature", tags: ["Temperature"], channel: "mqtt:topic:mqttbroker:temperature")
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
RUBY
|
|
27
|
+
|
|
28
|
+
output = Habdsl::DslParser.parse(input_code: input_code)
|
|
29
|
+
puts output.dsl
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
$ ruby habdsl.rb
|
|
34
|
+
|
|
35
|
+
Group house "House" <house> ["Location"]
|
|
36
|
+
Group livingroom "Living Room" <bedroom> (house) ["Location"]
|
|
37
|
+
Group sensor "Sensor" <siren> (livingroom) ["Equipment"]
|
|
38
|
+
Number temperature "Temperature" <temperature> (sensor) ["Temperature"] {channel="mqtt:topic:mqttbroker:temperature"}
|
|
39
|
+
```
|
|
40
|
+
When you have the output.dsl from habdsl, paste it into a file with the .items extension and place it in the openHAB items file storage directory.
|
|
41
|
+
|
|
42
|
+
As an example, assume that the name of the file to be pasted is `tempareture.items` and the openHAB items file storage directory is `/etc/openhab/items`.
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
$ ruby habdsl.rb >> /etc/openhab/items/temperature.items
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
When all is complete, the Items UI in openHAB will reflect the list of Items you have created.
|
|
49
|
+
|
|
50
|
+

|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
## Installation
|
|
54
|
+
|
|
55
|
+
Install as a gem
|
|
56
|
+
|
|
57
|
+
```ruby
|
|
58
|
+
gem 'habdsl'
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
And then execute:
|
|
62
|
+
|
|
63
|
+
$ bundle install
|
|
64
|
+
|
|
65
|
+
Or install it yourself as:
|
|
66
|
+
|
|
67
|
+
$ gem install habdsl
|
|
68
|
+
|
|
69
|
+
## Input formats
|
|
70
|
+
|
|
71
|
+
Before using `habdsl`, make sure your **Spreadsheet** and **Ruby internal DSL code** follow the expected formats:
|
|
72
|
+
|
|
73
|
+
### Ruby internal DSL format
|
|
74
|
+
The Ruby DSL defines a hierarchical structure used to describe semantic models for `openHAB`.
|
|
75
|
+
Each DSL element corresponds to an object (`location`, `equipment`, or `point`) and is subject to strict structural validation.
|
|
76
|
+
|
|
77
|
+
- `location` (e.g., a room or area)
|
|
78
|
+
- `equipment` (e.g., a sensor or appliance)
|
|
79
|
+
- `point` (e.g., a measurable property or control)
|
|
80
|
+
|
|
81
|
+
Each DSL block maps to a structured entity that can be validated and transformed into openHAB items.
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
#### Hierarchy Rules
|
|
86
|
+
|
|
87
|
+
- `location` can contain:
|
|
88
|
+
- other `locations` (nested)
|
|
89
|
+
- `equipment`
|
|
90
|
+
|
|
91
|
+
- `equipment` can contain:
|
|
92
|
+
- other `equipment'` (nested)
|
|
93
|
+
- `point`
|
|
94
|
+
|
|
95
|
+
> - `location`**may contain nested** `location`, as well as `equipment`.
|
|
96
|
+
> - `point` **must be defined inside an `equipment`**
|
|
97
|
+
> - `point` **cannot contain any child elements**
|
|
98
|
+
> - `equipment` **cannot contain** `location`
|
|
99
|
+
|
|
100
|
+
❗ Violating this hierarchy will result in a validation error.
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
#### DSL Argument Validation Rules
|
|
105
|
+
|
|
106
|
+
#### `location` block
|
|
107
|
+
|
|
108
|
+
```ruby
|
|
109
|
+
location(name:, label:, icon:, parent: nil) do
|
|
110
|
+
# ...
|
|
111
|
+
end
|
|
112
|
+
```
|
|
113
|
+
+ name
|
|
114
|
+
+ Must not start with a number.
|
|
115
|
+
+ Allowed characters: A-Z, a-z, 0-9, _ (underscores).
|
|
116
|
+
+ label
|
|
117
|
+
+ Any free-form string.
|
|
118
|
+
+ icon
|
|
119
|
+
+ Must be one of the icons listed in the openHAB Classic icon set:
|
|
120
|
+
https://www.openhab.org/docs/configuration/iconsets/classic/
|
|
121
|
+
+ parent
|
|
122
|
+
+ Optional.
|
|
123
|
+
+ Must be the name of a parent `location`.
|
|
124
|
+
+ If declared in a nested block, this is inferred automatically and does not need to be set explicitly.
|
|
125
|
+
+ type
|
|
126
|
+
+ Automatically set to `Group`, no need to specify.
|
|
127
|
+
+ tags
|
|
128
|
+
+ Automatically set to include `Location`, no need to specify.
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
#### `equipment` block
|
|
132
|
+
```ruby
|
|
133
|
+
equipment(name:, label:, icon:, parent: nil) do
|
|
134
|
+
# ...
|
|
135
|
+
end
|
|
136
|
+
```
|
|
137
|
+
+ name
|
|
138
|
+
+ Must not start with a number.
|
|
139
|
+
+ Allowed characters: A-Z, a-z, 0-9, _ (underscores).
|
|
140
|
+
+ label
|
|
141
|
+
+ Any free-form string.
|
|
142
|
+
+ icon
|
|
143
|
+
+ Must be one of the icons listed in the openHAB Classic icon set:
|
|
144
|
+
https://www.openhab.org/docs/configuration/iconsets/classic/
|
|
145
|
+
+ parent
|
|
146
|
+
+ Optional.
|
|
147
|
+
+ Must be the name of a parent `location` or `equipment`.
|
|
148
|
+
+ If declared in a nested block, this is inferred automatically and does not need to be set explicitly.
|
|
149
|
+
+ type
|
|
150
|
+
+ Automatically set to `Group`, no need to specify.
|
|
151
|
+
+ tags
|
|
152
|
+
+ Automatically set to include `Equipment`, no need to specify.
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
#### `point` block
|
|
157
|
+
```ruby
|
|
158
|
+
point(name:, label:, type:, icon:, tags:, parent: nil, channel: nil)
|
|
159
|
+
```
|
|
160
|
+
+ name
|
|
161
|
+
+ Must not start with a number.
|
|
162
|
+
+ Allowed characters: A-Z, a-z, 0-9, _ (underscores).
|
|
163
|
+
+ label
|
|
164
|
+
+ Any free-form string.
|
|
165
|
+
+ type
|
|
166
|
+
+ Must be one of the valid openHAB item types listed here:
|
|
167
|
+
https://www.openhab.org/docs/concepts/items.html
|
|
168
|
+
+ icon
|
|
169
|
+
+ Must be one of the icons listed in the openHAB Classic icon set:
|
|
170
|
+
https://www.openhab.org/docs/configuration/iconsets/classic/
|
|
171
|
+
+ tags
|
|
172
|
+
+ Must be one or more valid tags from the openHAB REST API endpoint:
|
|
173
|
+
```sh
|
|
174
|
+
$ curl -X GET "http://<openHAB_address>:<port>/rest/tags" -H "Accept: application/json"
|
|
175
|
+
```
|
|
176
|
+
+ parent
|
|
177
|
+
+ Optional.
|
|
178
|
+
+ Must be the name of a parent `Equipment`.
|
|
179
|
+
+ If declared in a nested block, this is inferred automatically.
|
|
180
|
+
+ channel
|
|
181
|
+
+ Optional.
|
|
182
|
+
+ Select the UID of the Things you want to link to this Items.
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
**Valid Example:**
|
|
186
|
+
```ruby
|
|
187
|
+
location(name: "house", label: "House", icon: "house") do
|
|
188
|
+
table.each do |room|
|
|
189
|
+
location(name: "#{room[:name]}", label: "#{room[:label]}", icon: "bedroom") do
|
|
190
|
+
equipment(name: "#{room[:name]}_sensor", label: "#{room[:name]}_Sensor", icon: "siren") do
|
|
191
|
+
point(name: "#{room[:name]}_temperature", label: "#{room[:name]} Temperature", type: "Number",
|
|
192
|
+
icon: "temperature", tags: ["Temperature"], channel: "mqtt:topic:mqttbroker:temperature")
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
---
|
|
200
|
+
***Explanation***
|
|
201
|
+
+ This DSL structure defines a root location called "house", and iterates over each room from a dataset to dynamically generate nested location, equipment, and point blocks.
|
|
202
|
+
|
|
203
|
+
+ Each location represents a room, each equipment block represents a sensor in the room, and each point block defines a specific data point (e.g., temperature).
|
|
204
|
+
|
|
205
|
+
+ The DSL will be compiled into openHAB Items configuration DSL automatically.
|
|
206
|
+
|
|
207
|
+
***About table***
|
|
208
|
+
+ The variable table is an array of hashes that is generated by parsing either a Spreadsheet (Excel) or JSON file.
|
|
209
|
+
+ It is produced by using the Habdsl::SheetParser.parse(...) or Habdsl::JsonParser.parse(...) methods.
|
|
210
|
+
+ Each hash in table corresponds to a row of data in the input file. For example:
|
|
211
|
+
|
|
212
|
+
```json
|
|
213
|
+
[
|
|
214
|
+
{ "name": "livingroom", "label": "Living Room" },
|
|
215
|
+
{ "name": "bedroom", "label": "Bed Room" },
|
|
216
|
+
{ "name": "kitchen", "label": "Kitchen" }
|
|
217
|
+
]
|
|
218
|
+
```
|
|
219
|
+
+ Using table.each, the code dynamically creates DSL definitions based on that data.
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
### Spreadsheet
|
|
226
|
+
The Spreadsheet serves as a data source that is combined with your Ruby internal DSL code to automatically generate openHAB Items configuration DSL.
|
|
227
|
+
|
|
228
|
+
Each row (after the header) represents a unit of configuration, such as a room, a device, or a sensor, and contains fields like name and label. These fields are injected into the DSL using iteration (typically via table.each), allowing you to dynamically define multiple items based on spreadsheet contents.
|
|
229
|
+
|
|
230
|
+
- The **first row** must contain the **headers** (column names).
|
|
231
|
+
- Each **subsequent row** represents one configuration entry (e.g., an openHAB item).
|
|
232
|
+
- The headers can be arbitrary, but must match the keys used in the Ruby DSL code (e.g., `:name`, `:label`, etc.).
|
|
233
|
+
|
|
234
|
+
**Example:**
|
|
235
|
+
|
|
236
|
+
| name | label |
|
|
237
|
+
|-------|--------|
|
|
238
|
+
| livingroom | Living Room |
|
|
239
|
+
| bedroom | Bed Room |
|
|
240
|
+
| kitchen | Kitchen |
|
|
241
|
+
|
|
242
|
+
### JSON
|
|
243
|
+
The JSON file provides structured data that will be dynamically combined with your Ruby internal DSL code to generate openHAB Items configuration DSL.
|
|
244
|
+
|
|
245
|
+
Each object in the JSON array corresponds to a unit of configuration — for example, a room or device — and the data it contains (such as name and label) is injected into the DSL using iteration (typically via table.each).
|
|
246
|
+
|
|
247
|
+
+ The JSON file must represent an array of objects (dictionaries/hashes).
|
|
248
|
+
|
|
249
|
+
+ Each object represents one configuration entry (e.g., an openHAB item).
|
|
250
|
+
|
|
251
|
+
+ The keys in each object should match the ones used in the Ruby DSL code (e.g., :name, :label, etc.).
|
|
252
|
+
|
|
253
|
+
**Example:**
|
|
254
|
+
|
|
255
|
+
```json
|
|
256
|
+
[
|
|
257
|
+
{ "name": "livingroom", "label": "Living Room" },
|
|
258
|
+
{ "name": "bedroom", "label": "Bed Room" },
|
|
259
|
+
{ "name": "kitchen", "label": "Kitchen" }
|
|
260
|
+
]
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
## Usage
|
|
265
|
+
### Examples
|
|
266
|
+
The sample code is placed in example/.
|
|
267
|
+
Try it out by executing it.
|
|
268
|
+
|
|
269
|
+
```sh
|
|
270
|
+
$ bundle exec ruby example/example_dslparser.rb
|
|
271
|
+
$ bundle exec ruby example/example_jsonparser.rb
|
|
272
|
+
$ bundle exec ruby example/example_sheetparser.rb
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
### Make openHAB's Items configuration DSL from SpreadSheet
|
|
277
|
+
You can use the `Habdsl::SheetParser` class so habdsl automatically parses an Spreadsheet and Ruby internal DSL code, combining them to generate openHAB Items configuration DSL for item configuration.
|
|
278
|
+
|
|
279
|
+
#### Example usage
|
|
280
|
+
```ruby
|
|
281
|
+
require 'habdsl'
|
|
282
|
+
|
|
283
|
+
input_code = File.read("path/to/your_ruby_dsl_file")
|
|
284
|
+
excel_path = "path/to/your_excel_file"
|
|
285
|
+
output = Habdsl::SheetParser.parse(input_code: input_code, excel_path: excel_path)
|
|
286
|
+
output.table
|
|
287
|
+
# => Returns an array of hashes.
|
|
288
|
+
# Each hash maps the spreadsheet’s headers (from the first row) to the corresponding values in each data row.
|
|
289
|
+
# For example, if the headers are :name and :label:
|
|
290
|
+
# [
|
|
291
|
+
# { "name": "livingroom", "label": "Living Room" },
|
|
292
|
+
# { "name": "bedroom", "label": "Bed Room" },
|
|
293
|
+
# { "name": "kitchen", "label": "Kitchen" }
|
|
294
|
+
# ]
|
|
295
|
+
|
|
296
|
+
output.dsl
|
|
297
|
+
# => Returns a string containing the generated openHAB Items configuration DSL.
|
|
298
|
+
# For example,
|
|
299
|
+
|
|
300
|
+
# Group house "House" <house> ["Location"]
|
|
301
|
+
# Group livingroom "Living Room" <bedroom> (house) ["Location"]
|
|
302
|
+
# Group livingroom_sensor "livingroom_Sensor" <siren> (livingroom) ["Equipment"]
|
|
303
|
+
# Number livingroom_temperature "livingroom Temperature" <temperature> (livingroom_sensor) ["Temperature"]
|
|
304
|
+
# Group bedroom "Bed Room" <bedroom> (house) ["Location"]
|
|
305
|
+
# Group bedroom_sensor "bedroom_Sensor" <siren> (bedroom) ["Equipment"]
|
|
306
|
+
# Number bedroom_temperature "bedroom Temperature" <temperature> (bedroom_sensor) ["Temperature"]
|
|
307
|
+
# Group kitchen "Kitchen" <bedroom> (house) ["Location"]
|
|
308
|
+
# Group kitchen_sensor "kitchen_Sensor" <siren> (kitchen) ["Equipment"]
|
|
309
|
+
# Number kitchen_temperature "kitchen Temperature" <temperature> (kitchen_sensor) ["Temperature"]
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
### Make openHAB's Items configuration DSL from JSON
|
|
313
|
+
You can use the `Habdsl::JsonParser` class so habdsl automatically parses an JSON code and Ruby internal DSL code, combining them to generate openHAB Items configuration DSL for item configuration.
|
|
314
|
+
|
|
315
|
+
#### Example usage
|
|
316
|
+
```ruby
|
|
317
|
+
require 'habdsl'
|
|
318
|
+
|
|
319
|
+
input_code = File.read("path/to/your_ruby_dsl_file")
|
|
320
|
+
json_code = File.read("path/to/your_json_file")
|
|
321
|
+
output = Habdsl::JsonParser.parse(input_code: input_code, json_code: json_code)
|
|
322
|
+
output.table
|
|
323
|
+
# => Returns an array of hashes.
|
|
324
|
+
# For example,
|
|
325
|
+
# [
|
|
326
|
+
# { "name": "livingroom", "label": "Living Room" },
|
|
327
|
+
# { "name": "bedroom", "label": "Bed Room" },
|
|
328
|
+
# { "name": "kitchen", "label": "Kitchen" }
|
|
329
|
+
# ]
|
|
330
|
+
|
|
331
|
+
output.dsl
|
|
332
|
+
# => Returns a string containing the generated openHAB Items configuration DSL
|
|
333
|
+
# For example,
|
|
334
|
+
|
|
335
|
+
# Group house "House" <house> ["Location"]
|
|
336
|
+
# Group livingroom "Living Room" <bedroom> (house) ["Location"]
|
|
337
|
+
# Group livingroom_sensor "livingroom_Sensor" <siren> (livingroom) ["Equipment"]
|
|
338
|
+
# Number livingroom_temperature "livingroom Temperature" <temperature> (livingroom_sensor) ["Temperature"]
|
|
339
|
+
# Group bedroom "Bed Room" <bedroom> (house) ["Location"]
|
|
340
|
+
# Group bedroom_sensor "bedroom_Sensor" <siren> (bedroom) ["Equipment"]
|
|
341
|
+
# Number bedroom_temperature "bedroom Temperature" <temperature> (bedroom_sensor) ["Temperature"]
|
|
342
|
+
# Group kitchen "Kitchen" <bedroom> (house) ["Location"]
|
|
343
|
+
# Group kitchen_sensor "kitchen_Sensor" <siren> (kitchen) ["Equipment"]
|
|
344
|
+
# Number kitchen_temperature "kitchen Temperature" <temperature> (kitchen_sensor) ["Temperature"]
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
### Make openHAB's Items configuration DSL from Only DSL
|
|
348
|
+
You can use the `Habdsl::DslParser` class so habdsl automatically parses an Ruby internal DSL code and generate openHAB Items configuration DSL.
|
|
349
|
+
|
|
350
|
+
#### Example usage
|
|
351
|
+
```ruby
|
|
352
|
+
require 'habdsl'
|
|
353
|
+
|
|
354
|
+
input_code = <<~RUBY
|
|
355
|
+
location(name: "house", label: "House", icon: "house") do
|
|
356
|
+
location(name: "livingroom", label: "Living Room", icon: "bedroom") do
|
|
357
|
+
equipment(name: "sensor", label: "Sensor", icon: "siren") do
|
|
358
|
+
point(name: "temperature", label: "Temperature", type: "Number",
|
|
359
|
+
icon: "temperature", tags: ["Temperature"])
|
|
360
|
+
end
|
|
361
|
+
end
|
|
362
|
+
end
|
|
363
|
+
RUBY
|
|
364
|
+
|
|
365
|
+
output = Habdsl::DslParser.parse(input_code: input_code)
|
|
366
|
+
output.table
|
|
367
|
+
# => Returns nil.
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
output.dsl
|
|
371
|
+
# => Returns a string containing the generated openHAB Items configuration DSL
|
|
372
|
+
# For example,
|
|
373
|
+
|
|
374
|
+
# Group house "House" <house> ["Location"]
|
|
375
|
+
# Group livingroom "Living Room" <bedroom> (house) ["Location"]
|
|
376
|
+
# Group livingroom_sensor "livingroom_Sensor" <siren> (livingroom) ["Equipment"]
|
|
377
|
+
# Number livingroom_temperature "livingroom Temperature" <temperature> (livingroom_sensor) ["Temperature"]
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
### Make openHAB's Items configuration DSL by using `bin/habdsl_cli.rb`
|
|
383
|
+
|
|
384
|
+
You can use the `bin/habdsl_cli.rb` script to interactively select an Spreadsheet file, input Ruby internal DSL via the terminal, and automatically generate openHAB Items configuration DSL.
|
|
385
|
+
|
|
386
|
+
This command-line tool performs the following steps:
|
|
387
|
+
|
|
388
|
+
1. Prompts you to navigate directories and select a spreadsheet file (`.xls`, `.xlsx`, or `.xlsm`).
|
|
389
|
+
2. Accepts Ruby internal DSL code via standard input (end with two consecutive empty lines or `Ctrl + D`).
|
|
390
|
+
3. Combines the spreadsheet data and DSL to generate openHAB Items configuration DSL.
|
|
391
|
+
4. Prompts you for an output filename and saves the generated DSL under the `output/` directory.
|
|
392
|
+
|
|
393
|
+
#### Example usage
|
|
394
|
+
|
|
395
|
+
```sh
|
|
396
|
+
$ bundle exec ruby bin/habdsl_cli.rb
|
|
397
|
+
```
|
|
398
|
+
You'll then be prompted to:
|
|
399
|
+
|
|
400
|
+
1. Select an Spreadsheet file
|
|
401
|
+
2. Enter your Ruby DSL code
|
|
402
|
+
3. Provide a name for the output file (e.g., my_items.items)
|
|
403
|
+
4. After completion, the result will be saved in:
|
|
404
|
+
`output/your_output_file.items`
|
|
405
|
+
|
|
406
|
+
If any syntax errors or processing issues occur, they will be printed to the console with a detailed backtrace.
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
## Development
|
|
410
|
+
|
|
411
|
+
TODO
|
|
412
|
+
|
|
413
|
+
## Contributing
|
|
414
|
+
|
|
415
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/habdsl. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/habdsl/blob/main/CODE_OF_CONDUCT.md).
|
|
416
|
+
|
|
417
|
+
## License
|
|
418
|
+
|
|
419
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
420
|
+
|
|
421
|
+
## Code of Conduct
|
|
422
|
+
|
|
423
|
+
Everyone interacting in the JayFlavoredMarkdown project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/habdsl/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
location(name: "house", label: "House", icon: "house") do
|
|
4
|
+
table.each do |room|
|
|
5
|
+
location(name: "#{room[:name]}", label: "#{room[:label]}", icon: "bedroom") do
|
|
6
|
+
equipment(name: "#{room[:name]}_sensor", label: "#{room[:name]}_Sensor", icon: "siren") do
|
|
7
|
+
point(name: "#{room[:name]}_temperature", label: "#{room[:name]} Temperature", type: "Number",
|
|
8
|
+
icon: "temperature", tags: ["Temperature"], channel: "mqtt:topic:mqtt_topic:temperature")
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "#{Dir.pwd}/lib/habdsl"
|
|
4
|
+
|
|
5
|
+
input_code = <<~RUBY
|
|
6
|
+
location(name: "house", label: "House", icon: "house") do
|
|
7
|
+
location(name: "livingroom", label: "Living Room", icon: "bedroom") do
|
|
8
|
+
equipment(name: "sensor", label: "Sensor", icon: "siren") do
|
|
9
|
+
point(name: "temperature", label: "Temperature", type: "Number",
|
|
10
|
+
icon: "temperature", tags: ["Temperature"], channel: "mqtt:topic:mqttbroker:temperature")
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
RUBY
|
|
15
|
+
|
|
16
|
+
begin
|
|
17
|
+
output = Habdsl::DslParser.parse(input_code: input_code)
|
|
18
|
+
puts "=== Generated table ==="
|
|
19
|
+
p output.table
|
|
20
|
+
puts ""
|
|
21
|
+
puts "=== Generated DSL ==="
|
|
22
|
+
puts output.dsl
|
|
23
|
+
rescue StandardError => e
|
|
24
|
+
puts "エラー: #{e.message}"
|
|
25
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "#{Dir.pwd}/lib/habdsl"
|
|
4
|
+
|
|
5
|
+
input_code = File.read("#{Dir.pwd}/example/example_dsl.rb")
|
|
6
|
+
|
|
7
|
+
json_code = File.read("#{Dir.pwd}/example/example.json")
|
|
8
|
+
|
|
9
|
+
begin
|
|
10
|
+
output = Habdsl::JsonParser.parse(input_code: input_code, json_code: json_code)
|
|
11
|
+
puts "=== Generated table ==="
|
|
12
|
+
p output.table
|
|
13
|
+
puts ""
|
|
14
|
+
puts "=== Generated DSL ==="
|
|
15
|
+
puts output.dsl
|
|
16
|
+
rescue StandardError => e
|
|
17
|
+
puts "エラー: #{e.message}"
|
|
18
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "#{Dir.pwd}/lib/habdsl"
|
|
4
|
+
|
|
5
|
+
input_code = File.read("#{Dir.pwd}/example/example_dsl.rb")
|
|
6
|
+
|
|
7
|
+
excel_path = "#{Dir.pwd}/example/example.xlsx"
|
|
8
|
+
|
|
9
|
+
begin
|
|
10
|
+
output = Habdsl::SheetParser.parse(input_code: input_code, excel_path: excel_path)
|
|
11
|
+
puts "=== Generated table ==="
|
|
12
|
+
p output.table
|
|
13
|
+
puts ""
|
|
14
|
+
puts "=== Generated DSL ==="
|
|
15
|
+
puts output.dsl
|
|
16
|
+
rescue StandardError => e
|
|
17
|
+
puts "エラー: #{e.message}"
|
|
18
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "ripper"
|
|
4
|
+
require_relative "model/dsl"
|
|
5
|
+
require_relative "result_parser"
|
|
6
|
+
|
|
7
|
+
module Habdsl
|
|
8
|
+
# Class that parses DSL code and generates a result.
|
|
9
|
+
class BaseParser
|
|
10
|
+
class DSLValidationError < StandardError; end
|
|
11
|
+
|
|
12
|
+
def self.evaluate_dsl(input_code:, table:)
|
|
13
|
+
raise DSLValidationError, "Syntax error: DSL code contains a syntax error." if Ripper.sexp(input_code).nil?
|
|
14
|
+
|
|
15
|
+
if table.any? && !input_code.include?("table")
|
|
16
|
+
raise DSLValidationError, "'table' is not used in the DSL code but table data was provided."
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
dsl = Habdsl::Model::Dsl.new
|
|
20
|
+
defined_objects = []
|
|
21
|
+
|
|
22
|
+
eval_context = Object.new
|
|
23
|
+
|
|
24
|
+
eval_context.define_singleton_method(:location) do |**args, &block|
|
|
25
|
+
loc = dsl.location(**args, &block)
|
|
26
|
+
defined_objects << loc
|
|
27
|
+
loc
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
eval_context.define_singleton_method(:equipment) do |**args, &block|
|
|
31
|
+
eq = dsl.equipment(**args, &block)
|
|
32
|
+
defined_objects << eq
|
|
33
|
+
eq
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
eval_context.define_singleton_method(:point) do |**args|
|
|
37
|
+
dsl.point(**args)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
eval_context.define_singleton_method(:table) { table }
|
|
41
|
+
|
|
42
|
+
begin
|
|
43
|
+
eval_context.instance_eval(input_code)
|
|
44
|
+
rescue StandardError => e
|
|
45
|
+
raise DSLValidationError, "Runtime error during DSL evaluation: #{e.class} - #{e.message}"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
generated_output = defined_objects.map(&:to_s).join("\n")
|
|
49
|
+
|
|
50
|
+
if input_code.include?("table") && table.is_a?(Array) && !table.empty?
|
|
51
|
+
# Check if table keys were actually used in the generated output
|
|
52
|
+
unused_keys = table.first.keys.reject do |key|
|
|
53
|
+
table.any? {|row| generated_output.include?(row[key].to_s) }
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
if unused_keys.any?
|
|
57
|
+
raise DSLValidationError,
|
|
58
|
+
"Validation error: The following table keys are not used in the generated DSL output: #{unused_keys.join(', ')}"
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
ResultParser.new(table: table, dsl: generated_output)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "ripper"
|
|
4
|
+
require_relative "model/dsl"
|
|
5
|
+
require_relative "result_parser"
|
|
6
|
+
|
|
7
|
+
module Habdsl
|
|
8
|
+
# DSLのみを評価してopenHAB設定DSLを生成するパーサ
|
|
9
|
+
class DslParser
|
|
10
|
+
class DSLValidationError < StandardError; end
|
|
11
|
+
|
|
12
|
+
def self.parse(input_code:)
|
|
13
|
+
raise DSLValidationError, "Syntax error: DSL code contains a syntax error." if Ripper.sexp(input_code).nil?
|
|
14
|
+
|
|
15
|
+
dsl = Habdsl::Model::Dsl.new
|
|
16
|
+
defined_objects = []
|
|
17
|
+
|
|
18
|
+
eval_context = Object.new
|
|
19
|
+
|
|
20
|
+
eval_context.define_singleton_method(:location) do |**args, &block|
|
|
21
|
+
loc = dsl.location(**args, &block)
|
|
22
|
+
defined_objects << loc
|
|
23
|
+
loc
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
eval_context.define_singleton_method(:equipment) do |**args, &block|
|
|
27
|
+
eq = dsl.equipment(**args, &block)
|
|
28
|
+
defined_objects << eq
|
|
29
|
+
eq
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
eval_context.define_singleton_method(:point) do |**args|
|
|
33
|
+
dsl.point(**args)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
eval_context.instance_eval(input_code)
|
|
37
|
+
|
|
38
|
+
generated_output = defined_objects.map(&:to_s).join("\n")
|
|
39
|
+
|
|
40
|
+
ResultParser.new(dsl: generated_output, table: nil)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require_relative "base_parser"
|
|
5
|
+
|
|
6
|
+
module Habdsl
|
|
7
|
+
# Parser for JSON input
|
|
8
|
+
class JsonParser
|
|
9
|
+
def self.parse(input_code:, json_code:)
|
|
10
|
+
table = JSON.parse(json_code, symbolize_names: true)
|
|
11
|
+
|
|
12
|
+
raise ArgumentError, "Parsed JSON must be an Array of hashes" unless table.is_a?(Array)
|
|
13
|
+
|
|
14
|
+
raise ArgumentError, "JSON data array is empty" if table.empty?
|
|
15
|
+
|
|
16
|
+
BaseParser.evaluate_dsl(input_code: input_code, table: table)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "location"
|
|
4
|
+
require_relative "equipment"
|
|
5
|
+
require_relative "point"
|
|
6
|
+
|
|
7
|
+
module Habdsl
|
|
8
|
+
module Model
|
|
9
|
+
# DSL for defining locations, equipment, and points
|
|
10
|
+
class Dsl
|
|
11
|
+
def location(name:, label:, icon:, parent: nil, &)
|
|
12
|
+
loc = Habdsl::Model::Location.new(name: name, label: label, icon: icon, parent: parent)
|
|
13
|
+
loc.instance_eval(&) if block_given?
|
|
14
|
+
loc
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def equipment(name:, label:, icon:, parent: nil, &)
|
|
18
|
+
eq = Habdsl::Model::Equipment.new(name: name, label: label, icon: icon, parent: parent)
|
|
19
|
+
eq.instance_eval(&) if block_given?
|
|
20
|
+
eq
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def point(name:, label:, type:, icon:, tags:, parent: nil, channel: nil)
|
|
24
|
+
Habdsl::Model::Point.new(name: name, label: label, type: type, icon: icon, tags: tags, parent: parent,
|
|
25
|
+
channel: channel)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Habdsl
|
|
4
|
+
module Model
|
|
5
|
+
# Represents a piece of equipment in the DSL.
|
|
6
|
+
class Equipment
|
|
7
|
+
attr_reader :name, :label, :icon, :parent, :points, :sub_equipments
|
|
8
|
+
|
|
9
|
+
def initialize(name:, label:, icon:, parent: nil)
|
|
10
|
+
validate!(name, "name")
|
|
11
|
+
validate!(icon, "icon")
|
|
12
|
+
|
|
13
|
+
@name = name
|
|
14
|
+
@label = label
|
|
15
|
+
@icon = icon
|
|
16
|
+
@parent = parent
|
|
17
|
+
@points = []
|
|
18
|
+
@sub_equipments = []
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def equipment(name:, label:, icon:, parent: nil, &)
|
|
22
|
+
eq = Equipment.new(name: name, label: label, icon: icon, parent: parent)
|
|
23
|
+
eq.instance_eval(&) if block_given?
|
|
24
|
+
@sub_equipments << eq
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def point(name:, label:, type:, icon:, tags:, parent: nil, channel: nil)
|
|
28
|
+
@points << Point.new(name: name, label: label, type: type, icon: icon, tags: tags, parent: parent,
|
|
29
|
+
channel: channel)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def location(*)
|
|
33
|
+
raise "Error: location cannot be nested inside equipment"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def to_s(parent_name = nil)
|
|
37
|
+
result = "Group #{@name} \"#{@label}\" <#{@icon}>"
|
|
38
|
+
result += format_parent(parent_name, @parent)
|
|
39
|
+
result += " [\"Equipment\"]\n"
|
|
40
|
+
|
|
41
|
+
@sub_equipments.each {|sub_eq| result += sub_eq.to_s(@name) }
|
|
42
|
+
@points.each {|pt| result += pt.to_s(@name) }
|
|
43
|
+
|
|
44
|
+
result
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
|
|
49
|
+
def validate!(value, field)
|
|
50
|
+
return if value.nil?
|
|
51
|
+
return if value.match?(/\A[a-zA-Z_][a-zA-Z0-9_]*\z/)
|
|
52
|
+
|
|
53
|
+
raise ArgumentError, "#{field} is invalid: '#{value}'"
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def format_parent(structure_parent, explicit_parent)
|
|
57
|
+
return "" if structure_parent.nil? && explicit_parent.nil?
|
|
58
|
+
|
|
59
|
+
values = [structure_parent, explicit_parent].compact
|
|
60
|
+
" (#{values.join(', ')})"
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Habdsl
|
|
4
|
+
module Model
|
|
5
|
+
# Represents a location in the structure.
|
|
6
|
+
class Location
|
|
7
|
+
attr_reader :name, :label, :icon, :parent, :sub_locations, :equipments
|
|
8
|
+
|
|
9
|
+
def initialize(name:, label:, icon:, parent: nil)
|
|
10
|
+
validate!(name, "name")
|
|
11
|
+
validate!(icon, "icon")
|
|
12
|
+
|
|
13
|
+
@name = name
|
|
14
|
+
@label = label
|
|
15
|
+
@icon = icon
|
|
16
|
+
@parent = parent
|
|
17
|
+
@sub_locations = []
|
|
18
|
+
@equipments = []
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def location(name:, label:, icon:, parent: nil, &)
|
|
22
|
+
loc = Location.new(name: name, label: label, icon: icon, parent: parent)
|
|
23
|
+
loc.instance_eval(&) if block_given?
|
|
24
|
+
@sub_locations << loc
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def equipment(name:, label:, icon:, parent: nil, &)
|
|
28
|
+
eq = Equipment.new(name: name, label: label, icon: icon, parent: parent)
|
|
29
|
+
eq.instance_eval(&) if block_given?
|
|
30
|
+
@equipments << eq
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def point(*)
|
|
34
|
+
raise "Error: point cannot be directly under location. Use equipment to wrap it."
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def to_s(parent_name = nil)
|
|
38
|
+
result = "Group #{@name} \"#{@label}\" <#{@icon}>"
|
|
39
|
+
result += format_parent(parent_name, @parent)
|
|
40
|
+
result += " [\"Location\"]\n"
|
|
41
|
+
|
|
42
|
+
@sub_locations.each {|sub_loc| result += sub_loc.to_s(@name) }
|
|
43
|
+
@equipments.each {|eq| result += eq.to_s(@name) }
|
|
44
|
+
|
|
45
|
+
result
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
def validate!(value, field)
|
|
51
|
+
return if value.nil?
|
|
52
|
+
return if value.match?(/\A[a-zA-Z_][a-zA-Z0-9_]*\z/)
|
|
53
|
+
|
|
54
|
+
raise ArgumentError, "#{field} is invalid: '#{value}'"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def format_parent(structure_parent, explicit_parent)
|
|
58
|
+
return "" if structure_parent.nil? && explicit_parent.nil?
|
|
59
|
+
|
|
60
|
+
values = [structure_parent, explicit_parent].compact
|
|
61
|
+
" (#{values.join(', ')})"
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Habdsl
|
|
4
|
+
module Model
|
|
5
|
+
# Represents a point in the model.
|
|
6
|
+
class Point
|
|
7
|
+
attr_reader :name, :label, :type, :icon, :parent, :tags
|
|
8
|
+
|
|
9
|
+
def initialize(name:, label:, type:, icon:, tags:, parent: nil, channel: nil)
|
|
10
|
+
validate!(name, "name")
|
|
11
|
+
validate!(type, "type")
|
|
12
|
+
validate!(icon, "icon")
|
|
13
|
+
|
|
14
|
+
@name = name
|
|
15
|
+
@label = label
|
|
16
|
+
@type = type
|
|
17
|
+
@icon = icon
|
|
18
|
+
@tags = tags
|
|
19
|
+
@parent = parent
|
|
20
|
+
@channel = channel
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def location(*)
|
|
24
|
+
raise "Error: location cannot be nested inside point"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def equipment(*)
|
|
28
|
+
raise "Error: equipment cannot be nested inside point"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def to_s(parent_name = nil)
|
|
32
|
+
result = "#{@type} #{@name} \"#{@label}\" <#{@icon}>"
|
|
33
|
+
result += format_parent(parent_name, @parent)
|
|
34
|
+
result += " [#{@tags.map {|t| "\"#{t}\"" }.join(', ')}]"
|
|
35
|
+
result += " {channel=\"#{@channel}\"}" if @channel
|
|
36
|
+
result += "\n"
|
|
37
|
+
result
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def validate!(value, field)
|
|
43
|
+
return if value.nil?
|
|
44
|
+
return if value.match?(/\A[a-zA-Z_][a-zA-Z0-9_]*\z/)
|
|
45
|
+
|
|
46
|
+
raise ArgumentError, "#{field} is invalid: '#{value}'"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def format_parent(structure_parent, explicit_parent)
|
|
50
|
+
return "" if structure_parent.nil? && explicit_parent.nil?
|
|
51
|
+
|
|
52
|
+
values = [structure_parent, explicit_parent].compact
|
|
53
|
+
" (#{values.join(', ')})"
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "roo"
|
|
4
|
+
require_relative "base_parser"
|
|
5
|
+
|
|
6
|
+
module Habdsl
|
|
7
|
+
# Parser for Spreadsheet input
|
|
8
|
+
class SheetParser
|
|
9
|
+
def self.parse(input_code:, excel_path:)
|
|
10
|
+
raise ArgumentError, "Excel file does not exist: #{excel_path}" unless File.exist?(excel_path)
|
|
11
|
+
|
|
12
|
+
xlsx = Roo::Spreadsheet.open(excel_path.to_s)
|
|
13
|
+
sheet = xlsx.sheet(0)
|
|
14
|
+
headers = sheet.row(1).map(&:to_sym)
|
|
15
|
+
|
|
16
|
+
table = []
|
|
17
|
+
(2..sheet.last_row).each do |row_num|
|
|
18
|
+
row = sheet.row(row_num)
|
|
19
|
+
data = Hash[headers.zip(row)]
|
|
20
|
+
table << data
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
raise ArgumentError, "No data rows found in sheet" if table.empty?
|
|
24
|
+
|
|
25
|
+
BaseParser.evaluate_dsl(input_code: input_code, table: table)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
data/lib/habdsl.rb
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "habdsl/version"
|
|
4
|
+
require_relative "habdsl/model/dsl"
|
|
5
|
+
require_relative "habdsl/base_parser"
|
|
6
|
+
require_relative "habdsl/dsl_parser"
|
|
7
|
+
require_relative "habdsl/json_parser"
|
|
8
|
+
require_relative "habdsl/result_parser"
|
|
9
|
+
require_relative "habdsl/sheet_parser"
|
|
10
|
+
|
|
11
|
+
# Habdsl is a DSL (Domain-Specific Language) for defining and parsing
|
|
12
|
+
# structured data in a human-readable format. It provides a way to create
|
|
13
|
+
# and manipulate data structures using a simple and intuitive syntax.
|
|
14
|
+
#
|
|
15
|
+
# The library includes parsers for converting DSL code into JSON format,
|
|
16
|
+
# as well as for parsing JSON data into structured objects.
|
|
17
|
+
#
|
|
18
|
+
# @example Basic Usage
|
|
19
|
+
# require "habdsl"
|
|
20
|
+
#
|
|
21
|
+
# dsl_code = <<~DSL
|
|
22
|
+
# # Your DSL code here
|
|
23
|
+
# DSL
|
|
24
|
+
#
|
|
25
|
+
# json_code = <<~JSON
|
|
26
|
+
# # Your JSON code here
|
|
27
|
+
# JSON
|
|
28
|
+
#
|
|
29
|
+
# parser = Habdsl::JsonParser.new
|
|
30
|
+
# result = parser.parse(input_code: dsl_code, json_code: json_code)
|
|
31
|
+
#
|
|
32
|
+
# puts result.table
|
|
33
|
+
# puts result.dsl
|
|
34
|
+
module Habdsl
|
|
35
|
+
class Error < StandardError; end
|
|
36
|
+
# Your code goes here...
|
|
37
|
+
# You can define additional convenience methods or constants here if needed.
|
|
38
|
+
end
|
data/output/.keep
ADDED
|
File without changes
|
data/sig/habdsl.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: habdsl
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Nomura Laboratory
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2025-05-27 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: json
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 2.6.3
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 2.6.3
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: roo
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 2.10.1
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: 2.10.1
|
|
41
|
+
description: HABDSL is a DSL tool that reads Excel files and outputs openHAB-compatible
|
|
42
|
+
DSL configuration.
|
|
43
|
+
email:
|
|
44
|
+
- ''
|
|
45
|
+
executables: []
|
|
46
|
+
extensions: []
|
|
47
|
+
extra_rdoc_files: []
|
|
48
|
+
files:
|
|
49
|
+
- ".rspec"
|
|
50
|
+
- ".rubocop.yml"
|
|
51
|
+
- CHANGELOG.md
|
|
52
|
+
- CODE_OF_CONDUCT.md
|
|
53
|
+
- LICENSE.txt
|
|
54
|
+
- README.md
|
|
55
|
+
- Rakefile
|
|
56
|
+
- doc/tempareture_items.png
|
|
57
|
+
- example/example.json
|
|
58
|
+
- example/example.xlsx
|
|
59
|
+
- example/example_dsl.rb
|
|
60
|
+
- example/example_dslparser.rb
|
|
61
|
+
- example/example_jsonparser.rb
|
|
62
|
+
- example/example_sheetparser.rb
|
|
63
|
+
- lib/habdsl.rb
|
|
64
|
+
- lib/habdsl/base_parser.rb
|
|
65
|
+
- lib/habdsl/dsl_parser.rb
|
|
66
|
+
- lib/habdsl/json_parser.rb
|
|
67
|
+
- lib/habdsl/model/dsl.rb
|
|
68
|
+
- lib/habdsl/model/equipment.rb
|
|
69
|
+
- lib/habdsl/model/location.rb
|
|
70
|
+
- lib/habdsl/model/point.rb
|
|
71
|
+
- lib/habdsl/result_parser.rb
|
|
72
|
+
- lib/habdsl/sheet_parser.rb
|
|
73
|
+
- lib/habdsl/version.rb
|
|
74
|
+
- output/.keep
|
|
75
|
+
- sig/habdsl.rbs
|
|
76
|
+
homepage: https://github.com/nomlab/habdsl
|
|
77
|
+
licenses:
|
|
78
|
+
- MIT
|
|
79
|
+
metadata:
|
|
80
|
+
rubygems_mfa_required: 'true'
|
|
81
|
+
post_install_message:
|
|
82
|
+
rdoc_options: []
|
|
83
|
+
require_paths:
|
|
84
|
+
- lib
|
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ">="
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: 3.3.0
|
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
|
+
requirements:
|
|
92
|
+
- - ">="
|
|
93
|
+
- !ruby/object:Gem::Version
|
|
94
|
+
version: '0'
|
|
95
|
+
requirements: []
|
|
96
|
+
rubygems_version: 3.5.3
|
|
97
|
+
signing_key:
|
|
98
|
+
specification_version: 4
|
|
99
|
+
summary: A DSL for generating openHAB-compatible config from Excel.
|
|
100
|
+
test_files: []
|