@atmosx/event-product-parser 3.0.4 → 3.0.7

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.
Files changed (212) hide show
  1. package/{CONTRIBUTING.md → .github/CONTRIBUTING.md} +1 -1
  2. package/.github/SUPPORTED_EVENTS.md +370 -0
  3. package/.github/docs/docs-functions.md +30 -0
  4. package/.github/docs/docs-general-configurations.md +199 -0
  5. package/.github/docs/docs-integrations.md +38 -0
  6. package/.github/docs/docs-listeners.md +28 -0
  7. package/.github/docs/docs-source-configuration.md +218 -0
  8. package/.github/docs/docs-technical.md +50 -0
  9. package/.github/logo.png +0 -0
  10. package/.github/samples/SAMPLE_EAS_GENERATION.wav +0 -0
  11. package/.github/samples/SAMPLE_GEOJSON.json +204 -0
  12. package/.github/samples/SAMPLE_TEXT_PRODUCT.txt +119 -0
  13. package/README.md +157 -337
  14. package/dist/cjs/index.cjs +24125 -15815
  15. package/dist/esm/index.mjs +24144 -15839
  16. package/package.json +56 -53
  17. package/pnpm-workspace.yaml +4 -0
  18. package/src/bootstrap.ts +83 -43
  19. package/src/building/CreateEvent.ts +54 -0
  20. package/src/{@building/building.clean.ts → building/GetCleanedEvent.ts} +4 -4
  21. package/src/building/GetEventAttachments.ts +88 -0
  22. package/src/building/GetEventDirection.ts +28 -0
  23. package/src/{@building/building.enhance.ts → building/GetEventEnhancedName.ts} +13 -17
  24. package/src/{@building/building.geometry.ts → building/GetEventGeometry.ts} +19 -13
  25. package/src/{@building/building.headers.ts → building/GetEventHeader.ts} +13 -13
  26. package/src/{@building/building.polygon.ts → building/GetEventNodes.ts} +26 -22
  27. package/src/{@building/building.office.ts → building/GetEventOffice.ts} +14 -14
  28. package/src/building/GetEventProperties.ts +105 -0
  29. package/src/{@building/building.signature.ts → building/GetEventSignature.ts} +16 -17
  30. package/src/{@core/core.createListener.ts → building/GetEventTags.ts} +11 -6
  31. package/src/building/GetEventTheme.ts +26 -0
  32. package/src/building/GetEventTracking.ts +64 -0
  33. package/src/components/audio/GenerateAudioMessage.ts +149 -0
  34. package/src/components/audio/components/GetCleanDescription.ts +91 -0
  35. package/src/{@modules/@eas/eas.getFloatPCM16.ts → components/audio/components/GetFloatPCM16.ts} +3 -3
  36. package/src/{@modules/@eas/eas.getMergedPCM16.ts → components/audio/components/GetMergedPCM16.ts} +3 -3
  37. package/src/{@modules/@eas/eas.getPCM16.ts → components/audio/components/GetPCM16.ts} +13 -8
  38. package/src/{@modules/@eas/eas.getPCMToFloat.ts → components/audio/components/GetPCMToFloat.ts} +3 -3
  39. package/src/{@modules/@eas/eas.getSampledPCM16.ts → components/audio/components/GetSampledPCM16.ts} +14 -8
  40. package/src/components/audio/components/GetTTS.ts +61 -0
  41. package/src/{@modules/@eas/eas.getWavPCM16.ts → components/audio/components/GetWavPCM16.ts} +8 -8
  42. package/src/{@modules/@eas/eas.setAFSK.ts → components/audio/components/SetAFSK.ts} +12 -8
  43. package/src/{@modules/@eas/eas.setAsciiToBits.ts → components/audio/components/SetAsciiToBits.ts} +3 -4
  44. package/src/{@modules/@eas/eas.setAttentionTone.ts → components/audio/components/SetAttentionTone.ts} +11 -6
  45. package/src/{@modules/@eas/eas.setNoise.ts → components/audio/components/SetNoise.ts} +14 -8
  46. package/src/{@modules/@eas/eas.setRadioEffect.ts → components/audio/components/SetRadioEffect.ts} +14 -8
  47. package/src/{@modules/@eas/eas.setSameHeader.ts → components/audio/components/SetSameHeader.ts} +22 -15
  48. package/src/{@modules/@utilities/utilities.setWarning.ts → components/database/CreateQuery.ts} +18 -13
  49. package/src/{@modules/@database/database.cache.ts → components/database/GetCachedEvents.ts} +19 -14
  50. package/src/components/database/ImportBoundaries.ts +168 -0
  51. package/src/components/database/ImportBroadcastify.ts +60 -0
  52. package/src/components/database/ImportCities.ts +87 -0
  53. package/src/{@modules/@database/database.shapefiles.ts → components/database/ImportShapefiles.ts} +31 -37
  54. package/src/components/database/ImportStanza.ts +43 -0
  55. package/src/components/database/InitializeDatabase.ts +71 -0
  56. package/src/components/images/GenerateGraphic.ts +234 -0
  57. package/src/components/images/components/CreateRectangle.ts +22 -0
  58. package/src/components/images/components/CreateText.ts +22 -0
  59. package/src/components/images/modules/CreateSVG.ts +37 -0
  60. package/src/components/images/modules/GetGeographicalBoundaries.ts +72 -0
  61. package/src/components/images/modules/GetGeographicalCities.ts +58 -0
  62. package/src/components/images/modules/GetGeographicalEvents.ts +48 -0
  63. package/src/components/images/modules/GetGeometryBounds.ts +82 -0
  64. package/src/components/images/modules/GetParsedBoundary.ts +58 -0
  65. package/src/components/images/modules/GetSVGPath.ts +53 -0
  66. package/src/components/images/modules/NormalizePolygon.ts +57 -0
  67. package/src/{@modules/@stanza/stanza.getAwipsType.ts → components/stanza/GetAwipsType.ts} +19 -18
  68. package/src/components/stanza/ValidateStanza.ts +62 -0
  69. package/src/components/utilities/CreateHttp.ts +83 -0
  70. package/src/{@modules/@utilities/utilities.getFormattedTime.ts → components/utilities/GetFormattedTime.ts} +3 -3
  71. package/src/components/utilities/GetLatestIssuance.ts +32 -0
  72. package/src/components/utilities/GetMatched.ts +38 -0
  73. package/src/{@modules/@utilities/utilities.getShapeNearestPoint.ts → components/utilities/GetShapeNearestPoint.ts} +24 -19
  74. package/src/components/utilities/GetUnionPolygon.ts +36 -0
  75. package/src/components/utilities/QueueManager.ts +70 -0
  76. package/src/components/utilities/SetCronSchedule.ts +98 -0
  77. package/src/{@core/core.clearEvents.ts → components/utilities/SetDebug.ts} +17 -12
  78. package/src/components/utilities/SetEventEmit.ts +42 -0
  79. package/src/{@modules/@utilities/utilities.setSettings.ts → components/utilities/SetSettings.ts} +7 -7
  80. package/src/{@modules/@utilities/utilities.setSleep.ts → components/utilities/SetSleep.ts} +5 -5
  81. package/src/components/utilities/SetTimeoutAction.ts +64 -0
  82. package/src/components/utilities/SetWarning.ts +47 -0
  83. package/src/{@modules/@xmpp/xmpp.xDeploy.ts → components/xmpp/DeployXMPP.ts} +26 -27
  84. package/src/components/xmpp/ErrorXMPP.ts +27 -0
  85. package/src/{@modules/@xmpp/xmpp.xError.ts → components/xmpp/OfflineXMPP.ts} +17 -9
  86. package/src/{@modules/@xmpp/xmpp.xOnline.ts → components/xmpp/OnlineXMPP.ts} +21 -20
  87. package/src/components/xmpp/ReconnectXMPP.ts +64 -0
  88. package/src/components/xmpp/StanzaXMPP.ts +63 -0
  89. package/src/{@building/building.tags.ts → core/ClearEvents.ts} +12 -5
  90. package/src/{@core/core.getVersion.ts → core/GetEvents.ts} +5 -6
  91. package/src/core/GetNodes.ts +24 -0
  92. package/src/{@core/core.getNodes.ts → core/GetRandomEvent.ts} +5 -6
  93. package/src/core/GetVersion.ts +24 -0
  94. package/src/core/ManualEvent.ts +55 -0
  95. package/src/core/QueryStanza.ts +40 -0
  96. package/src/core/SetNode.ts +87 -0
  97. package/src/core/StartService.ts +62 -0
  98. package/src/core/StopService.ts +32 -0
  99. package/src/enums/AWIPS.ts +366 -0
  100. package/src/{@dictionaries/dictionaries.eventActions.ts → enums/Actions.ts} +3 -3
  101. package/src/{@dictionaries/dictionaries.eventCancelMessages.ts → enums/Cancellation.ts} +4 -3
  102. package/src/{@dictionaries/dictionaries.eventCauses.ts → enums/Causes.ts} +3 -3
  103. package/src/{@dictionaries/dictionaries.statusCorrelationText.ts → enums/Correlations.ts} +3 -3
  104. package/src/enums/Enhanced.ts +70 -0
  105. package/src/{@dictionaries/dictionaries.eventTypes.ts → enums/Events.ts} +3 -3
  106. package/src/{@dictionaries/dictionaries.regExp.ts → enums/Expressions.ts} +5 -4
  107. package/src/{@core/core.getEvents.ts → enums/GlobalFilter.ts} +7 -8
  108. package/src/{@dictionaries/dictionaries.hailStrings.ts → enums/Hail.ts} +9 -4
  109. package/src/{@dictionaries/dictionaries.officeICAOs.ts → enums/ICAO.ts} +5 -3
  110. package/src/enums/Matches.ts +60 -0
  111. package/src/{@dictionaries/dictionaries.eventProducts.ts → enums/Products.ts} +3 -3
  112. package/src/{@dictionaries/dictionaries.eventRecords.ts → enums/Records.ts} +3 -3
  113. package/src/{@dictionaries/dictionaries.eventSeverity.ts → enums/Severity.ts} +3 -3
  114. package/src/{@dictionaries/dictionaries.shapefileLinks.ts → enums/Shapefiles.ts} +3 -3
  115. package/src/enums/States.ts +136 -0
  116. package/src/{@dictionaries/dictionaries.eventStatus.ts → enums/Status.ts} +3 -3
  117. package/src/enums/Strings.ts +22 -0
  118. package/src/enums/Tags.ts +172 -0
  119. package/src/{@dictionaries/dictionaries.testSignatures.ts → enums/Testing.ts} +5 -4
  120. package/src/enums/Themes.ts +70 -0
  121. package/src/events/ParseAPI.ts +129 -0
  122. package/src/{@events/events.text.ts → events/ParseText.ts} +32 -23
  123. package/src/{@events/events.ugc.ts → events/ParseUGC.ts} +36 -26
  124. package/src/events/ParseVTEC.ts +88 -0
  125. package/src/index.ts +60 -38
  126. package/src/manager/MakeEvents.ts +110 -0
  127. package/src/manager/RemoveEvent.ts +74 -0
  128. package/src/{@manager/manager.setHash.ts → manager/SetHash.ts} +19 -15
  129. package/src/manager/UpdateEvents.ts +35 -0
  130. package/src/manager/UpdateNode.ts +62 -0
  131. package/src/manager/ValidateEvents.ts +144 -0
  132. package/src/{@parsers/@hvtec/hvtec.extract.ts → parsers/hvtec/HVExtract.ts} +13 -13
  133. package/src/parsers/text/GetDescriptionFromProduct.ts +59 -0
  134. package/src/parsers/text/GetEmbededText.ts +57 -0
  135. package/src/parsers/text/GetPolygonFromProduct.ts +71 -0
  136. package/src/parsers/text/GetStringText.ts +58 -0
  137. package/src/{@parsers/@text/text.getTextFromProduct.ts → parsers/text/GetTextFromProduct.ts} +12 -12
  138. package/src/parsers/ugc/GetCache.ts +24 -0
  139. package/src/{@parsers/@ugc/ugc.expiry.ts → parsers/ugc/GetExpiry.ts} +3 -3
  140. package/src/{@parsers/@ugc/ugc.header.ts → parsers/ugc/GetHeader.ts} +6 -6
  141. package/src/parsers/ugc/GetLocations.ts +57 -0
  142. package/src/parsers/ugc/GetZonePolygon.ts +73 -0
  143. package/src/{@parsers/@ugc/ugc.zones.ts → parsers/ugc/GetZones.ts} +3 -3
  144. package/src/{@modules/@utilities/utilities.setListener.ts → parsers/ugc/SetCache.ts} +13 -10
  145. package/src/{@parsers/@ugc/ugc.extract.ts → parsers/ugc/UGCExtract.ts} +15 -15
  146. package/src/{@parsers/@pvtec/pvtec.expires.ts → parsers/vtec/GetExpiry.ts} +3 -3
  147. package/src/parsers/vtec/VTECExtract.ts +49 -0
  148. package/src/{@types/types.attributes.ts → static/Attributes.ts} +2 -3
  149. package/src/{@types/type.event.ts → static/Event.ts} +11 -8
  150. package/src/{@types/type.properties.ts → static/Properties.ts} +17 -10
  151. package/src/{@types/types.stanza.ts → static/Stanza.ts} +2 -2
  152. package/src/tasks/CreateTasks.ts +123 -0
  153. package/src/{@core/core.stop.ts → tasks/TaskGenerateJSON.ts} +14 -12
  154. package/src/tasks/TaskGenerateText.ts +34 -0
  155. package/src/tasks/TaskSendNTFY.ts +97 -0
  156. package/src/tasks/TaskSendWebhook.ts +107 -0
  157. package/src/types/Actions.ts +37 -0
  158. package/src/{@types/types.hvtec.ts → types/HVTEC.ts} +6 -6
  159. package/src/{@types/types.hash.ts → types/Hash.ts} +5 -5
  160. package/src/types/Nodes.ts +29 -0
  161. package/src/{@types/types.settings.ts → types/Settings.ts} +42 -10
  162. package/src/{@types/types.compiled.ts → types/StanzaCompiled.ts} +15 -14
  163. package/src/{@types/types.ugc.ts → types/UGC.ts} +5 -5
  164. package/src/{@modules/@utilities/utilities.getSettings.ts → types/VTEC.ts} +12 -7
  165. package/tools/build.js +47 -0
  166. package/tsconfig.json +28 -1
  167. package/dist/index.d.mts +0 -239
  168. package/dist/index.d.ts +0 -239
  169. package/src/@building/building.create.ts +0 -42
  170. package/src/@building/building.properties.ts +0 -89
  171. package/src/@building/building.tracking.ts +0 -68
  172. package/src/@building/building.validate.ts +0 -147
  173. package/src/@core/core.getRandomEvent.ts +0 -25
  174. package/src/@core/core.setNode.ts +0 -81
  175. package/src/@core/core.start.ts +0 -61
  176. package/src/@dictionaries/dictionaries.betterEventNames.ts +0 -99
  177. package/src/@dictionaries/dictionaries.eventAwipAbreviations.ts +0 -364
  178. package/src/@dictionaries/dictionaries.eventTags.ts +0 -93
  179. package/src/@dictionaries/dictionaries.eventsMatchText.ts +0 -36
  180. package/src/@dictionaries/dictionaries.transcribedMessageReplacements.ts +0 -68
  181. package/src/@events/events.api.ts +0 -113
  182. package/src/@events/events.vtec.ts +0 -87
  183. package/src/@manager/manager.mkEvent.ts +0 -96
  184. package/src/@manager/manager.rmEvent.ts +0 -49
  185. package/src/@manager/manager.updateEvents.ts +0 -35
  186. package/src/@manager/manager.updateNodes.ts +0 -55
  187. package/src/@manager/manager.updateWebhooks.ts +0 -50
  188. package/src/@modules/@database/database.init.ts +0 -45
  189. package/src/@modules/@database/database.stanza.ts +0 -48
  190. package/src/@modules/@eas/eas.setEasTone.ts +0 -137
  191. package/src/@modules/@stanza/stanza.validate.ts +0 -50
  192. package/src/@modules/@utilities/utilities.createHttp.ts +0 -85
  193. package/src/@modules/@utilities/utilities.createWebhook.ts +0 -115
  194. package/src/@modules/@utilities/utilities.setCronSchedule.ts +0 -65
  195. package/src/@modules/@utilities/utilities.setEventEmit.ts +0 -41
  196. package/src/@modules/@utilities/utilities.setTimeoutAction.ts +0 -59
  197. package/src/@modules/@xmpp/xmpp.xOffline.ts +0 -38
  198. package/src/@modules/@xmpp/xmpp.xReconnect.ts +0 -64
  199. package/src/@modules/@xmpp/xmpp.xStanza.ts +0 -63
  200. package/src/@parsers/@pvtec/pvtec.extract.ts +0 -49
  201. package/src/@parsers/@text/text.getDescriptionFromProduct.ts +0 -53
  202. package/src/@parsers/@text/text.getPolygonFromProduct.ts +0 -32
  203. package/src/@parsers/@text/text.getXML.ts +0 -61
  204. package/src/@parsers/@ugc/ugc.coordinates.ts +0 -110
  205. package/src/@parsers/@ugc/ugc.locations.ts +0 -29
  206. package/src/@types/types.pvtec.ts +0 -30
  207. package/src/@types/types.webhook.ts +0 -27
  208. package/test.js +0 -91
  209. package/tsup.config.ts +0 -16
  210. /package/{CODE_OF_CONDUCT.md → .github/CODE_OF_CONDUCT.md} +0 -0
  211. /package/{LICENSE → .github/LICENSE} +0 -0
  212. /package/{SECURITY.md → .github/SECURITY.md} +0 -0
@@ -0,0 +1,168 @@
1
+ /*
2
+ _ _ _ __ __
3
+ /\ | | | | (_) \ \ / /
4
+ / \ | |_ _ __ ___ ___ ___ _ __ | |__ ___ _ __ _ ___ \ V /
5
+ / /\ \| __| '_ ` _ \ / _ \/ __| '_ \| '_ \ / _ \ '__| |/ __| > <
6
+ / ____ \ |_| | | | | | (_) \__ \ |_) | | | | __/ | | | (__ / . \
7
+ /_/ \_\__|_| |_| |_|\___/|___/ .__/|_| |_|\___|_| |_|\___/_/ \_\
8
+ | |
9
+ |_|
10
+
11
+ Created with ♥ by the AtmosphericX Team (KiyoWx, StarflightWx, & CJ Ziegler)
12
+ Discord: https://atmosphericx-discord.scriptkitty.cafe
13
+ Ko-Fi: https://ko-fi.com/k3yomi
14
+ Documentation: https://atmosphericx.scriptkitty.cafe/documentation
15
+
16
+ Independent Package: @atmosx/event-product-parser
17
+
18
+ */
19
+
20
+ import { EnumStateFIPS } from "@Enums/States"
21
+ import { Bootstrap } from "@Bootstrap"
22
+ import { CreateHttp } from "@Utilities/CreateHttp"
23
+ import { CreateQuery } from "@Database/CreateQuery"
24
+ import { SetWarning } from "@Utilities/SetWarning"
25
+ import { feature } from "topojson-client"
26
+
27
+ type BoundaryRow = {
28
+ id: string
29
+ type: `state` | `county`
30
+ state: string
31
+ name: string
32
+ geometry: string
33
+ }
34
+
35
+ export const ImportBoundaries = async (): Promise<void> => {
36
+ try {
37
+ const existing = CreateQuery({ Query: `SELECT type, COUNT(*) AS count FROM boundaries GROUP BY type`}) as { type: string, count: number }[]
38
+ const existingStates = existing.find(row => row.type === `state`)?.count ?? 0
39
+ const existingCounties = existing.find(row => row.type === `county`)?.count ?? 0
40
+
41
+ if (existingStates > 0 || existingCounties > 0) {
42
+ SetWarning({ Message: `Boundary database already contains ${existingStates} states and ${existingCounties} counties, skipping import` })
43
+ return
44
+ }
45
+
46
+ const boundaries = await CreateHttp({
47
+ URL: Bootstrap.Settings.BoundarySettings.BoundaryDatabase,
48
+ Timeout: 5000
49
+ })
50
+
51
+ if (boundaries.error || !boundaries.message) {
52
+ SetWarning({ Message: `Failed to download boundary database` })
53
+ return
54
+ }
55
+
56
+ let database: any
57
+
58
+ try {
59
+ database = JSON.parse(boundaries.message)
60
+ } catch {
61
+ SetWarning({ Message: `Failed to parse boundary database` })
62
+ return
63
+ }
64
+
65
+ if (database?.type !== `Topology` || !database?.objects?.states || !database?.objects?.counties) {
66
+ SetWarning({ Message: `Boundary database is missing states or counties` })
67
+ return
68
+ }
69
+
70
+ let states: any
71
+ let counties: any
72
+
73
+ try {
74
+ states = feature(database, database.objects.states)
75
+ counties = feature(database, database.objects.counties)
76
+ } catch (error: unknown) {
77
+ const message = error instanceof Error ? error.message : String(error)
78
+ SetWarning({ Message: `Failed to convert boundary database: ${message}` })
79
+ return
80
+ }
81
+
82
+ if (!Array.isArray(states?.features) || !Array.isArray(counties?.features)) {
83
+ SetWarning({ Message: `Boundary database contains invalid state or county data` })
84
+ return
85
+ }
86
+
87
+ const insert = `INSERT OR REPLACE INTO boundaries (id, type, state, name, geometry) VALUES (?, ?, ?, ?, ?)`
88
+ const stateRows: BoundaryRow[] = []
89
+ const countyRows: BoundaryRow[] = []
90
+ let skippedStates = 0
91
+ let skippedCounties = 0
92
+
93
+ for (const state of states.features) {
94
+ if (!state?.geometry || state.id === undefined || state.id === null) {
95
+ skippedStates++
96
+ continue
97
+ }
98
+
99
+ const fips = String(state.id).padStart(2, `0`)
100
+ const abbreviation = EnumStateFIPS[fips]
101
+
102
+ if (!abbreviation) {
103
+ skippedStates++
104
+ continue
105
+ }
106
+
107
+ stateRows.push({
108
+ id: fips,
109
+ type: `state`,
110
+ state: abbreviation,
111
+ name: state.properties?.name ?? abbreviation,
112
+ geometry: JSON.stringify(state.geometry)
113
+ })
114
+ }
115
+
116
+ for (const county of counties.features) {
117
+ if (!county?.geometry || county.id === undefined || county.id === null) {
118
+ skippedCounties++
119
+ continue
120
+ }
121
+
122
+ const fips = String(county.id).padStart(5, `0`)
123
+ const stateFIPS = fips.slice(0, 2)
124
+ const abbreviation = EnumStateFIPS[stateFIPS]
125
+
126
+ if (!abbreviation) {
127
+ skippedCounties++
128
+ continue
129
+ }
130
+
131
+ countyRows.push({
132
+ id: fips,
133
+ type: `county`,
134
+ state: abbreviation,
135
+ name: county.properties?.name ?? fips,
136
+ geometry: JSON.stringify(county.geometry)
137
+ })
138
+ }
139
+
140
+ SetWarning({ Message: `Importing ${stateRows.length} states and ${countyRows.length} counties` })
141
+
142
+ const transaction = Bootstrap.Database.transaction((rows: BoundaryRow[]) => {
143
+ for (const row of rows) {
144
+ CreateQuery({
145
+ Query: insert,
146
+ Parameters: [row.id, row.type, row.state, row.name, row.geometry]
147
+ })
148
+ }
149
+ })
150
+
151
+ transaction(stateRows)
152
+ transaction(countyRows)
153
+
154
+ const imported = CreateQuery({ Query: `SELECT type, COUNT(*) AS count FROM boundaries GROUP BY type`}) as { type: string, count: number }[]
155
+ const importedStates = imported.find(row => row.type === `state`)?.count ?? 0
156
+ const importedCounties = imported.find(row => row.type === `county`)?.count ?? 0
157
+
158
+ if (importedStates !== stateRows.length || importedCounties !== countyRows.length) {
159
+ SetWarning({ Message: `Boundary import verification failed: ${importedStates} states and ${importedCounties} counties` })
160
+ return
161
+ }
162
+
163
+ SetWarning({ Message: `Imported ${importedStates} states and ${importedCounties} counties` })
164
+ } catch (error: unknown) {
165
+ const message = error instanceof Error ? error.message : String(error)
166
+ SetWarning({ Message: `An error occurred while importing boundaries: ${message}` })
167
+ }
168
+ }
@@ -0,0 +1,60 @@
1
+ /*
2
+ _ _ _ __ __
3
+ /\ | | | | (_) \ \ / /
4
+ / \ | |_ _ __ ___ ___ ___ _ __ | |__ ___ _ __ _ ___ \ V /
5
+ / /\ \| __| '_ ` _ \ / _ \/ __| '_ \| '_ \ / _ \ '__| |/ __| > <
6
+ / ____ \ |_| | | | | | (_) \__ \ |_) | | | | __/ | | | (__ / . \
7
+ /_/ \_\__|_| |_| |_|\___/|___/ .__/|_| |_|\___|_| |_|\___/_/ \_\
8
+ | |
9
+ |_|
10
+
11
+ Created with ♥ by the AtmosphericX Team (KiyoWx, StarflightWx, & CJ Ziegler)
12
+ Discord: https://atmosphericx-discord.scriptkitty.cafe
13
+ Ko-Fi: https://ko-fi.com/k3yomi
14
+ Documentation: https://atmosphericx.scriptkitty.cafe/documentation
15
+
16
+ Independent Package: @atmosx/event-product-parser
17
+
18
+ */
19
+
20
+ import { Bootstrap } from "@Bootstrap"
21
+ import { CreateHttp } from "@Utilities/CreateHttp"
22
+ import { SetWarning } from "@Utilities/SetWarning"
23
+ import { CreateQuery } from "@Database/CreateQuery";
24
+
25
+ export const ImportBroadcastify = async (): Promise<void> => {
26
+ const settings = Bootstrap.Settings;
27
+ const broadcastify = await CreateHttp({
28
+ URL: settings.BroadcastifySettings.BroadcastifyDatabase,
29
+ Timeout: 5000,
30
+ });
31
+ if (!broadcastify.error) {
32
+ const states = JSON.parse(broadcastify.message);
33
+ const insert = `INSERT INTO broadcastify (state, county, feed, type, link) VALUES (?, ?, ?, ?, ?)`;
34
+ const transaction = Bootstrap.Database.transaction((rows: any[]) => {
35
+ for (const row of rows) {
36
+ CreateQuery({ Query: insert, Parameters: row });
37
+ }
38
+ });
39
+ const batch: any[] = [];
40
+ for (const state of states) {
41
+ SetWarning({ Message: `Importing ${state.counties.length} counties from ${state.state} for Broadcastify` });
42
+ for (const county of state.counties) {
43
+ const countyName = county.county;
44
+ for (const feed of county.feeds) {
45
+ batch.push([
46
+ state.state,
47
+ countyName,
48
+ feed.name,
49
+ feed.type,
50
+ feed.link
51
+ ]);
52
+ }
53
+ }
54
+ }
55
+ transaction(batch);
56
+ }
57
+ if (broadcastify.error) {
58
+ SetWarning({ Message: `Error importing Broadcastify data: ${broadcastify.message}` });
59
+ }
60
+ };
@@ -0,0 +1,87 @@
1
+ /*
2
+ _ _ _ __ __
3
+ /\ | | | | (_) \ \ / /
4
+ / \ | |_ _ __ ___ ___ ___ _ __ | |__ ___ _ __ _ ___ \ V /
5
+ / /\ \| __| '_ ` _ \ / _ \/ __| '_ \| '_ \ / _ \ '__| |/ __| > <
6
+ / ____ \ |_| | | | | | (_) \__ \ |_) | | | | __/ | | | (__ / . \
7
+ /_/ \_\__|_| |_| |_|\___/|___/ .__/|_| |_|\___|_| |_|\___/_/ \_\
8
+ | |
9
+ |_|
10
+
11
+ Created with ♥ by the AtmosphericX Team (KiyoWx, StarflightWx, & CJ Ziegler)
12
+ Discord: https://atmosphericx-discord.scriptkitty.cafe
13
+ Ko-Fi: https://ko-fi.com/k3yomi
14
+ Documentation: https://atmosphericx.scriptkitty.cafe/documentation
15
+
16
+ Independent Package: @atmosx/event-product-parser
17
+
18
+ */
19
+ import { EnumStates } from "@Enums/States"
20
+ import { Bootstrap } from "@Bootstrap"
21
+ import { CreateHttp } from "@Utilities/CreateHttp"
22
+ import { CreateQuery } from "@Database/CreateQuery"
23
+ import { SetWarning } from "@Utilities/SetWarning"
24
+
25
+ interface CityRow {
26
+ id: string
27
+ name: string
28
+ state: string
29
+ county: string
30
+ population: number | string
31
+ lat: number | string
32
+ lon: number | string
33
+ }
34
+
35
+ export const ImportCities = async (): Promise<void> => {
36
+ const cities = await CreateHttp({
37
+ URL: Bootstrap.Settings.BoundarySettings.CityDatabase,
38
+ Timeout: 5000
39
+ })
40
+
41
+ if (cities.error || !cities.message) {
42
+ SetWarning({ Message: `Failed to download city database` })
43
+ return
44
+ }
45
+
46
+ let database: unknown
47
+
48
+ try {
49
+ database = JSON.parse(cities.message)
50
+ } catch {
51
+ SetWarning({ Message: `Failed to parse city database` })
52
+ return
53
+ }
54
+
55
+ if (!Array.isArray(database)) {
56
+ SetWarning({ Message: `Failed to parse city database` })
57
+ return
58
+ }
59
+
60
+ const StateCodes: Record<string, string> = Object.fromEntries(
61
+ Object.entries(EnumStates).map(([code, name]) => [name, code])
62
+ );
63
+ const insert = `INSERT INTO cities (id, name, state, county, population, lat, lon) VALUES (?, ?, ?, ?, ?, ?, ?)`
64
+ const cityRows: CityRow[] = []
65
+ for (const city of database) {
66
+ if (city.country != `US`) continue
67
+ cityRows.push({
68
+ id: city.id,
69
+ name: city.name,
70
+ state: StateCodes[city.admin1] ?? null,
71
+ county: city.admin2,
72
+ population: city.pop ?? null,
73
+ lat: city.lat,
74
+ lon: city.lon
75
+ })
76
+ }
77
+ SetWarning({ Message: `Importing ${cityRows.length} cities` })
78
+ const transaction = Bootstrap.Database.transaction((rows: CityRow[]) => {
79
+ for (const row of rows) {
80
+ CreateQuery({
81
+ Query: insert,
82
+ Parameters: [row.id, row.name, row.state, row.county, row.population, row.lat, row.lon]
83
+ })
84
+ }
85
+ })
86
+ transaction(cityRows)
87
+ }
@@ -11,55 +11,50 @@
11
11
  Created with ♥ by the AtmosphericX Team (KiyoWx, StarflightWx, & CJ Ziegler)
12
12
  Discord: https://atmosphericx-discord.scriptkitty.cafe
13
13
  Ko-Fi: https://ko-fi.com/k3yomi
14
- Documentation: http://localhost/documentation | https://atmosphericx.scriptkitty.cafe/documentation
14
+ Documentation: https://atmosphericx.scriptkitty.cafe/documentation
15
15
 
16
- Internal Package: @atmosx/event-product-parser
16
+ Independent Package: @atmosx/event-product-parser
17
17
 
18
18
  */
19
19
 
20
- import fs from 'fs'
21
- import { resolve, extname} from 'path'
22
- import { loadAsync } from 'jszip'
23
- import { read } from 'shapefile'
24
- import { TypeSettings } from "../../@types/types.settings"
25
- import { bootstrap } from "../../bootstrap"
26
- import { setSleep } from '../@utilities/utilities.setSleep'
27
- import { setWarning } from '../@utilities/utilities.setWarning'
28
- import { shapefileLinks } from '../../@dictionaries/dictionaries.shapefileLinks'
20
+ import { EnumShapefiles } from "@Enums/Shapefiles"
21
+ import { Bootstrap } from "@Bootstrap"
22
+ import { SetSleep } from "@Utilities/SetSleep"
23
+ import { SetWarning } from "@Utilities/SetWarning"
24
+ import { CreateQuery } from "@Database/CreateQuery"
25
+ import { existsSync, mkdirSync, writeFileSync, unlinkSync, rm } from "fs"
26
+ import { resolve, extname} from "path"
27
+ import { loadAsync } from "jszip"
28
+ import { read } from "shapefile"
29
29
 
30
- export const importShapefiles = async (): Promise<void> => {
31
- const settings = bootstrap.settings as TypeSettings;
30
+ export const ImportShapefiles = async (): Promise<void> => {
32
31
  try {
33
- const tShapefiles = bootstrap.database
34
- .prepare(`SELECT COUNT(*) AS count FROM shapefiles`)
35
- .get().count;
36
- if (tShapefiles === 0) {
37
- await setSleep({timeout: 1e3});
38
- setWarning({ message: `Shapefiles are currently building, please DO NOT close your terminal. The shapefiles will not finish and will remain incomplete. If you do mess up, you will need to delete ${settings.Database} and restart the application.` })
39
- for (const shapefile of shapefileLinks) {
32
+ const tShapefiles = CreateQuery({ Query: `SELECT COUNT(*) AS count FROM shapefiles` })[0] as { count: number };
33
+ if (tShapefiles.count === 0) {
34
+ await SetSleep({Timeout: 1e3});
35
+ for (const shapefile of EnumShapefiles) {
40
36
  const response = await fetch(shapefile.link);
41
37
  const arrayBuff = await response.arrayBuffer();
42
38
  const content = await loadAsync(arrayBuff);
43
- const directory = resolve(__dirname, `../../shapefiles`);
44
- if (!fs.existsSync(directory)) {
45
- fs.mkdirSync(directory, { recursive: true });
39
+ const directory = resolve(__dirname, `shapefiles`);
40
+ if (!existsSync(directory)) {
41
+ mkdirSync(directory, { recursive: true });
46
42
  }
47
43
  for (const file of Object.keys(content.files)) {
48
44
  if (file.endsWith('.shp') || file.endsWith('.dbf')) {
49
45
  const data = await content.files[file].async(`nodebuffer`)
50
46
  const output = resolve(directory, `${shapefile?.name ?? ``}_${shapefile?.id ?? ``}${extname(file)}`)
51
- fs.writeFileSync(output, data)
47
+ writeFileSync(output, data)
52
48
  }
53
49
  }
54
- const filepath = resolve(__dirname, '../../shapefiles', shapefile.name + '_' + shapefile.id)
50
+ const filepath = resolve(__dirname, 'shapefiles', shapefile.name + '_' + shapefile.id)
55
51
  const { features } = await read(
56
52
  filepath,
57
53
  filepath,
58
54
  );
59
- setWarning({ message: `Importing ${features.length} features from ${shapefile.name}_${shapefile.id}` })
60
- const insert = bootstrap.database
61
- .prepare(`INSERT OR REPLACE INTO shapefiles (id, location, geometry) VALUES (?, ?, ?)`)
62
- const transaction = bootstrap.database.transaction((entries: any[]) => {
55
+ SetWarning({ Message: `Importing ${features.length} features from ${shapefile.name}_${shapefile.id} for Shapefiles` })
56
+ const insert = `INSERT INTO shapefiles (id, location, source, geometry) VALUES (?, ?, ?, ?)`;
57
+ const transaction = Bootstrap.Database.transaction((entries: any[]) => {
63
58
  for (const entry of entries) {
64
59
  const { properties, geometry } = entry;
65
60
  let final: string, location: string;
@@ -79,18 +74,17 @@ export const importShapefiles = async (): Promise<void> => {
79
74
  final = properties.ID ?? properties.WFO
80
75
  location = properties.NAME;
81
76
  }
82
- insert.run(final, location, JSON.stringify(geometry));
77
+ CreateQuery({ Query: insert, Parameters: [final, location, `${shapefile.name}_${shapefile.id}`, JSON.stringify(geometry)] })
83
78
  }
84
79
  })
85
- fs.unlinkSync(`${filepath}.shp`)
86
- fs.unlinkSync(`${filepath}.dbf`)
87
- setWarning({ message: `Cleaned up temporary files for ${shapefile.name}_${shapefile.id}` })
80
+ unlinkSync(`${filepath}.shp`)
81
+ unlinkSync(`${filepath}.dbf`)
88
82
  transaction(features)
89
83
  }
90
- setWarning({ message: `Shapefiles have finished compiling, you can now continue and close this terminal` })
91
- fs.rm(resolve(__dirname, '../../shapefiles'), { recursive: true, force: true }, () => {});
84
+ rm(resolve(__dirname, 'shapefiles'), { recursive: true, force: true }, () => {});
92
85
  }
93
- } catch (error) {
94
- setWarning( {message: `An error occurred while compiling shapefiles: ${error.message}` })
86
+ } catch (error: unknown) {
87
+ const message = error instanceof Error ? error.message : String(error);
88
+ SetWarning( {Message: `An error occurred while compiling shapefiles: ${message}` })
95
89
  }
96
90
  }
@@ -0,0 +1,43 @@
1
+ /*
2
+ _ _ _ __ __
3
+ /\ | | | | (_) \ \ / /
4
+ / \ | |_ _ __ ___ ___ ___ _ __ | |__ ___ _ __ _ ___ \ V /
5
+ / /\ \| __| '_ ` _ \ / _ \/ __| '_ \| '_ \ / _ \ '__| |/ __| > <
6
+ / ____ \ |_| | | | | | (_) \__ \ |_) | | | | __/ | | | (__ / . \
7
+ /_/ \_\__|_| |_| |_|\___/|___/ .__/|_| |_|\___|_| |_|\___/_/ \_\
8
+ | |
9
+ |_|
10
+
11
+ Created with ♥ by the AtmosphericX Team (KiyoWx, StarflightWx, & CJ Ziegler)
12
+ Discord: https://atmosphericx-discord.scriptkitty.cafe
13
+ Ko-Fi: https://ko-fi.com/k3yomi
14
+ Documentation: https://atmosphericx.scriptkitty.cafe/documentation
15
+
16
+ Independent Package: @atmosx/event-product-parser
17
+
18
+ */
19
+
20
+ import { TypeSettings } from "Types/Settings"
21
+ import { TypeStanzaCompiled } from "Types/StanzaCompiled"
22
+ import { Bootstrap } from "@Bootstrap"
23
+ import { SetWarning } from "@Utilities/SetWarning"
24
+ import { CreateQuery } from "@Database/CreateQuery"
25
+
26
+ export const ImportStanza = async (Stanza: TypeStanzaCompiled): Promise<void> => {
27
+ const settings = Bootstrap.Settings as TypeSettings;
28
+ try {
29
+ if (!settings.NOAAWeatherWireServiceSettings.CacheSettings.Enabled) { return }
30
+ CreateQuery({ Query: `INSERT OR IGNORE INTO stanzas (type, stanza, issued) VALUES (?, ?, ?)`, Parameters: [Stanza.Type.Type, JSON.stringify(Stanza), Stanza.Attributes.issue] })
31
+ const count = CreateQuery({ Query: `SELECT COUNT(*) as total FROM stanzas` }) as { total: number };
32
+ const max = settings.NOAAWeatherWireServiceSettings.CacheSettings.MaxDatabaseHistory;
33
+ if (count.total > max) {
34
+ const toDelete = count.total - max;
35
+ if (toDelete > 0) {
36
+ CreateQuery({ Query: `DELETE FROM stanzas WHERE id IN (SELECT id FROM stanzas ORDER BY issued ASC LIMIT ?)`, Parameters: [toDelete] })
37
+ }
38
+ }
39
+ } catch (error: unknown) {
40
+ const message = error instanceof Error ? error.message : String(error);
41
+ SetWarning({ Message: `An error occurred while importing stanza: ${message}` })
42
+ }
43
+ }
@@ -0,0 +1,71 @@
1
+ /*
2
+ _ _ _ __ __
3
+ /\ | | | | (_) \ \ / /
4
+ / \ | |_ _ __ ___ ___ ___ _ __ | |__ ___ _ __ _ ___ \ V /
5
+ / /\ \| __| '_ ` _ \ / _ \/ __| '_ \| '_ \ / _ \ '__| |/ __| > <
6
+ / ____ \ |_| | | | | | (_) \__ \ |_) | | | | __/ | | | (__ / . \
7
+ /_/ \_\__|_| |_| |_|\___/|___/ .__/|_| |_|\___|_| |_|\___/_/ \_\
8
+ | |
9
+ |_|
10
+
11
+ Created with ♥ by the AtmosphericX Team (KiyoWx, StarflightWx, & CJ Ziegler)
12
+ Discord: https://atmosphericx-discord.scriptkitty.cafe
13
+ Ko-Fi: https://ko-fi.com/k3yomi
14
+ Documentation: https://atmosphericx.scriptkitty.cafe/documentation
15
+
16
+ Independent Package: @atmosx/event-product-parser
17
+
18
+ */
19
+
20
+ import { TypeSettings } from "Types/Settings"
21
+ import { Bootstrap } from "@Bootstrap"
22
+ import { SetWarning } from "@Utilities/SetWarning"
23
+ import { ImportShapefiles } from "@Database/ImportShapefiles"
24
+ import { ImportBroadcastify } from "@Database/ImportBroadcastify"
25
+ import { ImportBoundaries } from "@Database/ImportBoundaries"
26
+ import { ImportCities } from "@Database/ImportCities"
27
+ import { CreateQuery } from "@Database/CreateQuery"
28
+ import { existsSync, writeFileSync } from "fs"
29
+ import sqlite3 from "better-sqlite3"
30
+
31
+
32
+ export const InitializeDatabase = async (): Promise<void> => {
33
+ const settings = Bootstrap.Settings as TypeSettings;
34
+ try {
35
+ if (!existsSync(settings.Database)) {
36
+ writeFileSync(settings.Database, '')
37
+ SetWarning({ Message: `Creating new database at ${settings.Database}` })
38
+ }
39
+ Bootstrap.Database = new sqlite3(settings.Database);
40
+ CreateQuery({ Query: `CREATE TABLE IF NOT EXISTS stanzas ( id INTEGER PRIMARY KEY AUTOINCREMENT, type TEXT, issued TEXT, stanza TEXT )` })
41
+ const isNeedingShapefiles = CreateQuery({ Query: `SELECT name FROM sqlite_master WHERE type='table' AND name='shapefiles';` }) as { name: string }[];
42
+ const isNeedingBroadcastify = CreateQuery({ Query: `SELECT name FROM sqlite_master WHERE type='table' AND name='broadcastify';` }) as { name: string }[];
43
+ const isNeedingBoundaries = CreateQuery({ Query: `SELECT name FROM sqlite_master WHERE type='table' AND name='boundaries';` }) as { name: string }[];
44
+ const isNeedingCities = CreateQuery({ Query: `SELECT name FROM sqlite_master WHERE type='table' AND name='cities';` }) as { name: string }[];
45
+
46
+ if (isNeedingShapefiles.length === 0 || isNeedingBroadcastify.length === 0 || isNeedingBoundaries.length === 0 || isNeedingCities.length === 0) {
47
+ CreateQuery({ Query: `CREATE TABLE IF NOT EXISTS broadcastify ( state TEXT, county TEXT, feed TEXT, type TEXT, link TEXT)` })
48
+ CreateQuery({ Query: `CREATE TABLE IF NOT EXISTS shapefiles ( id, location TEXT, source TEXT, geometry TEXT )` })
49
+ CreateQuery({ Query: `CREATE INDEX IF NOT EXISTS idx_shapefiles_id ON shapefiles (id)` })
50
+ CreateQuery({ Query: `CREATE TABLE IF NOT EXISTS boundaries (id TEXT PRIMARY KEY, type TEXT NOT NULL, state TEXT, name TEXT, geometry TEXT NOT NULL );` })
51
+ CreateQuery({ Query: `CREATE TABLE IF NOT EXISTS cities ( id TEXT PRIMARY KEY, name TEXT, state TEXT, county TEXT, population TEXT, lat REAL NOT NULL, lon REAL NOT NULL);`});
52
+ SetWarning({Message: `Required database tables are currently building, please ${Bootstrap.Colors.Red}DO NOT${Bootstrap.Colors.Reset} close your terminal. The building will not finish and will remain incomplete. If you do mess up, you will need to delete ${settings.Database} and restart the application.` })
53
+ if (isNeedingCities.length === 0) {
54
+ await ImportCities();
55
+ }
56
+ if (isNeedingBroadcastify.length === 0) {
57
+ await ImportBroadcastify();
58
+ }
59
+ if (isNeedingBoundaries.length === 0) {
60
+ await ImportBoundaries();
61
+ }
62
+ if (isNeedingShapefiles.length === 0) {
63
+ await ImportShapefiles();
64
+ }
65
+ SetWarning({ Message: `Database initialization complete. You may now close your terminal or continue using the application.` })
66
+ }
67
+ } catch (error: unknown) {
68
+ const message = error instanceof Error ? error.message : String(error);
69
+ SetWarning({ Message: `An error occurred while initializing the database: ${message}` })
70
+ }
71
+ }