ข้ามไปยังเนื้อหาหลัก

เอกสารออกแบบฐานข้อมูล MyEMS

เอกสารนี้จัดทำขึ้นสำหรับโปรแกรมเมอร์ อธิบายรายละเอียดสถาปัตยกรรมฐานข้อมูล โครงสร้างตาราง และแนวคิดการออกแบบของระบบบริหารจัดการพลังงาน MyEMS


การออกแบบสถาปัตยกรรมฐานข้อมูล

แนวคิดการออกแบบ

  1. แยกข้อมูล: แยกข้อมูลออกเป็นฐานข้อมูลต่าง ๆ ตามประเภทและการใช้งาน เพื่อป้องกันฐานข้อมูลเดียวมีขนาดใหญ่เกินไป
  2. แยกอ่าน-เขียน: ข้อมูลประวัติจะถูกจัดเก็บในรูปแบบ time series เพื่อให้สามารถค้นหาได้อย่างมีประสิทธิภาพ
  3. ขยายแนวนอน: ฐานข้อมูลขนาดใหญ่ (historical_db, energy_db) สามารถขยายได้อย่างอิสระ
  4. มาตรฐานเดียวกัน: ฐานข้อมูลทั้งหมดใช้ชุดอักขระและกฎการเรียงลำดับเดียวกัน

การตั้งค่าฐานข้อมูล

ฐานข้อมูลทั้งหมดใช้การตั้งค่าดังนี้:

  • ชุดอักขระ: utf8mb4 (รองรับ UTF-8 ครบถ้วน รวม emoji)
  • กฎการเรียงลำดับ: utf8mb4_unicode_ci (กฎ Unicode)
  • Storage engine: InnoDB (ค่าเริ่มต้น รองรับธุรกรรมและ foreign key)

กฎการตั้งชื่อ

  • ชื่อฐานข้อมูล: myems_{Function}_d_db (ตัวพิมพ์เล็ก คั่นด้วยขีดล่าง)
  • ชื่อตาราง: tbl_{Entity Name} (ตัวพิมพ์เล็ก คั่นด้วยขีดล่าง)
  • ชื่อฟิลด์: ตัวพิมพ์เล็ก คั่นด้วยขีดล่าง เช่น start_datetime_utc
  • ชื่อดัชนี: tbl_{Table Name}_index_{Sequence Number}

รายละเอียดฐานข้อมูล

1. Myems_system_deb (ฐานข้อมูลการตั้งค่าระบบ)

วัตถุประสงค์: เก็บข้อมูลการตั้งค่าและเมตาดาต้าพื้นฐานของระบบ เป็นฐานข้อมูลแกนกลางของระบบทั้งหมด

คุณสมบัติ:

  • มีจำนวนตารางมากที่สุด (ประมาณ 150+ ตาราง)
  • ปริมาณข้อมูลไม่มาก แต่โครงสร้างซับซ้อน
  • มีตารางความสัมพันธ์จำนวนมาก

การแบ่งกลุ่มตารางหลัก:

1.1 ตารางการตั้งค่าพื้นฐาน

table nameInstructionskey field
tbl_energy_categoriesEnergy classification (electricity, water, gas, cold, heat, etc.)id, name, unit_of_measure, kgce, kgco2e
tbl_energy_itemsEnergy consumption sub items (lighting, air conditioning, power, etc.)id, name, energy_category_id
tbl_cost_centerscost centerid, name, external_id
tbl_data_sourcesData Source Configurationid, name, gateway_id, protocol, connection
tbl_protocolsProtocol Configurationid, name, protocol_type

1.2 ตารางจัดการอุปกรณ์

table nameInstructionskey field
tbl_equipmentsDevice Informationid, name, uuid, equipment_type_id, cost_center_id
tbl_combined_equipmentsCombination device (combination of multiple devices)id, name, is_input_counted, is_output_counted
tbl_metersMeter informationid, name, uuid, energy_category_id, is_counted
tbl_offline_metersOffline meter (manually entered)id, name, energy_category_id
tbl_virtual_metersVirtual Meter (Calculated)Id, name, expression (in JSON format)
tbl_pointsData point informationid, name, data_source_id, object_type, object_id

1.3 ตารางการจัดระเบียบพื้นที่

table nameInstructionskey field
tbl_spacesSpatial information (rooms, floors, etc.)id, name, uuid, parent_space_id, area
tbl_storesStore Informationid, name, uuid, space_id
tbl_tenantstenant informationid, name, uuid, space_id
tbl_shopfloorsWorkshop informationid, name, uuid, space_id

1.4 ตารางความสัมพันธ์

ระบบใช้ตารางความสัมพันธ์จำนวนมากเพื่อสร้างความสัมพันธ์แบบ many-to-many:

  • tbl_equipments_meters: Association between equipment and meters
  • tbl_equipments_offline_meters: Association between equipment and offline meters
  • tbl_equipments_virtual_meters: Association between equipment and virtual meters
  • tbl_spaces_equipments: The association between space and equipment
  • tbl_spaces_meters: The association between space and meters
  • tbl_combined_equipments_equipments: Association between combined equipment and devices
  • etc...

1.5 รายการอุปกรณ์พลังงานใหม่

table nameInstructionskey field
tbl_photovoltaic_power_stationsphotovoltaic power stationid, name, capacity, contact_id
tbl_energy_storage_containersEnergy storage containerid, name, rated_capacity, rated_power
tbl_energy_storage_power_stationsenergy storage power stationid, name, rated_capacity
tbl_microgridsmicrogridid, name, address
tbl_charging_stationscharging stationid, name, rated_capacity, rated_power

1.6 ตารางควบคุมและตารางกำหนดเวลา

table nameInstructionskey field
tbl_commandscontrol commandId, name, topic, payload (in JSON format)
tbl_control_modescontrol modeid, name, is_active
tbl_control_modes_timesControl mode time periodid, control_mode_id, start_time_of_day, end_time_of_day

1.7 ตารางการตั้งค่าอื่น ๆ

  • tbl_contacts: Contact information
  • tbl_distribution_systems: Distribution systems
  • tbl_distribution_circuits: Distribution circuits
  • tbl_energy_flow_diagrams: Energy Flow Diagram
  • tbl_tariffs: Electricity pricing configuration
  • tbl_working_calendars: Work calendars
  • tbl_web_messages: Web Messages

ข้อควรระวังในการพัฒนา:

  • ทุกตารางมี 'id' (BIGINT AUTO-INCREMENT) เป็น primary key
  • ตารางส่วนใหญ่มีฟิลด์ 'uuid' (CHAR(36)) สำหรับเชื่อมต่อกับระบบภายนอก
  • ตารางความสัมพันธ์มักมีแค่ 'id' และ foreign key สองตัว
  • ฟิลด์ JSON ใช้ชนิด 'LONGTEXT' ในการเก็บข้อมูล JSON

2. Myems_historics_db (ฐานข้อมูลข้อมูลประวัติ)

วัตถุประสงค์: เก็บข้อมูลมอนิเตอร์แบบเรียลไทม์และข้อมูลประวัติ เป็นฐานข้อมูลที่มีปริมาณข้อมูลมากที่สุดในระบบ

คุณสมบัติ:

  • ปริมาณข้อมูลมหาศาล ใช้การจัดเก็บแบบ time series
  • มีทั้งข้อมูลดิบและตารางแคชค่าล่าสุด
  • รองรับการติดป้ายคุณภาพข้อมูล (is_bad, is_published)

โครงสร้างตารางหลัก:

table nameInstructionskey fieldIndex strategy
tbl_analog_valueAnalog historical datapoint_id, utc_date_time, actual_value, is_bad, is_published(point_id, utc_date_time), (utc_date_time)
tbl_analog_value_latestLatest analog value (cache)point_id, utc_date_time, actual_value(point_id, utc_date_time)
tbl_digital_valueDigital historical datapoint_id, utc_date_time, actual_value (INT)(point_id, utc_date_time), (utc_date_time)
tbl_digital_value_latestLatest numerical value (cache)point_id, utc_date_time, actual_value(point_id, utc_date_time)
tbl_energy_valueHistorical energy consumption datapoint_id, utc_date_time, actual_value, is_bad, is_published(point_id, utc_date_time), (utc_date_time)
tbl_energy_value_latestLatest energy consumption value (cache)point_id, utc_date_time, actual_value(point_id, utc_date_time)
tbl_text_valueText volume historical datapoint_id, utc_date_time, actual_value (LONGTEXT)(point_id, utc_date_time), (utc_date_time)
tbl_text_value_latestLatest text volume value (cache)point_id, utc_date_time, actual_value(point_id, utc_date_time)

ตารางเก็บไฟล์:

table nameInstructionskey field
tbl_cost_filesCost document (Excel/CSV)file_name, uuid, upload_datetime_utc, status, file_object (LONGBLOB)
tbl_offline_meter_filesOffline meter data filefile_name, uuid, upload_datetime_utc, status, file_object
tbl_data_repair_filesData repair filefile_name, uuid, upload_datetime_utc, status, file_object
tbl_energy_plan_filesEnergy consumption plan documentfile_name, uuid, upload_datetime_utc, status, file_object

คำอธิบายชนิดข้อมูล:

  • practice_value: DECIMAL (21,6) - รองรับค่าตัวเลขความละเอียดสูง 6 ตำแหน่งทศนิยม
  • utc_date_time: DATETIME - เวลา UTC ทุกฟิลด์เวลาใช้ UTC
  • is_bad: BOOL - ป้ายคุณภาพข้อมูล True คือข้อมูลเสีย
  • is_published: BOOL - ป้ายเผยแพร่ True คือเผยแพร่แล้ว

ข้อควรระวังในการพัฒนา:

  • ฟิลด์เวลาทั้งหมดใช้ UTC และแสดงผลที่หน้าบ้านเป็นเวลาท้องถิ่น
  • ตาราง _latest ใช้สำหรับค้นหาค่าล่าสุดอย่างรวดเร็ว
  • ตารางไฟล์ใช้ LONGBLOB เก็บไฟล์ไบนารี ต้องระวังขนาดไฟล์
  • ควรลบข้อมูลประวัติเป็นประจำเพื่อป้องกันตารางใหญ่เกินไป

3. Myems_energy_db (ฐานข้อมูลการใช้พลังงาน)

วัตถุประสงค์: เก็บสถิติการใช้พลังงานของอุปกรณ์ต่าง ๆ และรวมผลตามชั่วโมง วัน เดือน ปี

คุณสมบัติ:

  • ข้อมูลถูกคำนวณและสร้างโดยบริการ myems-aggregation
  • แบ่งตามช่วงเวลาเป็นตารางรายชั่วโมง รายวัน รายเดือน รายปี
  • รองรับสถิติตามหมวดพลังงานและรายการใช้พลังงาน

กฎการตั้งชื่อตาราง:

  • tbl_{Object Type}_{Direction}_{Classification}_{Time Granularity}
  • ประเภทของวัตถุ: meter, equipment, combined_equipment, space, store, tenant, shopfloor
  • ทิศทาง: input (ขาเข้า), output (ขาออก)
  • หมวดหมู่: category (หมวดพลังงาน), item (รายการใช้พลังงาน)
  • ช่วงเวลา: hourly, daily, monthly, yearly

โครงสร้างตารางหลัก:

3.1 Energy consumption meter

table nameInstructionskey field
tbl_meter_hourlyHourly energy consumption of the metermeter_id, start_datetime_utc, actual_value
tbl_meter_dailyDaily energy consumption of the metermeter_id, start_datetime_utc, actual_value
tbl_meter_monthlyMonthly energy consumption of the metermeter_id, start_datetime_utc, actual_value
tbl_meter_yearlyAnnual energy consumption of the metermeter_id, start_datetime_utc, actual_value
tbl_offline_meter_hourlyOffline meter hourly energy consumptionoffline_meter_id, start_datetime_utc, actual_value
tbl_virtual_meter_hourlyVirtual meter hourly energy consumptionvirtual_meter_id, start_datetime_utc, actual_value

3.2 Equipment Energy Consumption Table

table nameInstructionskey field
tbl_equipment_input_category_hourlyEquipment input energy consumption (by category)equipment_id, energy_category_id, start_datetime_utc, actual_value
tbl_equipment_input_item_hourlyEquipment input energy consumption (by item)equipment_id, energy_item_id, start_datetime_utc, actual_value
tbl_equipment_output_category_hourlyEquipment output energy consumption (by category)equipment_id, energy_category_id, start_datetime_utc, actual_value
tbl_combined_equipment_input_category_hourlyCombination device input energy consumption (by category)combined_equipment_id, energy_category_id, start_datetime_utc, actual_value
tbl_combined_equipment_output_category_hourlyEnergy consumption of combined equipment output (by classification)combined_equipment_id, energy_category_id, start_datetime_utc, actual_value

3.3 Space Energy Consumption Table

table nameInstructionskey field
tbl_space_input_category_hourlySpace input energy consumption (by category)space_id, energy_category_id, start_datetime_utc, actual_value
tbl_space_input_item_hourlySpace input energy consumption (by item)space_id, energy_item_id, start_datetime_utc, actual_value
tbl_space_output_category_hourlySpace output energy consumption (by classification)space_id, energy_category_id, start_datetime_utc, actual_value
tbl_store_input_category_hourlyStore input energy consumptionstore_id, energy_category_id, start_datetime_utc, actual_value
tbl_tenant_input_category_hourlyTenant inputs energy consumptiontenant_id, energy_category_id, start_datetime_utc, actual_value
tbl_shopfloor_input_category_hourlyWorkshop input energy consumptionshopfloor_id, energy_category_id, start_datetime_utc, actual_value

3.4 Energy Consumption Table of New Energy Equipment

table nameInstructionskey field
tbl_photovoltaic_power_station_hourlyHourly power generation of photovoltaic power stationphotovoltaic_power_station_id, start_datetime_utc, actual_value
tbl_energy_storage_container_charge_hourlyCharging capacity of energy storage containerenergy_storage_container_id, start_datetime_utc, actual_value
tbl_energy_storage_container_discharge_hourlyDischarge capacity of energy storage containerenergy_storage_container_id, start_datetime_utc, actual_value
tbl_energy_storage_container_grid_buy_hourlyEnergy storage container purchases electricityenergy_storage_container_id, start_datetime_utc, actual_value
tbl_energy_storage_container_grid_sell_hourlyElectricity sales from energy storage containersenergy_storage_container_id, start_datetime_utc, actual_value
tbl_microgrid_charge_hourlyMicrogrid charging capacitymicrogrid_id, start_datetime_utc, actual_value
tbl_microgrid_discharge_hourlyMicrogrid discharge capacitymicrogrid_id, start_datetime_utc, actual_value

การออกแบบดัชนี:

  • ทุกตารางมีดัชนีผสม: (Object ID, Classification ID, start_datetime_utc) หรือ (Object ID, start_datetime_utc)
  • รองรับการค้นหาความเร็วตามวัตถุและช่วงเวลา

ข้อควรระวังในการพัฒนา:

  • start_datetime_utc คือเวลาเริ่มต้นของช่วงเวลา (เช่น 2024-01-01 00:00:00 คือ 1 ม.ค. 0:00-1:00)
  • actual_value คือค่ารวม ไม่ใช่ค่าดิบ
  • ข้อมูลถูกคำนวณเป็นระยะ ๆ โดยบริการ aggregation ไม่ได้เขียนแบบเรียลไทม์
  • ต้องระวังการแปลงเขตเวลาเมื่อค้นหา

4. myems_billing_db (ฐานข้อมูลการคิดเงิน)

วัตถุประสงค์: เก็บข้อมูลการใช้พลังงานที่เกี่ยวข้องกับการคิดเงิน โครงสร้างคล้าย myems_energy_db แต่ข้อมูลคำนวณตามอัตราค่าไฟ

คุณสมบัติ:

  • โครงสร้างตารางเหมือน myems_energy_db
  • ข้อมูลคำนวณโดย myems-aggregation ตามการตั้งค่าอัตราค่าไฟ
  • รองรับกฎการคิดเงินที่ซับซ้อน เช่น อัตราตามช่วงเวลา อัตราขั้นบันได

ตารางหลัก:

  • โครงสร้างตารางเหมือน myems_energy_db
  • ค่าข้อมูลคูณด้วยอัตราค่าไฟที่เกี่ยวข้อง หน่วยเป็นสกุลเงิน (เช่น CNY, USD)

หมายเหตุสำหรับการพัฒนา:

  • ข้อมูลคิดเงินขึ้นกับการตั้งค่าอัตราใน myems_system_db.tbl_tariffs
  • ต้องเชื่อมโยงกับ cost center (cost_center)
  • รองรับกลยุทธ์อัตราหลายแบบ (ตามช่วงเวลา, ขั้นบันได, ตามกำลัง ฯลฯ)

5. myems_carbon_db (ฐานข้อมูลคาร์บอน)

วัตถุประสงค์: เก็บข้อมูลพลังงานที่เกี่ยวข้องกับการปล่อยคาร์บอนสำหรับคำนวณคาร์บอนฟุตพริ้นท์

คุณสมบัติ:

  • โครงสร้างตารางเหมือน myems_energy_db
  • ข้อมูลคำนวณโดย myems-aggregation ตามค่าสัมประสิทธิ์การปล่อยคาร์บอน
  • ค่าสัมประสิทธิ์การปล่อยคาร์บอนเก็บใน myems_system_db.tbl_energy_categories.kgco2e

ตารางหลัก:

  • โครงสร้างตารางเหมือน myems_energy_db
  • ค่าข้อมูลคูณด้วยค่าสัมประสิทธิ์คาร์บอน หน่วยเป็น kgCO2e (กิโลกรัม CO₂ เทียบเท่า)

หมายเหตุสำหรับการพัฒนา:

  • ค่าสัมประสิทธิ์คาร์บอนอาจเปลี่ยนแปลงตามเวลา ต้องรองรับประวัติ
  • พลังงานแต่ละประเภท (ไฟฟ้า, ก๊าซ, น้ำมัน ฯลฯ) มีค่าสัมประสิทธิ์ต่างกัน
  • รองรับการคำนวณคาร์บอน Scope 1, 2, 3

6. myems_energy_baseline_db (ฐานข้อมูลเส้นฐานพลังงาน)

วัตถุประสงค์: เก็บข้อมูลเส้นฐานพลังงานสำหรับวิเคราะห์การประหยัดและประเมินประสิทธิภาพ

คุณสมบัติ:

  • โครงสร้างตารางคล้าย myems_energy_db
  • ข้อมูลเส้นฐานคำนวณจากข้อมูลประวัติหรือค่ามาตรฐาน
  • ใช้เปรียบเทียบกับการใช้จริงเพื่อคำนวณการประหยัด

ตารางหลัก:

  • โครงสร้างตารางเหมือน myems_energy_db
  • เก็บค่าเส้นฐานแทนค่าจริง

หมายเหตุสำหรับการพัฒนา:

  • ข้อมูลเส้นฐานต้องอัปเดตเป็นประจำ
  • รองรับหลายวิธีการคำนวณ (ค่าเฉลี่ยประวัติ, การวิเคราะห์ถดถอย, ค่ามาตรฐาน ฯลฯ)

7. Myems_energy_model_deb (ฐานข้อมูลโมเดลการใช้พลังงาน)

วัตถุประสงค์: เก็บข้อมูลโมเดลการใช้พลังงาน 8760 ชั่วโมงต่อปี

คุณสมบัติ:

  • แต่ละวัตถุเก็บ 8760 แถว (ข้อมูลรายชั่วโมง 1 ปี)
  • ใช้สำหรับพยากรณ์และวางแผนการใช้พลังงาน
  • ชื่อตารางลงท้ายด้วย _8760

ตารางหลัก:

table nameInstructionskey field
tbl_meter_8760Meter 8760 hour modelmeter_id, start_datetime_utc, actual_value
tbl_equipment_input_category_8760Equipment input energy consumption modelequipment_id, energy_category_id, start_datetime_utc, actual_value
tbl_combined_equipment_input_category_8760Combination device input energy consumption modelcombined_equipment_id, energy_category_id, start_datetime_utc, actual_value
tbl_space_input_category_8760Space input energy consumption modelspace_id, energy_category_id, start_datetime_utc, actual_value
tbl_shopfloor_input_category_8760Workshop input energy consumption modelshopfloor_id, energy_category_id, start_datetime_utc, actual_value
tbl_store_input_category_8760Store input energy consumption modelstore_id, energy_category_id, start_datetime_utc, actual_value
tbl_tenant_input_category_8760Tenant input energy consumption modeltenant_id, energy_category_id, start_datetime_utc, actual_value

ข้อควรระวังในการพัฒนา:

  • โมเดล 8760 ชั่วโมงมักสร้างจากข้อมูลประวัติหรือโมเดลมาตรฐาน
  • ใช้สำหรับพยากรณ์และจัดทำงบประมาณรายปี
  • รองรับการดูตามมิติ สัปดาห์ เดือน ไตรมาส ฯลฯ

8. myems_energy_plan_db (ฐานข้อมูลแผนพลังงาน)

วัตถุประสงค์: เก็บข้อมูลแผนและเป้าหมายการใช้พลังงาน

คุณสมบัติ:

  • โครงสร้างตารางคล้าย myems_energy_db
  • เก็บค่าแผนแทนค่าจริง
  • ใช้สำหรับจัดทำงบประมาณและบริหารเป้าหมาย

ตารางหลัก:

  • โครงสร้างตารางเหมือน myems_energy_db
  • ข้อมูลมาจากไฟล์แผนหรือกรอกด้วยมือ

หมายเหตุสำหรับการพัฒนา:

  • ข้อมูลแผนต้องเปรียบเทียบกับข้อมูลจริงเพื่อวิเคราะห์
  • รองรับแผนหลายระดับ (รายปี รายเดือน รายสัปดาห์ ฯลฯ)

9. myems_energy_prediction_db (ฐานข้อมูลการพยากรณ์พลังงาน)

วัตถุประสงค์: เก็บข้อมูลการพยากรณ์การใช้พลังงาน

คุณสมบัติ:

  • โครงสร้างตารางคล้าย myems_energy_db
  • เก็บค่าพยากรณ์แทนค่าจริง
  • ใช้สำหรับพยากรณ์และแจ้งเตือนการใช้พลังงาน

ตารางหลัก:

  • โครงสร้างตารางเหมือน myems_energy_db
  • ข้อมูลสร้างโดยอัลกอริทึมพยากรณ์

หมายเหตุสำหรับการพัฒนา:

  • ข้อมูลพยากรณ์ต้องอัปเดตเป็นประจำ
  • รองรับอัลกอริทึมพยากรณ์หลายแบบ (time series, machine learning ฯลฯ)
  • ความแม่นยำต้องปรับปรุงอย่างต่อเนื่อง

10. Myems_fdd_db (ฐานข้อมูลวินิจฉัยความผิดปกติ)

วัตถุประสงค์: เก็บข้อมูลที่เกี่ยวข้องกับการตรวจจับและวินิจฉัยความผิดปกติ

คุณสมบัติ:

  • รองรับช่องทางแจ้งเตือนหลายแบบ (web, email, SMS, WeChat, โทรศัพท์)
  • rule engine รองรับตรรกะการตรวจจับความผิดปกติที่ซับซ้อน
  • รองรับการยืนยันและจัดการข้อความแจ้งเตือน

โครงสร้างตารางหลัก:

table nameInstructionskey field
tbl_rulesDiagnostic rulesid, name, category, fdd_code, priority, channel, expression (JSON), message_template, is_enabled
tbl_web_messagesWeb Messageid, rule_id, user_id, subject, category, priority, message, status, belong_to_object_type, belong_to_object_id
tbl_email_messagesEmail messageid, rule_id, recipient_name, recipient_email, subject, message, attachment_file_name, status
tbl_text_messages_outboxSMS inboxid, rule_id, recipient_mobile, message, status, acknowledge_code
tbl_text_messages_inboxsms inboxid, sender_mobile, message, status
tbl_wechat_messages_outboxWeChat message sender boxid, rule_id, recipient_openid, message_template_id, message_data (JSON)
tbl_wechat_messages_inboxWeChat message inboxid, sender_openid, message, status
tbl_email_serversEmail Server Configurationid, host, port, requires_authentication, user_name, password, from_addr
tbl_wechat_configsWeChat configurationid, api_server, app_id, app_secret, access_token, expires_datetime_utc

หมวดหมู่กฎ (category):

  • REALTIME: แจ้งเตือนเรียลไทม์
  • SYSTEM: แจ้งเตือนระบบ
  • SPACE: แจ้งเตือนพื้นที่
  • METER: แจ้งเตือนมิเตอร์
  • TENANT: แจ้งเตือนผู้เช่า
  • STORE: แจ้งเตือนร้านค้า
  • SHOPFLOOR: แจ้งเตือนโรงงาน
  • EQUIPMENT: แจ้งเตือนอุปกรณ์
  • COMBINEDEQUIPMENT: แจ้งเตือนอุปกรณ์รวม

ระดับความสำคัญ (priority):

  • CRITICAL: วิกฤต
  • HIGH: สูง
  • MEDIUM: กลาง
  • LOW: ต่ำ

หมายเหตุสำหรับการพัฒนา:

  • ฟิลด์ expression เก็บนิพจน์กฎในรูปแบบ JSON
  • message_template รองรับการแทนค่าตัวแปร (เช่น $name, $value)
  • กฎรองรับทั้งการรันตามเวลาและรันทันที
  • สถานะข้อความ: newsentacknowledged / timeout

11. myems_user_db (ฐานข้อมูลผู้ใช้)

วัตถุประสงค์: เก็บข้อมูลยืนยันตัวตนผู้ใช้, API key, อีเมล ฯลฯ

คุณสมบัติ:

  • ปริมาณข้อมูลน้อย แต่ต้องการความปลอดภัยสูง
  • รองรับการจัดการสิทธิ์ผู้ใช้
  • รองรับการยืนยันตัวตนด้วย API key

โครงสร้างตารางหลัก:

table nameInstructionskey field
tbl_usersUser informationid, name, uuid, display_name, email, salt, password, is_admin, is_read_only, privilege_id, account_expiration_datetime_utc, password_expiration_datetime_utc, failed_login_count
tbl_privilegesPermission ConfigurationId, name, data (in JSON format)
tbl_sessionsuser sessionid, user_uuid, token, utc_expires
tbl_api_keysAPI Keyid, name, token, created_datetime_utc, expires_datetime_utc
tbl_email_messagesEmail messageid, recipient_name, recipient_email, subject, message, attachment_file_name, status, scheduled_datetime_utc
tbl_email_message_sessionsEmail conversationid, recipient_email, token, expires_datetime_utc
tbl_logsOperation logid, user_uuid, request_datetime_utc, request_method, resource_type, resource_id, request_body (JSON)
tbl_notificationsNotification messageid, user_id, created_datetime_utc, status, subject, message, url
tbl_new_usersNew user (to be activated)id, name, uuid, display_name, email, salt, password
tbl_verification_codesCAPTCHAid, recipient_email, verification_code, created_datetime_utc, expires_datetime_utc

การออกแบบความปลอดภัย:

  • รหัสผ่านเก็บแบบ salt+hash ไม่มี plain text
  • รองรับวันหมดอายุบัญชีและรหัสผ่าน
  • จำกัดจำนวนครั้งที่เข้าสู่ระบบผิดพลาด
  • API key รองรับวันหมดอายุ

หมายเหตุสำหรับการพัฒนา:

  • ฟิลด์รหัสผ่านต้องเข้ารหัสก่อนเก็บ ห้าม query ตรง
  • ควรลบ session token ที่หมดอายุเป็นระยะ
  • log การดำเนินการควรบันทึกทุกการกระทำสำคัญเพื่อการตรวจสอบ
  • สถานะแจ้งเตือน: unreadreadarchived

12. myems_reporting_db (ฐานข้อมูลรายงาน)

วัตถุประสงค์: เก็บข้อความอีเมลและไฟล์แนบที่เกี่ยวข้องกับรายงาน

คุณสมบัติ:

  • ปริมาณข้อมูลน้อย
  • รองรับการเก็บเทมเพลตรายงานและไฟล์ที่สร้าง

โครงสร้างตารางหลัก:

table nameInstructionskey field
tbl_reportsReport Configurationid, name, uuid, expression (JSON), is_enabled, last_run_datetime_utc, next_run_datetime_utc, is_run_immediately
tbl_reports_filesreport documentid, uuid, create_datetime_utc, file_name, file_type (xlsx/pdf/docx), file_object (LONGBLOB)
tbl_template_filesReport Template Fileid, uuid, report_id, file_name, file_type, file_object
tbl_email_messagesEmail messageid, recipient_name, recipient_email, subject, message, attachment_file_name, attachment_file_object, status

หมายเหตุสำหรับการพัฒนา:

  • ไฟล์รายงานรองรับ Excel, PDF, Word
  • ไฟล์เทมเพลตใช้สำหรับสร้างรายงาน
  • รายงานรองรับทั้งการสร้างตามเวลาและสร้างทันที
  • ไฟล์เก็บด้วย LONGBLOB ต้องระวังขนาดไฟล์

13. myems_production_db (ฐานข้อมูลการผลิต)

วัตถุประสงค์: เก็บข้อมูลผลิตภัณฑ์ที่เกี่ยวข้องกับการผลิต

ลักษณะ:

  • ปริมาณข้อมูลน้อย
  • ใช้สำหรับวิเคราะห์ความสัมพันธ์กับการใช้พลังงานในการผลิต

โครงสร้างตารางหลัก:

table nameInstructionskey field
tbl_productsProduct Informationid, name, uuid, unit_of_measure, tag, standard_product_coefficient
tbl_teamsTeam Informationid, name, uuid, description
tbl_shiftsShift informationid, shopfloor_id, team_id, product_id, product_count, start_datetime_utc, end_datetime_utc, reference_timestamp
tbl_shopfloor_hourlyWorkshop hourly outputid, shopfloor_id, start_datetime_utc, product_id, product_count
tbl_space_hourlySpace hour outputid, space_id, start_datetime_utc, product_id, product_count
tbl_shopfloors_productsWorkshop and product associationid, shopfloor_id, product_id
tbl_shopfloors_teamsWorkshop and team associationid, shopfloor_id, team_id

ข้อควรระวังในการพัฒนา:

  • ข้อมูลการผลิตใช้คำนวณการใช้พลังงานต่อหน่วยผลิตภัณฑ์
  • รองรับสถิติตามมิติต่าง ๆ เช่น ผลิตภัณฑ์ ทีม โรงงาน ฯลฯ
  • เชื่อมโยงกับข้อมูลพลังงานเพื่อคำนวณตัวชี้วัดประสิทธิภาพ

data-flow-relationship (ความสัมพันธ์ของกระแสข้อมูล)

กระบวนการรวบรวมข้อมูล

กระแสการรับข้อมูล

อุปกรณ์ / เซ็นเซอร์
↓ (Modbus TCP / MQTT / HTTP)
myems-modbus-tcp (บริการรวบรวมข้อมูล)
↓ (เขียน)
myems_historical_db.tbl_analog_value / tbl_digital_value / tbl_energy_value
↓ (การปกติ화ข้อมูล)
myems-normalization (บริการปกติ화)
↓ (การทำความสะอาดข้อมูล)
myems-cleaning (บริการทำความสะอาด)
↓ (การรวมข้อมูล)
myems-aggregation (บริการรวม)
↓ (เขียน)
myems_energy_db (ข้อมูลพลังงาน)
myems_billing_db (ข้อมูลการเรียกเก็บเงิน)
myems_carbon_db (ข้อมูลการปล่อยมลพิษคาร์บอน)

กระแสการค้นหาข้อมูล

คำขอของผู้ใช้

myems-api (บริการ API)
↓ (ค้นหา)
myems_system_db (ข้อมูลกำหนดค่า)
myems_historical_db (ข้อมูลประวัติ)
myems_energy_db (ข้อมูลพลังงาน)
↓ (ตอบสนอง)
myems-web / myems-admin (การแสดงบนหน้าแอปพลิเคชันฝั่งหน้า)

แผนภาพความสัมพันธ์ข้อมูล

myems_system_db.tbl_points
↓ (point_id)
myems_historical_db.tbl_analog_value
↓ (การคำนวณรวม)
myems_energy_db.tbl_meter_hourly
↓ (ลิงก์)
myems_system_db.tbl_meters
↓ (ลิงก์)
myems_system_db.tbl_equipments
↓ (ลิงก์)
myems_system_db.tbl_spaces

table-structure-design-specification (ข้อกำหนดการออกแบบโครงสร้างตาราง)

ฟิลด์ทั่วไป

ทุกตารางมีฟิลด์ทั่วไปต่อไปนี้:

ชื่อฟิลด์ประเภทคำอธิบาย
idBIGINT NOT NULL AUTO_INCREMENTคีย์หลัก เพิ่มอัตโนมัติ
nameVARCHAR(255)ชื่อ
uuidCHAR(36)UUID ใช้สำหรับบูรณาการระบบภายนอก
descriptionVARCHAR(255)คำอธิบาย (ไม่บังคับ)

ฟิลด์เวลา

ชื่อฟิลด์ประเภทคำอธิบาย
utc_date_timeDATETIMEเวลา UTC (ตารางข้อมูลประวัติ)
start_datetime_utcDATETIMEเวลาเริ่มต้นของช่วงเวลา (ตารางข้อมูลรวม)
created_datetime_utcDATETIMEเวลาสร้าง
updated_datetime_utcDATETIMEเวลาอัปเดต
last_run_datetime_utcDATETIMEเวลาเรียกใช้งานล่าสุด
next_run_datetime_utcDATETIMEเวลาเรียกใช้งานถัดไป

คำเตือน: ทุกฟิลด์เวลาควรใช้เวลา UTC เป็นหนึ่งเดียว และการแสดงบนหน้าแอปพลิเคชันฝั่งหน้าจะแปลงเป็นเวลาท้องถิ่น

ฟิลด์ตัวเลข

ชื่อฟิลด์ประเภทคำอธิบาย
actual_valueDECIMAL(21, 6)ค่าจริง รองรับความแม่นยำสูง (ทศนิยม 6 ตำแหน่ง)
set_valueDECIMAL(21, 6)ค่าที่ตั้ง
rated_capacityDECIMAL(21, 6)ความจุที่กำหนด
rated_powerDECIMAL(21, 6)พลังงานที่กำหนด

ฟิลด์ JSON

ชื่อฟิลด์ประเภทคำอธิบาย
connectionLONGTEXTกำหนดค่าการเชื่อมต่อ (รูปแบบ JSON)
expressionLONGTEXTนิพจน์ (รูปแบบ JSON)
payloadLONGTEXTโหลด (รูปแบบ JSON)
dataLONGTEXTข้อมูล (รูปแบบ JSON)

หมายเหตุ: ฟิลด์ JSON เก็บสตริง JSON ที่มีรูปแบบ และต้องแยกวิเคราะห์ก่อนใช้

ฟิลด์สถานะ

ชื่อฟิลด์ประเภทคำอธิบาย
is_enabledBOOLเปิดใช้งานหรือไม่
is_activeBOOLสถานะการเปิดใช้งาน
is_badBOOLมีข้อมูลเสียหรือไม่
is_publishedBOOLได้รับการเผยแพร่หรือไม่
is_countedBOOLรวมในสถิติหรือไม่
statusVARCHAR(32)สถานะ (ตัวอย่าง: new, sent, done, error)

การออกแบบดัชนี

ดัชนีคีย์หลัก:

  • ทุกตารางมี PRIMARY KEY (id)

ดัชนีไม่ซ้ำ:

  • ฟิลด์หลัก (เช่น name, uuid) มักมีดัชนีไม่ซ้ำ

ดัชนีรวม:

  • สร้างดัชนีรวมสำหรับชุดฟิลด์ที่ค้นหาบ่อย
  • ตัวอย่าง: (point_id, utc_date_time), (meter_id, start_datetime_utc)

ดัชนีเวลา:

  • ฟิลด์เวลามักมีดัชนีแยกต่างหากและรองรับค้นหาช่วงเวลา