Browse Source

fix/1

master
!Root 4 months ago
parent
commit
c2345b3030
7 changed files with 74 additions and 44 deletions
  1. +16
    -8
      src/App.js
  2. +2
    -2
      src/core/dynamic.js
  3. +21
    -6
      src/core/interpreter.js
  4. +13
    -13
      src/dynamic.js
  5. +0
    -3
      src/module/sms/package.json
  6. +16
    -6
      src/views/More/index.js
  7. +6
    -6
      src/views/Rack/index.js

+ 16
- 8
src/App.js View File

@ -9,7 +9,7 @@ import { useDispatch } from 'react-redux';
import { useDeviceList, useDeviceActive, useReports, useSelectedReport } from './provider/device.helper';
// core
import { Storage } from './core/storage';
import { useDevicesAsString, useSMSData } from './core/interpreter';
import { useDevicesAsString, useSMSData, canUseSMS } from './core/interpreter';
import { Shared } from './core/share';
import { useSMSRead } from './core/sms.read';
import { useSMSListener } from './core/sms.listen';
@ -31,6 +31,7 @@ export const App = () => {
const [selectedReport, setSelectedReport] = useSelectedReport(dispatch);
const [loading, setLoading] = useState(true);
const [inited, setInited] = useState(false);
useEffect(() => {
const subscription = AppState.addEventListener('change', (appState) => {
@ -53,6 +54,10 @@ export const App = () => {
// on active device change => read reports and set report and set last report
useEffect(() => {
if (inited === false) {
return;
}
console.log('App.js/useEffect/deviceActive');
onActiveDeviceChange();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [deviceActive]);
@ -64,6 +69,7 @@ export const App = () => {
Storage.ListDevice(),
Storage.GetDeviceActive(),
]);
setInited(true);
// set devices or java via shared
const _str_devices = useDevicesAsString(_deviceList);
Shared.SendDevices(_str_devices);
@ -112,10 +118,10 @@ export const App = () => {
for (let i = 0; i < SMSs.list.length; i++) {
const text = SMSs.list[i].body;
if (
text.includes('Hum:') &&
canUseSMS(text) &&
SMSs.list[i].address === _deviceList[_deviceActive].phone
) {
let _data = useSMSData(SMSs.list[i].body);
let _data = useSMSData(text);
if (_data.date.length !== 0) {
_reports.push(_data);
} // convert each string sms to readable data
@ -149,6 +155,7 @@ export const App = () => {
const onActiveDeviceChange = async () => {
try {
console.log('App.js/onActiveDeviceChange');
setLoading(true);
setReports([]);
setSelectedReport(-1);
@ -160,8 +167,8 @@ export const App = () => {
let _reports = [];
for (let i = 0; i < SMSs.list.length; i++) {
const text = SMSs.list[i].body;
if (text.includes('Hum:') && SMSs.list[i].address === devices[deviceActive].phone) {
let _data = useSMSData(SMSs.list[i].body);
if (canUseSMS(text) && SMSs.list[i].address === devices[deviceActive].phone) {
let _data = useSMSData(text);
if (_data.date.length !== 0) {
_reports.push(_data);
} // convert each string sms to readable data
@ -177,13 +184,14 @@ export const App = () => {
}
} catch (error) {
setLoading(false);
console.error('App.js/onActiveDevuceChange');
console.error('App.js/onActiveDeviceChange');
console.error(error);
}
};
const onAppStateActive = async () => {
try {
console.log('App.js/onAppSteteActive');
setLoading(true);
const _deviceList = await Storage.ListDevice();
@ -200,11 +208,11 @@ export const App = () => {
for (let i = 0; i < SMSs.list.length; i++) {
const text = SMSs.list[i].body;
if (
text.includes('Hum:') &&
canUseSMS(text) &&
phones.includes(SMSs.list[i].address) &&
(selectedPhone === undefined || selectedPhone === SMSs.list[i].address) // if there were no selected phone number or read sms phone number was selected phone
) {
let _data = useSMSData(SMSs.list[i].body);
let _data = useSMSData(text);
if (_data.date.length !== 0) {
_reports.push(_data);
} // convert each string sms to readable data

+ 2
- 2
src/core/dynamic.js View File

@ -5,7 +5,7 @@ import dynamic from '../dynamic';
* @param {{day: string, date: string, data: { status: boolean, key: string, value: string | undefined }[]}} report selected report
* @returns {{status: boolean, icon: any, label: string}[]}
*/
export const useDyanmic = (report) => {
export const useDynamic = (report) => {
try {
let output = [];
for (let item of report.data) {
@ -39,5 +39,5 @@ export const useDyanmic = (report) => {
* @return {undefined | { type: 'icon' | 'icondouble', key: string, translate: string, iconok: any, iconerr: any }}
*/
const getDynamicByKey = (key) => {
return dynamic.find((item) => key.includes(item.key));
return dynamic.find((item) => key.includes(item.translate));
};

+ 21
- 6
src/core/interpreter.js View File

@ -1,3 +1,12 @@
/**
* Check message can use or noot
* @param {string} text SMS message text
* @return {boolean} can use or not
*/
export const canUseSMS = (text) => {
return text.includes('گزارش');
};
/**
* convert smsed text data to code and readable data
* @param {string} text SMS message text
@ -10,7 +19,9 @@ export const useSMSData = (text) => {
data: [],
};
if (!text || typeof text !== 'string') return output;
if (!text || typeof text !== 'string') {
return output;
}
try {
const splitBreakLines = text.split('\n');
@ -18,7 +29,7 @@ export const useSMSData = (text) => {
output.day = splitBreakLines[0].trim();
output.date = splitBreakLines[1].trim();
for (let i = 3; i < splitBreakLines.length; i++) {
for (let i = 4; i < splitBreakLines.length; i++) {
output.data.push(translateDataRow(splitBreakLines[i].trim()));
}
@ -44,13 +55,17 @@ const translateDataRow = (text) => {
output.status = text.slice(0, 1) === '+';
text = text.replace(text.slice(0, 1), '');
} else {
output.status = true;
output.status = null;
}
const splitKeyValue = text.split(':');
if (text.includes(':')) {
const splitKeyValue = text.split(':');
output.key = splitKeyValue[0];
output.value = splitKeyValue[1];
output.key = splitKeyValue[0];
output.value = splitKeyValue[1];
} else {
output.key = text;
}
return output;
};

+ 13
- 13
src/dynamic.js View File

@ -1,67 +1,67 @@
export default [
{
type: 'iconvaluable',
key: 'Sm1',
key: 'نشت آب يک',
translate: 'نشت دود 1',
},
{
type: 'iconvaluable',
key: 'Sm2',
key: 'نشت اب دو',
translate: 'نشت دود 1',
},
{
type: 'iconvaluable',
key: 'Li1',
key: 'نشست آب یک',
translate: 'نشت آب 1',
},
{
type: 'iconvaluable',
key: 'Li2',
key: 'نشست آب دو',
translate: 'نشت آب 2',
},
{
type: 'iconvaluable',
key: 'Pow',
key: 'برق شهری',
translate: 'برق شهری',
},
{
type: 'iconvaluable',
key: 'Pow',
key: 'برق اضطراری',
translate: 'برق اضطراری',
},
{
type: 'iconvaluable',
key: 'Se1',
key: 'سنسور یک',
translate: 'سنسور 1',
},
{
type: 'iconvaluable',
key: 'Se2',
key: 'سنسور دو',
translate: 'سنسور 2',
},
{
type: 'iconvaluable',
key: 'Ser',
key: 'سرور',
translate: 'سرور',
},
{
type: 'iconvaluable',
key: 'Pc',
key: 'رایانه',
translate: 'کامپیوتر',
},
{
type: 'iconvaluable',
key: 'Dra',
key: 'درب رک',
translate: 'درب رک',
},
{
type: 'iconvaluable',
key: 'Dro',
key: 'درب اتاق',
translate: 'درب اتاق',
},
{
type: 'icondouble',
key: 'Ac',
key: 'کولر',
translate: 'کولر',
},
];

+ 0
- 3
src/module/sms/package.json View File

@ -3,9 +3,6 @@
"version": "2.1.0",
"description": "React-native module that gets sms from android device",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/FatehAK/react-native-get-sms-android-v2.git"

+ 16
- 6
src/views/More/index.js View File

@ -1,5 +1,5 @@
import React from 'react';
import { View, Image } from 'react-native';
import { View } from 'react-native';
// material
import { Text, Card } from 'react-native-paper';
import dynamic from '../../dynamic';
@ -14,11 +14,21 @@ export const More = () => {
const Item = ({ status, label, value }) => {
return (
<Card style={{ width: 120, margin: 5, backgroundColor: status ? '#00c853' : '#f44336' }}>
<Card
style={{
width: 120,
maxHeight: 85,
margin: 5,
backgroundColor: status ? '#00c853' : '#f44336',
}}>
<Card.Content style={{ justifyContent: 'center', alignItems: 'center', height: 72 }}>
<Text style={{ color: status ? '#424242' : '#fff' }}>{label ?? ''}</Text>
{value && (
<Text style={{ fontSize: 12, color: status ? '#424242' : '#fff' }}>{value}</Text>
{value && value.length !== 0 ? (
<Text style={{ fontSize: 12, color: status ? '#424242' : '#fff' }}>
{value.toString() ?? ''}
</Text>
) : (
<></>
)}
</Card.Content>
</Card>
@ -35,10 +45,10 @@ export const More = () => {
marginTop: 12,
}}>
{reports[index].data.map((item, i) => {
const data = dynamic.find((k) => k.key.includes(item.key));
const data = dynamic.find((k) => k.key === item.key);
if (data) {
const value = item.value;
const label = data.translate;
const label = item.key;
if (data.type === 'icondouble') {
let firstValue = false,

+ 6
- 6
src/views/Rack/index.js View File

@ -41,25 +41,25 @@ export const Rack = () => {
<Item
styles={{ minWidth: 120, width: width / 2 - 22 }}
label="دما محیط"
value={`${reports[index].data.find((item) => item.key === 'Tem').value} C`}
status={reports[index].data.find((item) => item.key === 'Tem').status}
value={`${reports[index].data.find((item) => item.key === 'دما').value} C`}
status={reports[index].data.find((item) => item.key === 'دما').status}
/>
<Item
styles={{ minWidth: 120, width: width / 2 - 22 }}
label="رطوبت محیط"
value={`${reports[index].data.find((item) => item.key === 'Hum').value}`}
status={reports[index].data.find((item) => item.key === 'Hum').status}
value={`${reports[index].data.find((item) => item.key === 'رطوبت').value}`}
status={reports[index].data.find((item) => item.key === 'رطوبت').status}
/>
</Items>
<Text style={{ margin: 8 }}>رک ها</Text>
<Items>
{reports[index].data
.filter((item) => item.key.includes('Rt'))
.filter((item) => item.key.includes('رک '))
.map((item, i) => (
<Item
key={i}
styles={{ minWidth: width / 3 - 16, maxWidth: width / 3 - 16 }}
label={`دما رک ${item.key.replace('Rt', '')}`}
label={`دما ${item.key}`}
value={`${item.value} C`}
status={item.status}
/>

Loading…
Cancel
Save