Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[blockly] Remove Nashorn support & Check for required automation add-on in script edit #2743

Merged
merged 32 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1ac4526
check language installed for blockly and when running
mherwege Sep 16, 2024
0d00dfa
audio
mherwege Sep 16, 2024
05784e3
color
mherwege Sep 16, 2024
bbb6fa5
dateoffsets
mherwege Sep 16, 2024
595f5ba
dicts
mherwege Sep 16, 2024
4bce94c
ephemeris
mherwege Sep 16, 2024
9eaf2f6
eventbus
mherwege Sep 16, 2024
72fbb3c
http
mherwege Sep 16, 2024
e04741b
items
mherwege Sep 16, 2024
42f17f0
list
mherwege Sep 16, 2024
10f90ca
logging
mherwege Sep 16, 2024
5f89905
logic
mherwege Sep 16, 2024
ac61e43
math
mherwege Sep 16, 2024
4776c4e
metadata
mherwege Sep 16, 2024
051269c
notifications
mherwege Sep 16, 2024
04f268f
persistence
mherwege Sep 16, 2024
1bf664e
scripts
mherwege Sep 16, 2024
1d1737a
text
mherwege Sep 16, 2024
a7f34c4
things
mherwege Sep 16, 2024
bc566fa
timers
mherwege Sep 16, 2024
6ed546d
uom
mherwege Sep 16, 2024
9766f69
valuestorage
mherwege Sep 16, 2024
eb25d8c
remove Nashorn in templates and index
mherwege Sep 16, 2024
72d96b0
remove unused utils
mherwege Sep 16, 2024
0685997
message to convert
mherwege Sep 16, 2024
742bdf6
js scripting check in module wizards
mherwege Sep 16, 2024
953f1da
always check JS scripting installed
mherwege Sep 16, 2024
4664b84
Fix JS installed check not properly working for action/condition modu…
florian-h05 Sep 16, 2024
a398d7f
Change naming of computed value to be camel case
florian-h05 Sep 16, 2024
f9b7b8e
blockly-editor: Declare emits
florian-h05 Sep 16, 2024
bcdd276
Notification blocks: Fix code generation for log notification block
florian-h05 Sep 16, 2024
fda6c28
Move add-on names for MIME types to asset
florian-h05 Sep 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const AddonNames = {
'application/javascript': 'JavaScript Scripting',
'application/javascript;version=ECMAScript-5.1': 'JavaScript Scripting (Nashorn)',
'application/x-groovy': 'Groovy Scripting',
'application/x-python': 'Jython Scripting',
'application/x-ruby': 'JRuby Scripting'
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Blockly from 'blockly'
import { javascriptGenerator } from 'blockly/javascript.js'
import { FieldSlider } from '@blockly/field-slider'

export default function (f7, isGraalJs, sinks, voices) {
export default function (f7, sinks, voices) {
Blockly.Blocks['oh_volumeslider'] = {
init: function () {
this.appendDummyInput()
Expand Down Expand Up @@ -60,8 +60,7 @@ export default function (f7, isGraalJs, sinks, voices) {
let fileName = javascriptGenerator.valueToCode(block, 'fileName', javascriptGenerator.ORDER_ATOMIC)
let sinkName = javascriptGenerator.valueToCode(block, 'sinkName', javascriptGenerator.ORDER_ATOMIC).replace('(', '').replace(/[()]/g, '')

const audio = (isGraalJs) ? 'actions.Audio' : addAudio()
return `${audio}.playSound(${sinkName}, ${fileName});\n`
return `actions.Audio.playSound(${sinkName}, ${fileName});\n`
}

/*
Expand Down Expand Up @@ -98,12 +97,7 @@ export default function (f7, isGraalJs, sinks, voices) {
let sinkName = javascriptGenerator.valueToCode(block, 'sinkName', javascriptGenerator.ORDER_ATOMIC).replace('(', '').replace(/[()]/g, '')
let volume = javascriptGenerator.valueToCode(block, 'volume', javascriptGenerator.ORDER_ATOMIC).replace(/'/g, '')

if (isGraalJs) {
return `actions.Audio.playSound(${sinkName}, ${fileName}, (${volume}/100));\n`
} else {
const audio = addAudio()
return `${audio}.playSound(${sinkName}, ${fileName}, new PercentType(${volume}));\n`
}
return `actions.Audio.playSound(${sinkName}, ${fileName}, (${volume}/100));\n`
}

/*
Expand Down Expand Up @@ -135,8 +129,7 @@ export default function (f7, isGraalJs, sinks, voices) {
let url = javascriptGenerator.valueToCode(block, 'url', javascriptGenerator.ORDER_ATOMIC)
let sinkName = javascriptGenerator.valueToCode(block, 'sinkName', javascriptGenerator.ORDER_ATOMIC).replace('(', '').replace(/[()]/g, '')

const audio = (isGraalJs) ? 'actions.Audio' : addAudio()
return `${audio}.playStream(${sinkName}, ${url});\n`
return `actions.Audio.playStream(${sinkName}, ${url});\n`
}

/*
Expand Down Expand Up @@ -165,8 +158,7 @@ export default function (f7, isGraalJs, sinks, voices) {
let url = block.getFieldValue('url')
let sinkName = javascriptGenerator.valueToCode(block, 'sinkName', javascriptGenerator.ORDER_ATOMIC).replace('(', '').replace(/[()]/g, '')

const audio = (isGraalJs) ? 'actions.Audio' : addAudio()
return `${audio}.playStream(${sinkName}, null);\n`
return `actions.Audio.playStream(${sinkName}, null);\n`
}

/*
Expand Down Expand Up @@ -201,8 +193,7 @@ export default function (f7, isGraalJs, sinks, voices) {
const voiceName = javascriptGenerator.valueToCode(block, 'voice', javascriptGenerator.ORDER_ATOMIC).replace('(', '').replace(/[()]/g, '')
const deviceSink = javascriptGenerator.valueToCode(block, 'deviceSink', javascriptGenerator.ORDER_ATOMIC).replace('(', '').replace(/[()]/g, '')

const voice = (isGraalJs) ? 'actions.Voice' : addVoice()
return `${voice}.say(${textToSay}, ${voiceName}, ${deviceSink});\n`
return `actions.Voice.say(${textToSay}, ${voiceName}, ${deviceSink});\n`
}

/*
Expand Down Expand Up @@ -263,16 +254,4 @@ export default function (f7, isGraalJs, sinks, voices) {
let voiceName = block.getFieldValue('voiceName')
return [`'${voiceName}'`, javascriptGenerator.ORDER_NONE]
}

function addAudio () {
return javascriptGenerator.provideFunction_(
'audio',
['var ' + javascriptGenerator.FUNCTION_NAME_PLACEHOLDER_ + ' = Java.type(\'org.openhab.core.model.script.actions.Audio\');'])
}

function addVoice () {
return javascriptGenerator.provideFunction_(
'voice',
['var ' + javascriptGenerator.FUNCTION_NAME_PLACEHOLDER_ + ' = Java.type(\'org.openhab.core.model.script.actions.Voice\');'])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Blockly from 'blockly'
import { javascriptGenerator } from 'blockly/javascript.js'
import { blockGetCheckedInputType } from '@/assets/definitions/blockly/utils.js'

export default function (f7, isGraalJs) {
export default function (f7) {
/*
* converts a hex color string in to an openHAB hue-saturation-brightness string
* Block
Expand Down Expand Up @@ -69,12 +69,7 @@ export default function (f7, isGraalJs) {
const inputType = blockGetCheckedInputType(block, 'item')
let attributeName = block.getFieldValue('attributeName')

let code = ''
if (isGraalJs) {
code = (inputType === 'oh_item') ? `items.getItem(${theItem}).rawState.get${attributeName}()` : `${theItem}.rawState.get${attributeName}()`
} else {
code = (inputType === 'oh_item') ? `itemRegistry.getItem(${theItem}).getRawState().get${attributeName}()` : `${theItem}.getRawState().get${attributeName}()`
}
const code = (inputType === 'oh_item') ? `items.getItem(${theItem}).rawState.get${attributeName}()` : `${theItem}.rawState.get${attributeName}()`
return [code, 0]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import Blockly from 'blockly'
import { javascriptGenerator } from 'blockly/javascript.js'
import { FieldDatePicker } from './fields/date-field.js'
import { addDateSupport, addDateComparisonSupportNashorn, addDateComparisonSupportGraalVM, addGetZdtComponent, addChrono } from './utils.js'
import { addDateComparisonSupport } from './utils.js'

export default function (f7, isGraalJs) {
export default function (f7) {
/*
* Typed (DayOffset) block that can be used with the Ephemeris check block
* Note that the block basically returns a zero day offset for the check
Expand Down Expand Up @@ -80,8 +80,7 @@ export default function (f7, isGraalJs) {
* Code part
*/
javascriptGenerator.forBlock['oh_zdt_now'] = function (block) {
const zdt = (isGraalJs) ? 'time.ZonedDateTime' : addDateSupport()[1]
return [`${zdt}.now()`, javascriptGenerator.ORDER_NONE]
return ['time.ZonedDateTime.now()', javascriptGenerator.ORDER_NONE]
}

/*
Expand Down Expand Up @@ -117,8 +116,7 @@ export default function (f7, isGraalJs) {
const plusMinus = block.getFieldValue('plusminus')
const period = block.getFieldValue('period')

const zdt = (isGraalJs) ? 'time.ZonedDateTime' : addDateSupport()[1]
return [`${zdt}.now().${plusMinus}${period}(${offsetValue})`, javascriptGenerator.ORDER_ATOMIC]
return [`time.ZonedDateTime.now().${plusMinus}${period}(${offsetValue})`, javascriptGenerator.ORDER_ATOMIC]
}

/*
Expand Down Expand Up @@ -168,15 +166,8 @@ export default function (f7, isGraalJs) {
const minute = javascriptGenerator.valueToCode(block, 'minute', javascriptGenerator.ORDER_ATOMIC)
const second = javascriptGenerator.valueToCode(block, 'second', javascriptGenerator.ORDER_ATOMIC)

if (isGraalJs) {
const code = `time.ZonedDateTime.now().withYear(${year}).withMonth(${month}).withDayOfMonth(${day}).withHour(${hour}).withMinute(${minute}).withSecond(${second}).withNano(0)`
return [code, javascriptGenerator.ORDER_ATOMIC]
} else {
let [dtf, zdt, gzdt, czdt] = addDateSupport()
let stringToParse = `${czdt}(${year}, ${month} ,${day}, ${hour}, ${minute}, ${second}, 0, ${zdt}.now().getOffset().getId(), ${zdt}.now().getZone().getId())`
let code = `${zdt}.parse(${stringToParse}, ${dtf}.ISO_ZONED_DATE_TIME)`
return [code, javascriptGenerator.ORDER_ATOMIC]
}
const code = `time.ZonedDateTime.now().withYear(${year}).withMonth(${month}).withDayOfMonth(${day}).withHour(${hour}).withMinute(${minute}).withSecond(${second}).withNano(0)`
return [code, javascriptGenerator.ORDER_ATOMIC]
}

/*
Expand All @@ -202,8 +193,7 @@ export default function (f7, isGraalJs) {
*/
javascriptGenerator.forBlock['oh_zdt'] = function (block) {
const day = block.getFieldValue('day')
const getZonedDateTime = (isGraalJs) ? 'time.toZDT' : addDateSupport()[2]
return [`${getZonedDateTime}('${day}')`, javascriptGenerator.ORDER_NONE]
return [`time.toZDT('${day}')`, javascriptGenerator.ORDER_NONE]
}

/*
Expand Down Expand Up @@ -244,8 +234,7 @@ export default function (f7, isGraalJs) {
*/
javascriptGenerator.forBlock['oh_zdt_fromText'] = function (block) {
const day = javascriptGenerator.valueToCode(block, 'day', javascriptGenerator.ORDER_ATOMIC)
const getZonedDateTime = (isGraalJs) ? 'time.toZDT' : addDateSupport()[2]
return [`${getZonedDateTime}(${day})`, javascriptGenerator.ORDER_NONE]
return [`time.toZDT(${day})`, javascriptGenerator.ORDER_NONE]
}

/*
Expand All @@ -271,11 +260,7 @@ export default function (f7, isGraalJs) {
*/
javascriptGenerator.forBlock['oh_zdt_fromItem'] = function (block) {
const itemName = javascriptGenerator.valueToCode(block, 'itemName', javascriptGenerator.ORDER_ATOMIC)
if (isGraalJs) {
return [`time.toZDT(items.getItem(${itemName}))`, javascriptGenerator.ORDER_NONE]
} else {
return [`itemRegistry.getItem(${itemName}).getState().getZonedDateTime()`, javascriptGenerator.ORDER_NONE]
}
return [`time.toZDT(items.getItem(${itemName}))`, javascriptGenerator.ORDER_NONE]
}

const nextImage =
Expand Down Expand Up @@ -645,18 +630,17 @@ export default function (f7, isGraalJs) {
javascriptGenerator.forBlock['oh_zdt_toText'] = function (block) {
const date = javascriptGenerator.valueToCode(block, 'date', javascriptGenerator.ORDER_ATOMIC)
const withtime = block.getFieldValue('withtime')
const dtf = (isGraalJs) ? 'time.DateTimeFormatter' : addDateSupport()[0]

let code = ''
if (withtime === 'with') {
code = `${date}.format(${dtf}.ofPattern('yyyy-MM-dd HH:mm:ss'))`
code = `${date}.format(time.DateTimeFormatter.ofPattern('yyyy-MM-dd HH:mm:ss'))`
} else if (withtime === 'without') {
code = `${date}.format(${dtf}.ofPattern('yyyy-MM-dd'))`
code = `${date}.format(time.DateTimeFormatter.ofPattern('yyyy-MM-dd'))`
} else if (withtime === 'withPattern') {
const pattern = javascriptGenerator.valueToCode(block, 'pattern', javascriptGenerator.ORDER_ATOMIC)
code = `${date}.format(${dtf}.ofPattern(${pattern}))`
code = `${date}.format(time.DateTimeFormatter.ofPattern(${pattern}))`
} else {
code = `${date}.format(${dtf}.ofPattern('yyyy-MM-dd\\'T\\'HH:mm:ss.SSSZ'))`
code = `${date}.format(time.DateTimeFormatter.ofPattern('yyyy-MM-dd\\'T\\'HH:mm:ss.SSSZ'))`
}

return [code, javascriptGenerator.ORDER_NONE]
Expand Down Expand Up @@ -701,7 +685,7 @@ export default function (f7, isGraalJs) {
const precision = block.getFieldValue('precision')
const dateComparison = block.getFieldValue('dateComparison')

const zdtCompare = (isGraalJs) ? addDateComparisonSupportGraalVM() : addDateComparisonSupportNashorn()
const zdtCompare = addDateComparisonSupport()
return [`${zdtCompare}(${zdtOne}, ${zdtTwo}, '${operation}', '${precision}', '${dateComparison}')`, javascriptGenerator.ORDER_NONE]
}

Expand Down Expand Up @@ -737,21 +721,14 @@ export default function (f7, isGraalJs) {
* Code part
*/
javascriptGenerator.forBlock['oh_zdt_between'] = function (block) {
let zdtCompare = (isGraalJs) ? addDateComparisonSupportGraalVM() : addDateComparisonSupportNashorn()
let zdtCompare = addDateComparisonSupport()
let zdtOne = javascriptGenerator.valueToCode(block, 'zdtOne', javascriptGenerator.ORDER_ATOMIC)
let zdtTwo = javascriptGenerator.valueToCode(block, 'zdtTwo', javascriptGenerator.ORDER_ATOMIC)
let zdtThree = javascriptGenerator.valueToCode(block, 'zdtThree', javascriptGenerator.ORDER_ATOMIC)
let dateComparison = block.getFieldValue('dateComparison')

if (isGraalJs) {
const op = new Map([['dateandtime', 'isBetweenDateTimes'], ['date', 'isBetweenDates'], ['time', 'isBetweenTimes']]).get(dateComparison)
return [`${zdtOne}.${op}(${zdtTwo}, ${zdtThree})`, javascriptGenerator.ORDER_NONE]
} else {
let codeLow = `${zdtCompare}(${zdtTwo}, ${zdtOne}, 'beforeEqual', 'nanos', '${dateComparison}')`
let codeHigh = `${zdtCompare}(${zdtOne}, ${zdtThree}, 'beforeEqual', 'nanos', '${dateComparison}')`
let code = `(${codeLow} && ${codeHigh})`
return [code, javascriptGenerator.ORDER_NONE]
}
const op = new Map([['dateandtime', 'isBetweenDateTimes'], ['date', 'isBetweenDates'], ['time', 'isBetweenTimes']]).get(dateComparison)
return [`${zdtOne}.${op}(${zdtTwo}, ${zdtThree})`, javascriptGenerator.ORDER_NONE]
}

/*
Expand Down Expand Up @@ -781,39 +758,18 @@ export default function (f7, isGraalJs) {
const zdt = javascriptGenerator.valueToCode(block, 'zdt', javascriptGenerator.ORDER_ATOMIC)
let temporalPart = block.getFieldValue('temporalPart')

if (isGraalJs) {
const op = new Map([['getYear', 'year'], ['getMonthValue', 'monthValue'], ['getDayOfMonth', 'dayOfMonth'], ['getDayOfWeek', 'dayOfWeek().value'], ['getDayOfYear', 'dayOfYear'], ['getHour', 'hour'], ['getMinute', 'minute'], ['getSecond', 'second'], ['getNano', 'nano']])
switch (temporalPart) {
case 'getMilli':
temporalPart = 'getLong(time.ChronoField.MILLI_OF_SECOND)'
break
case 'getMicro':
temporalPart = 'getLong(time.ChronoField.MICRO_OF_SECOND) % 1000'
break
default:
temporalPart = op.get(temporalPart) + '()'
}
return [`${zdt}.${temporalPart}`, javascriptGenerator.ORDER_NONE]
} else {
const getZdtComponent = addGetZdtComponent()
const chrono = addChrono()
switch (temporalPart) {
case 'getMilli':
temporalPart = `getLong(${chrono}.MILLI_OF_SECOND)`
break
case 'getMicro':
temporalPart = `getLong(${chrono}.MICRO_OF_SECOND) % 1000`
break
case 'getNano':
temporalPart = `getLong(${chrono}.NANO_OF_SECOND) % 1000`
break
default:
temporalPart += '()'
}

let code = `${getZdtComponent}(${zdt}.${temporalPart})`
return [code, javascriptGenerator.ORDER_NONE]
const op = new Map([['getYear', 'year'], ['getMonthValue', 'monthValue'], ['getDayOfMonth', 'dayOfMonth'], ['getDayOfWeek', 'dayOfWeek().value'], ['getDayOfYear', 'dayOfYear'], ['getHour', 'hour'], ['getMinute', 'minute'], ['getSecond', 'second'], ['getNano', 'nano']])
switch (temporalPart) {
case 'getMilli':
temporalPart = 'getLong(time.ChronoField.MILLI_OF_SECOND)'
break
case 'getMicro':
temporalPart = 'getLong(time.ChronoField.MICRO_OF_SECOND) % 1000'
break
default:
temporalPart = op.get(temporalPart) + '()'
}
return [`${zdt}.${temporalPart}`, javascriptGenerator.ORDER_NONE]
}

/*
Expand Down Expand Up @@ -847,11 +803,7 @@ export default function (f7, isGraalJs) {
const zdtOne = javascriptGenerator.valueToCode(block, 'zdtOne', javascriptGenerator.ORDER_ATOMIC)
const zdtTwo = javascriptGenerator.valueToCode(block, 'zdtTwo', javascriptGenerator.ORDER_ATOMIC)

const chronoUnit = (isGraalJs) ? 'time.ChronoUnit' : javascriptGenerator.provideFunction_(
'chronoUnit',
['var ' + javascriptGenerator.FUNCTION_NAME_PLACEHOLDER_ + ' = Java.type("java.time.temporal.ChronoUnit");'])

let code = `${chronoUnit}.${temporalPart}.between(${zdtOne},${zdtTwo})`
const code = `time.ChronoUnit.${temporalPart}.between(${zdtOne},${zdtTwo})`
return [code, javascriptGenerator.ORDER_NONE]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Blockly from 'blockly'
import { javascriptGenerator } from 'blockly/javascript.js'

export default function (f7, isGraalJs) {
export default function (f7) {
Blockly.Blocks['dicts_create_with'] = {
/**
* Block for creating a list with any number of elements of any type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Blockly from 'blockly'
import { javascriptGenerator } from 'blockly/javascript.js'

export default function (f7, isGraalJs) {
export default function (f7) {
/*
* Checks if the provided day is a
* - bank holiday (needs to be configured in openHAB
Expand Down Expand Up @@ -38,20 +38,19 @@ export default function (f7, isGraalJs) {
* Code part
*/
javascriptGenerator.forBlock['oh_ephemeris_check'] = function (block) {
const ephemeris = (isGraalJs) ? 'actions.Ephemeris' : addEphemeris()
const dayInfo = javascriptGenerator.valueToCode(block, 'dayInfo', javascriptGenerator.ORDER_NONE)
const checkType = block.getFieldValue('checkType')
let code = ''

switch (checkType) {
case 'weekend':
code += `${ephemeris}.isWeekend(${dayInfo})`
code += `actions.Ephemeris.isWeekend(${dayInfo})`
break
case 'weekday':
code += `!${ephemeris}.isWeekend(${dayInfo})`
code += `!actions.Ephemeris.isWeekend(${dayInfo})`
break
case 'holiday':
code += `${ephemeris}.isBankHoliday(${dayInfo})`
code += `actions.Ephemeris.isBankHoliday(${dayInfo})`
break
}
return [code, javascriptGenerator.ORDER_FUNCTION_CALL]
Expand Down Expand Up @@ -80,9 +79,8 @@ export default function (f7, isGraalJs) {
* Code part
*/
javascriptGenerator.forBlock['oh_ephemeris_getHolidayName'] = function (block) {
const ephemeris = (isGraalJs) ? 'actions.Ephemeris' : addEphemeris()
const dayInfo = javascriptGenerator.valueToCode(block, 'dayInfo', javascriptGenerator.ORDER_NONE)
return [`${ephemeris}.getBankHolidayName(${dayInfo})`, javascriptGenerator.ORDER_NONE]
return [`actions.Ephemeris.getBankHolidayName(${dayInfo})`, javascriptGenerator.ORDER_NONE]
}

/*
Expand All @@ -107,17 +105,7 @@ export default function (f7, isGraalJs) {
* Code part
*/
javascriptGenerator.forBlock['oh_ephemeris_getDaysUntilHoliday'] = function (block) {
const ephemeris = (isGraalJs) ? 'actions.Ephemeris' : addEphemeris()
const holidayName = javascriptGenerator.valueToCode(block, 'holidayName', javascriptGenerator.ORDER_NONE)
return [`${ephemeris}.getDaysUntil(${holidayName})`, javascriptGenerator.ORDER_NONE]
}

/*
* Add ephemeris support to rule
*/
function addEphemeris () {
return javascriptGenerator.provideFunction_(
'ephemeris',
['var ' + javascriptGenerator.FUNCTION_NAME_PLACEHOLDER_ + ' = Java.type("org.openhab.core.model.script.actions.Ephemeris");'])
return [`actions.Ephemeris.getDaysUntil(${holidayName})`, javascriptGenerator.ORDER_NONE]
}
}
Loading